Skip to content

Commit

Permalink
Merge pull request #254 from TransbankDevelopers/feat/support-php-8.2
Browse files Browse the repository at this point in the history
feat: support php 8.2
  • Loading branch information
mastudillot authored Sep 23, 2024
2 parents 9a04cf6 + 287a402 commit bb798d8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 56 deletions.
4 changes: 2 additions & 2 deletions plugin/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"require": {
"transbank/transbank-sdk": "~2.0",
"ext-json": "*",
"php": ">=7.0",
"php": "^8.2",
"monolog/monolog": "^1.27"
},
"config": {
"platform": {
"php": "7.0"
"php": "8.2"
}
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions plugin/shared/Helpers/InfoUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class InfoUtil
*/
public static function getValidatephp()
{
if (version_compare(phpversion(), '8.1.8', '<=') &&
version_compare(phpversion(), '7.0.0', '>=')) {
if (version_compare(phpversion(), '8.3.11', '<=') &&
version_compare(phpversion(), '8.2.0', '>=')) {
return [
'status' => 'OK',
'version' => phpversion(),
Expand Down
137 changes: 87 additions & 50 deletions plugin/src/Controllers/TransactionStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,74 +8,111 @@

class TransactionStatusController
{


public static function getStatus()
const HTTP_OK = 200;
const HTTP_UNPROCESSABLE_ENTITY = 422;
public function getStatus()
{
$response = [
'body' => [
'message' => 'No se pudo obtener el estado de la transacción'
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
// Check for nonce security
$nonce = sanitize_text_field($_POST['nonce']);

if (!wp_verify_nonce($nonce, 'my-ajax-nonce')) {
wp_send_json($response['body'], $response['code']);
return;
}

$orderId = filter_input(INPUT_POST, 'order_id', FILTER_SANITIZE_NUMBER_INT);
$buyOrder = filter_input(INPUT_POST, 'buy_order', FILTER_SANITIZE_STRING);
$token = filter_input(INPUT_POST, 'token', FILTER_SANITIZE_STRING);
$orderId = filter_input(INPUT_POST, 'order_id', FILTER_DEFAULT);
$orderId = htmlspecialchars($orderId, ENT_QUOTES, 'UTF-8');
$buyOrder = filter_input(INPUT_POST, 'buy_order', FILTER_DEFAULT);
$buyOrder = htmlspecialchars($buyOrder, ENT_QUOTES, 'UTF-8');
$token = filter_input(INPUT_POST, 'token', FILTER_DEFAULT);
$token = htmlspecialchars($token, ENT_QUOTES, 'UTF-8');

$transaction = Transaction::getApprovedByOrderId($orderId);
if (!$transaction) {
wp_send_json([
'message' => 'No hay transacciones webpay aprobadas para esta orden',
], 401);
}
try {
$transaction = Transaction::getApprovedByOrderId($orderId);
if (!$transaction) {
$response = [
'body' => [
'message' => 'No hay transacciones webpay aprobadas para esta orden'
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
}

if ($transaction->product == Transaction::PRODUCT_WEBPAY_ONECLICK) {
if ($transaction->buy_order !== $buyOrder) {
$response = [
'body' => [
'message' => 'El buy_order enviado y el buy_order de la transacción no coinciden'
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
}

if ($transaction->product == Transaction::PRODUCT_WEBPAY_ONECLICK) {
if ($transaction->buy_order !== $buyOrder) {
wp_send_json([
'message' => 'El buy_order enviado y el buy_order de la transacción no coinciden',
], 401);
$response = [
'body' => $this->getStatusForOneclickTransaction($orderId, $buyOrder),
'code' => self::HTTP_OK
];
}
$oneclickTransbankSdk = TbkFactory::createOneclickTransbankSdk();
$status = $oneclickTransbankSdk->status($orderId, $buyOrder);
$statusArray = json_decode(json_encode($status), true);
$firstDetail = json_decode(json_encode($status->getDetails()[0]), true);

$response = array_merge($statusArray, $firstDetail);
$formattedDate = TbkResponseUtil::transactionDateToLocalDate($status->getTransactionDate());
$response['transactionDate'] = $formattedDate;
unset($response['details']);
wp_send_json([
'product' => $transaction->product,
'status' => $response,
'raw' => $status,
]);

return;
}
if ($transaction->token !== $token) {
$response = [
'body' => [
'message' => 'El token enviado y el token de la transacción no coinciden'
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
}

if ($transaction->token !== $token) {
wp_send_json([
'message' => 'El token enviado y el token de la transacción no coinciden',
], 401);
}
$response = [
'body' => $this->getStatusForWebpayTransaction($orderId, $token),
'code' => self::HTTP_OK
];

try {
$webpayplusTransbankSdk = TbkFactory::createWebpayplusTransbankSdk();
$resp = $webpayplusTransbankSdk->status($transaction->order_id, $transaction->token);
$formattedDate = TbkResponseUtil::transactionDateToLocalDate($resp->getTransactionDate());
$modifiedResponse = clone $resp;
$modifiedResponse->setTransactionDate($formattedDate);
wp_send_json([
'product' => $transaction->product,
'status' => $modifiedResponse,
'raw' => $resp,
]);
wp_send_json($response['body'], $response['code']);
} catch (\Exception $e) {
wp_send_json([
'message' => $e->getMessage(),
], 422);
}
}
}

private function getStatusForWebpayTransaction(string $orderId, string $token)
{
$webpayplusTransbankSdk = TbkFactory::createWebpayplusTransbankSdk();
$resp = $webpayplusTransbankSdk->status($orderId, $token);
$formattedDate = TbkResponseUtil::transactionDateToLocalDate($resp->getTransactionDate());
$modifiedResponse = clone $resp;
$modifiedResponse->setTransactionDate($formattedDate);

return [
'product' => Transaction::PRODUCT_WEBPAY_PLUS,
'status' => $modifiedResponse,
'raw' => $resp,
];
}

private function getStatusForOneclickTransaction(string $orderId, string $buyOrder)
{
$oneclickTransbankSdk = TbkFactory::createOneclickTransbankSdk();
$status = $oneclickTransbankSdk->status($orderId, $buyOrder);
$statusArray = json_decode(json_encode($status), true);
$firstDetail = json_decode(json_encode($status->getDetails()[0]), true);

$response = array_merge($statusArray, $firstDetail);
$formattedDate = TbkResponseUtil::transactionDateToLocalDate($status->getTransactionDate());
$response['transactionDate'] = $formattedDate;
unset($response['details']);

return [
'product' => Transaction::PRODUCT_WEBPAY_ONECLICK,
'status' => $response,
'raw' => $status,
];
}
}
5 changes: 3 additions & 2 deletions plugin/webpay-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

add_action('wp_ajax_check_connection', ConnectionCheck::class.'::check');
add_action('wp_ajax_check_exist_tables', TableCheck::class.'::check');
add_action('wp_ajax_get_transaction_status', TransactionStatusController::class.'::getStatus');
add_action('wp_ajax_get_transaction_status', [new TransactionStatusController(), 'getStatus']);
add_action('woocommerce_before_cart', 'transbank_rest_before_cart');

add_action('woocommerce_before_checkout_form', 'transbank_rest_check_cancelled_checkout');
Expand Down Expand Up @@ -124,7 +124,8 @@ function registerPaymentGateways() {
function registerAdminMenu() {
add_action('admin_menu', function () {
add_submenu_page('woocommerce', __('Configuración de Webpay Plus', 'transbank_wc_plugin'), 'Webpay Plus', 'administrator', 'transbank_webpay_plus_rest', function () {
$tab = filter_input(INPUT_GET, 'tbk_tab', FILTER_SANITIZE_STRING);
$tab = filter_input(INPUT_GET, 'tbk_tab', FILTER_DEFAULT);
$tab = htmlspecialchars($tab, ENT_QUOTES, 'UTF-8');
if (!in_array($tab, ['healthcheck', 'logs', 'transactions'])) {
wp_redirect(admin_url('admin.php?page=wc-settings&tab=checkout&section=transbank_webpay_plus_rest&tbk_tab=options'));
}
Expand Down

0 comments on commit bb798d8

Please sign in to comment.