From 4cbb8d60e345a8b69d312ec2390e77a060d346d6 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 8 Aug 2024 17:57:27 -0400 Subject: [PATCH 01/21] feat: already processed exception --- .../Webpay/AlreadyProcessedException.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 plugin/shared/Exceptions/Webpay/AlreadyProcessedException.php diff --git a/plugin/shared/Exceptions/Webpay/AlreadyProcessedException.php b/plugin/shared/Exceptions/Webpay/AlreadyProcessedException.php new file mode 100644 index 00000000..65c0d411 --- /dev/null +++ b/plugin/shared/Exceptions/Webpay/AlreadyProcessedException.php @@ -0,0 +1,32 @@ +transaction = $transaction; + $this->flow = $flow; + parent::__construct($message, $previous); + } + + public function getTransaction() + { + return $this->transaction; + } + + public function getFlow() + { + return $this->flow; + } +} From 13852c3f7995e8d371a7bdb805c574e36450d5a8 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:08:36 -0400 Subject: [PATCH 02/21] refactor: delete unused exception --- .../Exceptions/Webpay/WithoutTokenWebpayException.php | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 plugin/shared/Exceptions/Webpay/WithoutTokenWebpayException.php diff --git a/plugin/shared/Exceptions/Webpay/WithoutTokenWebpayException.php b/plugin/shared/Exceptions/Webpay/WithoutTokenWebpayException.php deleted file mode 100644 index 3314c615..00000000 --- a/plugin/shared/Exceptions/Webpay/WithoutTokenWebpayException.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Fri, 9 Aug 2024 16:12:45 -0400 Subject: [PATCH 03/21] refactor: typo in success variable --- plugin/src/Blocks/js/notice_handler.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/src/Blocks/js/notice_handler.js b/plugin/src/Blocks/js/notice_handler.js index 32bf81b6..aa1723d8 100644 --- a/plugin/src/Blocks/js/notice_handler.js +++ b/plugin/src/Blocks/js/notice_handler.js @@ -4,13 +4,13 @@ const params = new URLSearchParams(url.search); const hasTbkData = params.has('transbank_status'); const noticeTypes = { - SUCESS: 'success', + SUCCESS: 'success', ERROR: 'error' } const oneClickNoticeData = { 0: { message: 'La tarjeta ha sido inscrita satisfactoriamente. Aún no se realiza ningún cobro. Ahora puedes realizar el pago.', - type: noticeTypes.SUCESS }, + type: noticeTypes.SUCCESS }, 1: { message: 'La inscripción fue cancelada automáticamente por estar inactiva mucho tiempo.', type: noticeTypes.ERROR }, 2: { message: 'No se recibió el token de la inscripción.', @@ -27,7 +27,7 @@ const oneClickNoticeData = { const webPayNoticeNoticeData = { 7: { message: 'Transacción aprobada', - type: noticeTypes.SUCESS }, + type: noticeTypes.SUCCESS }, 8: { message: 'El usuario intentó pagar esta orden nuevamente, cuando esta ya estaba pagada.', type: noticeTypes.ERROR }, 9: { message: 'El usuario intentó pagar una orden con estado inválido.', @@ -60,7 +60,7 @@ export const noticeHandler = ( productId ) => { const noticeMessage = productNoticeData[statusCode]['message']; const notificationType = productNoticeData[statusCode]['type']; switch (notificationType){ - case noticeTypes.SUCESS: + case noticeTypes.SUCCESS: wp.data.dispatch('core/notices').createSuccessNotice( noticeMessage, { context: 'wc/checkout' } ); break; case noticeTypes.ERROR: From 7645be76d3df560d56cde9ec77b0860e6bb24bc6 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:14:41 -0400 Subject: [PATCH 04/21] refactor: update notice messages --- plugin/src/Blocks/js/notice_handler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugin/src/Blocks/js/notice_handler.js b/plugin/src/Blocks/js/notice_handler.js index aa1723d8..076f38be 100644 --- a/plugin/src/Blocks/js/notice_handler.js +++ b/plugin/src/Blocks/js/notice_handler.js @@ -28,11 +28,11 @@ const oneClickNoticeData = { const webPayNoticeNoticeData = { 7: { message: 'Transacción aprobada', type: noticeTypes.SUCCESS }, - 8: { message: 'El usuario intentó pagar esta orden nuevamente, cuando esta ya estaba pagada.', + 8: { message: 'Ocurrió un error al confirmar la transacción, ya se encontraba procesada.', type: noticeTypes.ERROR }, 9: { message: 'El usuario intentó pagar una orden con estado inválido.', type: noticeTypes.ERROR }, - 10: { message: 'La transacción fue cancelada automáticamente por estar inactiva mucho tiempo en el formulario de pago de Webpay. Puede reintentar el pago', + 10: { message: 'La transacción fue cancelada por exceder el tiempo en el formulario de Webpay.', type: noticeTypes.ERROR }, 11: { message: 'El usuario canceló la transacción en el formulario de pago, pero esta orden ya estaba pagada o en un estado diferente a INICIALIZADO', type: noticeTypes.ERROR }, @@ -44,7 +44,7 @@ const webPayNoticeNoticeData = { type: noticeTypes.ERROR }, 15: { message: 'El commit de la transacción ha sido rechazada en Transbank', type: noticeTypes.ERROR }, - 16: { message: 'Ocurrió un error al ejecutar el commit de la transacción.', + 16: { message: 'Ocurrió un error al confirmar la transacción, favor intente nuevamente.', type: noticeTypes.ERROR }, 17: { message: 'Ocurrió un error inesperado.', type: noticeTypes.ERROR } From 4164012e6eeeb155c43552a6fbcb9f5d353e8a63 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:17:21 -0400 Subject: [PATCH 05/21] style: format the blockshelper file --- plugin/src/Helpers/BlocksHelper.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugin/src/Helpers/BlocksHelper.php b/plugin/src/Helpers/BlocksHelper.php index eee10b79..93679205 100644 --- a/plugin/src/Helpers/BlocksHelper.php +++ b/plugin/src/Helpers/BlocksHelper.php @@ -24,14 +24,16 @@ class BlocksHelper const WEBPAY_COMMIT_ERROR = 16; const WEBPAY_EXCEPTION = 17; - public static function checkBlocksEnabled() { + public static function checkBlocksEnabled() + { $checkout_page = wc_get_page_id('checkout'); return has_block('woocommerce/checkout', $checkout_page); } - public static function addLegacyNotices($message, $type) { + public static function addLegacyNotices($message, $type) + { if (self::checkBlocksEnabled()) { return; } From eea6c5223f596cd2ede681a8e174f9e45309b672 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:44:01 -0400 Subject: [PATCH 06/21] refactor: change response logic --- plugin/src/Controllers/ResponseController.php | 65 +++++++++---------- 1 file changed, 29 insertions(+), 36 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index 83cb1e6a..72cb9b49 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -8,6 +8,7 @@ use Transbank\WooCommerce\WebpayRest\Models\Transaction; use Transbank\WooCommerce\WebpayRest\Helpers\HposHelper; use Transbank\WooCommerce\WebpayRest\Helpers\BlocksHelper; +use Transbank\Plugin\Exceptions\Webpay\AlreadyProcessedException; use Transbank\Plugin\Exceptions\Webpay\TimeoutWebpayException; use Transbank\Plugin\Exceptions\Webpay\UserCancelWebpayException; use Transbank\Plugin\Exceptions\Webpay\DoubleTokenWebpayException; @@ -48,64 +49,47 @@ public function __construct(array $pluginConfig) * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Transbank\Plugin\Exceptions\TokenNotFoundOnDatabaseException */ - public function response($postData) + + public function response($requestMethod, $params) { + $requestData = [ + 'method' => $requestMethod, + 'params' => $params + ]; + try { - $transaction = $this->webpayplusTransbankSdk->processRequestFromTbkReturn($_SERVER, $_GET, $_POST); + $transaction = $this->webpayplusTransbankSdk->handleRequestFromTbkReturn($requestData); $wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id); - if ($wooCommerceOrder->is_paid()) { - // TODO: Revisar porqué no se muestra el mensaje de abajo. H4x - //SessionMessageHelper::set('Orden ya ha sido pagada.', 'notice'); - $errorMessage = 'El usuario intentó pagar esta orden nuevamente, cuando esta ya estaba pagada.'; - $this->webpayplusTransbankSdk->logError($errorMessage); - $this->webpayplusTransbankSdk->saveTransactionWithErrorByTransaction($transaction, 'transbank_webpay_plus_already_paid_transaction', $errorMessage); - $wooCommerceOrder->add_order_note($errorMessage); - do_action('transbank_webpay_plus_already_paid_transaction', $wooCommerceOrder); - return wp_safe_redirect($wooCommerceOrder->get_checkout_order_received_url()); - } - if (!$wooCommerceOrder->needs_payment()) { - // TODO: Revisar porqué no se muestra el mensaje de abajo. - //SessionMessageHelper::set('El estado de la orden no permite que sea pagada. Comuníquese con la tienda.', 'error'); - $errorMessage = 'El usuario intentó pagar la orden cuando estaba en estado: '.$wooCommerceOrder->get_status().".\n".'No se ejecutó captura del pago de esta solicitud.'; - $this->webpayplusTransbankSdk->logError($errorMessage); - $this->webpayplusTransbankSdk->saveTransactionWithErrorByTransaction($transaction, 'transbank_webpay_plus_paying_transaction_that_does_not_needs_payment', $errorMessage); - $wooCommerceOrder->add_order_note($errorMessage); - do_action('transbank_webpay_plus_paying_transaction_that_does_not_needs_payment', $wooCommerceOrder); - return wp_safe_redirect($wooCommerceOrder->get_checkout_order_received_url()); - } $commitResponse = $this->webpayplusTransbankSdk->commitTransaction($transaction->order_id, $transaction->token); $this->completeWooCommerceOrder($wooCommerceOrder, $commitResponse, $transaction); + do_action('wc_transbank_webpay_plus_transaction_approved', [ 'order' => $wooCommerceOrder->get_data(), 'transbankTransaction' => $transaction ]); return wp_redirect($wooCommerceOrder->get_checkout_order_received_url()); - } catch (TimeoutWebpayException $e) { $this->throwError($e->getMessage()); + do_action('transbank_webpay_plus_timeout_on_form'); $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_TIMEOUT); - wp_redirect($urlWithErrorCode); + return wp_redirect($urlWithErrorCode); } catch (UserCancelWebpayException $e) { $params = ['transbank_webpayplus_cancelled_order' => 1]; $redirectUrl = add_query_arg($params, wc_get_checkout_url()); $transaction = $e->getTransaction(); $wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id); - if ($transaction->status !== Transaction::STATUS_INITIALIZED || $wooCommerceOrder->is_paid()) { - $wooCommerceOrder->add_order_note('El usuario canceló la transacción en el formulario de pago, pero esta orden ya estaba pagada o en un estado diferente a INICIALIZADO'); - wp_safe_redirect($redirectUrl); - return; - } $this->setOrderAsCancelledByUser($wooCommerceOrder, $transaction); + do_action('transbank_webpay_plus_transaction_cancelled_by_user', $wooCommerceOrder, $transaction); $urlWithErrorCode = $this->addErrorQueryParams($redirectUrl, BlocksHelper::WEBPAY_USER_CANCELED); - wp_safe_redirect($urlWithErrorCode); - return; + return wp_safe_redirect($urlWithErrorCode); } catch (DoubleTokenWebpayException $e) { $this->throwError($e->getMessage()); + do_action('transbank_webpay_plus_unexpected_error'); $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_DOUBLE_TOKEN); - wp_redirect($urlWithErrorCode); + return wp_redirect($urlWithErrorCode); } catch (InvalidStatusWebpayException $e) { $errorMessage = 'No se puede confirmar la transacción, estado de transacción invalido.'; $wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id); @@ -115,7 +99,6 @@ public function response($postData) 'order' => $wooCommerceOrder->get_data(), 'transbankTransaction' => $e->getTransaction() ]); - $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_INVALID_STATUS); return wp_redirect($urlWithErrorCode); } catch (RejectedCommitWebpayException $e) { @@ -129,7 +112,6 @@ public function response($postData) 'transbankTransaction' => $transaction, 'transbankResponse' => $commitResponse ]); - return wp_redirect($wooCommerceOrder->get_checkout_order_received_url()); } catch (CommitWebpayException $e) { $errorMessage = 'Error al confirmar la transacción de Transbank'; @@ -140,16 +122,27 @@ public function response($postData) 'order' => $wooCommerceOrder->get_data(), 'transbankTransaction' => $e->getTransaction() ]); - $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_COMMIT_ERROR); return wp_redirect($urlWithErrorCode); + } catch (AlreadyProcessedException $e) { + $errorMessage = 'Error al confirmar la transacción, ya fue procesada anteriormente'; + $transaction = $e->getTransaction(); + $order_id = $transaction['order_id']; + $wooCommerceOrder = $this->getWooCommerceOrderById($order_id); + $wooCommerceOrder->add_order_note($errorMessage); + + if ($e->getFlow() == 'Normal') { + return wp_redirect($wooCommerceOrder->get_checkout_order_received_url()); + } + + $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_ALREADY_PROCESSED); + return wp_redirect($urlWithErrorCode); } catch (\Exception $e) { $this->throwError($e->getMessage()); do_action('transbank_webpay_plus_unexpected_error'); $urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_EXCEPTION); wp_redirect($urlWithErrorCode); } - return ""; } /** From de7784387085f3f2e2b687f38ef99cbd59c95ab6 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:45:52 -0400 Subject: [PATCH 07/21] refactor: update check ipn response --- .../WC_Gateway_Transbank_Webpay_Plus_REST.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php index 3fdbe9d2..124175eb 100644 --- a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php +++ b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php @@ -223,9 +223,10 @@ public function check_ipn_response() ob_clean(); if (isset($_POST)) { header('HTTP/1.1 200 OK'); - $data = ($_SERVER['REQUEST_METHOD'] === 'GET') ? $_GET : $_POST; + $requestMethod = $_SERVER['REQUEST_METHOD']; + $params = ($requestMethod === 'GET') ? $_GET : $_POST; - return (new ResponseController($this->config))->response($data); + return (new ResponseController($this->config))->response($requestMethod, $params); } else { echo 'Ocurrió un error al procesar su compra'; } From 2c535b9951a7e2e25b5d2b9b02b945f1bf06a87a Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:54:19 -0400 Subject: [PATCH 08/21] refactor: rename unused var --- plugin/src/Helpers/BlocksHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/Helpers/BlocksHelper.php b/plugin/src/Helpers/BlocksHelper.php index 93679205..3b8e6154 100644 --- a/plugin/src/Helpers/BlocksHelper.php +++ b/plugin/src/Helpers/BlocksHelper.php @@ -13,7 +13,7 @@ class BlocksHelper const ONECLICK_FINISH_ERROR = 5; const ONECLICK_REJECTED_INSCRIPTION = 6; const WEBPAY_APPROVED = 7; - const WEBPAY_ALREADY_PAID = 8; + const WEBPAY_ALREADY_PROCESSED = 8; const WEBPAY_INVALID_ORDER_STATUS = 9; const WEBPAY_TIMEOUT = 10; const WEBPAY_USER_CANCELED_ALREADY_PAID = 11; From e22ab236728bb61ebf89a5110860b963bc34d2a4 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:56:45 -0400 Subject: [PATCH 09/21] refactor: rename php docs class reference --- plugin/src/Controllers/ResponseController.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index 72cb9b49..5fe81272 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -25,11 +25,10 @@ class ResponseController * @var array */ protected $pluginConfig; - protected $logger; /** - * @var Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk + * @var \Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk */ protected $webpayplusTransbankSdk; From 6b804af5b66557b09cf60d927952d7b28854b0ad Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 16:57:42 -0400 Subject: [PATCH 10/21] style: format the response controller --- plugin/src/Controllers/ResponseController.php | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index 5fe81272..f1c706ae 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -181,8 +181,7 @@ protected function completeWooCommerceOrder( WC_Order $wooCommerceOrder, TransactionCommitResponse $commitResponse, $webpayTransaction - ) - { + ) { $status = TbkResponseUtil::getStatus($commitResponse->getStatus()); $paymentType = TbkResponseUtil::getPaymentType($commitResponse->getPaymentTypeCode()); $date_accepted = new DateTime($commitResponse->getTransactionDate(), new DateTimeZone('UTC')); @@ -213,7 +212,8 @@ protected function completeWooCommerceOrder( $maskedBuyOrder = $this->webpayplusTransbankSdk->dataMasker->maskBuyOrder($commitResponse->getBuyOrder()); $this->logger->logInfo( - 'C.5. Transacción con commit exitoso en Transbank y guardado => OC: '.$maskedBuyOrder); + 'C.5. Transacción con commit exitoso en Transbank y guardado => OC: ' . $maskedBuyOrder + ); $this->setAfterPaymentOrderStatus($wooCommerceOrder); } @@ -227,8 +227,7 @@ protected function setWooCommerceOrderAsFailed( WC_Order $wooCommerceOrder, $webpayTransaction, TransactionCommitResponse $commitResponse - ) - { + ) { $_SESSION['woocommerce_order_failed'] = true; $wooCommerceOrder->update_status('failed'); if ($commitResponse !== null) { @@ -241,14 +240,14 @@ protected function setWooCommerceOrderAsFailed( $webpayTransaction->token ); - $this->logger->logError('C.5. Respuesta de tbk commit fallido => token: '.$webpayTransaction->token); + $this->logger->logError('C.5. Respuesta de tbk commit fallido => token: ' . $webpayTransaction->token); $this->logger->logError(json_encode($commitResponse)); } Transaction::update( $webpayTransaction->id, [ - 'status' => Transaction::STATUS_FAILED, + 'status' => Transaction::STATUS_FAILED, 'transbank_response' => json_encode($commitResponse), ] ); @@ -321,18 +320,19 @@ protected function throwError(string $msg) /** * @param WC_Order $order */ - private function setAfterPaymentOrderStatus(WC_Order $order){ + private function setAfterPaymentOrderStatus(WC_Order $order) + { $status = $this->pluginConfig['STATUS_AFTER_PAYMENT']; - if ($status == ''){ + if ($status == '') { $order->payment_complete(); - } - else{ + } else { $order->payment_complete(); $order->update_status($status); } } - protected function addErrorQueryParams($url, $errorCode) { + protected function addErrorQueryParams($url, $errorCode) + { $params = ['transbank_status' => $errorCode]; return add_query_arg($params, $url); } From 5bc0e447d4dc0a92468dde14ff6421f79ae9d131 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 17:01:45 -0400 Subject: [PATCH 11/21] feat: import webpay plus transaction class --- plugin/src/WebpayplusTransbankSdk.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugin/src/WebpayplusTransbankSdk.php b/plugin/src/WebpayplusTransbankSdk.php index cf9c89eb..663b896d 100644 --- a/plugin/src/WebpayplusTransbankSdk.php +++ b/plugin/src/WebpayplusTransbankSdk.php @@ -7,6 +7,7 @@ use Transbank\WooCommerce\WebpayRest\Models\Transaction; use Transbank\WooCommerce\WebpayRest\Helpers\ErrorUtil; use Transbank\WooCommerce\WebpayRest\Helpers\MaskData; +use Transbank\Webpay\WebpayPlus\Transaction as WebpayPlusTransaction; use Transbank\Plugin\Exceptions\Webpay\TimeoutWebpayException; use Transbank\Plugin\Exceptions\Webpay\UserCancelWebpayException; use Transbank\Plugin\Exceptions\Webpay\DoubleTokenWebpayException; @@ -30,7 +31,7 @@ class WebpayplusTransbankSdk extends TransbankSdk const OPTION_KEY = 'woocommerce_transbank_webpay_plus_rest_settings'; /** - * @var \Transbank\Webpay\WebpayPlus\Transaction + * @var WebpayPlusTransaction */ protected $webpayplusTransaction; @@ -38,7 +39,7 @@ public function __construct($log, $environment, $commerceCode, $apiKey) { $this->log = $log; $this->options = $this->createOptions($environment, $commerceCode, $apiKey); - $this->webpayplusTransaction = new \Transbank\Webpay\WebpayPlus\Transaction($this->options); + $this->webpayplusTransaction = new WebpayPlusTransaction($this->options); $this->dataMasker = new MaskData($this->getEnviroment()); } @@ -47,7 +48,7 @@ public function __construct($log, $environment, $commerceCode, $apiKey) */ private function createOptions($environment, $commerceCode, $apiKey) { - $options = \Transbank\Webpay\WebpayPlus\Transaction::getDefaultOptions(); + $options = WebpayPlusTransaction::getDefaultOptions(); if ($environment == Options::ENVIRONMENT_PRODUCTION) { $options = Options::forProduction($commerceCode, $apiKey); } From 3350355a75ea36c8fc66fd78c71674a166247d19 Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 17:06:33 -0400 Subject: [PATCH 12/21] refactor: change process request from tbk return --- plugin/src/WebpayplusTransbankSdk.php | 198 ++++++++++++++++++++------ 1 file changed, 151 insertions(+), 47 deletions(-) diff --git a/plugin/src/WebpayplusTransbankSdk.php b/plugin/src/WebpayplusTransbankSdk.php index 663b896d..ce1ee741 100644 --- a/plugin/src/WebpayplusTransbankSdk.php +++ b/plugin/src/WebpayplusTransbankSdk.php @@ -8,6 +8,7 @@ use Transbank\WooCommerce\WebpayRest\Helpers\ErrorUtil; use Transbank\WooCommerce\WebpayRest\Helpers\MaskData; use Transbank\Webpay\WebpayPlus\Transaction as WebpayPlusTransaction; +use Transbank\Plugin\Exceptions\Webpay\AlreadyProcessedException; use Transbank\Plugin\Exceptions\Webpay\TimeoutWebpayException; use Transbank\Plugin\Exceptions\Webpay\UserCancelWebpayException; use Transbank\Plugin\Exceptions\Webpay\DoubleTokenWebpayException; @@ -30,6 +31,17 @@ class WebpayplusTransbankSdk extends TransbankSdk const OPTION_KEY = 'woocommerce_transbank_webpay_plus_rest_settings'; + const WEBPAY_NORMAL_FLOW = 'Normal'; + const WEBPAY_TIMEOUT_FLOW = 'Timeout'; + const WEBPAY_ABORTED_FLOW = 'Aborted'; + const WEBPAY_ERROR_FLOW = 'Error'; + + const WEBPAY_ALREADY_PROCESSED_MESSAGE = 'La transacción fue procesada anteriormente.'; + const WEBPAY_FAILED_FLOW_MESSAGE = 'Transacción no autorizada.'; + const WEBPAY_TIMEOUT_FLOW_MESSAGE = 'Tiempo excedido en el formulario de Webpay.'; + const WEBPAY_ABORTED_FLOW_MESSAGE = 'Orden anulada por el usuario.'; + const WEBPAY_ERROR_FLOW_MESSAGE = 'Orden cancelada por un error en el formulario de pago.'; + /** * @var WebpayPlusTransaction */ @@ -287,53 +299,6 @@ public function refundTransaction($orderId, $amount)//NotFoundRefundTransactionW /* Metodo COMMIT */ - - public function processRequestFromTbkReturn($server, $get, $post) - { - $method = $server['REQUEST_METHOD']; - $params = $method === 'GET' ? $get : $post; - $tbkToken = isset($params["TBK_TOKEN"]) ? $params['TBK_TOKEN'] : null; - $tbkSessionId = isset($params["TBK_ID_SESION"]) ? $params['TBK_ID_SESION'] : null; - $tbkOrdenCompra = isset($params["TBK_ORDEN_COMPRA"]) ? $params['TBK_ORDEN_COMPRA'] : null; - $tokenWs = isset($params["token_ws"]) ? $params['token_ws'] : null; - - $params1 = [ - 'method' => $method, - 'params' => $params - ]; - - $this->logInfoData('', 'Retornando desde el sitio de Transbank para realizar el commit', $params1); - - if (!isset($tokenWs) && !isset($tbkToken)) { - $errorMessage = 'La transacción fue cancelada automáticamente por estar inactiva mucho tiempo en el formulario de pago de Webpay. Puede reintentar el pago'; - $transaction = null; - if (isset($tbkOrdenCompra) && isset($tbkSessionId)) { - $transaction = Transaction::getByBuyOrderAndSessionId($tbkOrdenCompra, $tbkSessionId); - $this->errorExecution(isset($transaction) ? $transaction->order_id : 0, 'commit', $params1, 'TimeoutWebpayException', $errorMessage, $errorMessage); - $this->saveTransactionWithErrorByTransaction($transaction, 'TimeoutWebpayException', $errorMessage); - } else { - $this->errorExecution(0, 'commit', $params1, 'TimeoutWebpayException', $errorMessage, $errorMessage); - } - throw new TimeoutWebpayException($errorMessage, $tbkOrdenCompra, $tbkSessionId, $transaction); - } - - if (!isset($tokenWs) && isset($tbkToken)) { - $errorMessage = 'La transacción fue anulada por el usuario.'; - $transaction = $this->saveTransactionWithErrorByToken($tbkToken, 'UserCancelWebpayException', $errorMessage); - $this->errorExecution($transaction->order_id, 'commit', $params1, 'UserCancelWebpayException', $errorMessage, $errorMessage); - throw new UserCancelWebpayException($errorMessage, $tbkToken, $transaction); - } - - if (isset($tbkToken) && isset($tokenWs)) { - $errorMessage = 'El pago es inválido.'; - $transaction = $this->saveTransactionWithErrorByToken($tbkToken, 'DoubleTokenWebpayException', $errorMessage); - $this->errorExecution($transaction->order_id, 'commit', $params1, 'DoubleTokenWebpayException', $errorMessage, $errorMessage); - throw new DoubleTokenWebpayException($errorMessage, $tbkToken, $tokenWs, $transaction); - } - - return Transaction::getByToken($tokenWs); - } - public function commitInner($orderId, $token, $transaction) { $params = ['token' => $token]; @@ -416,5 +381,144 @@ public function saveTransactionWithErrorByToken($token, $error, $detailError) return $transaction; } + private function isTransactionProcessed(string $token): bool + { + $transaction = Transaction::getByToken($token); + $status = $transaction->status; + + return $status != Transaction::STATUS_INITIALIZED; + } + + private function handleProcessedTransaction(string $token) + { + $transaction = get_object_vars(Transaction::getByToken($token)) ?? null; + $buyOrder = $transaction['buy_order'] ?? null; + $status = $transaction['status'] ?? null; + $logMessage = self::WEBPAY_ALREADY_PROCESSED_MESSAGE; + + if ($status == Transaction::STATUS_APPROVED) { + $this->logInfoData($buyOrder, $logMessage, $transaction); + throw new AlreadyProcessedException($logMessage, $transaction, self::WEBPAY_NORMAL_FLOW); + } + + if ($status == Transaction::STATUS_TIMEOUT) { + $logMessage = self::WEBPAY_TIMEOUT_FLOW_MESSAGE; + $this->logInfoData($buyOrder, $logMessage, $transaction); + throw new AlreadyProcessedException($logMessage, $transaction, self::WEBPAY_TIMEOUT_FLOW); + } + + if ($status == Transaction::STATUS_ABORTED_BY_USER) { + $logMessage = self::WEBPAY_ABORTED_FLOW_MESSAGE; + $this->logInfoData($buyOrder, $logMessage, $transaction); + throw new AlreadyProcessedException($logMessage, $transaction, self::WEBPAY_ABORTED_FLOW); + } + + if ($status == Transaction::STATUS_FAILED) { + $logMessage = self::WEBPAY_FAILED_FLOW_MESSAGE; + $this->logInfoData($buyOrder, $logMessage, $transaction); + throw new AlreadyProcessedException($logMessage, $transaction, self::WEBPAY_ERROR_FLOW); + } + } + + private function detectFlow(array $params): string + { + $tokenWs = $params['token_ws'] ?? null; + $tbkToken = $params['TBK_TOKEN'] ?? null; + $tbkSessionId = $params['TBK_ID_SESION'] ?? null; + + if ($tokenWs && !$tbkToken && !$tbkSessionId) { + return self::WEBPAY_NORMAL_FLOW; + } + if ($tbkSessionId && !$tbkToken && !$tokenWs) { + return self::WEBPAY_TIMEOUT_FLOW; + } + if ($tbkToken && $tbkSessionId && !$tokenWs) { + return self::WEBPAY_ABORTED_FLOW; + } + return self::WEBPAY_ERROR_FLOW; + } + + public function handleRequestFromTbkReturn(array $requestData) + { + $params = $requestData['params'] ?? null; + $tokenWs = $params['token_ws'] ?? null; + $tbkToken = $params['TBK_TOKEN'] ?? null; + $sessionId = $params['TBK_ID_SESION'] ?? null; + $buyOrder = $params['TBK_ORDEN_COMPRA'] ?? null; + $flow = $this->detectFlow($params); + + if ($flow == self::WEBPAY_NORMAL_FLOW) { + return $this->handleNormalFlow($tokenWs); + } + if ($flow == self::WEBPAY_TIMEOUT_FLOW) { + return $this->handleTimeoutFlow($sessionId, $buyOrder); + } + if ($flow == self::WEBPAY_ABORTED_FLOW) { + return $this->handleAbortedFlow($tbkToken); + } + return $this->handleErrorFlow($tokenWs, $tbkToken); + } + + private function handleNormalFlow(string $token) + { + $this->logInfo("Flujo normal detectado, token: $token"); + + if ($this->isTransactionProcessed($token)) { + return $this->handleProcessedTransaction($token); + } + + return Transaction::getByToken($token); + } + + private function handleTimeoutFlow(string $sessionId, string $buyOrder) + { + $this->logInfo("Flujo de timeout detectado, sessionId: $sessionId, buyOrder: $buyOrder"); + $transaction = Transaction::getByBuyOrderAndSessionId($buyOrder, $sessionId) ?? null; + $token = $transaction->token ?? null; + + if ($this->isTransactionProcessed($token)) { + return $this->handleProcessedTransaction($token); + } + + $errorMessage = self::WEBPAY_TIMEOUT_FLOW_MESSAGE; + $orderId = $transaction->order_id ?? 0; + $data = [ + 'TBK_ID_SESION' => $sessionId, + 'TBK_ORDEN_COMPRA' => $buyOrder + ]; + $this->errorExecution($orderId, 'commit', $data, 'TimeoutWebpayException', $errorMessage, $errorMessage); + $this->saveTransactionWithErrorByTransaction($transaction, 'TimeoutWebpayException', $errorMessage); + throw new TimeoutWebpayException($errorMessage, $buyOrder, $sessionId, $transaction); + } + + private function handleAbortedFlow(string $token) + { + $this->logInfo("Flujo de pago abortado detectado, token: $token"); + + if ($this->isTransactionProcessed($token)) { + return $this->handleProcessedTransaction($token); + } + + $errorMessage = self::WEBPAY_ABORTED_FLOW_MESSAGE; + $transaction = $this->saveTransactionWithErrorByToken($token, 'UserCancelWebpayException', $errorMessage); + $this->errorExecution($transaction->order_id, 'commit', $token, 'UserCancelWebpayException', $errorMessage, $errorMessage); + throw new UserCancelWebpayException($errorMessage, $token, $transaction); + } + + private function handleErrorFlow(string $token, string $tbkToken) + { + $this->logInfo("Flujo con error en el formulario detectado, token: $token"); + + if ($this->isTransactionProcessed($token)) { + return $this->handleProcessedTransaction($token); + } + + $errorMessage = self::WEBPAY_ERROR_FLOW_MESSAGE; + $transaction = $this->saveTransactionWithErrorByToken($tbkToken, 'DoubleTokenWebpayException', $errorMessage); + $this->errorExecution($transaction->order_id, 'commit', $token, 'DoubleTokenWebpayException', $errorMessage, $errorMessage); + throw new DoubleTokenWebpayException($errorMessage, $tbkToken, $token, $transaction); + } + + } From 71c525c20e6270f49ad117cacc8648c03fe3988c Mon Sep 17 00:00:00 2001 From: Fernando Date: Fri, 9 Aug 2024 17:09:01 -0400 Subject: [PATCH 13/21] feat: update const for transaction status --- plugin/src/Models/Transaction.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/src/Models/Transaction.php b/plugin/src/Models/Transaction.php index 58e7a250..32114df3 100644 --- a/plugin/src/Models/Transaction.php +++ b/plugin/src/Models/Transaction.php @@ -10,9 +10,10 @@ class Transaction extends BaseModel const STATUS_PREPARED = 'prepared'; const STATUS_INITIALIZED = 'initialized'; - const STATUS_FAILED = 'failed'; - const STATUS_ABORTED_BY_USER = 'aborted_by_user'; const STATUS_APPROVED = 'approved'; + const STATUS_TIMEOUT = 'timeout'; + const STATUS_ABORTED_BY_USER = 'aborted_by_user'; + const STATUS_FAILED = 'failed'; const PRODUCT_WEBPAY_PLUS = 'webpay_plus'; const PRODUCT_WEBPAY_ONECLICK = 'webpay_oneclick'; From 7d9a2f0592d79cb2d00a57eef20cfa5ec6f1e3dd Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 11:44:18 -0400 Subject: [PATCH 14/21] feat: add log for request info --- .../WC_Gateway_Transbank_Webpay_Plus_REST.php | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php index 124175eb..d3e9eff0 100644 --- a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php +++ b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php @@ -60,12 +60,12 @@ public function __construct() ]; $this->config = [ - 'MODO' => trim($this->get_option('webpay_rest_environment', 'TEST')), - 'COMMERCE_CODE' => trim( + 'MODO' => trim($this->get_option('webpay_rest_environment', 'TEST')), + 'COMMERCE_CODE' => trim( $this->get_option('webpay_rest_commerce_code', WebpayPlus::DEFAULT_COMMERCE_CODE) ), - 'API_KEY' => $this->get_option('webpay_rest_api_key', WebpayPlus::DEFAULT_API_KEY), - 'ECOMMERCE' => 'woocommerce', + 'API_KEY' => $this->get_option('webpay_rest_api_key', WebpayPlus::DEFAULT_API_KEY), + 'ECOMMERCE' => 'woocommerce', 'STATUS_AFTER_PAYMENT' => $this->get_option('webpay_rest_after_payment_order_status', ''), ]; @@ -166,14 +166,14 @@ public function init_form_fields() $this->form_fields = [ 'enabled' => [ - 'title' => __('Activar/Desactivar', 'transbank_webpay_plus_rest'), - 'type' => 'checkbox', + 'title' => __('Activar/Desactivar', 'transbank_webpay_plus_rest'), + 'type' => 'checkbox', 'default' => 'yes', ], 'webpay_rest_environment' => [ - 'title' => __('Ambiente', 'transbank_webpay_plus_rest'), - 'type' => 'select', - 'desc_tip' => $environmentDescription, + 'title' => __('Ambiente', 'transbank_webpay_plus_rest'), + 'type' => 'select', + 'desc_tip' => $environmentDescription, 'options' => [ 'TEST' => __('Integración', 'transbank_webpay_plus_rest'), 'LIVE' => __('Producción', 'transbank_webpay_plus_rest'), @@ -181,34 +181,34 @@ public function init_form_fields() 'default' => 'TEST', ], 'webpay_rest_commerce_code' => [ - 'title' => __('Código de Comercio Producción', 'transbank_webpay_plus_rest'), + 'title' => __('Código de Comercio Producción', 'transbank_webpay_plus_rest'), 'placeholder' => 'Ej: 597012345678', - 'desc_tip' => $commerceCodeDescription, - 'type' => 'text', - 'default' => '', + 'desc_tip' => $commerceCodeDescription, + 'type' => 'text', + 'default' => '', ], 'webpay_rest_api_key' => [ - 'title' => __('API Key (llave secreta) producción', 'transbank_webpay_plus_rest'), - 'type' => 'password', + 'title' => __('API Key (llave secreta) producción', 'transbank_webpay_plus_rest'), + 'type' => 'password', 'placeholder' => 'Ej: XXXXXXXXXXXXXXXXXXXXXXXXXXXX', - 'desc_tip' => $apiKeyDescription, - 'default' => '', + 'desc_tip' => $apiKeyDescription, + 'default' => '', ], 'webpay_rest_after_payment_order_status' => [ - 'title' => __('Order Status', 'transbank_webpay_plus_rest'), - 'type' => 'select', - 'desc_tip' => 'Define el estado de la orden luego del pago exitoso.', + 'title' => __('Order Status', 'transbank_webpay_plus_rest'), + 'type' => 'select', + 'desc_tip' => 'Define el estado de la orden luego del pago exitoso.', 'options' => [ '' => 'Default', 'processing' => 'Processing', - 'completed' => 'Completed', + 'completed' => 'Completed', ], 'default' => '', ], 'webpay_rest_payment_gateway_description' => [ - 'title' => __('Descripción', 'transbank_webpay_plus_rest'), - 'type' => 'textarea', - 'desc_tip' => 'Define la descripción del medio de pago.', + 'title' => __('Descripción', 'transbank_webpay_plus_rest'), + 'type' => 'textarea', + 'desc_tip' => 'Define la descripción del medio de pago.', 'default' => self::PAYMENT_GW_DESCRIPTION, 'class' => 'admin-textarea' ] @@ -225,6 +225,9 @@ public function check_ipn_response() header('HTTP/1.1 200 OK'); $requestMethod = $_SERVER['REQUEST_METHOD']; $params = ($requestMethod === 'GET') ? $_GET : $_POST; + $this->log->logInfo('Procesando retorno desde formulario de Webpay.'); + $this->log->logInfo("Request: method -> $requestMethod"); + $this->log->logInfo('Request: payload -> ' . json_encode($params)); return (new ResponseController($this->config))->response($requestMethod, $params); } else { @@ -249,7 +252,7 @@ public function process_payment($order_id) $createResponse = $this->webpayplusTransbankSdk->createTransaction($order->get_id(), $amount, $returnUrl); do_action('transbank_webpay_plus_transaction_started', $order, $createResponse->token); return [ - 'result' => 'success', + 'result' => 'success', 'redirect' => $createResponse->url . '?token_ws=' . $createResponse->token, ]; } catch (CreateWebpayException $e) { From e224ade1eca8dd8e88e1f2fc216ab77a36f9459c Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 12:55:46 -0400 Subject: [PATCH 15/21] refactor: delete unnecessary if statement --- .../WC_Gateway_Transbank_Webpay_Plus_REST.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php index d3e9eff0..ac014347 100644 --- a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php +++ b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php @@ -221,18 +221,14 @@ public function init_form_fields() public function check_ipn_response() { ob_clean(); - if (isset($_POST)) { - header('HTTP/1.1 200 OK'); - $requestMethod = $_SERVER['REQUEST_METHOD']; - $params = ($requestMethod === 'GET') ? $_GET : $_POST; - $this->log->logInfo('Procesando retorno desde formulario de Webpay.'); - $this->log->logInfo("Request: method -> $requestMethod"); - $this->log->logInfo('Request: payload -> ' . json_encode($params)); + header('HTTP/1.1 200 OK'); + $requestMethod = $_SERVER['REQUEST_METHOD']; + $params = ($requestMethod === 'GET') ? $_GET : $_POST; + $this->log->logInfo('Procesando retorno desde formulario de Webpay.'); + $this->log->logInfo("Request: method -> $requestMethod"); + $this->log->logInfo('Request: payload -> ' . json_encode($params)); - return (new ResponseController($this->config))->response($requestMethod, $params); - } else { - echo 'Ocurrió un error al procesar su compra'; - } + return (new ResponseController($this->config))->response($requestMethod, $params); } From fa9eeab6f628c1de4fb421b904237e0f065b1d21 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 12:56:47 -0400 Subject: [PATCH 16/21] refactor: delete info in ipn function --- .../PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php index ac014347..c8335fbd 100644 --- a/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php +++ b/plugin/src/PaymentGateways/WC_Gateway_Transbank_Webpay_Plus_REST.php @@ -224,9 +224,6 @@ public function check_ipn_response() header('HTTP/1.1 200 OK'); $requestMethod = $_SERVER['REQUEST_METHOD']; $params = ($requestMethod === 'GET') ? $_GET : $_POST; - $this->log->logInfo('Procesando retorno desde formulario de Webpay.'); - $this->log->logInfo("Request: method -> $requestMethod"); - $this->log->logInfo('Request: payload -> ' . json_encode($params)); return (new ResponseController($this->config))->response($requestMethod, $params); } From ef3cf694946da92a934699a22ef7442eb31d5d0c Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 12:57:11 -0400 Subject: [PATCH 17/21] feat: add log for request info in controller --- plugin/src/Controllers/ResponseController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index f1c706ae..abef563f 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -51,6 +51,10 @@ public function __construct(array $pluginConfig) public function response($requestMethod, $params) { + $this->logger->logInfo('Procesando retorno desde formulario de Webpay.'); + $this->logger->logInfo("Request: method -> $requestMethod"); + $this->logger->logInfo('Request: payload -> ' . json_encode($params)); + $requestData = [ 'method' => $requestMethod, 'params' => $params From f52c5ca17af101a37a42b6c93c514f0e794d57f3 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 13:15:58 -0400 Subject: [PATCH 18/21] refactor: params are received directly --- plugin/src/WebpayplusTransbankSdk.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/src/WebpayplusTransbankSdk.php b/plugin/src/WebpayplusTransbankSdk.php index ce1ee741..fa7f36a2 100644 --- a/plugin/src/WebpayplusTransbankSdk.php +++ b/plugin/src/WebpayplusTransbankSdk.php @@ -438,9 +438,8 @@ private function detectFlow(array $params): string return self::WEBPAY_ERROR_FLOW; } - public function handleRequestFromTbkReturn(array $requestData) + public function handleRequestFromTbkReturn(array $params) { - $params = $requestData['params'] ?? null; $tokenWs = $params['token_ws'] ?? null; $tbkToken = $params['TBK_TOKEN'] ?? null; $sessionId = $params['TBK_ID_SESION'] ?? null; From 247d205bd3952753e0bba689226abac3b4365bae Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 13:17:41 -0400 Subject: [PATCH 19/21] refactor: request data creation is removed --- plugin/src/Controllers/ResponseController.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index abef563f..2b0862d2 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -55,13 +55,8 @@ public function response($requestMethod, $params) $this->logger->logInfo("Request: method -> $requestMethod"); $this->logger->logInfo('Request: payload -> ' . json_encode($params)); - $requestData = [ - 'method' => $requestMethod, - 'params' => $params - ]; - try { - $transaction = $this->webpayplusTransbankSdk->handleRequestFromTbkReturn($requestData); + $transaction = $this->webpayplusTransbankSdk->handleRequestFromTbkReturn($params); $wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id); $commitResponse = $this->webpayplusTransbankSdk->commitTransaction($transaction->order_id, $transaction->token); $this->completeWooCommerceOrder($wooCommerceOrder, $commitResponse, $transaction); From 369cdad40aa96b181d4f0d40f1023ebcb5654cbf Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 17:10:10 -0400 Subject: [PATCH 20/21] feat: use webpay plus sdk class --- plugin/src/Controllers/ResponseController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index 2b0862d2..fccd7613 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -4,6 +4,7 @@ use DateTime; use DateTimeZone; +use Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk; use Transbank\Webpay\WebpayPlus\Responses\TransactionCommitResponse; use Transbank\WooCommerce\WebpayRest\Models\Transaction; use Transbank\WooCommerce\WebpayRest\Helpers\HposHelper; @@ -28,7 +29,7 @@ class ResponseController protected $logger; /** - * @var \Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk + * @var WebpayplusTransbankSdk */ protected $webpayplusTransbankSdk; @@ -129,7 +130,7 @@ public function response($requestMethod, $params) $wooCommerceOrder = $this->getWooCommerceOrderById($order_id); $wooCommerceOrder->add_order_note($errorMessage); - if ($e->getFlow() == 'Normal') { + if ($e->getFlow() == WebpayplusTransbankSdk::WEBPAY_NORMAL_FLOW) { return wp_redirect($wooCommerceOrder->get_checkout_order_received_url()); } From f0437b719e9ebfb73b4f001d2bcc0e3ea755ab1e Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 13 Aug 2024 17:10:54 -0400 Subject: [PATCH 21/21] refactor: use camel case in order id --- plugin/src/Controllers/ResponseController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/src/Controllers/ResponseController.php b/plugin/src/Controllers/ResponseController.php index fccd7613..e5322dc8 100644 --- a/plugin/src/Controllers/ResponseController.php +++ b/plugin/src/Controllers/ResponseController.php @@ -126,8 +126,8 @@ public function response($requestMethod, $params) } catch (AlreadyProcessedException $e) { $errorMessage = 'Error al confirmar la transacción, ya fue procesada anteriormente'; $transaction = $e->getTransaction(); - $order_id = $transaction['order_id']; - $wooCommerceOrder = $this->getWooCommerceOrderById($order_id); + $orderId = $transaction['order_id']; + $wooCommerceOrder = $this->getWooCommerceOrderById($orderId); $wooCommerceOrder->add_order_note($errorMessage); if ($e->getFlow() == WebpayplusTransbankSdk::WEBPAY_NORMAL_FLOW) {