Browse Source

Option to delete meta data on order completion and other improvements

main
fekt 2 years ago
parent
commit
7e3d34330b
  1. 143
      hush-payment-gateway.php
  2. 4
      readme.txt

143
hush-payment-gateway.php

@ -25,11 +25,13 @@ defined( 'ABSPATH' ) or exit;
// Include phpqrcode lib
include_once('phpqrcode.php');
if(!class_exists('QRtools')){
include_once('phpqrcode.php');
}
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))){
return;
}
@ -41,7 +43,7 @@ if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins',
* @param array $gateways all available WC gateways
* @return array $gateways all WC gateways + hush gateway
*/
add_filter( 'woocommerce_payment_gateways', 'hush_add_to_gateways' );
add_filter('woocommerce_payment_gateways', 'hush_add_to_gateways');
function hush_add_to_gateways( $gateways ) {
$gateways[] = 'Hush_WC_Gateway';
return $gateways;
@ -55,7 +57,7 @@ function hush_add_to_gateways( $gateways ) {
* @param array $links all plugin links
* @return array $links all plugin links + our custom links (i.e., "Settings")
*/
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'hush_gateway_plugin_links' );
add_filter('plugin_action_links_' . plugin_basename( __FILE__ ), 'hush_gateway_plugin_links');
function hush_gateway_plugin_links( $links ) {
$plugin_links = array(
@ -70,23 +72,25 @@ function hush_gateway_plugin_links( $links ) {
* Gets current HUSH price from CoinGecko API
*/
function get_hush_price() {
// Get plugin options
$options = get_option('woocommerce_hush_gateway_settings', 'default text');
// Get HUSH price from CoinGecko and convert amount to send
$request = wp_remote_get('https://api.coingecko.com/api/v3/simple/price?ids=hush&vs_currencies=btc%2Cusd%2Ceur%2Ceth%2Cgbp%2Ccny%2Cjpy%2Cidr%2Crub%2Ccad%2Csgd%2Cchf%2Cinr%2Caud%2Cinr%2Ckrw%2Cthb%2Cnzd%2Czar%2Cvef%2Cxau%2Cxag%2Cvnd%2Csar%2Ctwd%2Caed%2Cars%2Cbdt%2Cbhd%2Cbmd%2Cbrl%2Cclp%2Cczk%2Cdkk%2Chuf%2Cils%2Ckwd%2Clkr%2Cpkr%2Cnok%2Ctry%2Csek%2Cmxn%2Cuah%2Chkd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true');
if( is_wp_error( $request ) ) {
if(is_wp_error($request)){
return false;
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body, true);
$body = wp_remote_retrieve_body($request);
$data = json_decode($body, true);
if( ! empty( $data ) ) {
if(!empty($data)){
$hushPrice = $data['hush']['usd'];
return $hushPrice;
if(is_numeric($hushPrice)){
return $hushPrice;
}
else{
return false;
}
}
else{
return false;
@ -99,22 +103,25 @@ function get_hush_price() {
*/
function get_hush_amount($orderAmt, $hushPrice, $markup) {
// Set amount that should be sent based on USD and markup
$hushAmt = $orderAmt / $hushPrice;
$hushAmt = $hushAmt + ($hushAmt * $markup);
$hushAmt = number_format((float)$hushAmt, 8, '.', '');
return $hushAmt;
if(is_numeric($orderAmt) && is_numeric($hushPrice) && is_numeric($markup)){
$hushAmt = $orderAmt / $hushPrice;
$hushAmt = $hushAmt + ($hushAmt * $markup);
$hushAmt = number_format((float)$hushAmt, 8, '.', '');
return $hushAmt;
}
else{
return false;
}
}
/**
* Set HUSH payment gateway description on checkout page
*/
add_filter( 'woocommerce_gateway_description', 'hush_payment_gateway_description', 25, 2 );
function hush_payment_gateway_description( $description, $gateway_id ) {
add_filter('woocommerce_gateway_description', 'hush_payment_gateway_description', 25, 2);
function hush_payment_gateway_description($description, $gateway_id) {
if( 'hush_gateway' === $gateway_id ) {
if('hush_gateway' === $gateway_id) {
//Get cart object
$cart = WC()->cart;
@ -133,7 +140,7 @@ function hush_payment_gateway_description( $description, $gateway_id ) {
$hushAmt = get_hush_amount($cart->total, $hushPrice, $markup);
//Set Description
$description .= "<p><strong>HUSH Price:</strong> $".get_hush_price()."<br/> <strong>HUSH Amount Estimate: </strong>".$hushAmt." <br/>(Including ".$markupPercent."% volatility markup)</p>";
$description .= "<p><strong>HUSH Price:</strong> $".esc_html(get_hush_price())."<br/> <strong>HUSH Amount Estimate: </strong>".esc_html($hushAmt)." <br/>(Including ".esc_html($markupPercent)."% volatility markup)</p>";
$description .= "<p><br/><br/>Speak & Transact Freely! Your order will not be processed and shipped until HUSH payment has been received and confirmed. Exact amount of HUSH to send is specified after placing order.</p>";
}
@ -145,7 +152,7 @@ function hush_payment_gateway_description( $description, $gateway_id ) {
* Set and save expected Hush payment details when order created
*/
add_action('woocommerce_checkout_create_order', 'set_hush_payment_details', 20, 2);
function set_hush_payment_details( $order, $data ) {
function set_hush_payment_details($order, $data) {
// Get order amount
$orderAmt = $order->get_total();
@ -170,9 +177,9 @@ function set_hush_payment_details( $order, $data ) {
$hushAmt = get_hush_amount($orderAmt, $hushPrice, $markup);
// Save payment instruction related meta for verifying orders later
$order->update_meta_data( '_hush_price', $hushPrice );
$order->update_meta_data( '_hush_receive_address', $hushAddress);
$order->update_meta_data( '_hush_expected', $hushAmt );
$order->update_meta_data( '_hush_price', sanitize_text_field($hushPrice));
$order->update_meta_data( '_hush_receive_address', sanitize_text_field(hushAddress));
$order->update_meta_data( '_hush_expected', sanitize_text_field($hushAmt));
$order->save();
}
@ -180,18 +187,18 @@ function set_hush_payment_details( $order, $data ) {
/**
* Display expected HUSH payment details on the admin order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'hush_display_admin_order_meta', 10, 1 );
add_action('woocommerce_admin_order_data_after_billing_address', 'hush_display_admin_order_meta', 10, 1);
function hush_display_admin_order_meta($order){
echo "<p><strong>Receive Address:</strong> ". get_post_meta( $order->get_id(), '_hush_receive_address', true ) . "</p>";
echo "<p><strong>Expected HUSH:</strong> ". get_post_meta( $order->get_id(), '_hush_expected', true ) . "</p>";
echo "<p><strong>Exchange Rate:</strong> $". get_post_meta( $order->get_id(), '_hush_price', true ) . " USD </p>";
echo("<p><strong>Receive Address:</strong> ". esc_html(get_post_meta($order->get_id(), '_hush_receive_address', true)) . "</p>");
echo("<p><strong>Expected HUSH:</strong> ". esc_html(get_post_meta($order->get_id(), '_hush_expected', true)) . "</p>");
echo("<p><strong>Exchange Rate:</strong> $". esc_html(get_post_meta($order->get_id(), '_hush_price', true)) . " USD </p>");
}
/**
* Change default order notes field to include memo info
*/
add_filter( 'woocommerce_checkout_fields', 'hush_woocommerce_checkout_fields' );
add_filter('woocommerce_checkout_fields', 'hush_woocommerce_checkout_fields');
function hush_woocommerce_checkout_fields( $fields )
{
$fields['order']['order_comments']['label'] = 'Memo And/Or Special Instructions';
@ -204,7 +211,7 @@ function hush_woocommerce_checkout_fields( $fields )
/**
* Display HUSH amount on frontend for products
*/
add_filter( 'woocommerce_get_price_html', 'hush_amount_after_price' );
add_filter('woocommerce_get_price_html', 'hush_amount_after_price');
function hush_amount_after_price($price){
// Get plugin options
@ -229,7 +236,7 @@ function hush_amount_after_price($price){
$hushAmt = get_hush_amount($cleanPrice, $hushPrice, $markup);
// Return price and HUSH amount
return $price . " (~".number_format($hushAmt, 2)." HUSH)";
return $price . " (~".number_format(esc_html($hushAmt), 2)." HUSH)";
}
else{
return $price;
@ -240,7 +247,7 @@ function hush_amount_after_price($price){
/**
* Display HUSH amount with total on frontend
*/
add_filter( 'woocommerce_cart_total', 'hush_total_message', 10, 1 );
add_filter('woocommerce_cart_total', 'hush_total_message', 10, 1);
function hush_total_message( $price ) {
// Get plugin options
@ -264,7 +271,7 @@ function hush_total_message( $price ) {
$hushAmt = get_hush_amount($cleanPrice, $hushPrice, $markup);
// Return price and HUSH amount
return $price . " (~".number_format($hushAmt, 2)." HUSH)";
return $price . " (~".number_format(esc_html($hushAmt), 2)." HUSH)";
}
else{
return $price;
@ -272,6 +279,28 @@ function hush_total_message( $price ) {
}
/**
* Delete Hush related meta data when order status changes to completed
*/
add_action('woocommerce_order_status_changed', 'hush_order_status_completed', 10, 3);
function hush_order_status_completed($order_id, $old_status, $new_status)
{
// Get plugin options
$options = get_option('woocommerce_hush_gateway_settings', 'default text');
// Get meta option
$deleteMeta = $options['deleteMeta'];
if($deleteMeta == "Yes"){
if(($old_status == 'processing' OR $old_status == 'on-hold') && $new_status == 'completed'){
delete_post_meta($order_id, '_hush_price');
delete_post_meta($order_id, '_hush_receive_address');
delete_post_meta($order_id, '_hush_expected');
}
}
}
/**
* Hush Payment Gateway
* Provides simple instructions for customers to pay with Hush
@ -283,7 +312,7 @@ function hush_total_message( $price ) {
* @package WooCommerce/Classes/Payment
* @author Hush Developers
*/
add_action( 'plugins_loaded', 'hush_gateway_init', 11 );
add_action('plugins_loaded', 'hush_gateway_init', 11);
function hush_gateway_init() {
class Hush_WC_Gateway extends WC_Payment_Gateway {
@ -305,9 +334,11 @@ function hush_gateway_init() {
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->volatilityMarkup = $this->get_option( 'volatilityMarkup' );
$this->checkoutInstructions = $this->get_option( 'checkoutInstructions' );
$this->volatilityMarkup = $this->get_option( 'volatilityMarkup' );
$this->checkoutInstructions = $this->get_option( 'checkoutInstructions' );
$this->hushAddresses = $this->get_option( 'hushAddresses' );
$this->displayOnFrontend = $this->get_option( 'displayOnFrontend' );
$this->deleteMeta = $this->get_option( 'deleteMeta' );
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
@ -348,7 +379,7 @@ function hush_gateway_init() {
),
'hushAddresses' => array(
'title' => __( 'HUSH Address:', 'hush-wc-gateway' ),
'title' => __( 'HUSH Addresses:', 'hush-wc-gateway' ),
'type' => 'textarea',
'description' => __( 'Enter a comma separated list of Hush addresses to be used for receiving payments. You can enter as many as you want and update this setting as often as you like for added privacy. During checkout, 1 address from this list will be chosen randomly for the customer to send payment to. It is recommended to submit at least 10 addresses and change once a month for privacy.' ),
'default' => __( '', 'hush-wc-gateway' ),
@ -373,7 +404,19 @@ function hush_gateway_init() {
'Yes' => 'Yes',
'No' => 'No'
)
),
),
'deleteMeta' => array(
'title' => __( 'Delete HUSH related meta on order completion?:', 'hush-wc-gateway' ),
'type' => 'select',
'description' => __( 'This will delete HUSH related meta keys (price, amount, and receive address) on an order when status changes from processing to completed.', 'hush-wc-gateway' ),
'default' => __( 'Yes', 'hush-wc-gateway' ),
'desc_tip' => true,
'options' => array(
'Yes' => 'Yes',
'No' => 'No'
)
),
) );
}
@ -415,7 +458,7 @@ function hush_gateway_init() {
}
// Set timestamp for filename uniqueness
$timestamp = $_SERVER['REQUEST_TIME'];
$timestamp = time();
// Set and create uploads directory for plugin (wp-content/uploads/hush)
$upload_dir = wp_upload_dir();
@ -423,7 +466,7 @@ function hush_gateway_init() {
if(!file_exists($upload_dir)) wp_mkdir_p($upload_dir);
// Set logo path for adding to QR code
$logo_path = plugins_url( 'hush-icon-dark.jpg' , __FILE__ );
$logo_path = plugins_url('hush-icon-dark.jpg' , __FILE__);
// Set QR code path
$qr_path = $upload_dir.'/qr_code-'.$timestamp.'.png';
@ -454,20 +497,20 @@ function hush_gateway_init() {
$logo_qr_height = $logo_height/$scale;
// Create image with QR code and logo
imagecopyresampled($qr, $logo, $qr_width/3, $qr_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
imagecopyresampled($qr, $logo, $qr_width/3, $qr_height/3.5, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Save QR code with logo
imagepng($qr,$qr_path);
// Display instructions for customer to send payment to
$htmlOutput .= "<h2>HUSH Payment Instructions</h2>";
$htmlOutput .= $this->checkoutInstructions."<br/><br/>";
$htmlOutput .= "<strong>Address to send HUSH to: </strong><input type='text' value='".$hushAddress."' id='hushAddress' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Amount to send: </strong>(Exchange Rate is </strong>".$hushPrice." USD + ". $this->volatilityMarkup * 100 ."% volatility markup)<br/><input type='text' value='".$hushAmt."' id='hushAmt' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Memo: </strong>(Optional and uses submitted order notes)<br/><input type='text' value='".$hushMemo."' id='hushMemo' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= esc_html($this->checkoutInstructions)."<br/><br/>";
$htmlOutput .= "<strong>Address to send HUSH to: </strong><input type='text' value='".esc_html($hushAddress)."' id='hushAddress' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Amount to send: </strong>(Exchange Rate is </strong>".esc_html($hushPrice)." USD + ". esc_html($this->volatilityMarkup * 100) ."% volatility markup)<br/><input type='text' value='".esc_html($hushAmt)."' id='hushAmt' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Memo: </strong>(Optional and uses submitted order notes)<br/><input type='text' value='".esc_html($hushMemo)."' id='hushMemo' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "An auto-generated HUSH URI may be used instead for easier/quicker payment. In lite or full wallet, go to <strong>File > Pay HUSH URI</strong> and copy paste the URI below. Do not modify this URI. Orders will only be fulfilled if expected amount of HUSH is received.<br/><br/>";
$htmlOutput .= "<strong>Pay HUSH URI: </strong><input type='text' value='".$hushURI."' id='hushURI' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Silent Dragon Android QR Code: </strong><br/><img src='/wp-content/uploads/hush/qr_code-".$timestamp.".png' /><br/><br/>";
$htmlOutput .= "<strong>Pay HUSH URI: </strong><input type='text' value='".esc_html($hushURI)."' id='hushURI' style='width:100%' readonly='readonly'/><br/><br/>";
$htmlOutput .= "<strong>Silent Dragon Android QR Code: </strong><br/><img src='/wp-content/uploads/hush/qr_code-".esc_html($timestamp).".png' /><br/><br/>";
echo $htmlOutput;
}
@ -499,5 +542,5 @@ function hush_gateway_init() {
'redirect' => $this->get_return_url( $order )
);
}
} // end \Hush_WC_Gateway class
} // end \Hush_WC_Gateway class
}

4
readme.txt

@ -3,7 +3,7 @@ Contributors: Hush Developers
Donate link: https://hush.is
Tags: cryptocurrency, crypto, checkout, woocommerce, e-commerce, ecommerce, store, payments, gateway, paypal, cryptocurrency checkout, hush, zk-snarks, equihash, silentdragon, silentdragonlite
Requires at least: 4.7
Tested up to: 5.9.3
Tested up to: 6.0.0
Stable tag: 1.0.0
Requires PHP: 5.2
License: GPLv3
@ -88,6 +88,6 @@ In some cases you may have to manually install the plugin, to do so is fairly si
== Changelog ==
= 1.0 =
= 1.0.0 =
* Initial release. hush.is/privacy - Speak And Transact Freely.
Loading…
Cancel
Save