From 15540d890f221d74506182cf0520ca80205e21f3 Mon Sep 17 00:00:00 2001 From: Lucas Seinfeld Date: Fri, 18 Oct 2019 12:18:20 +0200 Subject: [PATCH 1/6] Add result parameter to error return url --- CHANGELOG.md | 4 ++++ src/classes/includes/payment-gateway-cloud-creditcard.php | 2 +- src/woocommerce-payment-gateway-cloud.php | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15150f7..00b61d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes +## Unreleased +### Changed +- Add result parameter to error return url + ## v1.5.0 (2019-10-17) ### Changed - Remove redundant transaction request option read diff --git a/src/classes/includes/payment-gateway-cloud-creditcard.php b/src/classes/includes/payment-gateway-cloud-creditcard.php index 92eebb6..b673ba4 100644 --- a/src/classes/includes/payment-gateway-cloud-creditcard.php +++ b/src/classes/includes/payment-gateway-cloud-creditcard.php @@ -144,7 +144,7 @@ public function process_payment($orderId) ->setCallbackUrl($this->callbackUrl) ->setCancelUrl(wc_get_checkout_url()) ->setSuccessUrl($this->get_return_url($this->order)) - ->setErrorUrl($this->get_return_url($this->order)); + ->setErrorUrl(add_query_arg(['gateway_return_result' => 'error'], wc_get_checkout_url())); /** * integration key is set -> seamless diff --git a/src/woocommerce-payment-gateway-cloud.php b/src/woocommerce-payment-gateway-cloud.php index 459c37b..821d196 100644 --- a/src/woocommerce-payment-gateway-cloud.php +++ b/src/woocommerce-payment-gateway-cloud.php @@ -35,4 +35,10 @@ } return $methods; }, 0); + + add_filter('woocommerce_checkout_before_customer_details', function(){ + if(!empty($_GET['gateway_return_result']) && $_GET['gateway_return_result'] == 'error') { + wc_print_notice(__('Payment failed or was declined', 'woocommerce'), 'error'); + } + }, 0, 0); }); From 287fb216590481b0220b4b953fd8ee73a8a2fe45 Mon Sep 17 00:00:00 2001 From: Lucas Seinfeld Date: Fri, 18 Oct 2019 12:19:08 +0200 Subject: [PATCH 2/6] version bump --- build.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.php b/build.php index 8a89434..a6616c6 100644 --- a/build.php +++ b/build.php @@ -2,7 +2,7 @@ /** * extension source version */ -$version = '1.5.0'; +$version = '1.6.0'; /** * dist filename From 629815bb38a5af377f3bedc158074648460b6085 Mon Sep 17 00:00:00 2001 From: Thomas Payer <1170097+thomaspayer@users.noreply.github.com> Date: Fri, 18 Oct 2019 12:28:38 +0200 Subject: [PATCH 3/6] Unique orderId, set payment status more explicit --- CHANGELOG.md | 2 ++ .../payment-gateway-cloud-creditcard.php | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00b61d7..9628261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## Unreleased ### Changed - Add result parameter to error return url +- Unique orderId +- Set payment status more explicit ## v1.5.0 (2019-10-17) ### Changed diff --git a/src/classes/includes/payment-gateway-cloud-creditcard.php b/src/classes/includes/payment-gateway-cloud-creditcard.php index b673ba4..3247daa 100644 --- a/src/classes/includes/payment-gateway-cloud-creditcard.php +++ b/src/classes/includes/payment-gateway-cloud-creditcard.php @@ -64,6 +64,20 @@ public function hide_payment_gateways_on_pay_for_order_page($available_gateways) return $available_gateways; } + private function encodeOrderId($orderId) + { + return $orderId . '-' . date('YmdHis') . substr(sha1(uniqid()), 0, 10); + } + + private function decodeOrderId($orderId) + { + if (!strpos($orderId, '-') === false) { + return $orderId; + } + + return substr($orderId, 0, strrpos($orderId, '-')); + } + public function process_payment($orderId) { global $woocommerce; @@ -136,7 +150,7 @@ public function process_payment($orderId) break; } - $transaction->setTransactionId($orderId) + $transaction->setTransactionId($this->encodeOrderId($orderId)) ->setAmount(floatval($this->order->get_total())) ->setCurrency($this->order->get_currency()) ->setCustomer($customer) @@ -227,9 +241,23 @@ public function process_callback() $client->validateCallbackWithGlobals(); $callbackResult = $client->readCallback(file_get_contents('php://input')); - $this->order = new WC_Order($callbackResult->getTransactionId()); + $this->order = new WC_Order($this->decodeOrderId($callbackResult->getTransactionId())); if ($callbackResult->getResult() == \PaymentGatewayCloud\Client\Callback\Result::RESULT_OK) { - $this->order->payment_complete(); + switch ($callbackResult->getTransactionType()) { + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_DEBIT: + $this->order->payment_complete(); + break; + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_CAPTURE: + $this->order->payment_complete(); + break; + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_VOID: + $this->order->update_status('cancelled', __('Void', 'woocommerce')); + break; + + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_PREAUTHORIZE: + $this->order->update_status('on-hold', __('Awaiting capture/void', 'woocommerce')); + break; + } } die("OK"); From de3176702ccc8461ed5fcd3e1a661bc42b623546 Mon Sep 17 00:00:00 2001 From: Thomas Payer <1170097+thomaspayer@users.noreply.github.com> Date: Fri, 18 Oct 2019 13:27:48 +0200 Subject: [PATCH 4/6] clearify failed status --- src/classes/includes/payment-gateway-cloud-creditcard.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/classes/includes/payment-gateway-cloud-creditcard.php b/src/classes/includes/payment-gateway-cloud-creditcard.php index 3247daa..6a28822 100644 --- a/src/classes/includes/payment-gateway-cloud-creditcard.php +++ b/src/classes/includes/payment-gateway-cloud-creditcard.php @@ -258,6 +258,14 @@ public function process_callback() $this->order->update_status('on-hold', __('Awaiting capture/void', 'woocommerce')); break; } + } elseif ($callbackResult->getResult() == \PaymentGatewayCloud\Client\Callback\Result::RESULT_ERROR) { + switch ($callbackResult->getTransactionType()) { + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_DEBIT: + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_CAPTURE: + case \PaymentGatewayCloud\Client\Callback\Result::TYPE_VOID: + $this->order->update_status('failed', __('Error', 'woocommerce')); + break; + } } die("OK"); From aefef1bc2c138c4df840212ab1ac7b29ecb20cef Mon Sep 17 00:00:00 2001 From: Lucas Seinfeld Date: Fri, 18 Oct 2019 13:41:46 +0200 Subject: [PATCH 5/6] - Error return url redirect to checkout page with error message - Set payment status more explicitly - Unique order IDs in transaction - Handle Void/Capture postback --- CHANGELOG.md | 7 ++--- .../payment-gateway-cloud-creditcard.php | 27 ++++++++++++++----- src/woocommerce-payment-gateway-cloud.php | 3 ++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9628261..7e1122a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,10 @@ ## Unreleased ### Changed -- Add result parameter to error return url -- Unique orderId -- Set payment status more explicit +- Error return url redirect to checkout page with error message +- Set payment status more explicitly +- Unique order IDs in transaction +- Handle Void/Capture postback ## v1.5.0 (2019-10-17) ### Changed diff --git a/src/classes/includes/payment-gateway-cloud-creditcard.php b/src/classes/includes/payment-gateway-cloud-creditcard.php index 6a28822..d3cf168 100644 --- a/src/classes/includes/payment-gateway-cloud-creditcard.php +++ b/src/classes/includes/payment-gateway-cloud-creditcard.php @@ -71,11 +71,24 @@ private function encodeOrderId($orderId) private function decodeOrderId($orderId) { - if (!strpos($orderId, '-') === false) { + if (strpos($orderId, '-') === false) { return $orderId; } - return substr($orderId, 0, strrpos($orderId, '-')); + $orderIdParts = explode('-', $orderId); + + if(count($orderIdParts) === 2) { + $orderId = $orderIdParts[0]; + } + + /** + * void/capture will prefix the transaction id + */ + if(count($orderIdParts) === 3) { + $orderId = $orderIdParts[1]; + } + + return $orderId; } public function process_payment($orderId) @@ -158,7 +171,7 @@ public function process_payment($orderId) ->setCallbackUrl($this->callbackUrl) ->setCancelUrl(wc_get_checkout_url()) ->setSuccessUrl($this->get_return_url($this->order)) - ->setErrorUrl(add_query_arg(['gateway_return_result' => 'error'], wc_get_checkout_url())); + ->setErrorUrl(add_query_arg(['gateway_return_result' => 'error'], $this->order->get_checkout_payment_url(false))); /** * integration key is set -> seamless @@ -245,15 +258,12 @@ public function process_callback() if ($callbackResult->getResult() == \PaymentGatewayCloud\Client\Callback\Result::RESULT_OK) { switch ($callbackResult->getTransactionType()) { case \PaymentGatewayCloud\Client\Callback\Result::TYPE_DEBIT: - $this->order->payment_complete(); - break; case \PaymentGatewayCloud\Client\Callback\Result::TYPE_CAPTURE: $this->order->payment_complete(); break; case \PaymentGatewayCloud\Client\Callback\Result::TYPE_VOID: $this->order->update_status('cancelled', __('Void', 'woocommerce')); break; - case \PaymentGatewayCloud\Client\Callback\Result::TYPE_PREAUTHORIZE: $this->order->update_status('on-hold', __('Awaiting capture/void', 'woocommerce')); break; @@ -417,6 +427,11 @@ private function extraData3DS() // 3ds:browserTimezone // 3ds:browserUserAgent + /** + * force 3ds flow + */ + // '3dsecure' => 'mandatory', + /** * Additional 3ds 2.0 data */ diff --git a/src/woocommerce-payment-gateway-cloud.php b/src/woocommerce-payment-gateway-cloud.php index 821d196..c5dd2db 100644 --- a/src/woocommerce-payment-gateway-cloud.php +++ b/src/woocommerce-payment-gateway-cloud.php @@ -36,7 +36,8 @@ return $methods; }, 0); - add_filter('woocommerce_checkout_before_customer_details', function(){ + // add_filter('woocommerce_before_checkout_form', function(){ + add_filter('the_content', function(){ if(!empty($_GET['gateway_return_result']) && $_GET['gateway_return_result'] == 'error') { wc_print_notice(__('Payment failed or was declined', 'woocommerce'), 'error'); } From b2e9a8e95d2803e79c6636405cb75417f0a65788 Mon Sep 17 00:00:00 2001 From: Lucas Seinfeld Date: Fri, 18 Oct 2019 13:46:53 +0200 Subject: [PATCH 6/6] add content filter for checkout pay page for return errors --- src/woocommerce-payment-gateway-cloud.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/woocommerce-payment-gateway-cloud.php b/src/woocommerce-payment-gateway-cloud.php index c5dd2db..7e0c7d9 100644 --- a/src/woocommerce-payment-gateway-cloud.php +++ b/src/woocommerce-payment-gateway-cloud.php @@ -37,9 +37,12 @@ }, 0); // add_filter('woocommerce_before_checkout_form', function(){ - add_filter('the_content', function(){ - if(!empty($_GET['gateway_return_result']) && $_GET['gateway_return_result'] == 'error') { - wc_print_notice(__('Payment failed or was declined', 'woocommerce'), 'error'); + add_filter('the_content', function($content){ + if(is_checkout_pay_page()) { + if(!empty($_GET['gateway_return_result']) && $_GET['gateway_return_result'] == 'error') { + wc_print_notice(__('Payment failed or was declined', 'woocommerce'), 'error'); + } } - }, 0, 0); + return $content; + }, 0, 1); });