From d4d257cd01fdeb026e372d8588a5d23c54368a43 Mon Sep 17 00:00:00 2001 From: doncho Date: Thu, 6 Feb 2025 12:27:31 +0200 Subject: [PATCH] Add transaction verification call to the workflow. --- src/CommonParameters.php | 10 +++++ src/Gateway.php | 32 +++++++++++++++ src/Message/CheckoutPageResponse.php | 15 +------ src/Message/GetTransactionRequest.php | 29 ++++++++++++++ src/Message/GetTransactionResponse.php | 55 ++++++++++++++++++++++++++ 5 files changed, 128 insertions(+), 13 deletions(-) create mode 100644 src/Message/GetTransactionRequest.php create mode 100644 src/Message/GetTransactionResponse.php diff --git a/src/CommonParameters.php b/src/CommonParameters.php index c3cec61..a8aa7c5 100644 --- a/src/CommonParameters.php +++ b/src/CommonParameters.php @@ -133,4 +133,14 @@ public function getBaseUrlParam(): string { return $this->getParameter('baseUrlParam'); } + + public function setTrnType(string $value): void + { + $this->setParameter('trnType', $value); + } + + public function getTrnType(): string + { + return $this->getParameter('trnType'); + } } diff --git a/src/Gateway.php b/src/Gateway.php index f468a72..9f064ea 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -3,13 +3,20 @@ namespace Ampeco\OmnipayApcopay; use Ampeco\OmnipayApcopay\Message\CheckoutPageRequest; +use Ampeco\OmnipayApcopay\Message\GetTransactionRequest; use Ampeco\OmnipayApcopay\Message\Request; use Ampeco\OmnipayApcopay\Message\ApcopayNotification; +use Ampeco\OmnipayPayze\Message\Response; use Omnipay\Common\AbstractGateway; use Omnipay\Common\Message\RequestInterface; class Gateway extends AbstractGateway { + private const STATUS_APPROVED = 'APPROVED'; + private const STATUS_CAPTURED = 'CAPTURED'; + private const STATUS_VOIDED = 'VOIDED'; + private const STATUS_DECLINED = 'DECLINED'; + public function getName(): string { return 'Apcopay'; @@ -50,6 +57,31 @@ public function initial(array $options = array()): RequestInterface return $this->createRequest(Request::class, $options); } + public function getTransaction(array $options = array()): RequestInterface + { + return $this->createRequest(GetTransactionRequest::class, $options); + } + + public function getCapturedTransactionStatus(): string + { + return self::STATUS_CAPTURED; + } + + public function getAuthTransactionStatus(): string + { + return self::STATUS_APPROVED; + } + + public function getVoidedTransactionStatus(): string + { + return self::STATUS_VOIDED; + } + + public function getDeclinedTransactionStatus(): string + { + return self::STATUS_DECLINED; + } + public function getAvailableCurrencies(): array { return [ diff --git a/src/Message/CheckoutPageResponse.php b/src/Message/CheckoutPageResponse.php index cc7b44f..c105b5c 100644 --- a/src/Message/CheckoutPageResponse.php +++ b/src/Message/CheckoutPageResponse.php @@ -2,31 +2,20 @@ namespace Ampeco\OmnipayApcopay\Message; -use Exception; use Omnipay\Common\Message\AbstractResponse; use Omnipay\Common\Message\RedirectResponseInterface; use Omnipay\Common\Message\RequestInterface; class CheckoutPageResponse extends AbstractResponse implements RedirectResponseInterface { - private const STATUS_OK = 'OK'; - public function __construct(RequestInterface $request, array $data, protected int $code) { - try { - $pattern = '/params=([^&"]*)/'; - $params = preg_match($pattern, $data['message'] ?? '', $matches) ? urldecode($matches[1] ?? '') : null; - $response = $params ? json_decode(json_encode(simplexml_load_string($params)), true) : $data; - } catch (Exception $e) { - $response = $data; - } - - parent::__construct($request, $response); + parent::__construct($request, $data); } public function isSuccessful() : bool { - return $this->code == 302 && (($this->data['Result'] ?? '') === self::STATUS_OK); + return $this->code == 302; } public function isRedirect(): bool diff --git a/src/Message/GetTransactionRequest.php b/src/Message/GetTransactionRequest.php new file mode 100644 index 0000000..b234d14 --- /dev/null +++ b/src/Message/GetTransactionRequest.php @@ -0,0 +1,29 @@ +getTrnType()); + } + + public function getData(): array + { + return [ + 'MerchID' => $this->getMerchantCode(), + 'MerchPass' => $this->getMerchantPassword(), + 'Oref' => $this->getReference(), + ]; + } +} diff --git a/src/Message/GetTransactionResponse.php b/src/Message/GetTransactionResponse.php new file mode 100644 index 0000000..783bbae --- /dev/null +++ b/src/Message/GetTransactionResponse.php @@ -0,0 +1,55 @@ +transactionStatus = null; + $this->transactionReference = null; + + parent::__construct($request, $data); + $this->setTransactionData(); + } + + public function isSuccessful() : bool + { + return $this->code == 200 && isset($this->data['Result']) && $this->data['Result'] == self::RESULT_OK; + } + + public function getTransactionStatus(): ?string + { + return $this->transactionStatus; + } + + public function getTransactionReference(): ?string + { + return $this->transactionReference; + } + + private function setTransactionData(): void + { + if (!isset($this->data['Transactions']) || !is_array($this->data['Transactions'])) { + return; + } + + foreach ($this->data['Transactions'] as $transaction) { + if (isset($transaction['TrnType']) && $transaction['TrnType'] === $this->trnType + && isset($transaction['BankAccept']) && $transaction['BankAccept'] === self::BANK_ACCEPT_YES + ) { + $this->transactionStatus = $transaction['BankResponse'] ?? null; + $this->transactionReference = $transaction['PSPID'] ?? null; + } + } + } +}