Skip to content

Commit

Permalink
Add transaction verification call to the workflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
doncho-toromanov committed Feb 6, 2025
1 parent d8f4eef commit d4d257c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/CommonParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
32 changes: 32 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 [
Expand Down
15 changes: 2 additions & 13 deletions src/Message/CheckoutPageResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/Message/GetTransactionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Ampeco\OmnipayApcopay\Message;

use Omnipay\Common\Message\ResponseInterface;

class GetTransactionRequest extends AbstractRequest
{
private const API_GET_TRANSACTION_ENDPOINT = '/MerchantTools/MerchantTools.svc/getTransactionsByORef';

public function getEndpoint(): string
{
return self::API_GET_TRANSACTION_ENDPOINT;
}

protected function createResponse(array $data, int $statusCode): ResponseInterface
{
return new GetTransactionResponse($this, $data, $statusCode, $this->getTrnType());
}

public function getData(): array
{
return [
'MerchID' => $this->getMerchantCode(),
'MerchPass' => $this->getMerchantPassword(),
'Oref' => $this->getReference(),
];
}
}
55 changes: 55 additions & 0 deletions src/Message/GetTransactionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Ampeco\OmnipayApcopay\Message;

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RequestInterface;

class GetTransactionResponse extends AbstractResponse
{
private const BANK_ACCEPT_YES = 'YES';
private const RESULT_OK = 'OK';

private ?string $transactionStatus;
private ?string $transactionReference;

public function __construct(RequestInterface $request, array $data, private int $code, private string $trnType)
{
$this->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;
}
}
}
}

0 comments on commit d4d257c

Please sign in to comment.