Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve information for transaction status #263

Merged
merged 19 commits into from
Nov 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c4130e5
refactor: improve code flow
mastudillot Nov 10, 2024
fd88140
refactor: split code for transaction status meta box
mastudillot Nov 11, 2024
b23df79
feat: update expired query status error message
mastudillot Nov 13, 2024
589fa9b
refactor: reduce code in catch block for status method
mastudillot Nov 13, 2024
a0ad2e6
feat: add specific error message in catch block for oneclick exception
mastudillot Nov 13, 2024
8c176df
feat: improve exception message for oneclick status
mastudillot Nov 13, 2024
5e0a6cd
refactor: use constants for exception message
mastudillot Nov 13, 2024
ca901d8
feat: add getCommonFieldsStatusFormatted method
mastudillot Nov 13, 2024
3f51149
feat: add getWebpayStatusFormattedResponse method
mastudillot Nov 13, 2024
b063b68
feat: add getOneclickStatusFormattedResponse method
mastudillot Nov 13, 2024
317b38f
feat: add variables for colors in global context
mastudillot Nov 13, 2024
5b973c2
feat: improve information in status meta box
mastudillot Nov 13, 2024
fd2709c
fix: typo in exception message
mastudillot Nov 13, 2024
43c5a55
feat: remove duplicated status error message
mastudillot Nov 13, 2024
4c5a646
feat: add ajax hooks in init event
mastudillot Nov 13, 2024
f85f4d9
feat: add log to status controller
mastudillot Nov 13, 2024
f6be020
feat: register hook add_meta_boxes in the correct order
mastudillot Nov 13, 2024
f384083
feat: use default status error message in api mismatch exception
mastudillot Nov 13, 2024
c522ae9
feat: add field key for status payload params in log
mastudillot Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: improve code flow
mastudillot committed Nov 13, 2024
commit c4130e5c7c8be85593f07032c16d298874009c64
125 changes: 75 additions & 50 deletions plugin/src/Controllers/TransactionStatusController.php
Original file line number Diff line number Diff line change
@@ -10,69 +10,39 @@ class TransactionStatusController
{
const HTTP_OK = 200;
const HTTP_UNPROCESSABLE_ENTITY = 422;
public function getStatus()
const DEFAULT_ERROR_MESSAGE = 'No se pudo obtener el estado de la transacción';
const NO_TRANSACTION_ERROR_MESSAGE = 'No hay transacciones webpay aprobadas para esta orden';
const BUY_ORDER_MISMATCH_ERROR_MESSAGE = 'El buy_order enviado y el buy_order de la transacción no coinciden';
const TOKEN_MISMATCH_ERROR_MESSAGE = 'El token enviado y el token de la transacción no coinciden';
public function getStatus(): void
{
$response = [
'body' => [
'message' => 'No se pudo obtener el estado de la transacción'
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
'message' => self::DEFAULT_ERROR_MESSAGE
]
];
// 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']);
wp_send_json($response['body'], self::HTTP_UNPROCESSABLE_ENTITY);
return;
}

$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');
$orderId = $this->getSecureInputValue('order_id');
$buyOrder = $this->getSecureInputValue('buy_order');
$token = $this->getSecureInputValue('token');

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
];
}

$response = [
'body' => $this->getStatusForOneclickTransaction($orderId, $buyOrder),
'code' => self::HTTP_OK
];
}

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) {
$response['body'] = self::NO_TRANSACTION_ERROR_MESSAGE;
wp_send_json($response['body'], self::HTTP_UNPROCESSABLE_ENTITY);
return;
}

$response = [
'body' => $this->getStatusForWebpayTransaction($orderId, $token),
'code' => self::HTTP_OK
];
$response = $this->handleGetStatus($transaction, $orderId, $buyOrder, $token);

wp_send_json($response['body'], $response['code']);
} catch (\Exception $e) {
@@ -82,10 +52,59 @@ public function getStatus()
}
}

private function handleGetStatus(object $transaction, string $orderId, string $buyOrder, string $token): array
{
if ($transaction->product == Transaction::PRODUCT_WEBPAY_ONECLICK) {
return $this->handleOneclickStatus($orderId, $buyOrder, $transaction->buy_order);
}

return $this->handleWebpayStatus($orderId, $token, $transaction->token);
}

private function handleOneclickStatus(
string $orderId,
string $requestBuyOrder,
string $transactionBuyOrder
): array {
if ($transactionBuyOrder !== $requestBuyOrder) {
return [
'body' => [
'message' => self::BUY_ORDER_MISMATCH_ERROR_MESSAGE
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
}

return [
'body' => $this->getStatusForOneclickTransaction($orderId, $transactionBuyOrder),
'code' => self::HTTP_OK
];
}

private function handleWebpayStatus(
string $orderId,
string $requestToken,
string $transactionToken
): array {
if ($transactionToken !== $requestToken) {
return [
'body' => [
'message' => self::TOKEN_MISMATCH_ERROR_MESSAGE
],
'code' => self::HTTP_UNPROCESSABLE_ENTITY
];
}

return [
'body' => $this->getStatusForWebpayTransaction($orderId, $transactionToken),
'code' => self::HTTP_OK
];
}

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

private function getStatusForOneclickTransaction(string $orderId, string $buyOrder)
{
$oneclickTransbankSdk = TbkFactory::createOneclickTransbankSdk();
$status = $oneclickTransbankSdk->status($orderId, $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);

@@ -115,4 +134,10 @@ private function getStatusForOneclickTransaction(string $orderId, string $buyOrd
'raw' => $status,
];
}

private function getSecureInputValue(string $varName): string
{
$tmpValue = filter_input(INPUT_POST, $varName, FILTER_DEFAULT);
return htmlspecialchars($tmpValue, ENT_QUOTES, 'UTF-8');
}
}