|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Paypal\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\App\RequestInterface; |
| 11 | +use Magento\Paypal\Controller\Payflow\ReturnUrl as Subject; |
| 12 | +use Magento\Paypal\Model\PayflowlinkFactory; |
| 13 | +use Magento\Sales\Model\Order; |
| 14 | +use Magento\Sales\Model\OrderFactory; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +class PayflowSilentPost |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected array $allowedOrderStates = [ |
| 23 | + Order::STATE_PROCESSING, |
| 24 | + Order::STATE_COMPLETE, |
| 25 | + Order::STATE_PAYMENT_REVIEW |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param RequestInterface $request |
| 30 | + * @param OrderFactory $orderFactory |
| 31 | + * @param PayflowlinkFactory $payflowlinkFactory |
| 32 | + * @param LoggerInterface $logger |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + private readonly RequestInterface $request, |
| 36 | + private readonly OrderFactory $orderFactory, |
| 37 | + private readonly PayflowlinkFactory $payflowlinkFactory, |
| 38 | + private readonly LoggerInterface $logger, |
| 39 | + ) { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Process payment if not already done via silent post |
| 44 | + * |
| 45 | + * @param Subject $subject |
| 46 | + * @return void |
| 47 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 48 | + * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 49 | + */ |
| 50 | + public function beforeExecute(Subject $subject): void |
| 51 | + { |
| 52 | + $data = $this->request->getParams(); |
| 53 | + if (!array_key_exists('INVNUM', $data) |
| 54 | + || !array_key_exists('RESPMSG', $data) |
| 55 | + || !array_key_exists('RESULT', $data)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $orderId = (string)$data['INVNUM']; |
| 60 | + if (!$orderId) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $order = $this->orderFactory->create()->loadByIncrementId($orderId); |
| 65 | + $payment = $order->getPayment(); |
| 66 | + if (in_array($order->getState(), $this->allowedOrderStates) || $payment->getLastTransId() |
| 67 | + || trim((string)$data['RESPMSG']) !== 'Approved' || (int)$data['RESULT'] !== 0) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + $paymentModel = $this->payflowlinkFactory->create(); |
| 72 | + try { |
| 73 | + $paymentModel->process($data); |
| 74 | + } catch (\Exception $e) { |
| 75 | + $this->logger->critical($e); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments