From d411ee9cd6a41bf7c2ea16d90cbffe1b90e24121 Mon Sep 17 00:00:00 2001 From: James Stewart Date: Wed, 20 Jan 2021 10:54:34 -0600 Subject: [PATCH] Upload plugin --- WC_Dogecash.php | 184 ++++++++++++++ css/cp-styles.css | 184 ++++++++++++++ img/cp-copy-icon.svg | 14 ++ img/plugin-logo.png | Bin 0 -> 3983 bytes img/progress-circle.svg | 16 ++ js/cp-script.js | 108 +++++++++ woocommerce-dogecash.php | 389 ++++++++++++++++++++++++++++++ woocommerce/checkout/form-pay.php | 176 ++++++++++++++ 8 files changed, 1071 insertions(+) create mode 100644 WC_Dogecash.php create mode 100644 css/cp-styles.css create mode 100644 img/cp-copy-icon.svg create mode 100644 img/plugin-logo.png create mode 100644 img/progress-circle.svg create mode 100644 js/cp-script.js create mode 100644 woocommerce-dogecash.php create mode 100644 woocommerce/checkout/form-pay.php diff --git a/WC_Dogecash.php b/WC_Dogecash.php new file mode 100644 index 0000000..f3d41a1 --- /dev/null +++ b/WC_Dogecash.php @@ -0,0 +1,184 @@ +id = 'degecash_payment'; + $this->method_title = __('DogeCash cryptocurrency payment','woocommerce-dogecash'); + $this->method_description = __('DogeCash Payment Gateway allows you to receive payments in DOGEC cryptocurrency','woocommerce-dogecash'); + $this->has_fields = true; + $this->init_form_fields(); + $this->init_settings(); + $this->enabled = $this->get_option('enabled'); + $this->title = $this->get_option('title'); + $this->description = $this->get_option('description'); + $this->payment_address = $this->get_option('payment_address'); + $this->confirmation_no = $this->get_option('confirmation_no'); + $this->max_time_limit = $this->get_option('max_time_limit'); + $this->cryptocurrency_used = "DOGEC"; + $this->default_currency_used = get_woocommerce_currency(); + $this->exchange_rate = $this->dogec_exchange_rate($this->default_currency_used); + $this->plugin_version = "1.0.2"; + + // Add support for "Woocommerce subscriptions" plugin + $this->dogec_remove_filter( 'template_redirect', 'maybe_setup_cart', 100 ); + $this->supports = array( + 'products', + 'subscriptions', + ); + + add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this, 'process_admin_options')); + } + public function init_form_fields(){ + $this->form_fields = array( + 'enabled' => array( + 'title' => __( 'Enable/Disable', 'woocommerce-dogecash' ), + 'type' => 'checkbox', + 'label' => __( 'Enable DogeCash Cryptocurrency Payment', 'woocommerce-dogecash' ), + 'default' => 'yes' + ), + 'title' => array( + 'title' => __( 'Method Title', 'woocommerce-dogecash' ), + 'type' => 'text', + 'default' => __( 'DogeCash Cryptocurrency Payment', 'woocommerce-dogecash' ), + 'desc_tip' => __( 'The payment method title which you want to appear to the customer in the checkout page.'), + ), + 'description' => array( + 'title' => __( 'Payment Description', 'woocommerce-dogecash' ), + 'type' => 'text', + 'default' => 'Please send the exact amount in DOGEC to the payment address bellow.', + 'desc_tip' => __( 'The payment description message which you want to appear to the customer on the payment page. You can pass a thank you note as well.' ), + ), + 'payment_address' => array( + 'title' => __( 'DogeCash Wallet Address', 'woocommerce-dogecash' ), + 'type' => 'text', + 'desc_tip' => __( 'DogeCash wallet address where you will receive DOGEC from sales.' ), + ), + 'confirmation_no' => array( + 'title' => __( 'Minimum Confirmations', 'woocommerce-dogecash' ), + 'type' => 'text', + 'default' => '5', + 'desc_tip' => __( 'Number of confirmations upon which the order will be considered as confirmed.' ), + ), + 'max_time_limit' => array( + 'title' => __( 'Maximum Payment Time (in Minutes)', 'woocommerce-dogecash' ), + 'type' => 'text', + 'default' => "15", + 'desc_tip' => __( 'Time allowed for a user to make the required payment.' ), + ) + ); + } + + /** + * Admin Panel Options + * - Options for bits like 'title' and availability on a country-by-country basis + * + * @since 1.0.0 + * @return void + */ + public function admin_options() { + ?> +

