Skip to content

Commit

Permalink
Merge pull request #256 from TransbankDevelopers/chore/prepare-releas…
Browse files Browse the repository at this point in the history
…e-1.9.2

chore: prepare release 1.9.2
  • Loading branch information
mastudillot authored Oct 15, 2024
2 parents 99c0105 + cadcca9 commit 18b2c01
Show file tree
Hide file tree
Showing 15 changed files with 457 additions and 253 deletions.
10 changes: 9 additions & 1 deletion plugin/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: TransbankDevelopers
Tags: transbank, webpay, oneclick, webpay plus, rest, chile
Requires at least: 5.3
Tested up to: 6.5.3
Tested up to: 6.6.2
Requires PHP: 7.4
Stable tag: VERSION_REPLACE_HERE
License: 3-Clause BSD License
Expand All @@ -14,6 +14,10 @@ Recibe pagos en línea con tarjetas de crédito, débito y prepago en tu WooComm
Recibe pagos en línea con tarjetas de crédito, débito y prepago en tu WooCommerce a través de Webpay Plus y Webpay Oneclick

== Changelog ==
= 1.9.2 =
Esta versión no tiene cambios en el comportamiento de las funcionalidades de la API.
*Se refina el flujo de pago de Webpay y Oneclick.

= 1.9.1 =
* Se arregla un mensaje de warning provocado por la función maskData en PHP mayor o igual a 8.x.
* Se arregla un problema que impedía encontrar el archivo de log al migrar el sitio de un servidor a otro.
Expand Down Expand Up @@ -169,6 +173,10 @@ Arreglado:
* Initial release.

== Upgrade Notice ==
= 1.9.2 =
Esta versión no tiene cambios en el comportamiento de las funcionalidades de la API.
*Se refina el flujo de pago de Webpay y Oneclick.

= 1.9.1 =
* Se arregla un mensaje de warning provocado por la función maskData en PHP mayor o igual a 8.x.
* Se arregla un problema que impedía encontrar el archivo de log al migrar el sitio de un servidor a otro.
Expand Down
32 changes: 32 additions & 0 deletions plugin/shared/Exceptions/Webpay/AlreadyProcessedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Transbank\Plugin\Exceptions\Webpay;

use Transbank\Plugin\Exceptions\BaseException;

class AlreadyProcessedException extends BaseException
{
private $transaction;
private $flow;

public function __construct(
$message,
$transaction,
$flow,
\Exception $previous = null
) {
$this->transaction = $transaction;
$this->flow = $flow;
parent::__construct($message, $previous);
}

public function getTransaction()
{
return $this->transaction;
}

public function getFlow()
{
return $this->flow;
}
}
10 changes: 0 additions & 10 deletions plugin/shared/Exceptions/Webpay/WithoutTokenWebpayException.php

This file was deleted.

14 changes: 7 additions & 7 deletions plugin/src/Blocks/js/notice_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -27,12 +27,12 @@ const oneClickNoticeData = {

const webPayNoticeNoticeData = {
7: { message: 'Transacción aprobada',
type: noticeTypes.SUCESS },
8: { message: 'El usuario intentó pagar esta orden nuevamente, cuando esta ya estaba pagada.',
type: noticeTypes.SUCCESS },
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 },
Expand All @@ -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 }
Expand All @@ -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:
Expand Down
134 changes: 53 additions & 81 deletions plugin/src/Controllers/ResponseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

use DateTime;
use DateTimeZone;
use Transbank\Plugin\Helpers\PluginLogger;
use Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk;
use Transbank\WooCommerce\WebpayRest\Helpers\TransactionResponseHandler;
use Transbank\Webpay\WebpayPlus\Responses\TransactionCommitResponse;
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;
Expand All @@ -22,15 +26,14 @@ class ResponseController
{
/**
* @var array
* @var PluginLogger
* @var WebpayplusTransbankSdk
* @var TransactionResponseHandler
*/
protected $pluginConfig;

protected $logger;

/**
* @var Transbank\WooCommerce\WebpayRest\WebpayplusTransbankSdk
*/
protected $webpayplusTransbankSdk;
protected array $pluginConfig;
protected PluginLogger $logger;
protected WebpayplusTransbankSdk $webpayplusTransbankSdk;
protected TransactionResponseHandler $transactionResponseHandler;

/**
* ResponseController constructor.
Expand All @@ -42,70 +45,50 @@ public function __construct(array $pluginConfig)
$this->logger = TbkFactory::createLogger();
$this->pluginConfig = $pluginConfig;
$this->webpayplusTransbankSdk = TbkFactory::createWebpayplusTransbankSdk();
$this->transactionResponseHandler = new TransactionResponseHandler();
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Transbank\Plugin\Exceptions\TokenNotFoundOnDatabaseException
*/
public function response($postData)

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));

