Skip to content

Commit

Permalink
Merge pull request #35 from payrexx/bugfix/PP-13303
Browse files Browse the repository at this point in the history
bugfix/PP-13303: Shopware 6 Transaction Issues
  • Loading branch information
vinothss4u authored Dec 3, 2024
2 parents 632c893 + 63f1cd7 commit c28095a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
31 changes: 28 additions & 3 deletions PayrexxPaymentGatewaySW6/src/Handler/PaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $
$this->customAsyncException($transactionId, $message);
}

// Delete gateway from all transactions.
foreach($order->getTransactions() as $transactionGateway) {
$oldGatewayId = $transactionGateway->getCustomFields()['gateway_id'] ?? '';
if ($oldGatewayId) {
$gatewayStatus = $this->payrexxApiService->deletePayrexxGateway(
$salesChannelId,
(int) $oldGatewayId
);
if ($gatewayStatus) {
$this->transactionHandler->saveTransactionCustomFields(
$salesChannelContext,
$transactionGateway->getId(),
[
'gateway_id' => '',
]
);
}
}
}

if (in_array($paymentMean, ['sofortueberweisung_de', 'postfinance_card', 'postfinance_efinance'])) {
throw new Exception('Unavailable payment method error');
}
Expand Down Expand Up @@ -198,6 +218,7 @@ public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $
public function finalize(AsyncPaymentTransactionStruct $shopwareTransaction, Request $request, SalesChannelContext $salesChannelContext): void
{
$context = $salesChannelContext->getContext();
$salesChannelId = $salesChannelContext->getSalesChannel()->getId();

$orderTransaction = $shopwareTransaction->getOrderTransaction();
$customFields = $orderTransaction->getCustomFields();
Expand All @@ -215,11 +236,15 @@ public function finalize(AsyncPaymentTransactionStruct $shopwareTransaction, Req
$this->customCustomerException($transactionId, 'Customer canceled the payment on the Payrexx page');
}

$gatewayIds = explode(',', (string) $gatewayId);
$payrexxGateway = $this->payrexxApiService->getPayrexxGateway(current($gatewayIds), $salesChannelContext->getSalesChannel()->getId());
$payrexxTransaction = $this->payrexxApiService->getTransactionByGateway($payrexxGateway, $salesChannelContext->getSalesChannel()->getId());
$gatewayIds = explode(',', (string) $gatewayId); // TODO: later remove explode.
$gatewayId = current($gatewayIds);
$payrexxGateway = $this->payrexxApiService->getPayrexxGateway($gatewayId, $salesChannelId);
$payrexxTransaction = $this->payrexxApiService->getTransactionByGateway($payrexxGateway, $salesChannelId);

if (!$payrexxTransaction && $totalAmount > 0) {
if ($gatewayId) {
$this->payrexxApiService->deletePayrexxGateway($salesChannelId, (int) $gatewayId);
}
$this->customCustomerException($transactionId, 'Customer canceled the payment on the Payrexx page');
}

Expand Down
24 changes: 6 additions & 18 deletions PayrexxPaymentGatewaySW6/src/Handler/TransactionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,6 @@ public function saveTransactionCustomFields($salesChannelContext, $transactionId
{
$transactionRepo = $this->container->get('order_transaction.repository');

// Manage existing gateway ids.
$criteria = new Criteria([$transactionId]);
$criteria->addAssociation('customFields');
$transaction = $transactionRepo->search($criteria, $salesChannelContext->getContext())->first();
if ($transaction) {
$customFields = $transaction->getCustomFields() ?? [];
$gatewayIds = $customFields['gateway_id'] ?? '';

if (!empty($gatewayIds)) {
// Save new gateway id first.
$newGatewayId = $details['gateway_id'] . ',' . $gatewayIds;
$newGatewayIds = array_slice(explode(',', $newGatewayId), 0, 5);
$details['gateway_id'] = implode(',', $newGatewayIds);
}
}

$transactionRepo->upsert([[
'id' => $transactionId,
'customFields' => $details
Expand Down Expand Up @@ -120,11 +104,15 @@ public function handleTransactionStatus(OrderTransactionEntity $orderTransaction
case Transaction::CANCELLED:
case Transaction::DECLINED:
case Transaction::EXPIRED:
if ($state !== null && in_array($orderTransaction->getStateMachineState()->getTechnicalName(), [OrderTransactionStates::STATE_CANCELLED, OrderTransactionStates::STATE_PAID])) break;
if ($state !== null && !in_array($orderTransaction->getStateMachineState()->getTechnicalName(), [OrderTransactionStates::STATE_OPEN, OrderTransactionStates::STATE_UNCONFIRMED])) {
break;
}
$this->transactionStateHandler->cancel($orderTransaction->getId(), $context);
break;
case Transaction::ERROR:
if ($state !== null && in_array($orderTransaction->getStateMachineState()->getTechnicalName(), [OrderTransactionStates::STATE_FAILED, OrderTransactionStates::STATE_PAID])) break;
if ($state !== null && !in_array($orderTransaction->getStateMachineState()->getTechnicalName(), [OrderTransactionStates::STATE_OPEN, OrderTransactionStates::STATE_UNCONFIRMED])) {
break;
}
$this->transactionStateHandler->fail($orderTransaction->getId(), $context);
break;
}
Expand Down
30 changes: 30 additions & 0 deletions PayrexxPaymentGatewaySW6/src/Service/PayrexxApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,34 @@ public function getPayrexxTransaction(int $payrexxTransactionId, $salesChannelId
return null;
}
}

/**
* Delete the payrexx gateway
*
* @param string $salesChannelId
* @param int $gatewayId
* @return bool
*/
public function deletePayrexxGateway(string $salesChannelId, int $gatewayId): bool
{
if (empty($gatewayId)) {
return true;
}

$payrexx = $this->getInterface($salesChannelId);
$gateway = $this->getPayrexxGateway($gatewayId, $salesChannelId);

if (!$gateway) {
return true; // Already deleted.
}
if ($payrexx && $gateway && !$this->getTransactionByGateway($gateway, $salesChannelId)) {
try {
$payrexx->delete($gateway);
return true;
} catch (\Payrexx\PayrexxException $e) {
// no action.
}
}
return false;
}
}

0 comments on commit c28095a

Please sign in to comment.