|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * NOTICE OF LICENSE |
| 4 | + * |
| 5 | + * This source file is subject to the MIT License |
| 6 | + * It is available through the world-wide-web at this URL: |
| 7 | + * https://tldrlegal.com/license/mit-license |
| 8 | + * If you are unable to obtain it through the world-wide-web, please send an email |
| 9 | + * to support@buckaroo.nl so we can send you a copy immediately. |
| 10 | + * |
| 11 | + * DISCLAIMER |
| 12 | + * |
| 13 | + * Do not edit or add to this file if you wish to upgrade this module to newer |
| 14 | + * versions in the future. If you wish to customize this module for your |
| 15 | + * needs please contact support@buckaroo.nl for more information. |
| 16 | + * |
| 17 | + * @copyright Copyright (c) Buckaroo B.V. |
| 18 | + * @license https://tldrlegal.com/license/mit-license |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace Buckaroo\Magento2Graphql\Plugin\Transaction; |
| 24 | + |
| 25 | +use Buckaroo\Magento2\Api\TransactionResponseInterface; |
| 26 | +use Buckaroo\Magento2\Helper\PaymentGroupTransaction; |
| 27 | +use Buckaroo\Magento2\Logging\Log; |
| 28 | +use Buckaroo\Magento2\Model\Transaction\Status\ProcessResponse; |
| 29 | +use Magento\Sales\Model\Order; |
| 30 | + |
| 31 | +/** |
| 32 | + * Plugin to handle group transaction saving in GraphQL checkout flow |
| 33 | + * |
| 34 | + * @api |
| 35 | + */ |
| 36 | +class ProcessResponsePlugin |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var PaymentGroupTransaction |
| 40 | + */ |
| 41 | + private PaymentGroupTransaction $groupTransactionHelper; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Log |
| 45 | + */ |
| 46 | + private Log $logger; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param PaymentGroupTransaction $groupTransactionHelper |
| 50 | + * @param Log $logger |
| 51 | + */ |
| 52 | + public function __construct( |
| 53 | + PaymentGroupTransaction $groupTransactionHelper, |
| 54 | + Log $logger |
| 55 | + ) { |
| 56 | + $this->groupTransactionHelper = $groupTransactionHelper; |
| 57 | + $this->logger = $logger; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Handle group transaction processing after successful response processing |
| 62 | + * |
| 63 | + * @param ProcessResponse $subject |
| 64 | + * @param array|null $result |
| 65 | + * @param TransactionResponseInterface $response |
| 66 | + * @param Order $order |
| 67 | + * @return array|null |
| 68 | + */ |
| 69 | + public function afterProcess( |
| 70 | + ProcessResponse $subject, |
| 71 | + ?array $result, |
| 72 | + TransactionResponseInterface $response, |
| 73 | + Order $order |
| 74 | + ): ?array { |
| 75 | + if (!$result) { |
| 76 | + return $result; |
| 77 | + } |
| 78 | + |
| 79 | + // Only process successful transactions |
| 80 | + if (!isset($result['payment_status']) || $result['payment_status'] !== 'success') { |
| 81 | + return $result; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + $this->processGroupTransactions($order, $response); |
| 86 | + } catch (\Exception $e) { |
| 87 | + $this->logger->addError( |
| 88 | + sprintf( |
| 89 | + 'GraphQL Group Transaction Plugin Error: %s | Order: %s', |
| 90 | + $e->getMessage(), |
| 91 | + $order->getIncrementId() |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return $result; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Process group transactions from payment additional information |
| 101 | + * |
| 102 | + * @param Order $order |
| 103 | + * @param TransactionResponseInterface $response |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + private function processGroupTransactions(Order $order, TransactionResponseInterface $response): void |
| 107 | + { |
| 108 | + $payment = $order->getPayment(); |
| 109 | + if (!$payment) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + $allTransactions = $payment->getAdditionalInformation('buckaroo_all_transactions'); |
| 114 | + if (!is_array($allTransactions) || count($allTransactions) <= 1) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $this->logger->addDebug( |
| 119 | + sprintf( |
| 120 | + 'Processing GraphQL Group Transactions for Order: %s | Transactions: %s', |
| 121 | + $order->getIncrementId(), |
| 122 | + json_encode(array_keys($allTransactions)) |
| 123 | + ) |
| 124 | + ); |
| 125 | + |
| 126 | + foreach ($allTransactions as $transactionId => $transactionData) { |
| 127 | + if (!$this->isValidTransactionData($transactionData)) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + $this->saveGroupTransaction($order, $transactionId, $transactionData, $response); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Validate transaction data structure |
| 137 | + * |
| 138 | + * @param mixed $transactionData |
| 139 | + * @return bool |
| 140 | + */ |
| 141 | + private function isValidTransactionData($transactionData): bool |
| 142 | + { |
| 143 | + return is_array($transactionData) |
| 144 | + && count($transactionData) >= 2 |
| 145 | + && is_string($transactionData[0]) |
| 146 | + && is_numeric($transactionData[1]); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Save individual group transaction to database |
| 151 | + * |
| 152 | + * @param Order $order |
| 153 | + * @param string $transactionId |
| 154 | + * @param array $transactionData |
| 155 | + * @param TransactionResponseInterface $response |
| 156 | + * @return void |
| 157 | + */ |
| 158 | + private function saveGroupTransaction( |
| 159 | + Order $order, |
| 160 | + string $transactionId, |
| 161 | + array $transactionData, |
| 162 | + TransactionResponseInterface $response |
| 163 | + ): void { |
| 164 | + $serviceCode = $transactionData[0]; |
| 165 | + $amount = (float)$transactionData[1]; |
| 166 | + |
| 167 | + // Build response array compatible with existing saveGroupTransaction method |
| 168 | + $groupTransactionResponse = [ |
| 169 | + 'Invoice' => $order->getIncrementId(), |
| 170 | + 'Key' => $transactionId, |
| 171 | + 'ServiceCode' => $serviceCode, |
| 172 | + 'Currency' => $order->getOrderCurrencyCode(), |
| 173 | + 'AmountDebit' => $amount, |
| 174 | + 'Status' => ['Code' => ['Code' => $response->getStatusCode()]], |
| 175 | + 'RequiredAction' => [ |
| 176 | + 'PayRemainderDetails' => [ |
| 177 | + 'GroupTransaction' => $this->extractGroupTransactionId($order) |
| 178 | + ] |
| 179 | + ], |
| 180 | + 'RelatedTransactions' => [ |
| 181 | + ['RelationType' => 'grouptransaction'] |
| 182 | + ] |
| 183 | + ]; |
| 184 | + |
| 185 | + $this->groupTransactionHelper->saveGroupTransaction($groupTransactionResponse); |
| 186 | + |
| 187 | + $this->logger->addDebug( |
| 188 | + sprintf( |
| 189 | + 'GraphQL Group Transaction Saved: %s | Service: %s | Amount: %s | Order: %s', |
| 190 | + $transactionId, |
| 191 | + $serviceCode, |
| 192 | + $amount, |
| 193 | + $order->getIncrementId() |
| 194 | + ) |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Extract group transaction ID from order payment |
| 200 | + * |
| 201 | + * @param Order $order |
| 202 | + * @return string|null |
| 203 | + */ |
| 204 | + private function extractGroupTransactionId(Order $order): ?string |
| 205 | + { |
| 206 | + $payment = $order->getPayment(); |
| 207 | + if (!$payment) { |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + // Try to get from original transaction key |
| 212 | + $originalKey = $payment->getAdditionalInformation('buckaroo_original_transaction_key'); |
| 213 | + if ($originalKey) { |
| 214 | + return $originalKey; |
| 215 | + } |
| 216 | + |
| 217 | + // Fallback to any transaction from buckaroo_all_transactions |
| 218 | + $allTransactions = $payment->getAdditionalInformation('buckaroo_all_transactions'); |
| 219 | + if (is_array($allTransactions) && !empty($allTransactions)) { |
| 220 | + return array_keys($allTransactions)[0]; |
| 221 | + } |
| 222 | + |
| 223 | + return null; |
| 224 | + } |
| 225 | + |
| 226 | +} |
0 commit comments