<?php
declare(strict_types=1);
namespace App\Infrastructure\PaymentGateway\IbanXS\Action;
use App\Infrastructure\PaymentGateway\IbanXS\Api;
use Payum\Core\Action\ActionInterface;
use Payum\Core\ApiAwareInterface;
use Payum\Core\ApiAwareTrait;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Capture;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Payum\Core\Request\Generic;
use App\Domain\Payment\Model\Payment as DomainPayment;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Types;
class CaptureAction implements ActionInterface, ApiAwareInterface, LoggerAwareInterface
{
use ApiAwareTrait;
use LoggerAwareTrait;
/**
* @var Api
*/
protected $api;
public function __construct()
{
$this->apiClass = Api::class;
}
public function execute($request): void
{
$paymentId = null;
if ($request instanceof Generic) {
$root = $request->getFirstModel();
if ($root instanceof DomainPayment) {
$paymentId = $root->getId();
}
}
$postData = ArrayObject::ensureArrayObject($request->getModel());
$response = $this->api->createHostedFlow($postData->toUnsafeArray(), $paymentId);
if (isset($response['uiUrlRedirect'])) {
$statusCode = 200;
$logs = [
'title' => 'Redirected to PISP',
];
try {
$logsJson = json_encode(
$logs,
JSON_THROW_ON_ERROR
| JSON_UNESCAPED_UNICODE
| JSON_UNESCAPED_SLASHES
| JSON_PRESERVE_ZERO_FRACTION
);
$conn = \Doctrine\DBAL\DriverManager::getConnection([
'url' => $_ENV['DATABASE_URL'] ?? 'mysql://user:pass@127.0.0.1:3306/your_db?charset=utf8mb4',
]);
$conn->insert(
'payment_logs',
[
'payment_id' => $paymentId,
'status_code' => $statusCode,
'logs' => $logsJson,
],
[
'payment_id' => is_numeric($paymentId) ? ParameterType::INTEGER : ParameterType::STRING,
'status_code' => ParameterType::INTEGER,
'logs' => Types::JSON,
]
);
} catch (\Throwable $e) {
}
throw new HttpRedirect($response['uiUrlRedirect']);
}
}
public function supports($request): bool
{
return
$request instanceof Capture
&& $request->getModel() instanceof \ArrayAccess;
}
}