src/Infrastructure/PaymentGateway/IbanXS/Action/CaptureAction.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Infrastructure\PaymentGateway\IbanXS\Action;
  4. use App\Infrastructure\PaymentGateway\IbanXS\Api;
  5. use Payum\Core\Action\ActionInterface;
  6. use Payum\Core\ApiAwareInterface;
  7. use Payum\Core\ApiAwareTrait;
  8. use Payum\Core\Bridge\Spl\ArrayObject;
  9. use Payum\Core\Reply\HttpRedirect;
  10. use Payum\Core\Request\Capture;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerAwareTrait;
  13. use Payum\Core\Request\Generic;
  14. use App\Domain\Payment\Model\Payment as DomainPayment;
  15. use Doctrine\DBAL\DriverManager;
  16. use Doctrine\DBAL\ParameterType;
  17. use Doctrine\DBAL\Types\Types;
  18. class CaptureAction implements ActionInterface, ApiAwareInterface, LoggerAwareInterface
  19. {
  20.     use ApiAwareTrait;
  21.     use LoggerAwareTrait;
  22.     /**
  23.      * @var Api
  24.      */
  25.     protected $api;
  26.     public function __construct()
  27.     {
  28.         $this->apiClass = Api::class;
  29.     }
  30.     public function execute($request): void
  31.     {
  32.         $paymentId = null;
  33.         if ($request instanceof Generic) {
  34.             $root = $request->getFirstModel();
  35.             if ($root instanceof DomainPayment) {
  36.                 $paymentId = $root->getId();
  37.             }
  38.         }
  39.         $postData = ArrayObject::ensureArrayObject($request->getModel());
  40.         $response = $this->api->createHostedFlow($postData->toUnsafeArray(), $paymentId);
  41.         if (isset($response['uiUrlRedirect'])) {
  42.             $statusCode = 200;
  43.             $logs = [
  44.                 'title' => 'Redirected to PISP',
  45.             ];
  46.             try {
  47.                 $logsJson = json_encode(
  48.                     $logs,
  49.                     JSON_THROW_ON_ERROR
  50.                         | JSON_UNESCAPED_UNICODE
  51.                         | JSON_UNESCAPED_SLASHES
  52.                         | JSON_PRESERVE_ZERO_FRACTION
  53.                 );
  54.                 $conn = \Doctrine\DBAL\DriverManager::getConnection([
  55.                     'url' => $_ENV['DATABASE_URL'] ?? 'mysql://user:pass@127.0.0.1:3306/your_db?charset=utf8mb4',
  56.                 ]);
  57.                 $conn->insert(
  58.                     'payment_logs',
  59.                     [
  60.                         'payment_id'  => $paymentId,
  61.                         'status_code' => $statusCode,
  62.                         'logs'        => $logsJson,
  63.                     ],
  64.                     [
  65.                         'payment_id'  => is_numeric($paymentId) ? ParameterType::INTEGER : ParameterType::STRING,
  66.                         'status_code' => ParameterType::INTEGER,
  67.                         'logs'        => Types::JSON,
  68.                     ]
  69.                 );
  70.             } catch (\Throwable $e) {
  71.             }
  72.             throw new HttpRedirect($response['uiUrlRedirect']);
  73.         }
  74.     }
  75.     public function supports($request): bool
  76.     {
  77.         return
  78.             $request instanceof Capture
  79.             && $request->getModel() instanceof \ArrayAccess;
  80.     }
  81. }