+

DogeCash Payment Gateway allows you to receive payments in DOGEC cryptocurrency

+ + generate_settings_html();?> +
+ cart->empty_cart(); + + // Redirect to order-pay page + return array( + 'result' => 'success', + 'redirect' => $order->get_checkout_payment_url( $on_checkout = false ) . '&cp=1' + ); + } + + + // Payent method structure on the checkout page + public function payment_fields(){ + ?> +
+ + + + + +
+ plugin logo + +
Exchange rate:
+ 1 cryptocurrency_used; ?> = exchange_rate, 5); ?> default_currency_used; ?> +
+
+ query_vars['order-pay'] ) && isset( $_GET['cp'] ) ) { + + // Take only filters on right hook name and priority + if ( ! isset( $wp_filter[ $hook_name ][ $priority ] ) || ! is_array( $wp_filter[ $hook_name ][ $priority ] ) ) { + return false; + } + + // Loop on filters registered + foreach ( (array) $wp_filter[ $hook_name ][ $priority ] as $unique_id => $filter_array ) { + // Test if filter is an array (always for class/method) + if ( isset( $filter_array['function'] ) && is_array( $filter_array['function'] ) ) { + // Test if object is a class and method is equal to param + if ( is_object( $filter_array['function'][0] ) && get_class( $filter_array['function'][0] ) && $filter_array['function'][1] == $method_name ) { + // Test for WordPress >= 4.7 WP_Hook class + if ( is_a( $wp_filter[ $hook_name ], 'WP_Hook' ) ) { + unset( $wp_filter[ $hook_name ]->callbacks[ $priority ][ $unique_id ] ); + } else { + unset( $wp_filter[ $hook_name ][ $priority ][ $unique_id ] ); + } + } + } + } + } + + return false; + } + + + } +} +?> diff --git a/css/cp-styles.css b/css/cp-styles.css new file mode 100644 index 0000000..4fa868d --- /dev/null +++ b/css/cp-styles.css @@ -0,0 +1,184 @@ +.cp-order-info-list { + list-style: disc; + padding: 30px !important; + margin-left: 0 !important; +} +@media only screen and (min-width: 980px) { + .cp-order-info-list { + list-style: none !important; + padding: 0 !important; + display: -webkit-box; + display: -ms-flexbox; + display: flex; + -webkit-box-orient: horizontal; + -webkit-box-direction: normal; + -ms-flex-direction: row; + flex-direction: row; + -webkit-box-pack: justify; + -ms-flex-pack: justify; + justify-content: space-between; + } + +} +.cp-box-wrapper { + display: block; + margin-bottom: 100px; +} +.cp-box-wrapper ::-moz-selection { + background: rgba(32,117,188,0.99); + color: #ffffff; + } +.cp-box-wrapper ::selection { + background: rgba(32,117,188,0.99); + color: #ffffff; + } + +@media only screen and (min-width: 980px) { + .cp-box-wrapper { + border-top: 1px solid #e2e2e2; + display: -webkit-box; + display: -ms-flexbox; + display: flex; + -webkit-box-orient: horizontal; + -webkit-box-direction: normal; + -ms-flex-direction: row; + flex-direction: row; + padding-top: 40px; + } + .cp-box-col-1 { + -ms-flex-preferred-size: 60%; + flex-basis: 60%; + max-width: 60%; + } + .cp-box-col-2 { + -ms-flex-preferred-size: 40%; + flex-basis: 40%; + max-width: 40%; + padding-left: 50px; + } + +} +.cp-box-wrapper h2 { + font-size: 31px !important; +} +.cp-input-box { + position: relative; +} +.cp-payment-amount { + color: #2075bc; +} +.cp-payment-msg { + font-size: 18px; + margin-bottom: 40px; +} +.cp-copy-btn { + border: none !important; + box-shadow: none !important; + cursor: pointer !important; + width: 36px !important; + height: 36px !important; + padding: 0 !important; + border: none !important; + background: none !important; + outline: none !important; + position: absolute !important; + right: 4px !important; + top: 0 !important; + text-align: center !important; + bottom: 0 !important; + margin: auto !important; + opacity: 0.6 !important; + line-height: 100%; +} +.cp-copy-address-btn:hover { + background: none !important; + opacity: 1 !important; +} +.cp-copy-btn:after { + opacity: 0; + -webkit-transition: opacity 0.3s ease; + -o-transition: opacity 0.3s ease; + transition: opacity 0.3s ease; +} +.cp-copy-btn.cp-copied:after { + font-size: 14px !important; + border-radius: 4px !important; + content: "Copied" !important; + display: inline-block !important; + background: #000000 !important; + color: #ffffff !important; + width: auto !important; + padding: 2px 8px !important; + position: absolute !important; + bottom: -30px !important; + left: -35% !important; + opacity: 1; + text-transform: none !important; +} +.cp-qr-code-holder { + text-align: center; + padding-top: 30px; +} +.cp-counter { + background: #2075bc; + color: #ffffff; + display: inline-block; + font-weight: 700; + padding: 7px 10px; + border-radius: 4px; + min-width: 70px; + text-align: center; + vertical-align: top; + margin: 4px 10px 0 0; + font-size: 14px; +} +.cp-payment-info { + display: inline-block; +} +.cp-payment-info-status { + font-weight: 700; + color: #2075bc; + display: inline-block; + font-size: 16px; + position: relative; +} +.cp-payment-info-status:after { + top: 5px; + display: inline-block; + content: ""; + background-image: url('../img/progress-circle.svg'); + background-size: 100%; + position: absolute; + height: 16px; + width: 16px; + right: -30px; +} +.cp-payment-info-text { + font-weight: 400; + color: #b3b3b3; + font-size: 14px; + margin: -5px 0 0 0; +} +.cp-payment-info-holder.cp-confirmed .cp-counter { + background: #5bb563; +} +.cp-payment-info-holder.cp-confirmed .cp-payment-info-status { + color: #5bb563; +} +.cp-payment-info-holder.cp-expired .cp-counter { + background: #d62525; +} +.cp-payment-info-holder.cp-expired .cp-payment-info-status { + color: #d62525; +} +.cp-payment-info-holder.cp-hidden { + display: none; +} +/* Input fields */ +.cp-payment-input { + border: 1px solid #cccccc !important; + width: 100% !important; +} +.cp-payment-input:focus { + border: 1px solid #2075bc !important; +} diff --git a/img/cp-copy-icon.svg b/img/cp-copy-icon.svg new file mode 100644 index 0000000..7152a4e --- /dev/null +++ b/img/cp-copy-icon.svg @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/img/plugin-logo.png b/img/plugin-logo.png new file mode 100644 index 0000000000000000000000000000000000000000..4b561590e5200d4a56c6b159ec3cc9d684e08425 GIT binary patch literal 3983 zcmV;A4{-2_P)001Qj1^@s6#j@f?0000PbVXQnQ*UN; zcVTj606}DLVr3vnZDD6+Qe|Oed2z{QJOBU>F-b&0RCwC#TnTg(MH>FQdwQ-+j!8l$ z7e~Ss4l(fn2_8{yRupwrWLNZgp{wgE-mA}DpU)MQMe$fX7R77Db45|aBP_b;B18l+ zDh5a(3Arb8U)??X|DEcjXEF%5?DN4)z4xc8tN!}Crt9nf>h5sSXcST4&$^ywm3i{83R9UNnmDp0)Io3sPJ0^UIN} zL^xXfo7bEgk(H=H#^1p!3K&E|vSnDWnDOA+=MU$}*4;gOgs0oL!S4&^0?&iOalp$m zSZ(Hu&RSZ%G6kjN1E8t_rHE{Ja$`Mo`a+PCW`ZeY`G(=Lvf}-lr=2ou@rPF&juU-! z$E?XDNP0ukZ8Z<}FfM~{NNYsOgMIIbLW zclqZLocENo!*!K25Q1f2)B|eU-UYbB5?D=!)J`Nb0D#Drnou2G=w;SHv zx);G03pNSCh!%uPZACHzFo6r9ASP;?JHh96LViw;)s|+3e7hZBKUk3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Qy#@%z>_QNvKC{kA;Xr6#-Dm`T5K<+nORovmmo2Z{{Ycbacs-g*&YVBA z2Z})tEM5F4)OYwmga|Y>x5A*}BG|FJ5q3AV!qmzNXl?6+LWdnrJ#`Z7-PZ<}%|9Ou zww|0VrDek1EfDc^)XG0>s6gBg6xcN^SM?8%0Oy_!zSr@Iv+5uYnmg zq9mk4Q0hM~%M^i}Y#R(4dn^dX*hh4-r_mG9QS^sOOx@aTSbTvsDx4Xwanw?D{y?Ry}?8xsLK5NWBijXi6j zf)O`8m1sl_D5tg{WrFt$!0oG-o^BdaUJY?kFy#SveZ3Jp{s4@smwRNq-*8t7E-3&WGyqi)zW?~eVqWQT-jc3nlN)yJgl#pO!0k`` ziH6B&gbC$k>eCQT5Yg}JkVCt^y7kz3_by36B?Z7k2A~SUcOP*G83R7s@a0#-8})BP z|3tBxO>pX%fsMxQhBJ;^uq^fETMB?bAAnl<^r3l|w1tFQyb?d$g^r#}5#*K6fP@Mlt0NZDD+Lk p)>Mu!1;9fBV5+3bFIE04zyMA386mDU=5PQ2002ovPDHLkV1jcsbL#*A literal 0 HcmV?d00001 diff --git a/img/progress-circle.svg b/img/progress-circle.svg new file mode 100644 index 0000000..9b28c89 --- /dev/null +++ b/img/progress-circle.svg @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/js/cp-script.js b/js/cp-script.js new file mode 100644 index 0000000..eaa82b2 --- /dev/null +++ b/js/cp-script.js @@ -0,0 +1,108 @@ +// Prevent conflict with other libraries +jQuery.noConflict(); + +(function( $ ) { + $(function() { + + //////////////////////////////// + // Countdown timer for transaction verification + //////////////////////////////// + var cpCount = parseInt($('input[name="cp_order_remaining_time"]').val()); + var cpCounter = setInterval(timer, 1000); + + function formatTime(seconds) { + var h = Math.floor(seconds / 3600), + m = Math.floor(seconds / 60) % 60, + s = seconds % 60; + if (h < 10) h = "0" + h; + if (m < 10) m = "0" + m; + if (s < 10) s = "0" + s; + return m + ":" + s; + } + + function timer() { + cpCount--; + if (cpCount < 0) { + return clearInterval(cpCounter); + } + $('.cp-counter').html(formatTime(cpCount)); + } + + + //////////////////////////////// + // Copy button action + //////////////////////////////// + $(document).on('click', '.cp-copy-btn', function(e){ + var btn = $(this); + var input = btn.closest('.cp-input-box').find('input'); + + input.select(); + document.execCommand("Copy"); + + btn.addClass('cp-copied'); + setTimeout( function(){ + btn.removeClass('cp-copied'); + }, 1000); + }); + + + //////////////////////////////// + // Countdown timer for transaction verification + //////////////////////////////// + var cp_interval; + verifyTransaction(); + + function verifyTransaction(){ + clearTimeout(cp_interval); + cp_interval = null; + var baseurl = window.location.origin; + + $.ajax({ + url: '/wp-admin/admin-ajax.php', + type: "POST", + data: { + action: "dogec_verify_payment", + order_id: $('input[name="cp_order_id"]').val() + }, + dataType: "json", + beforeSend: function(){ + + }, + success: function(response) { + console.log(response); + var order_info_holder = $('.cp-payment-info-holder'); + var order_status = $('.cp-payment-info-status'); + var counter = $('.cp-counter'); + + // Update status message + order_status.html(response.message); + + // Continue with payment verification requests + if (response.status == "waiting" || response.status == "detected" || response.status == "failed") { + if(response.status == "detected") { + clearInterval(cpCounter); + counter.html('00:00'); + } + + cp_interval = setTimeout(function(){ + verifyTransaction(); + }, 10000); + return false; + } + if(response.status == "confirmed") { + order_info_holder.addClass('cp-' + response.status); + + clearInterval(cpCounter); + counter.html('00:00'); + + setTimeout( function(){ + location.reload() + }, 2000); + + return false; + } + } + }); + } + }); +})(jQuery); diff --git a/woocommerce-dogecash.php b/woocommerce-dogecash.php new file mode 100644 index 0000000..151c91e --- /dev/null +++ b/woocommerce-dogecash.php @@ -0,0 +1,389 @@ +prefix . DOGEC_ORDERS_TABLE_NAME; + $charset_collate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE IF NOT EXISTS $db_table_name ( + id int(11) NOT NULL AUTO_INCREMENT, + transaction_id varchar(150) DEFAULT NULL, + payment_address varchar(150), + order_id varchar(250), + order_status varchar(250), + order_time varchar(250), + order_total varchar(50), + order_in_crypto varchar(50), + order_default_currency varchar(50), + order_crypto_exchange_rate varchar(50), + confirmation_no int(11) DEFAULT NULL, + PRIMARY KEY id (id) + ) $charset_collate;"; + + require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); + $res = dbDelta( $sql ); +} + + +// Check active plugins and see if woocommerce is installed +$active_plugins = apply_filters('active_plugins', get_option('active_plugins')); + +if(in_array('woocommerce/woocommerce.php', $active_plugins) || class_exists( 'WooCommerce') ){ + + register_activation_hook( __FILE__, 'dogec_create_transactions_table' ); + add_filter('woocommerce_payment_gateways', 'dogec_add_dogecash_crypto_gateway'); + + function dogec_add_dogecash_crypto_gateway( $gateways ){ + $gateways[] = 'WC_Dogecash'; + return $gateways; + } + + add_action('plugins_loaded', 'dogec_init_payment_gateway'); + + function dogec_init_payment_gateway(){ + require 'WC_Dogecash.php'; + } +} +else { + function dogec_admin_notice() { + echo "

Please install WooCommerce before using DogeCash Cryptocurrency Payment Gateway.

"; + deactivate_plugins('/woocommerce-dogecash/woocommerce-dogecash.php'); + wp_die(); + }; + add_action('admin_notices', 'dogec_admin_notice'); +} + + +// Add plugin scripts +function dogec_load_cp_scripts() { + if ( is_wc_endpoint_url( 'order-pay' ) ) { + wp_enqueue_style( 'cp-styles', plugins_url('/woocommerce-dogecash/css/cp-styles.css')); + wp_enqueue_script( 'cp-script', plugins_url('/woocommerce-dogecash/js/cp-script.js')); + } +} + +add_action('wp_enqueue_scripts', 'dogec_load_cp_scripts', 30); + + +// Processing of order +function dogec_process_order($order_id) { + global $wp; + $wc_dogec = new WC_Dogecash; + + $order_id = $wp->query_vars['order-pay']; + $order = wc_get_order($order_id); + $order_status = $order->get_status(); + + $order_crypto_exchange_rate = $wc_dogec->exchange_rate; + + // Redirect to "order received" page if the order is not pending payment + if($order_status !== 'pending') { + $redirect = $order->get_checkout_order_received_url(); + wp_safe_redirect($redirect); + exit; + } + + if ($order_crypto_exchange_rate == 0) { + wc_add_notice( 'There is an issue with fetching information about the current rates. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + + + if ($order_id > 0 && $order instanceof WC_Order) { + + global $wpdb; + $db_table_name = $wpdb->prefix . DOGEC_ORDERS_TABLE_NAME; + $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $db_table_name WHERE order_id = %d", $order_id)); + + if ($wpdb->last_error) { + wc_add_notice( 'There has been an error processing your order. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + + + // Record the order details for the first time + if($count == 0){ + + $payment_address = $wc_dogec->payment_address; + $order_total = $order->get_total(); + $order_in_crypto = dogec_order_total_in_crypto($order_total, $order_crypto_exchange_rate); + $order_currency = $order->get_currency(); + + $record_new = $wpdb->insert( $db_table_name, array( 'transaction_id' => "", 'payment_address' => $payment_address, 'order_id'=>$order_id, 'order_total' => $order_total, 'order_in_crypto' => $order_in_crypto, 'order_default_currency' => $order_currency, 'order_crypto_exchange_rate' => $order_crypto_exchange_rate, 'order_status' => 'pending', 'order_time'=>time()) ); + + if ($wpdb->last_error) { + wc_add_notice( 'There has been an error processing your order. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + } + } +} + +add_action("before_woocommerce_pay", "dogec_process_order", 20); + + + +// Verification of payment +function dogec_verify_payment() { + global $wpdb; + $db_table_name = $wpdb->prefix . DOGEC_ORDERS_TABLE_NAME; + + $wc_dogec = new WC_Dogecash; + + $order_id = $_POST['order_id']; + $order = new WC_Order($order_id); + + + $cp_order = dogec_get_cp_order_info($order_id); + $payment_address = $cp_order->payment_address; + $transaction_id = $cp_order->transaction_id; + $order_in_crypto = $cp_order->order_in_crypto; + $confirmation_no = $wc_dogec->confirmation_no; + $order_time = $cp_order->order_time; + $max_time_limit = $wc_dogec->max_time_limit; + $plugin_version = $wc_dogec->plugin_version; + + if(empty($transaction_id)){ + $transaction_id = "missing"; + } + $response = file_get_contents(DOGEC_API_URL ."?address=" . $payment_address . "&tx=" . $transaction_id . "&amount=" . $order_in_crypto . "&conf=" . $confirmation_no . "&otime=" . $order_time . "&mtime=" . $max_time_limit . "&pv=" . $plugin_version); + $response = json_decode($response); + + + if (!empty($response)) { + + if($response->status == "invalid") { + echo json_encode($response); + die(); + } + + // Check if transaction is expired + if($response->status == "expired") { + if($cp_order->order_status != "expired") { + $update = $wpdb->update( $db_table_name, array('transaction_id' => $response->transaction_id, 'order_status' => 'confirmed', 'confirmation_no' => $response->confirmations), array( 'order_id' => $order_id ) ); + } + } + + // Check if transaction exists + if($response->transaction_id != "" && strlen($response->transaction_id) == 64) { + $transactions = $wpdb->get_results($wpdb->prepare("SELECT id FROM $db_table_name WHERE transaction_id = %s AND order_id <> %d", $response->transaction_id, $order_id)); + + if ($wpdb->last_error) { + wc_add_notice( 'Unable to process the order. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + + if (count($transactions) > 0) { + echo json_encode(["status" => "failed"]); + die(); + } + } + + if($response->status == "detected") { + if(empty($cp_order->transaction_id)) { + $update = $wpdb->update( $db_table_name, array('transaction_id' => $response->transaction_id, 'order_status' => 'detected', 'confirmation_no' => $response->confirmations), array( 'order_id' => $order_id ) ); + } + } + + if($response->status == "confirmed") { + if($cp_order->order_status != "confirmed") { + $update = $wpdb->update( $db_table_name, array('transaction_id' => $response->transaction_id, 'order_status' => 'confirmed', 'confirmation_no' => $response->confirmations), array( 'order_id' => $order_id ) ); + $order->update_status('processing'); + } + } + + if ($wpdb->last_error) { + wc_add_notice( 'Unable to record transaction. Please contact the shop owner.', 'error' ); + wc_print_notices(); + exit; + } + + echo json_encode($response); + die(); + } + else { + echo json_encode(["status" => "failed"]); + die(); + } +} + +add_action("wp_ajax_dogec_verify_payment", "dogec_verify_payment"); +add_action("wp_ajax_nopriv_dogec_verify_payment", "dogec_verify_payment"); + + + +// Get information about the recorded order +function dogec_get_cp_order_info($order_id){ + global $wpdb; + $db_table_name = $wpdb->prefix . DOGEC_ORDERS_TABLE_NAME; + + $result = $wpdb->get_results($wpdb->prepare("SELECT * FROM $db_table_name WHERE order_id = %d", $order_id)); + + if ($wpdb->last_error) { + wc_add_notice( 'Unable to retrieve order details.', 'error' ); + wc_print_notices(); + exit; + } + + return $result[0]; +} + + +// Get information about the remaining time for order +function dogec_order_remaining_time($order_id) { + global $wpdb; + $db_table_name = $wpdb->prefix . DOGEC_ORDERS_TABLE_NAME; + + $wc_dogec = new WC_Dogecash; + $max_time_limit = $wc_dogec->max_time_limit * 60; // In seconds + + $now = new DateTime(); + $order_time = $wpdb->get_var($wpdb->prepare( "SELECT order_time FROM $db_table_name WHERE order_id = %d", $order_id) ); + + if ($wpdb->last_error) { + wc_add_notice( 'There has been an error processing your order. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + + $time = $max_time_limit - ($now->getTimestamp() - $order_time); + + return $time; +} + + + +// Create order total +function dogec_order_total_in_crypto($amount, $rate) { + $diffrence = 0.00002; + + // Different decimal points based on rate + if($rate > 100) { + $diffrence = 0.00000002; + $total = number_format($amount / $rate, 8); + } + else { + $diffrence = 0.00002; + $total = number_format($amount / $rate, 5); + } + + // Create unique amount for payment + $wc_dogec = new WC_Dogecash; + $max_time_limit = $wc_dogec->max_time_limit * 60; + + global $wpdb; + $db_table_name = $wpdb->prefix . DOGEC_ORDERS_TABLE_NAME; + $safe_period = $max_time_limit * 3; + + $other_amounts = $wpdb->get_results("SELECT order_in_crypto FROM $db_table_name WHERE order_status <> 'confirmed' AND order_time > (UNIX_TIMESTAMP(NOW()) - $safe_period)"); + + if ($wpdb->last_error) { + wc_add_notice( 'There has been an error processing your order. Please try again.', 'error' ); + wc_print_notices(); + exit; + } + + foreach($other_amounts as $amount){ + if($total == $amount->order_in_crypto){ + if($rate > 100) { + $total = number_format($total + $diffrence, 8); + } + else { + $total = number_format($total + $diffrence, 5); + } + } + } + + return $total; +} + + + +// Order received text +function dogec_order_received_text( $text, $order ) { + if ($order->has_status('completed')) { + $new = 'Thank you. Your order has been received!'; + } + else { + $new = ''; + } + return $new; +} + +add_filter('woocommerce_thankyou_order_received_text', 'dogec_order_received_text', 10, 2 ); + + + +// Plugin directory path +function dogec_plugin_path() { + return untrailingslashit( plugin_dir_path( __FILE__ ) ); +} + +add_filter( 'woocommerce_locate_template', 'dogec_woocommerce_locate_template', 10, 3 ); + + + +// Woocommerce plugin path in plugin +function dogec_woocommerce_locate_template( $template, $template_name, $template_path ) { + global $woocommerce; + + $_template = $template; + + if ( ! $template_path ) { + $template_path = $woocommerce->template_url; + } + + $plugin_path = dogec_plugin_path() . '/woocommerce/'; + + $template = locate_template ( + array( + $template_path . $template_name, + $template_name + ) + ); + + // Get the template from plugin, if it exists + if (file_exists( $plugin_path . $template_name ) ) { + $template = $plugin_path . $template_name; + } + + // Use default template + if ( ! $template ) { + $template = $_template; + } + + // Return template + return $template; +} + + + +// Add settings link +function dogec_add_plugin_page_settings_link( $links ) { + $links[] = '' . __('Settings') . ''; + return $links; + } + +add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'dogec_add_plugin_page_settings_link'); + + + +?> diff --git a/woocommerce/checkout/form-pay.php b/woocommerce/checkout/form-pay.php new file mode 100644 index 0000000..d17a863 --- /dev/null +++ b/woocommerce/checkout/form-pay.php @@ -0,0 +1,176 @@ +get_order_item_totals(); +$cp_order = dogec_get_cp_order_info($order->get_id()); + + +if($cp_order->order_status == "confirmed") { + $redirect = $order->get_checkout_order_received_url(); + wp_safe_redirect($redirect); + exit; +} + + +?> +get_payment_method() == $wc_dogec->id) : ?> + + + + +
+
    +
  • + + get_order_number(); ?> +
  • + +
  • + + get_date_created() ); ?> +
  • + +
  • + + order_in_crypto . " " . $wc_dogec->cryptocurrency_used . " (" . $cp_order->order_total . " " . $cp_order->order_default_currency . ")" ?> +
  • +