try {
$transaction = $this->webpayplusTransbankSdk->processRequestFromTbkReturn($_SERVER, $_GET, $_POST);
$transaction = $this->transactionResponseHandler->handleRequestFromTbkReturn($params);
$wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id);
if ($wooCommerceOrder->is_paid()) {
// TODO: Revisar porqué no se muestra el mensaje de abajo. H4x
//SessionMessageHelper::set('Orden <strong>ya ha sido pagada</strong>.', '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());

$redirectUrl = $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);
$redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_TIMEOUT);
} 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;
$redirectUrl = $this->addErrorQueryParams($redirectUrl, BlocksHelper::WEBPAY_USER_CANCELED);
} 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);
$redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_DOUBLE_TOKEN);
} catch (InvalidStatusWebpayException $e) {
$errorMessage = 'No se puede confirmar la transacción, estado de transacción invalido.';
$wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id);
Expand All @@ -115,9 +98,7 @@ 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);
$redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_INVALID_STATUS);
} catch (RejectedCommitWebpayException $e) {
$transaction = $e->getTransaction();
$commitResponse = $e->getCommitResponse();
Expand All @@ -129,8 +110,7 @@ public function response($postData)
'transbankTransaction' => $transaction,
'transbankResponse' => $commitResponse
]);

return wp_redirect($wooCommerceOrder->get_checkout_order_received_url());
$redirectUrl = $wooCommerceOrder->get_checkout_order_received_url();
} catch (CommitWebpayException $e) {
$errorMessage = 'Error al confirmar la transacción de Transbank';
$wooCommerceOrder = $this->getWooCommerceOrderById($transaction->order_id);
Expand All @@ -140,32 +120,24 @@ public function response($postData)
'order' => $wooCommerceOrder->get_data(),
'transbankTransaction' => $e->getTransaction()
]);
$redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_COMMIT_ERROR);
} catch (AlreadyProcessedException $e) {
$errorMessage = 'Error al confirmar la transacción, ya fue procesada anteriormente';
$transaction = $e->getTransaction();
$orderId = $transaction['order_id'];
$wooCommerceOrder = $this->getWooCommerceOrderById($orderId);
$wooCommerceOrder->add_order_note($errorMessage);

$urlWithErrorCode = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_COMMIT_ERROR);
return wp_redirect($urlWithErrorCode);
$e->getFlow() == TransactionResponseHandler::WEBPAY_NORMAL_FLOW
? $redirectUrl = $wooCommerceOrder->get_checkout_order_received_url()
: $redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_ALREADY_PROCESSED);
} 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 "";
}

/**
* @param $data
*
* @return |null
*/
protected function getTokenWs($data)
{
$token_ws = isset($data['token_ws']) ? $data['token_ws'] : (isset($data['TBK_TOKEN']) ? $data['TBK_TOKEN'] : null);
if (!isset($token_ws)) {
$this->throwError('No se encontró el token');
wp_redirect(wc_get_checkout_url());
$redirectUrl = $this->addErrorQueryParams(wc_get_checkout_url(), BlocksHelper::WEBPAY_EXCEPTION);
}

return $token_ws;
return wp_redirect($redirectUrl);
}

/**
Expand All @@ -189,8 +161,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'));
Expand Down Expand Up @@ -221,7 +192,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);
}
Expand All @@ -235,8 +207,7 @@ protected function setWooCommerceOrderAsFailed(
WC_Order $wooCommerceOrder,
$webpayTransaction,
TransactionCommitResponse $commitResponse
)
{
) {
$_SESSION['woocommerce_order_failed'] = true;
$wooCommerceOrder->update_status('failed');
if ($commitResponse !== null) {
Expand All @@ -249,14 +220,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),
]
);
Expand Down Expand Up @@ -329,18 +300,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);
}
Expand Down
Loading

0 comments on commit 18b2c01

Please sign in to comment.