From 86fc61ab6d7c1fac4360e7a85dc27ffb76146412 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Fri, 20 Aug 2021 10:46:20 +0700 Subject: [PATCH 01/14] Update brick to ver 1.6 --- .gitignore | 1 + src/assets/js/payment.js | 125 +++---------------- src/includes/class-paymentwall-brick.php | 147 ++++++++++++++--------- src/lib/paymentwall-php | 1 - src/paymentwall-for-woocommerce.php | 20 +++ src/templates/brick/form.html | 108 +++-------------- src/templates/pages/brick_form.php | 39 ++++++ 7 files changed, 177 insertions(+), 264 deletions(-) create mode 100644 .gitignore delete mode 160000 src/lib/paymentwall-php create mode 100644 src/templates/pages/brick_form.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 7255e8c..14a445d 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,113 +1,20 @@ -function paymentListener(orderId, baseUrl) { - var pwInterval = setInterval(function () { - var r = new XMLHttpRequest(); - r.open("POST", baseUrl + '/index.php?wc-api=paymentwall_gateway&action=ajax', true); - r.onreadystatechange = function () { - if (r.readyState != 4 || r.status != 200) return; - if (r.responseText) { - var data = JSON.parse(r.responseText); - if (data && data.status == '1') { - clearInterval(pwInterval); - location.href = data.url; - } - } - }; - var formData = new FormData(); - formData.append('order_id', orderId); - r.send(formData); - }, 5000); -} - var Brick_Payment = { brick: null, - form3Ds : '', - createBrick: function (public_key) { - this.brick = new Brick({ - public_key: public_key, - form: {formatter: true} - }, 'custom'); - }, - brickTokenizeCard: function () { - this.brick.tokenizeCard({ - card_number: jQuery('#card-number').val(), - card_expiration_month: jQuery('#card-expiration-month').val(), - card_expiration_year: jQuery('#card-expiration-year').val(), - card_cvv: jQuery('#card-cvv').val() - }, function (response) { - if (response.type == 'Error') { - var errors = "Brick error(s):
" + " - " + (typeof response.error === 'string' ? response.error : response.error.join("
- ")); - Brick_Payment.showNotification(errors, 'error'); - } else { - jQuery('#brick-token').val(response.token); - jQuery('#brick-fingerprint').val(Brick.getFingerprint()); - jQuery('#brick-get-token-success').val(1); - - Brick_Payment.sendPaymentRequest(); - window.addEventListener("message", Brick_Payment.threeDSecureMessageHandle, false); - } - }); - }, openConfirm3ds: function () { - var win = window.open("", "Brick: Verify 3D secure", "toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=1024, height=720"); - win.document.body.innerHTML += Brick_Payment.form3Ds; - win.document.forms[0].submit(); - return false; - }, threeDSecureMessageHandle: function (event) { - var origin = event.origin || event.originalEvent.origin; - if (origin !== "https://api.paymentwall.com") { - return; - } - Brick_Payment.showLoading(); - var brickData = JSON.parse(event.data); - if (brickData && brickData.event == '3dSecureComplete') { - jQuery('#hidden-brick-secure-token').val(brickData.data.secure_token); - jQuery('#hidden-brick-charge-id').val(brickData.data.charge_id); - Brick_Payment.sendPaymentRequest(); - } - }, - sendPaymentRequest: function () { - jQuery.ajax({ - type: 'POST', - url: '?wc-ajax=checkout', - data: jQuery('form.checkout').serialize(), - dataType: 'json', - encode: true, - beforeSend: function () { - Brick_Payment.showLoading(); - }, - success: function (response) { - if (response.result == 'success') { - Brick_Payment.showNotification(response.message); - window.location.href = response.redirect; - } else if (response.result == 'secure') { - Brick_Payment.form3Ds = response.secure; - var requireConfirm = "Please verify 3D-secure to continue checkout. Click here !"; - Brick_Payment.showNotification(requireConfirm); - } else if (response.result == 'failure') { - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html(response.messages); - jQuery('#brick-errors').show(); - } else { - Brick_Payment.showNotification(response.message, 'error'); + createBrick: function (public_key, amount, currency) { + "use strict"; + if (window.Brick !== undefined) { + return new Brick({ + public_key: public_key, + amount: amount, + currency: currency, + container: 'brick-payments-container', + action: 'wc-api=paymentwall_gateway&action=brick_charge', + form: { + show_zip: true, // show zip code + show_cardholder: true, + lang: 'en' } - } - }); - }, showNotification: function (message, type) { - type = (type != undefined) ? type : 'message'; - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html(''); - jQuery('#brick-errors').show(); - }, showLoading: function () { - jQuery('#brick-errors').hide(); - jQuery('#brick-loading').show(); + }, 'default') + } } -}; - -(function ($) { - $( document ).ready(function() { - $('.paymentwall-method .pw_payment_system').on('change', function () { - var paymentSystem = $(this).data('payment-system'); - var inputPaymentSystem = $('#pw_gateway'); - inputPaymentSystem.val(JSON.stringify(paymentSystem)); - }) - }) -})(jQuery); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2707716..2761f22 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -65,13 +65,20 @@ public function init_configs($isPingback = false) { * Displays credit card form */ public function payment_fields() { + $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); + $_SESSION['cart_total'] = WC()->cart->cart_contents_total; + $_SESSION['currency'] = $currency; + $_SESSION['private_key'] = $this->settings['privatekey']; + $_SESSION['public_key'] = $this->settings['publickey']; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); $this->tokenization_script(); $this->saved_payment_methods(); } + $brickFormUrl = PW_PLUGIN_URL . '/templates/pages/brick_form.php'; echo $this->get_template('brick/form.html', array( 'payment_id' => $this->id, @@ -79,7 +86,8 @@ public function payment_fields() { 'entry_card_number' => __("Card number", PW_TEXT_DOMAIN), 'entry_card_expiration' => __("Card expiration", PW_TEXT_DOMAIN), 'entry_card_cvv' => __("Card CVV", PW_TEXT_DOMAIN), - 'plugin_url' => PW_PLUGIN_URL + 'plugin_url' => PW_PLUGIN_URL, + 'brick_form_url' => $brickFormUrl, )); $hasSubscription = class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription(); @@ -114,11 +122,6 @@ public function process_payment($order_id) { * @throws Exception */ function prepare_card_info($order) { - if (!isset($_POST['brick'])) { - throw new Exception("Payment Invalid!"); - } - - $brick = $_POST['brick']; $data = array( 'amount' => $order->get_total(), 'currency' => $order->get_currency(), @@ -126,39 +129,10 @@ function prepare_card_info($order) { 'plan' => !method_exists($order, 'get_id') ? $order->id : $order->get_id(), 'description' => sprintf(__('%s - Order #%s', PW_TEXT_DOMAIN), esc_html(get_bloginfo('name', 'display')), $order->get_order_number()), ); - if ($brick['token'] && $brick['fingerprint']) { - $data = array_merge($data, array( - 'token' => $brick['token'], - 'fingerprint' => $brick['fingerprint'] - )); - } elseif (!empty($_POST['wc-brick-payment-token'])) { - $token = WC_Payment_Tokens::get($_POST['wc-brick-payment-token'])->get_token(); - $data = array_merge($data, array( - 'token' => $token - )); - } - if (!empty($brick['cc_brick_secure_token'])) { - $data['secure_token'] = $brick['cc_brick_secure_token']; - } - if (!empty($brick['cc_brick_charge_id'])) { - $data['charge_id'] = $brick['cc_brick_charge_id']; - } return $data; } - /** - * Add custom fields validation - */ - public function brick_fields_validation() { - if ($_POST['payment_method'] == $this->id) { - $brick = $_POST['brick']; - - if ((trim($brick['token']) == '' || trim($brick['fingerprint']) == '') && empty($_POST['wc-brick-payment-token'])) - wc_add_notice(sprintf(__('The %s payment has some errors. Please try again.', PW_TEXT_DOMAIN), $this->title), 'error'); - } - } - /** * @param $order * @return array @@ -166,33 +140,14 @@ public function brick_fields_validation() { */ public function process_standard_payment($order) { $return = array(); - $cardInfo = $this->prepare_card_info($order); - $charge = new Paymentwall_Charge(); - $charge->create(array_merge( - $this->prepare_user_profile_data($order), // for User Profile API - $cardInfo, - $this->get_extra_data($order) - )); - - $response = $charge->getPublicData(); - $responseData = json_decode($charge->getRawResponseData(), true); + session_start(); - if ($charge->isSuccessful() && empty($responseData['secure'])) { + $charge = $_SESSION['charge']; + $responseData = $_SESSION['charge_response_data']; + if (empty($responseData['secure'])) { $return['result'] = 'success'; $return['redirect'] = $this->process_success($order, $charge, $message); $return['message'] = $message; - - if (is_checkout() && !empty($_POST['wc-brick-new-payment-method']) && $_POST['wc-brick-payment-token'] == 'new') { - $token = new WC_Payment_Token_CC(); - $token->set_token($responseData['card']['token']); - $token->set_gateway_id($this->id); - $token->set_card_type($responseData['card']['type']); - $token->set_last4($responseData['card']['last4']); - $token->set_expiry_month($responseData['card']['exp_month']); - $token->set_expiry_year('20' . $responseData['card']['exp_year']); - $token->set_user_id(get_current_user_id()); - $token->save(); - } } elseif (!empty($responseData['secure'])) { WC()->session->set('orderId', !method_exists($order, 'get_id') ? $order->id : $order->get_id()); $return['result'] = 'secure'; @@ -217,16 +172,22 @@ public function get_extra_data($order) { } public function process_success($order, $charge, &$message) { - if ($charge->isCaptured()) { + if ($charge->isCaptured() && $charge->isUnderReview()) { // Add order note $order->add_order_note(sprintf( __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), $charge->getId())); // Payment complete - $message = "Your order has been received !"; + $message = "Your order has been received and is under review!"; } elseif ($charge->isUnderReview()) { $order->update_status('on-hold'); $message = 'Your order is under review !'; + } elseif ($charge->isCaptured()) { + $order->add_order_note(sprintf( + __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), + $charge->getId())); + // Payment complete + $message = "Your order has been received!"; } $thanksPage = $this->get_return_url($order); @@ -245,4 +206,70 @@ public static function get_available_payment_gateways( $available_gateways ) { return $available_gateways; } + + public function handle_brick_charge() + { + session_start(); + + $this->init_configs(); + $parameters = $_POST; + $chargeInfo = $this->getChargeInfo($parameters); + $charge = $this->createCharge($chargeInfo); + $response = $charge->getPublicData(); + $responseData = json_decode($charge->getRawResponseData(), true); + $result = []; + $result['payment'] = $responseData; + $result = array_merge($result, json_decode($response, true)); + $_SESSION['charge_response_data'] = $responseData; + $_SESSION['charge'] = $charge; + + if ($charge->isSuccessful()) { + if ($charge->isCaptured()) { + $result = json_encode($result); + echo $result; + } elseif ($charge->isUnderReview()) { + echo 'Under review'; + exit(); + } + } + else { + if (isset($result['payment']['secure']['formHTML'])) { + $resultError['success'] = 0; + $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; + $resultError = json_encode($resultError); + echo $resultError; + } + } + } + + function getChargeInfo($params) + { + $chargeInfo = [ + 'email' => $params['email'], + 'history[registration_date]' => '1489655092', + 'amount' => (float) $_SESSION['cart_total'], + 'currency' => $_SESSION['currency'], + 'token' => $params['brick_token'], + 'fingerprint' => $params['brick_fingerprint'], + 'description' => 'Brick Paymentwall' + ]; + if (isset($params['brick_charge_id']) && isset($params['brick_secure_token'])) { + $chargeInfo['charge_id'] = $params['brick_charge_id']; + $chargeInfo['secure_token'] = $params['brick_secure_token']; + } + if (!empty($params['brick_reference_id'])) { + $chargeInfo['reference_id'] = $params['brick_reference_id']; + } + + return $chargeInfo; + } + + function createCharge($chargeInfo) + { + $charge = new Paymentwall_Charge(); + $charge->create($chargeInfo); + + return $charge; + } + } diff --git a/src/lib/paymentwall-php b/src/lib/paymentwall-php deleted file mode 160000 index b23bfb6..0000000 --- a/src/lib/paymentwall-php +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b23bfb6dd5667e729f19f375de5dc5d07ccc81d4 diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9ef9900..95cab5d 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -230,3 +230,23 @@ class="input-radio pw_payment_system" name="payment_method" request ); + $siteUrl = get_site_url(); + if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { + return false; + } + // Check if it's a valid request. + $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); + if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { + $brick = new Paymentwall_Brick(); + $brick->handle_brick_charge(); + } + die('Process completed' ); +} + +add_action( 'template_redirect', 'handle_brick_charge', 0); + diff --git a/src/templates/brick/form.html b/src/templates/brick/form.html index 01b1cd7..cd4503f 100644 --- a/src/templates/brick/form.html +++ b/src/templates/brick/form.html @@ -1,98 +1,18 @@ - -
- - - - - - - - -

- - -

- -

- - - - / - -

- -

- - - - -

-
-
- + + + - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php new file mode 100644 index 0000000..4be40f4 --- /dev/null +++ b/src/templates/pages/brick_form.php @@ -0,0 +1,39 @@ + + +
+ + + \ No newline at end of file From 4c565a454a07a043d1b649a671e299163a9da5e6 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Wed, 1 Sep 2021 00:23:52 +0700 Subject: [PATCH 02/14] Fix action brick charge --- src/assets/js/payment.js | 4 ++-- src/includes/class-paymentwall-brick.php | 12 ++++++++---- src/includes/class-paymentwall-gateway.php | 11 ++++++++++- src/paymentwall-for-woocommerce.php | 19 ------------------- src/templates/pages/brick_form.php | 17 +++++++++-------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 14a445d..0263921 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,6 +1,6 @@ var Brick_Payment = { brick: null, - createBrick: function (public_key, amount, currency) { + createBrick: function (public_key, amount, currency, action) { "use strict"; if (window.Brick !== undefined) { return new Brick({ @@ -8,7 +8,7 @@ var Brick_Payment = { amount: amount, currency: currency, container: 'brick-payments-container', - action: 'wc-api=paymentwall_gateway&action=brick_charge', + action: action, form: { show_zip: true, // show zip code show_cardholder: true, diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2761f22..def6259 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -67,11 +67,13 @@ public function init_configs($isPingback = false) { public function payment_fields() { $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); $_SESSION['cart_total'] = WC()->cart->cart_contents_total; $_SESSION['currency'] = $currency; $_SESSION['private_key'] = $this->settings['privatekey']; $_SESSION['public_key'] = $this->settings['publickey']; + $_SESSION['brick_form_action'] = get_site_url() . '/?wc-api=paymentwall_gateway&action=brick_charge'; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); @@ -226,10 +228,11 @@ public function handle_brick_charge() if ($charge->isSuccessful()) { if ($charge->isCaptured()) { $result = json_encode($result); - echo $result; + var_dump($result); + die(); } elseif ($charge->isUnderReview()) { - echo 'Under review'; - exit(); + var_dump('under_review'); + die(); } } else { @@ -237,7 +240,8 @@ public function handle_brick_charge() $resultError['success'] = 0; $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; $resultError = json_encode($resultError); - echo $resultError; + var_dump($resultError); + die(); } } } diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 1f803eb..2ec7156 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -247,7 +247,7 @@ function ipn_response() { $payment->init_configs(true); $pingback_params = $_GET; - + $pingback = new Paymentwall_Pingback($pingback_params, $this->getRealClientIP()); if ($pingback->validate(true)) { @@ -332,6 +332,9 @@ function handle_action() { case 'ipn': $this->ipn_response(); break; + case 'brick_charge': + $this->handle_brick_charge(); + break; default: break; } @@ -541,4 +544,10 @@ public function save_data_to_session($name, $data) { } } + public function handle_brick_charge() + { + $paymentwallBrick = new Paymentwall_Brick(); + + return $paymentwallBrick->handle_brick_charge(); + } } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 95cab5d..9825be4 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -231,22 +231,3 @@ class="input-radio pw_payment_system" name="payment_method" } } -function handle_brick_charge() { - // Check if we're on the correct url - global $wp; - $current_slug = add_query_arg( array(), $wp->request ); - $siteUrl = get_site_url(); - if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { - return false; - } - // Check if it's a valid request. - $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); - if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { - $brick = new Paymentwall_Brick(); - $brick->handle_brick_charge(); - } - die('Process completed' ); -} - -add_action( 'template_redirect', 'handle_brick_charge', 0); - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php index 4be40f4..79cad5e 100644 --- a/src/templates/pages/brick_form.php +++ b/src/templates/pages/brick_form.php @@ -1,4 +1,5 @@ +
+ +
+ + + + + + + + +

+ + +

+ +

+ + + + / + +

+ +

+ + + + +

+
+
+ + diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php deleted file mode 100644 index 79cad5e..0000000 --- a/src/templates/pages/brick_form.php +++ /dev/null @@ -1,40 +0,0 @@ - - - -
- - - \ No newline at end of file From 2c3ab6e7023de16b1bfb61f9bfd5ec4f2d7a93c7 Mon Sep 17 00:00:00 2001 From: richardtong Date: Tue, 31 Aug 2021 18:21:31 +0700 Subject: [PATCH 04/14] [PLAT-57] Add support Russia language, update README --- README.md | 1 - readme.txt | 21 +++++++- .../paymentwall-for-woocommerce-ru_RU.mo | Bin 0 -> 741 bytes .../paymentwall-for-woocommerce-ru_RU.po | 46 ++++++++++++++++++ src/paymentwall-for-woocommerce.php | 11 +++-- 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/languages/paymentwall-for-woocommerce-ru_RU.mo create mode 100644 src/languages/paymentwall-for-woocommerce-ru_RU.po diff --git a/README.md b/README.md index 27bc416..983c07e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ Paymentwall module for WordPress WooCommerce. ### Requirement * Woocommerce v2.1 or greater * WordPress v4.0 or greater -* PHP 5.3 or greater # Installation To install Paymentwall WooCommerce module, please follow the [instructions](https://docs.paymentwall.com/modules/woocommerce). diff --git a/readme.txt b/readme.txt index e4da13b..8ac741a 100644 --- a/readme.txt +++ b/readme.txt @@ -2,8 +2,9 @@ Contributors: Paymentwall Tags: payment, paymentgateway, woocommerce, ecommerce Requires at least: 4.0 & WooCommerce 2.6+ -Tested up to: 4.8 & Woocommerce 3.1 -Stable tag: 1.6.2 +Tested up to: Wordpress 5.7.2 & Woocommerce 5.3 +PHP Version: 5.6 or higher +Stable tag: 1.7.3 License: The MIT License (MIT) Official Paymentwall module for WordPress WooCommerce. @@ -36,6 +37,22 @@ View our full installation guide: UxNz7wTgS-WwRYDHK|Mfw z=mm)rXI=pz(iSS<71l4nvoLl95hIP~@0%U(eEa+M&5t#d8>n|sACv(~XK5bso=SkU74m_g}grGnN)`Kbres?<2=%2Bz>q4o{mUY zac1xUvdawj!K0o_Xp3yR;8Ndf-lr%CBtw22mxKPpZ4v4M-SR12;(AOQGEz+Fgc+%D zM30FmXAvhO&Qc$Sxz;fg|I$9FoD2`^gCtihA^TFL+6R$Up$<0NuyliaV$G#N;KKSk zlrFs+xGwAf1%zV^+W~lgi=E&hx>XD9xb)Zl-eV+UO9@iyA^{y~3lwLjOP^)8Zu zJ0zodhPyEQwP|Dti$xod7Nxlf4oCZ>wZfHGJ>rV&iBL|ED<4|p*wkN;CtPr~+(A!< z5~jvv+HW)x8L}kS+!@Dj8i*^4tAFsErh|8$Wu)mmay+c>$`t4OkgeDy$S#>lY+SNi zv!9B$_NV=ddTlT4XZx)0*f literal 0 HcmV?d00001 diff --git a/src/languages/paymentwall-for-woocommerce-ru_RU.po b/src/languages/paymentwall-for-woocommerce-ru_RU.po new file mode 100644 index 0000000..5c9abf8 --- /dev/null +++ b/src/languages/paymentwall-for-woocommerce-ru_RU.po @@ -0,0 +1,46 @@ +msgid "" +msgstr "" +"Project-Id-Version: Paymentwall for WooCommerce\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2021-08-31 10:25+0000\n" +"PO-Revision-Date: 2021-09-01 09:00+0000\n" +"Last-Translator: \n" +"Language-Team: Russian\n" +"Language: ru-RU\n" +"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10 >= 2 && " +"n%10<=4 &&(n%100<10||n%100 >= 20)? 1 : 2);\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Loco https://localise.biz/\n" +"X-Loco-Version: 2.5.3; wp-5.7.2\n" +"X-Domain: paymentwall-for-woocommerce" + +#: includes/class-paymentwall-brick.php:27 +msgid "Brick" +msgstr "" + +#. Author URI of the plugin +msgid "http://www.paymentwall.com/" +msgstr "" + +#. URI of the plugin +msgid "https://www.paymentwall.com/en/documentation/WooCommerce/1409" +msgstr "" + +#. Description of the plugin +msgid "Official Paymentwall module for WordPress WooCommerce." +msgstr "" + +#. Name of the plugin +msgid "Paymentwall for WooCommerce" +msgstr "" + +#: includes/class-paymentwall-gateway.php:118 +#, php-format +msgid "Please continue the purchase via %s using the widget below." +msgstr "Продолжайте покупку через %s используя виджет ниже." + +#. Author of the plugin +msgid "The Paymentwall Team" +msgstr "" diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9ef9900..86eaf47 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -3,14 +3,14 @@ defined('ABSPATH') or exit(); /* * Plugin Name: Paymentwall for WooCommerce - * Plugin URI: https://www.paymentwall.com/en/documentation/WooCommerce/1409 + * Plugin URI: https://docs.paymentwall.com/modules/woocommerce * Description: Official Paymentwall module for WordPress WooCommerce. - * Version: 1.7.2 + * Version: 1.7.3 * Author: The Paymentwall Team * Author URI: http://www.paymentwall.com/ * Text Domain: paymentwall-for-woocommerce * License: The MIT License (MIT) - * + * Domain Path: /languages */ define('PW_TEXT_DOMAIN', 'paymentwall-for-woocommerce'); @@ -230,3 +230,8 @@ class="input-radio pw_payment_system" name="payment_method" Date: Fri, 20 Aug 2021 10:46:20 +0700 Subject: [PATCH 05/14] Update brick to ver 1.6 --- .gitignore | 1 + src/assets/js/payment.js | 125 +++---------------- src/includes/class-paymentwall-brick.php | 147 ++++++++++++++--------- src/lib/paymentwall-php | 1 - src/paymentwall-for-woocommerce.php | 20 +++ src/templates/brick/form.html | 108 +++-------------- src/templates/pages/brick_form.php | 39 ++++++ 7 files changed, 177 insertions(+), 264 deletions(-) create mode 100644 .gitignore delete mode 160000 src/lib/paymentwall-php create mode 100644 src/templates/pages/brick_form.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 7255e8c..14a445d 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,113 +1,20 @@ -function paymentListener(orderId, baseUrl) { - var pwInterval = setInterval(function () { - var r = new XMLHttpRequest(); - r.open("POST", baseUrl + '/index.php?wc-api=paymentwall_gateway&action=ajax', true); - r.onreadystatechange = function () { - if (r.readyState != 4 || r.status != 200) return; - if (r.responseText) { - var data = JSON.parse(r.responseText); - if (data && data.status == '1') { - clearInterval(pwInterval); - location.href = data.url; - } - } - }; - var formData = new FormData(); - formData.append('order_id', orderId); - r.send(formData); - }, 5000); -} - var Brick_Payment = { brick: null, - form3Ds : '', - createBrick: function (public_key) { - this.brick = new Brick({ - public_key: public_key, - form: {formatter: true} - }, 'custom'); - }, - brickTokenizeCard: function () { - this.brick.tokenizeCard({ - card_number: jQuery('#card-number').val(), - card_expiration_month: jQuery('#card-expiration-month').val(), - card_expiration_year: jQuery('#card-expiration-year').val(), - card_cvv: jQuery('#card-cvv').val() - }, function (response) { - if (response.type == 'Error') { - var errors = "Brick error(s):
" + " - " + (typeof response.error === 'string' ? response.error : response.error.join("
- ")); - Brick_Payment.showNotification(errors, 'error'); - } else { - jQuery('#brick-token').val(response.token); - jQuery('#brick-fingerprint').val(Brick.getFingerprint()); - jQuery('#brick-get-token-success').val(1); - - Brick_Payment.sendPaymentRequest(); - window.addEventListener("message", Brick_Payment.threeDSecureMessageHandle, false); - } - }); - }, openConfirm3ds: function () { - var win = window.open("", "Brick: Verify 3D secure", "toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=1024, height=720"); - win.document.body.innerHTML += Brick_Payment.form3Ds; - win.document.forms[0].submit(); - return false; - }, threeDSecureMessageHandle: function (event) { - var origin = event.origin || event.originalEvent.origin; - if (origin !== "https://api.paymentwall.com") { - return; - } - Brick_Payment.showLoading(); - var brickData = JSON.parse(event.data); - if (brickData && brickData.event == '3dSecureComplete') { - jQuery('#hidden-brick-secure-token').val(brickData.data.secure_token); - jQuery('#hidden-brick-charge-id').val(brickData.data.charge_id); - Brick_Payment.sendPaymentRequest(); - } - }, - sendPaymentRequest: function () { - jQuery.ajax({ - type: 'POST', - url: '?wc-ajax=checkout', - data: jQuery('form.checkout').serialize(), - dataType: 'json', - encode: true, - beforeSend: function () { - Brick_Payment.showLoading(); - }, - success: function (response) { - if (response.result == 'success') { - Brick_Payment.showNotification(response.message); - window.location.href = response.redirect; - } else if (response.result == 'secure') { - Brick_Payment.form3Ds = response.secure; - var requireConfirm = "Please verify 3D-secure to continue checkout. Click here !"; - Brick_Payment.showNotification(requireConfirm); - } else if (response.result == 'failure') { - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html(response.messages); - jQuery('#brick-errors').show(); - } else { - Brick_Payment.showNotification(response.message, 'error'); + createBrick: function (public_key, amount, currency) { + "use strict"; + if (window.Brick !== undefined) { + return new Brick({ + public_key: public_key, + amount: amount, + currency: currency, + container: 'brick-payments-container', + action: 'wc-api=paymentwall_gateway&action=brick_charge', + form: { + show_zip: true, // show zip code + show_cardholder: true, + lang: 'en' } - } - }); - }, showNotification: function (message, type) { - type = (type != undefined) ? type : 'message'; - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html('
  • ' + message + '
'); - jQuery('#brick-errors').show(); - }, showLoading: function () { - jQuery('#brick-errors').hide(); - jQuery('#brick-loading').show(); + }, 'default') + } } -}; - -(function ($) { - $( document ).ready(function() { - $('.paymentwall-method .pw_payment_system').on('change', function () { - var paymentSystem = $(this).data('payment-system'); - var inputPaymentSystem = $('#pw_gateway'); - inputPaymentSystem.val(JSON.stringify(paymentSystem)); - }) - }) -})(jQuery); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2707716..2761f22 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -65,13 +65,20 @@ public function init_configs($isPingback = false) { * Displays credit card form */ public function payment_fields() { + $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); + $_SESSION['cart_total'] = WC()->cart->cart_contents_total; + $_SESSION['currency'] = $currency; + $_SESSION['private_key'] = $this->settings['privatekey']; + $_SESSION['public_key'] = $this->settings['publickey']; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); $this->tokenization_script(); $this->saved_payment_methods(); } + $brickFormUrl = PW_PLUGIN_URL . '/templates/pages/brick_form.php'; echo $this->get_template('brick/form.html', array( 'payment_id' => $this->id, @@ -79,7 +86,8 @@ public function payment_fields() { 'entry_card_number' => __("Card number", PW_TEXT_DOMAIN), 'entry_card_expiration' => __("Card expiration", PW_TEXT_DOMAIN), 'entry_card_cvv' => __("Card CVV", PW_TEXT_DOMAIN), - 'plugin_url' => PW_PLUGIN_URL + 'plugin_url' => PW_PLUGIN_URL, + 'brick_form_url' => $brickFormUrl, )); $hasSubscription = class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription(); @@ -114,11 +122,6 @@ public function process_payment($order_id) { * @throws Exception */ function prepare_card_info($order) { - if (!isset($_POST['brick'])) { - throw new Exception("Payment Invalid!"); - } - - $brick = $_POST['brick']; $data = array( 'amount' => $order->get_total(), 'currency' => $order->get_currency(), @@ -126,39 +129,10 @@ function prepare_card_info($order) { 'plan' => !method_exists($order, 'get_id') ? $order->id : $order->get_id(), 'description' => sprintf(__('%s - Order #%s', PW_TEXT_DOMAIN), esc_html(get_bloginfo('name', 'display')), $order->get_order_number()), ); - if ($brick['token'] && $brick['fingerprint']) { - $data = array_merge($data, array( - 'token' => $brick['token'], - 'fingerprint' => $brick['fingerprint'] - )); - } elseif (!empty($_POST['wc-brick-payment-token'])) { - $token = WC_Payment_Tokens::get($_POST['wc-brick-payment-token'])->get_token(); - $data = array_merge($data, array( - 'token' => $token - )); - } - if (!empty($brick['cc_brick_secure_token'])) { - $data['secure_token'] = $brick['cc_brick_secure_token']; - } - if (!empty($brick['cc_brick_charge_id'])) { - $data['charge_id'] = $brick['cc_brick_charge_id']; - } return $data; } - /** - * Add custom fields validation - */ - public function brick_fields_validation() { - if ($_POST['payment_method'] == $this->id) { - $brick = $_POST['brick']; - - if ((trim($brick['token']) == '' || trim($brick['fingerprint']) == '') && empty($_POST['wc-brick-payment-token'])) - wc_add_notice(sprintf(__('The %s payment has some errors. Please try again.', PW_TEXT_DOMAIN), $this->title), 'error'); - } - } - /** * @param $order * @return array @@ -166,33 +140,14 @@ public function brick_fields_validation() { */ public function process_standard_payment($order) { $return = array(); - $cardInfo = $this->prepare_card_info($order); - $charge = new Paymentwall_Charge(); - $charge->create(array_merge( - $this->prepare_user_profile_data($order), // for User Profile API - $cardInfo, - $this->get_extra_data($order) - )); - - $response = $charge->getPublicData(); - $responseData = json_decode($charge->getRawResponseData(), true); + session_start(); - if ($charge->isSuccessful() && empty($responseData['secure'])) { + $charge = $_SESSION['charge']; + $responseData = $_SESSION['charge_response_data']; + if (empty($responseData['secure'])) { $return['result'] = 'success'; $return['redirect'] = $this->process_success($order, $charge, $message); $return['message'] = $message; - - if (is_checkout() && !empty($_POST['wc-brick-new-payment-method']) && $_POST['wc-brick-payment-token'] == 'new') { - $token = new WC_Payment_Token_CC(); - $token->set_token($responseData['card']['token']); - $token->set_gateway_id($this->id); - $token->set_card_type($responseData['card']['type']); - $token->set_last4($responseData['card']['last4']); - $token->set_expiry_month($responseData['card']['exp_month']); - $token->set_expiry_year('20' . $responseData['card']['exp_year']); - $token->set_user_id(get_current_user_id()); - $token->save(); - } } elseif (!empty($responseData['secure'])) { WC()->session->set('orderId', !method_exists($order, 'get_id') ? $order->id : $order->get_id()); $return['result'] = 'secure'; @@ -217,16 +172,22 @@ public function get_extra_data($order) { } public function process_success($order, $charge, &$message) { - if ($charge->isCaptured()) { + if ($charge->isCaptured() && $charge->isUnderReview()) { // Add order note $order->add_order_note(sprintf( __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), $charge->getId())); // Payment complete - $message = "Your order has been received !"; + $message = "Your order has been received and is under review!"; } elseif ($charge->isUnderReview()) { $order->update_status('on-hold'); $message = 'Your order is under review !'; + } elseif ($charge->isCaptured()) { + $order->add_order_note(sprintf( + __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), + $charge->getId())); + // Payment complete + $message = "Your order has been received!"; } $thanksPage = $this->get_return_url($order); @@ -245,4 +206,70 @@ public static function get_available_payment_gateways( $available_gateways ) { return $available_gateways; } + + public function handle_brick_charge() + { + session_start(); + + $this->init_configs(); + $parameters = $_POST; + $chargeInfo = $this->getChargeInfo($parameters); + $charge = $this->createCharge($chargeInfo); + $response = $charge->getPublicData(); + $responseData = json_decode($charge->getRawResponseData(), true); + $result = []; + $result['payment'] = $responseData; + $result = array_merge($result, json_decode($response, true)); + $_SESSION['charge_response_data'] = $responseData; + $_SESSION['charge'] = $charge; + + if ($charge->isSuccessful()) { + if ($charge->isCaptured()) { + $result = json_encode($result); + echo $result; + } elseif ($charge->isUnderReview()) { + echo 'Under review'; + exit(); + } + } + else { + if (isset($result['payment']['secure']['formHTML'])) { + $resultError['success'] = 0; + $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; + $resultError = json_encode($resultError); + echo $resultError; + } + } + } + + function getChargeInfo($params) + { + $chargeInfo = [ + 'email' => $params['email'], + 'history[registration_date]' => '1489655092', + 'amount' => (float) $_SESSION['cart_total'], + 'currency' => $_SESSION['currency'], + 'token' => $params['brick_token'], + 'fingerprint' => $params['brick_fingerprint'], + 'description' => 'Brick Paymentwall' + ]; + if (isset($params['brick_charge_id']) && isset($params['brick_secure_token'])) { + $chargeInfo['charge_id'] = $params['brick_charge_id']; + $chargeInfo['secure_token'] = $params['brick_secure_token']; + } + if (!empty($params['brick_reference_id'])) { + $chargeInfo['reference_id'] = $params['brick_reference_id']; + } + + return $chargeInfo; + } + + function createCharge($chargeInfo) + { + $charge = new Paymentwall_Charge(); + $charge->create($chargeInfo); + + return $charge; + } + } diff --git a/src/lib/paymentwall-php b/src/lib/paymentwall-php deleted file mode 160000 index b23bfb6..0000000 --- a/src/lib/paymentwall-php +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b23bfb6dd5667e729f19f375de5dc5d07ccc81d4 diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9ef9900..95cab5d 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -230,3 +230,23 @@ class="input-radio pw_payment_system" name="payment_method" request ); + $siteUrl = get_site_url(); + if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { + return false; + } + // Check if it's a valid request. + $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); + if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { + $brick = new Paymentwall_Brick(); + $brick->handle_brick_charge(); + } + die('Process completed' ); +} + +add_action( 'template_redirect', 'handle_brick_charge', 0); + diff --git a/src/templates/brick/form.html b/src/templates/brick/form.html index 01b1cd7..cd4503f 100644 --- a/src/templates/brick/form.html +++ b/src/templates/brick/form.html @@ -1,98 +1,18 @@ - -
- - - - - - - - -

- - -

- -

- - - - / - -

- -

- - - - -

-
-
- + + + - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php new file mode 100644 index 0000000..4be40f4 --- /dev/null +++ b/src/templates/pages/brick_form.php @@ -0,0 +1,39 @@ + + +
+ + + \ No newline at end of file From 592d94831750734c4cb4f652e680c5e8840ea98a Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Wed, 1 Sep 2021 00:23:52 +0700 Subject: [PATCH 06/14] Fix action brick charge --- src/assets/js/payment.js | 4 ++-- src/includes/class-paymentwall-brick.php | 12 ++++++++---- src/includes/class-paymentwall-gateway.php | 11 ++++++++++- src/paymentwall-for-woocommerce.php | 19 ------------------- src/templates/pages/brick_form.php | 17 +++++++++-------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 14a445d..0263921 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,6 +1,6 @@ var Brick_Payment = { brick: null, - createBrick: function (public_key, amount, currency) { + createBrick: function (public_key, amount, currency, action) { "use strict"; if (window.Brick !== undefined) { return new Brick({ @@ -8,7 +8,7 @@ var Brick_Payment = { amount: amount, currency: currency, container: 'brick-payments-container', - action: 'wc-api=paymentwall_gateway&action=brick_charge', + action: action, form: { show_zip: true, // show zip code show_cardholder: true, diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2761f22..def6259 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -67,11 +67,13 @@ public function init_configs($isPingback = false) { public function payment_fields() { $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); $_SESSION['cart_total'] = WC()->cart->cart_contents_total; $_SESSION['currency'] = $currency; $_SESSION['private_key'] = $this->settings['privatekey']; $_SESSION['public_key'] = $this->settings['publickey']; + $_SESSION['brick_form_action'] = get_site_url() . '/?wc-api=paymentwall_gateway&action=brick_charge'; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); @@ -226,10 +228,11 @@ public function handle_brick_charge() if ($charge->isSuccessful()) { if ($charge->isCaptured()) { $result = json_encode($result); - echo $result; + var_dump($result); + die(); } elseif ($charge->isUnderReview()) { - echo 'Under review'; - exit(); + var_dump('under_review'); + die(); } } else { @@ -237,7 +240,8 @@ public function handle_brick_charge() $resultError['success'] = 0; $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; $resultError = json_encode($resultError); - echo $resultError; + var_dump($resultError); + die(); } } } diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 1f803eb..2ec7156 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -247,7 +247,7 @@ function ipn_response() { $payment->init_configs(true); $pingback_params = $_GET; - + $pingback = new Paymentwall_Pingback($pingback_params, $this->getRealClientIP()); if ($pingback->validate(true)) { @@ -332,6 +332,9 @@ function handle_action() { case 'ipn': $this->ipn_response(); break; + case 'brick_charge': + $this->handle_brick_charge(); + break; default: break; } @@ -541,4 +544,10 @@ public function save_data_to_session($name, $data) { } } + public function handle_brick_charge() + { + $paymentwallBrick = new Paymentwall_Brick(); + + return $paymentwallBrick->handle_brick_charge(); + } } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 95cab5d..9825be4 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -231,22 +231,3 @@ class="input-radio pw_payment_system" name="payment_method" } } -function handle_brick_charge() { - // Check if we're on the correct url - global $wp; - $current_slug = add_query_arg( array(), $wp->request ); - $siteUrl = get_site_url(); - if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { - return false; - } - // Check if it's a valid request. - $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); - if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { - $brick = new Paymentwall_Brick(); - $brick->handle_brick_charge(); - } - die('Process completed' ); -} - -add_action( 'template_redirect', 'handle_brick_charge', 0); - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php index 4be40f4..79cad5e 100644 --- a/src/templates/pages/brick_form.php +++ b/src/templates/pages/brick_form.php @@ -1,4 +1,5 @@ +
+ +
+ + + + + + + + +

+ + +

+ +

+ + + + / + +

+ +

+ + + + +

+
+
+ + diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php deleted file mode 100644 index 79cad5e..0000000 --- a/src/templates/pages/brick_form.php +++ /dev/null @@ -1,40 +0,0 @@ - - - -
- - - \ No newline at end of file From a3588d08fdf9ac20f26bb6266489f83fe201c01e Mon Sep 17 00:00:00 2001 From: richardtong Date: Mon, 6 Sep 2021 13:12:45 +0700 Subject: [PATCH 08/14] Update brick to ver 1.6 # Conflicts: # src/paymentwall-for-woocommerce.php --- .gitignore | 1 + src/assets/js/payment.js | 125 +++---------------- src/includes/class-paymentwall-brick.php | 147 ++++++++++++++--------- src/lib/paymentwall-php | 1 - src/paymentwall-for-woocommerce.php | 20 +++ src/templates/brick/form.html | 108 +++-------------- src/templates/pages/brick_form.php | 39 ++++++ 7 files changed, 177 insertions(+), 264 deletions(-) create mode 100644 .gitignore delete mode 160000 src/lib/paymentwall-php create mode 100644 src/templates/pages/brick_form.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 7255e8c..14a445d 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,113 +1,20 @@ -function paymentListener(orderId, baseUrl) { - var pwInterval = setInterval(function () { - var r = new XMLHttpRequest(); - r.open("POST", baseUrl + '/index.php?wc-api=paymentwall_gateway&action=ajax', true); - r.onreadystatechange = function () { - if (r.readyState != 4 || r.status != 200) return; - if (r.responseText) { - var data = JSON.parse(r.responseText); - if (data && data.status == '1') { - clearInterval(pwInterval); - location.href = data.url; - } - } - }; - var formData = new FormData(); - formData.append('order_id', orderId); - r.send(formData); - }, 5000); -} - var Brick_Payment = { brick: null, - form3Ds : '', - createBrick: function (public_key) { - this.brick = new Brick({ - public_key: public_key, - form: {formatter: true} - }, 'custom'); - }, - brickTokenizeCard: function () { - this.brick.tokenizeCard({ - card_number: jQuery('#card-number').val(), - card_expiration_month: jQuery('#card-expiration-month').val(), - card_expiration_year: jQuery('#card-expiration-year').val(), - card_cvv: jQuery('#card-cvv').val() - }, function (response) { - if (response.type == 'Error') { - var errors = "Brick error(s):
" + " - " + (typeof response.error === 'string' ? response.error : response.error.join("
- ")); - Brick_Payment.showNotification(errors, 'error'); - } else { - jQuery('#brick-token').val(response.token); - jQuery('#brick-fingerprint').val(Brick.getFingerprint()); - jQuery('#brick-get-token-success').val(1); - - Brick_Payment.sendPaymentRequest(); - window.addEventListener("message", Brick_Payment.threeDSecureMessageHandle, false); - } - }); - }, openConfirm3ds: function () { - var win = window.open("", "Brick: Verify 3D secure", "toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=1024, height=720"); - win.document.body.innerHTML += Brick_Payment.form3Ds; - win.document.forms[0].submit(); - return false; - }, threeDSecureMessageHandle: function (event) { - var origin = event.origin || event.originalEvent.origin; - if (origin !== "https://api.paymentwall.com") { - return; - } - Brick_Payment.showLoading(); - var brickData = JSON.parse(event.data); - if (brickData && brickData.event == '3dSecureComplete') { - jQuery('#hidden-brick-secure-token').val(brickData.data.secure_token); - jQuery('#hidden-brick-charge-id').val(brickData.data.charge_id); - Brick_Payment.sendPaymentRequest(); - } - }, - sendPaymentRequest: function () { - jQuery.ajax({ - type: 'POST', - url: '?wc-ajax=checkout', - data: jQuery('form.checkout').serialize(), - dataType: 'json', - encode: true, - beforeSend: function () { - Brick_Payment.showLoading(); - }, - success: function (response) { - if (response.result == 'success') { - Brick_Payment.showNotification(response.message); - window.location.href = response.redirect; - } else if (response.result == 'secure') { - Brick_Payment.form3Ds = response.secure; - var requireConfirm = "Please verify 3D-secure to continue checkout. Click here !"; - Brick_Payment.showNotification(requireConfirm); - } else if (response.result == 'failure') { - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html(response.messages); - jQuery('#brick-errors').show(); - } else { - Brick_Payment.showNotification(response.message, 'error'); + createBrick: function (public_key, amount, currency) { + "use strict"; + if (window.Brick !== undefined) { + return new Brick({ + public_key: public_key, + amount: amount, + currency: currency, + container: 'brick-payments-container', + action: 'wc-api=paymentwall_gateway&action=brick_charge', + form: { + show_zip: true, // show zip code + show_cardholder: true, + lang: 'en' } - } - }); - }, showNotification: function (message, type) { - type = (type != undefined) ? type : 'message'; - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html('
  • ' + message + '
'); - jQuery('#brick-errors').show(); - }, showLoading: function () { - jQuery('#brick-errors').hide(); - jQuery('#brick-loading').show(); + }, 'default') + } } -}; - -(function ($) { - $( document ).ready(function() { - $('.paymentwall-method .pw_payment_system').on('change', function () { - var paymentSystem = $(this).data('payment-system'); - var inputPaymentSystem = $('#pw_gateway'); - inputPaymentSystem.val(JSON.stringify(paymentSystem)); - }) - }) -})(jQuery); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2707716..2761f22 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -65,13 +65,20 @@ public function init_configs($isPingback = false) { * Displays credit card form */ public function payment_fields() { + $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); + $_SESSION['cart_total'] = WC()->cart->cart_contents_total; + $_SESSION['currency'] = $currency; + $_SESSION['private_key'] = $this->settings['privatekey']; + $_SESSION['public_key'] = $this->settings['publickey']; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); $this->tokenization_script(); $this->saved_payment_methods(); } + $brickFormUrl = PW_PLUGIN_URL . '/templates/pages/brick_form.php'; echo $this->get_template('brick/form.html', array( 'payment_id' => $this->id, @@ -79,7 +86,8 @@ public function payment_fields() { 'entry_card_number' => __("Card number", PW_TEXT_DOMAIN), 'entry_card_expiration' => __("Card expiration", PW_TEXT_DOMAIN), 'entry_card_cvv' => __("Card CVV", PW_TEXT_DOMAIN), - 'plugin_url' => PW_PLUGIN_URL + 'plugin_url' => PW_PLUGIN_URL, + 'brick_form_url' => $brickFormUrl, )); $hasSubscription = class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription(); @@ -114,11 +122,6 @@ public function process_payment($order_id) { * @throws Exception */ function prepare_card_info($order) { - if (!isset($_POST['brick'])) { - throw new Exception("Payment Invalid!"); - } - - $brick = $_POST['brick']; $data = array( 'amount' => $order->get_total(), 'currency' => $order->get_currency(), @@ -126,39 +129,10 @@ function prepare_card_info($order) { 'plan' => !method_exists($order, 'get_id') ? $order->id : $order->get_id(), 'description' => sprintf(__('%s - Order #%s', PW_TEXT_DOMAIN), esc_html(get_bloginfo('name', 'display')), $order->get_order_number()), ); - if ($brick['token'] && $brick['fingerprint']) { - $data = array_merge($data, array( - 'token' => $brick['token'], - 'fingerprint' => $brick['fingerprint'] - )); - } elseif (!empty($_POST['wc-brick-payment-token'])) { - $token = WC_Payment_Tokens::get($_POST['wc-brick-payment-token'])->get_token(); - $data = array_merge($data, array( - 'token' => $token - )); - } - if (!empty($brick['cc_brick_secure_token'])) { - $data['secure_token'] = $brick['cc_brick_secure_token']; - } - if (!empty($brick['cc_brick_charge_id'])) { - $data['charge_id'] = $brick['cc_brick_charge_id']; - } return $data; } - /** - * Add custom fields validation - */ - public function brick_fields_validation() { - if ($_POST['payment_method'] == $this->id) { - $brick = $_POST['brick']; - - if ((trim($brick['token']) == '' || trim($brick['fingerprint']) == '') && empty($_POST['wc-brick-payment-token'])) - wc_add_notice(sprintf(__('The %s payment has some errors. Please try again.', PW_TEXT_DOMAIN), $this->title), 'error'); - } - } - /** * @param $order * @return array @@ -166,33 +140,14 @@ public function brick_fields_validation() { */ public function process_standard_payment($order) { $return = array(); - $cardInfo = $this->prepare_card_info($order); - $charge = new Paymentwall_Charge(); - $charge->create(array_merge( - $this->prepare_user_profile_data($order), // for User Profile API - $cardInfo, - $this->get_extra_data($order) - )); - - $response = $charge->getPublicData(); - $responseData = json_decode($charge->getRawResponseData(), true); + session_start(); - if ($charge->isSuccessful() && empty($responseData['secure'])) { + $charge = $_SESSION['charge']; + $responseData = $_SESSION['charge_response_data']; + if (empty($responseData['secure'])) { $return['result'] = 'success'; $return['redirect'] = $this->process_success($order, $charge, $message); $return['message'] = $message; - - if (is_checkout() && !empty($_POST['wc-brick-new-payment-method']) && $_POST['wc-brick-payment-token'] == 'new') { - $token = new WC_Payment_Token_CC(); - $token->set_token($responseData['card']['token']); - $token->set_gateway_id($this->id); - $token->set_card_type($responseData['card']['type']); - $token->set_last4($responseData['card']['last4']); - $token->set_expiry_month($responseData['card']['exp_month']); - $token->set_expiry_year('20' . $responseData['card']['exp_year']); - $token->set_user_id(get_current_user_id()); - $token->save(); - } } elseif (!empty($responseData['secure'])) { WC()->session->set('orderId', !method_exists($order, 'get_id') ? $order->id : $order->get_id()); $return['result'] = 'secure'; @@ -217,16 +172,22 @@ public function get_extra_data($order) { } public function process_success($order, $charge, &$message) { - if ($charge->isCaptured()) { + if ($charge->isCaptured() && $charge->isUnderReview()) { // Add order note $order->add_order_note(sprintf( __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), $charge->getId())); // Payment complete - $message = "Your order has been received !"; + $message = "Your order has been received and is under review!"; } elseif ($charge->isUnderReview()) { $order->update_status('on-hold'); $message = 'Your order is under review !'; + } elseif ($charge->isCaptured()) { + $order->add_order_note(sprintf( + __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), + $charge->getId())); + // Payment complete + $message = "Your order has been received!"; } $thanksPage = $this->get_return_url($order); @@ -245,4 +206,70 @@ public static function get_available_payment_gateways( $available_gateways ) { return $available_gateways; } + + public function handle_brick_charge() + { + session_start(); + + $this->init_configs(); + $parameters = $_POST; + $chargeInfo = $this->getChargeInfo($parameters); + $charge = $this->createCharge($chargeInfo); + $response = $charge->getPublicData(); + $responseData = json_decode($charge->getRawResponseData(), true); + $result = []; + $result['payment'] = $responseData; + $result = array_merge($result, json_decode($response, true)); + $_SESSION['charge_response_data'] = $responseData; + $_SESSION['charge'] = $charge; + + if ($charge->isSuccessful()) { + if ($charge->isCaptured()) { + $result = json_encode($result); + echo $result; + } elseif ($charge->isUnderReview()) { + echo 'Under review'; + exit(); + } + } + else { + if (isset($result['payment']['secure']['formHTML'])) { + $resultError['success'] = 0; + $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; + $resultError = json_encode($resultError); + echo $resultError; + } + } + } + + function getChargeInfo($params) + { + $chargeInfo = [ + 'email' => $params['email'], + 'history[registration_date]' => '1489655092', + 'amount' => (float) $_SESSION['cart_total'], + 'currency' => $_SESSION['currency'], + 'token' => $params['brick_token'], + 'fingerprint' => $params['brick_fingerprint'], + 'description' => 'Brick Paymentwall' + ]; + if (isset($params['brick_charge_id']) && isset($params['brick_secure_token'])) { + $chargeInfo['charge_id'] = $params['brick_charge_id']; + $chargeInfo['secure_token'] = $params['brick_secure_token']; + } + if (!empty($params['brick_reference_id'])) { + $chargeInfo['reference_id'] = $params['brick_reference_id']; + } + + return $chargeInfo; + } + + function createCharge($chargeInfo) + { + $charge = new Paymentwall_Charge(); + $charge->create($chargeInfo); + + return $charge; + } + } diff --git a/src/lib/paymentwall-php b/src/lib/paymentwall-php deleted file mode 160000 index b23bfb6..0000000 --- a/src/lib/paymentwall-php +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b23bfb6dd5667e729f19f375de5dc5d07ccc81d4 diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 86eaf47..9dc8bee 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -231,6 +231,26 @@ class="input-radio pw_payment_system" name="payment_method" } } +function handle_brick_charge() { + // Check if we're on the correct url + global $wp; + $current_slug = add_query_arg( array(), $wp->request ); + $siteUrl = get_site_url(); + if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { + return false; + } + // Check if it's a valid request. + $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); + if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { + $brick = new Paymentwall_Brick(); + $brick->handle_brick_charge(); + } + die('Process completed' ); +} + +add_action( 'template_redirect', 'handle_brick_charge', 0); + + add_action( 'init', 'paymentwall_load_textdomain' ); function paymentwall_load_textdomain() { load_plugin_textdomain( PW_TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); diff --git a/src/templates/brick/form.html b/src/templates/brick/form.html index 01b1cd7..cd4503f 100644 --- a/src/templates/brick/form.html +++ b/src/templates/brick/form.html @@ -1,98 +1,18 @@ - -
- - - - - - - - -

- - -

- -

- - - - / - -

- -

- - - - -

-
-
- + + + - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php new file mode 100644 index 0000000..4be40f4 --- /dev/null +++ b/src/templates/pages/brick_form.php @@ -0,0 +1,39 @@ + + +
+ + + \ No newline at end of file From 6cf9e91752d08b93f721988569380879fdcd12f0 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Wed, 1 Sep 2021 00:23:52 +0700 Subject: [PATCH 09/14] Fix action brick charge --- src/assets/js/payment.js | 4 ++-- src/includes/class-paymentwall-brick.php | 12 ++++++++---- src/includes/class-paymentwall-gateway.php | 11 ++++++++++- src/paymentwall-for-woocommerce.php | 19 ------------------- src/templates/pages/brick_form.php | 17 +++++++++-------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 14a445d..0263921 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,6 +1,6 @@ var Brick_Payment = { brick: null, - createBrick: function (public_key, amount, currency) { + createBrick: function (public_key, amount, currency, action) { "use strict"; if (window.Brick !== undefined) { return new Brick({ @@ -8,7 +8,7 @@ var Brick_Payment = { amount: amount, currency: currency, container: 'brick-payments-container', - action: 'wc-api=paymentwall_gateway&action=brick_charge', + action: action, form: { show_zip: true, // show zip code show_cardholder: true, diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2761f22..def6259 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -67,11 +67,13 @@ public function init_configs($isPingback = false) { public function payment_fields() { $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); $_SESSION['cart_total'] = WC()->cart->cart_contents_total; $_SESSION['currency'] = $currency; $_SESSION['private_key'] = $this->settings['privatekey']; $_SESSION['public_key'] = $this->settings['publickey']; + $_SESSION['brick_form_action'] = get_site_url() . '/?wc-api=paymentwall_gateway&action=brick_charge'; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); @@ -226,10 +228,11 @@ public function handle_brick_charge() if ($charge->isSuccessful()) { if ($charge->isCaptured()) { $result = json_encode($result); - echo $result; + var_dump($result); + die(); } elseif ($charge->isUnderReview()) { - echo 'Under review'; - exit(); + var_dump('under_review'); + die(); } } else { @@ -237,7 +240,8 @@ public function handle_brick_charge() $resultError['success'] = 0; $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; $resultError = json_encode($resultError); - echo $resultError; + var_dump($resultError); + die(); } } } diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 1f803eb..2ec7156 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -247,7 +247,7 @@ function ipn_response() { $payment->init_configs(true); $pingback_params = $_GET; - + $pingback = new Paymentwall_Pingback($pingback_params, $this->getRealClientIP()); if ($pingback->validate(true)) { @@ -332,6 +332,9 @@ function handle_action() { case 'ipn': $this->ipn_response(); break; + case 'brick_charge': + $this->handle_brick_charge(); + break; default: break; } @@ -541,4 +544,10 @@ public function save_data_to_session($name, $data) { } } + public function handle_brick_charge() + { + $paymentwallBrick = new Paymentwall_Brick(); + + return $paymentwallBrick->handle_brick_charge(); + } } diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 9dc8bee..016de12 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -231,25 +231,6 @@ class="input-radio pw_payment_system" name="payment_method" } } -function handle_brick_charge() { - // Check if we're on the correct url - global $wp; - $current_slug = add_query_arg( array(), $wp->request ); - $siteUrl = get_site_url(); - if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { - return false; - } - // Check if it's a valid request. - $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); - if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { - $brick = new Paymentwall_Brick(); - $brick->handle_brick_charge(); - } - die('Process completed' ); -} - -add_action( 'template_redirect', 'handle_brick_charge', 0); - add_action( 'init', 'paymentwall_load_textdomain' ); function paymentwall_load_textdomain() { diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php index 4be40f4..79cad5e 100644 --- a/src/templates/pages/brick_form.php +++ b/src/templates/pages/brick_form.php @@ -1,4 +1,5 @@ +
+ +
+ + + + + + + + +

+ + +

+ +

+ + + + / + +

+ +

+ + + + +

+
+
+ + diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php deleted file mode 100644 index 79cad5e..0000000 --- a/src/templates/pages/brick_form.php +++ /dev/null @@ -1,40 +0,0 @@ - - - -
- - - \ No newline at end of file From 4f7835f83941bf0307c623c4f737bd0510d93e91 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Fri, 20 Aug 2021 10:46:20 +0700 Subject: [PATCH 11/14] Update brick to ver 1.6 --- .gitignore | 1 + src/assets/js/payment.js | 125 +++---------------- src/includes/class-paymentwall-brick.php | 147 ++++++++++++++--------- src/lib/paymentwall-php | 1 - src/paymentwall-for-woocommerce.php | 20 +++ src/templates/brick/form.html | 108 +++-------------- src/templates/pages/brick_form.php | 39 ++++++ 7 files changed, 177 insertions(+), 264 deletions(-) create mode 100644 .gitignore delete mode 160000 src/lib/paymentwall-php create mode 100644 src/templates/pages/brick_form.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 7255e8c..14a445d 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,113 +1,20 @@ -function paymentListener(orderId, baseUrl) { - var pwInterval = setInterval(function () { - var r = new XMLHttpRequest(); - r.open("POST", baseUrl + '/index.php?wc-api=paymentwall_gateway&action=ajax', true); - r.onreadystatechange = function () { - if (r.readyState != 4 || r.status != 200) return; - if (r.responseText) { - var data = JSON.parse(r.responseText); - if (data && data.status == '1') { - clearInterval(pwInterval); - location.href = data.url; - } - } - }; - var formData = new FormData(); - formData.append('order_id', orderId); - r.send(formData); - }, 5000); -} - var Brick_Payment = { brick: null, - form3Ds : '', - createBrick: function (public_key) { - this.brick = new Brick({ - public_key: public_key, - form: {formatter: true} - }, 'custom'); - }, - brickTokenizeCard: function () { - this.brick.tokenizeCard({ - card_number: jQuery('#card-number').val(), - card_expiration_month: jQuery('#card-expiration-month').val(), - card_expiration_year: jQuery('#card-expiration-year').val(), - card_cvv: jQuery('#card-cvv').val() - }, function (response) { - if (response.type == 'Error') { - var errors = "Brick error(s):
" + " - " + (typeof response.error === 'string' ? response.error : response.error.join("
- ")); - Brick_Payment.showNotification(errors, 'error'); - } else { - jQuery('#brick-token').val(response.token); - jQuery('#brick-fingerprint').val(Brick.getFingerprint()); - jQuery('#brick-get-token-success').val(1); - - Brick_Payment.sendPaymentRequest(); - window.addEventListener("message", Brick_Payment.threeDSecureMessageHandle, false); - } - }); - }, openConfirm3ds: function () { - var win = window.open("", "Brick: Verify 3D secure", "toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=1024, height=720"); - win.document.body.innerHTML += Brick_Payment.form3Ds; - win.document.forms[0].submit(); - return false; - }, threeDSecureMessageHandle: function (event) { - var origin = event.origin || event.originalEvent.origin; - if (origin !== "https://api.paymentwall.com") { - return; - } - Brick_Payment.showLoading(); - var brickData = JSON.parse(event.data); - if (brickData && brickData.event == '3dSecureComplete') { - jQuery('#hidden-brick-secure-token').val(brickData.data.secure_token); - jQuery('#hidden-brick-charge-id').val(brickData.data.charge_id); - Brick_Payment.sendPaymentRequest(); - } - }, - sendPaymentRequest: function () { - jQuery.ajax({ - type: 'POST', - url: '?wc-ajax=checkout', - data: jQuery('form.checkout').serialize(), - dataType: 'json', - encode: true, - beforeSend: function () { - Brick_Payment.showLoading(); - }, - success: function (response) { - if (response.result == 'success') { - Brick_Payment.showNotification(response.message); - window.location.href = response.redirect; - } else if (response.result == 'secure') { - Brick_Payment.form3Ds = response.secure; - var requireConfirm = "Please verify 3D-secure to continue checkout. Click here !"; - Brick_Payment.showNotification(requireConfirm); - } else if (response.result == 'failure') { - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html(response.messages); - jQuery('#brick-errors').show(); - } else { - Brick_Payment.showNotification(response.message, 'error'); + createBrick: function (public_key, amount, currency) { + "use strict"; + if (window.Brick !== undefined) { + return new Brick({ + public_key: public_key, + amount: amount, + currency: currency, + container: 'brick-payments-container', + action: 'wc-api=paymentwall_gateway&action=brick_charge', + form: { + show_zip: true, // show zip code + show_cardholder: true, + lang: 'en' } - } - }); - }, showNotification: function (message, type) { - type = (type != undefined) ? type : 'message'; - jQuery('#brick-loading').hide(); - jQuery('#brick-errors').html('
  • ' + message + '
'); - jQuery('#brick-errors').show(); - }, showLoading: function () { - jQuery('#brick-errors').hide(); - jQuery('#brick-loading').show(); + }, 'default') + } } -}; - -(function ($) { - $( document ).ready(function() { - $('.paymentwall-method .pw_payment_system').on('change', function () { - var paymentSystem = $(this).data('payment-system'); - var inputPaymentSystem = $('#pw_gateway'); - inputPaymentSystem.val(JSON.stringify(paymentSystem)); - }) - }) -})(jQuery); \ No newline at end of file +}; \ No newline at end of file diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2707716..2761f22 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -65,13 +65,20 @@ public function init_configs($isPingback = false) { * Displays credit card form */ public function payment_fields() { + $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); + $_SESSION['cart_total'] = WC()->cart->cart_contents_total; + $_SESSION['currency'] = $currency; + $_SESSION['private_key'] = $this->settings['privatekey']; + $_SESSION['public_key'] = $this->settings['publickey']; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); $this->tokenization_script(); $this->saved_payment_methods(); } + $brickFormUrl = PW_PLUGIN_URL . '/templates/pages/brick_form.php'; echo $this->get_template('brick/form.html', array( 'payment_id' => $this->id, @@ -79,7 +86,8 @@ public function payment_fields() { 'entry_card_number' => __("Card number", PW_TEXT_DOMAIN), 'entry_card_expiration' => __("Card expiration", PW_TEXT_DOMAIN), 'entry_card_cvv' => __("Card CVV", PW_TEXT_DOMAIN), - 'plugin_url' => PW_PLUGIN_URL + 'plugin_url' => PW_PLUGIN_URL, + 'brick_form_url' => $brickFormUrl, )); $hasSubscription = class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscription(); @@ -114,11 +122,6 @@ public function process_payment($order_id) { * @throws Exception */ function prepare_card_info($order) { - if (!isset($_POST['brick'])) { - throw new Exception("Payment Invalid!"); - } - - $brick = $_POST['brick']; $data = array( 'amount' => $order->get_total(), 'currency' => $order->get_currency(), @@ -126,39 +129,10 @@ function prepare_card_info($order) { 'plan' => !method_exists($order, 'get_id') ? $order->id : $order->get_id(), 'description' => sprintf(__('%s - Order #%s', PW_TEXT_DOMAIN), esc_html(get_bloginfo('name', 'display')), $order->get_order_number()), ); - if ($brick['token'] && $brick['fingerprint']) { - $data = array_merge($data, array( - 'token' => $brick['token'], - 'fingerprint' => $brick['fingerprint'] - )); - } elseif (!empty($_POST['wc-brick-payment-token'])) { - $token = WC_Payment_Tokens::get($_POST['wc-brick-payment-token'])->get_token(); - $data = array_merge($data, array( - 'token' => $token - )); - } - if (!empty($brick['cc_brick_secure_token'])) { - $data['secure_token'] = $brick['cc_brick_secure_token']; - } - if (!empty($brick['cc_brick_charge_id'])) { - $data['charge_id'] = $brick['cc_brick_charge_id']; - } return $data; } - /** - * Add custom fields validation - */ - public function brick_fields_validation() { - if ($_POST['payment_method'] == $this->id) { - $brick = $_POST['brick']; - - if ((trim($brick['token']) == '' || trim($brick['fingerprint']) == '') && empty($_POST['wc-brick-payment-token'])) - wc_add_notice(sprintf(__('The %s payment has some errors. Please try again.', PW_TEXT_DOMAIN), $this->title), 'error'); - } - } - /** * @param $order * @return array @@ -166,33 +140,14 @@ public function brick_fields_validation() { */ public function process_standard_payment($order) { $return = array(); - $cardInfo = $this->prepare_card_info($order); - $charge = new Paymentwall_Charge(); - $charge->create(array_merge( - $this->prepare_user_profile_data($order), // for User Profile API - $cardInfo, - $this->get_extra_data($order) - )); - - $response = $charge->getPublicData(); - $responseData = json_decode($charge->getRawResponseData(), true); + session_start(); - if ($charge->isSuccessful() && empty($responseData['secure'])) { + $charge = $_SESSION['charge']; + $responseData = $_SESSION['charge_response_data']; + if (empty($responseData['secure'])) { $return['result'] = 'success'; $return['redirect'] = $this->process_success($order, $charge, $message); $return['message'] = $message; - - if (is_checkout() && !empty($_POST['wc-brick-new-payment-method']) && $_POST['wc-brick-payment-token'] == 'new') { - $token = new WC_Payment_Token_CC(); - $token->set_token($responseData['card']['token']); - $token->set_gateway_id($this->id); - $token->set_card_type($responseData['card']['type']); - $token->set_last4($responseData['card']['last4']); - $token->set_expiry_month($responseData['card']['exp_month']); - $token->set_expiry_year('20' . $responseData['card']['exp_year']); - $token->set_user_id(get_current_user_id()); - $token->save(); - } } elseif (!empty($responseData['secure'])) { WC()->session->set('orderId', !method_exists($order, 'get_id') ? $order->id : $order->get_id()); $return['result'] = 'secure'; @@ -217,16 +172,22 @@ public function get_extra_data($order) { } public function process_success($order, $charge, &$message) { - if ($charge->isCaptured()) { + if ($charge->isCaptured() && $charge->isUnderReview()) { // Add order note $order->add_order_note(sprintf( __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), $charge->getId())); // Payment complete - $message = "Your order has been received !"; + $message = "Your order has been received and is under review!"; } elseif ($charge->isUnderReview()) { $order->update_status('on-hold'); $message = 'Your order is under review !'; + } elseif ($charge->isCaptured()) { + $order->add_order_note(sprintf( + __('Brick payment approved (ID: %s)', PW_TEXT_DOMAIN), + $charge->getId())); + // Payment complete + $message = "Your order has been received!"; } $thanksPage = $this->get_return_url($order); @@ -245,4 +206,70 @@ public static function get_available_payment_gateways( $available_gateways ) { return $available_gateways; } + + public function handle_brick_charge() + { + session_start(); + + $this->init_configs(); + $parameters = $_POST; + $chargeInfo = $this->getChargeInfo($parameters); + $charge = $this->createCharge($chargeInfo); + $response = $charge->getPublicData(); + $responseData = json_decode($charge->getRawResponseData(), true); + $result = []; + $result['payment'] = $responseData; + $result = array_merge($result, json_decode($response, true)); + $_SESSION['charge_response_data'] = $responseData; + $_SESSION['charge'] = $charge; + + if ($charge->isSuccessful()) { + if ($charge->isCaptured()) { + $result = json_encode($result); + echo $result; + } elseif ($charge->isUnderReview()) { + echo 'Under review'; + exit(); + } + } + else { + if (isset($result['payment']['secure']['formHTML'])) { + $resultError['success'] = 0; + $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; + $resultError = json_encode($resultError); + echo $resultError; + } + } + } + + function getChargeInfo($params) + { + $chargeInfo = [ + 'email' => $params['email'], + 'history[registration_date]' => '1489655092', + 'amount' => (float) $_SESSION['cart_total'], + 'currency' => $_SESSION['currency'], + 'token' => $params['brick_token'], + 'fingerprint' => $params['brick_fingerprint'], + 'description' => 'Brick Paymentwall' + ]; + if (isset($params['brick_charge_id']) && isset($params['brick_secure_token'])) { + $chargeInfo['charge_id'] = $params['brick_charge_id']; + $chargeInfo['secure_token'] = $params['brick_secure_token']; + } + if (!empty($params['brick_reference_id'])) { + $chargeInfo['reference_id'] = $params['brick_reference_id']; + } + + return $chargeInfo; + } + + function createCharge($chargeInfo) + { + $charge = new Paymentwall_Charge(); + $charge->create($chargeInfo); + + return $charge; + } + } diff --git a/src/lib/paymentwall-php b/src/lib/paymentwall-php deleted file mode 160000 index b23bfb6..0000000 --- a/src/lib/paymentwall-php +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b23bfb6dd5667e729f19f375de5dc5d07ccc81d4 diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 016de12..136f546 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -236,3 +236,23 @@ class="input-radio pw_payment_system" name="payment_method" function paymentwall_load_textdomain() { load_plugin_textdomain( PW_TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } + +function handle_brick_charge() { + // Check if we're on the correct url + global $wp; + $current_slug = add_query_arg( array(), $wp->request ); + $siteUrl = get_site_url(); + if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { + return false; + } + // Check if it's a valid request. + $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); + if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { + $brick = new Paymentwall_Brick(); + $brick->handle_brick_charge(); + } + die('Process completed' ); +} + +add_action( 'template_redirect', 'handle_brick_charge', 0); + diff --git a/src/templates/brick/form.html b/src/templates/brick/form.html index 01b1cd7..cd4503f 100644 --- a/src/templates/brick/form.html +++ b/src/templates/brick/form.html @@ -1,98 +1,18 @@ - -
- - - - - - - - -

- - -

- -

- - - - / - -

- -

- - - - -

-
-
- + + + - diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php new file mode 100644 index 0000000..4be40f4 --- /dev/null +++ b/src/templates/pages/brick_form.php @@ -0,0 +1,39 @@ + + +
+ + + \ No newline at end of file From acda607c1e95b98f6935f0fedcb3308c57c27cd9 Mon Sep 17 00:00:00 2001 From: Peter Nguyen Date: Wed, 1 Sep 2021 00:23:52 +0700 Subject: [PATCH 12/14] Fix action brick charge --- src/assets/js/payment.js | 4 ++-- src/includes/class-paymentwall-brick.php | 12 ++++++++---- src/includes/class-paymentwall-gateway.php | 11 ++++++++++- src/templates/pages/brick_form.php | 17 +++++++++-------- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/assets/js/payment.js b/src/assets/js/payment.js index 14a445d..0263921 100644 --- a/src/assets/js/payment.js +++ b/src/assets/js/payment.js @@ -1,6 +1,6 @@ var Brick_Payment = { brick: null, - createBrick: function (public_key, amount, currency) { + createBrick: function (public_key, amount, currency, action) { "use strict"; if (window.Brick !== undefined) { return new Brick({ @@ -8,7 +8,7 @@ var Brick_Payment = { amount: amount, currency: currency, container: 'brick-payments-container', - action: 'wc-api=paymentwall_gateway&action=brick_charge', + action: action, form: { show_zip: true, // show zip code show_cardholder: true, diff --git a/src/includes/class-paymentwall-brick.php b/src/includes/class-paymentwall-brick.php index 2761f22..def6259 100644 --- a/src/includes/class-paymentwall-brick.php +++ b/src/includes/class-paymentwall-brick.php @@ -67,11 +67,13 @@ public function init_configs($isPingback = false) { public function payment_fields() { $currency = get_woocommerce_currency(); $display_tokenization = is_checkout() && $this->saved_cards; + session_start(); $_SESSION['cart_total'] = WC()->cart->cart_contents_total; $_SESSION['currency'] = $currency; $_SESSION['private_key'] = $this->settings['privatekey']; $_SESSION['public_key'] = $this->settings['publickey']; + $_SESSION['brick_form_action'] = get_site_url() . '/?wc-api=paymentwall_gateway&action=brick_charge'; if ( $display_tokenization ) { $this->supports = array_merge($this->supports, array('tokenization')); @@ -226,10 +228,11 @@ public function handle_brick_charge() if ($charge->isSuccessful()) { if ($charge->isCaptured()) { $result = json_encode($result); - echo $result; + var_dump($result); + die(); } elseif ($charge->isUnderReview()) { - echo 'Under review'; - exit(); + var_dump('under_review'); + die(); } } else { @@ -237,7 +240,8 @@ public function handle_brick_charge() $resultError['success'] = 0; $resultError['secure']['formHTML'] = $result['payment']['secure']['formHTML']; $resultError = json_encode($resultError); - echo $resultError; + var_dump($resultError); + die(); } } } diff --git a/src/includes/class-paymentwall-gateway.php b/src/includes/class-paymentwall-gateway.php index 1f803eb..2ec7156 100644 --- a/src/includes/class-paymentwall-gateway.php +++ b/src/includes/class-paymentwall-gateway.php @@ -247,7 +247,7 @@ function ipn_response() { $payment->init_configs(true); $pingback_params = $_GET; - + $pingback = new Paymentwall_Pingback($pingback_params, $this->getRealClientIP()); if ($pingback->validate(true)) { @@ -332,6 +332,9 @@ function handle_action() { case 'ipn': $this->ipn_response(); break; + case 'brick_charge': + $this->handle_brick_charge(); + break; default: break; } @@ -541,4 +544,10 @@ public function save_data_to_session($name, $data) { } } + public function handle_brick_charge() + { + $paymentwallBrick = new Paymentwall_Brick(); + + return $paymentwallBrick->handle_brick_charge(); + } } diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php index 4be40f4..79cad5e 100644 --- a/src/templates/pages/brick_form.php +++ b/src/templates/pages/brick_form.php @@ -1,4 +1,5 @@ +
+ +
+ + + + + + + + +

+ + +

+ +

+ + + + / + +

+ +

+ + + + +

+
+
+ + diff --git a/src/templates/pages/brick_form.php b/src/templates/pages/brick_form.php deleted file mode 100644 index 79cad5e..0000000 --- a/src/templates/pages/brick_form.php +++ /dev/null @@ -1,40 +0,0 @@ - - - -
- - - \ No newline at end of file From 10effd3406985126f3a56f2c3bd467226bb285b9 Mon Sep 17 00:00:00 2001 From: mason Date: Mon, 18 Oct 2021 16:07:40 +0700 Subject: [PATCH 14/14] Remove unused code --- src/paymentwall-for-woocommerce.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/paymentwall-for-woocommerce.php b/src/paymentwall-for-woocommerce.php index 136f546..28f7757 100644 --- a/src/paymentwall-for-woocommerce.php +++ b/src/paymentwall-for-woocommerce.php @@ -237,22 +237,3 @@ function paymentwall_load_textdomain() { load_plugin_textdomain( PW_TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } -function handle_brick_charge() { - // Check if we're on the correct url - global $wp; - $current_slug = add_query_arg( array(), $wp->request ); - $siteUrl = get_site_url(); - if ($current_slug !== str_replace($siteUrl . '/', '', PW_PLUGIN_URL) . '/templates/pages/wc-api=paymentwall_gateway&action=brick_charge') { - return false; - } - // Check if it's a valid request. - $nonce = filter_input(INPUT_GET, '_wpnonce', FILTER_SANITIZE_STRING); - if ( ! wp_verify_nonce( $nonce, 'NONCE_KEY')) { - $brick = new Paymentwall_Brick(); - $brick->handle_brick_charge(); - } - die('Process completed' ); -} - -add_action( 'template_redirect', 'handle_brick_charge', 0); -