+
+ + needs_payment() ) : ?> +
+
+

method_title ?>

+

get_id()) < 0) ? "The payment time for order has expired! Do not make any payments as they will be invalid! If you have already made a payment within the allowed time, please wait." : $wc_dogec->description ?>

+ +
Amount:
+
+ + +
+ +
+ +
Payment Address:
+
+ + +
+ +
+ +
+
00:00
+
+
Waiting for payment...
+
Exchange rate locked 1 cryptocurrency_used; ?> = order_crypto_exchange_rate, 5) . ' ' . $cp_order->order_default_currency; ?>
+
+
+
+
+
+ +
+
+
+ + + + +
+ + + + + + + + + + + get_items() ) > 0 ) : ?> + get_items() as $item_id => $item ) : ?> + + + + + + + + + + + + + + + + + + + +
+ get_name() ), $item, false ); // @codingStandardsIgnoreLine + + do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, false ); + + wc_display_item_meta( $item ); + + do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, false ); + ?> + ' . sprintf( '× %s', esc_html( $item->get_quantity() ) ) . '', $item ); ?>get_formatted_line_subtotal( $item ); ?>
+ +
+ needs_payment() ) : ?> +
    + $gateway ) ); + } + } else { + echo '
  • ' . apply_filters( 'woocommerce_no_available_payment_methods_message', __( 'Sorry, it seems that there are no available payment methods for your location. Please contact us if you require assistance or wish to make alternate arrangements.', 'woocommerce' ) ) . '
  • '; // @codingStandardsIgnoreLine + } + ?> +
+ +
+ + + + + + + ' . esc_html( $order_button_text ) . '' ); // @codingStandardsIgnoreLine ?> + + + + +
+
+
+