-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BP-2957 Set order to open on browser back, cancel group payment trans…
…action on a new payment request & BP-2960 (#149) * Skip giftcard pushes, let the order open * BP-2957 Set order to open on browser back, cancel group payment transaction on a new payment request & BP-2960 * changed version --------- Co-authored-by: Ivascu Madalin <madalin.ivascu@arnia.ro>
- Loading branch information
Showing
8 changed files
with
269 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 24 additions & 26 deletions
50
src/Resources/views/storefront/page/account/order/index.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
{% sw_extends '@Storefront/storefront/page/account/order/index.html.twig' %} | ||
|
||
{% block page_checkout_confirm_header %} | ||
|
||
{% if page.errorCode == 'PAYMENT_FAILED_ERROR_490' %} | ||
{% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { | ||
type: 'danger', | ||
content: 'buckaroo.statuscode_failed'|trans|sw_sanitize | ||
} %} | ||
{% elseif page.errorCode == 'PAYMENT_FAILED_ERROR_690' %} | ||
{% set lastPaymentTrans = page.order.transactions.last() %} | ||
{% set sesMessage = 'buckaroo.statuscode_failed'|trans|sw_sanitize %} | ||
{% if ( | ||
lastPaymentTrans !== null && | ||
lastPaymentTrans.paymentMethod !== null && | ||
lastPaymentTrans.paymentMethod.shortName === 'billink_payment_handler' | ||
) | ||
%} | ||
{% set sesMessage = 'buckaroo.billinkRejectedMessage'|trans|sw_sanitize %} | ||
{% endif %} | ||
{% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { | ||
type: 'danger', | ||
content: sesMessage | ||
} %} | ||
{% else %} | ||
{{ parent() }} | ||
{% endif %} | ||
{% if 'PAYMENT_FAILED_ERROR_' not in page.errorCode %} | ||
{{ parent() }} | ||
{% if page.errorCode == 'PAYMENT_FAILED_ERROR_490' %} | ||
{% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { | ||
type: 'danger', | ||
content: 'buckaroo.statuscode_failed'|trans|sw_sanitize | ||
} %} | ||
{% elseif page.errorCode == 'PAYMENT_FAILED_ERROR_690' %} | ||
{% set lastPaymentTrans = page.order.transactions.last() %} | ||
{% set sesMessage = 'buckaroo.statuscode_failed'|trans|sw_sanitize %} | ||
{% if ( | ||
lastPaymentTrans !== null && | ||
lastPaymentTrans.paymentMethod !== null && | ||
lastPaymentTrans.paymentMethod.shortName === 'billink_payment_handler' | ||
) | ||
%} | ||
{% set sesMessage = 'buckaroo.billinkRejectedMessage'|trans|sw_sanitize %} | ||
{% endif %} | ||
{% sw_include '@Storefront/storefront/utilities/alert.html.twig' with { | ||
type: 'danger', | ||
content: sesMessage | ||
} %} | ||
{% elseif 'PAYMENT_FAILED_ERROR_' not in page.errorCode %} | ||
{{ parent() }} | ||
{% else %} | ||
{{ parent() }} | ||
{% endif %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buckaroo\Shopware6\Service; | ||
|
||
use Buckaroo\Shopware6\Buckaroo\Client; | ||
use Buckaroo\Shopware6\Service\UrlService; | ||
use Shopware\Core\Checkout\Order\OrderEntity; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Buckaroo\Shopware6\Service\Buckaroo\ClientService; | ||
use Buckaroo\Shopware6\Buckaroo\ClientResponseInterface; | ||
use Buckaroo\Shopware6\Helpers\Constants\ResponseStatus; | ||
use Buckaroo\Shopware6\Helpers\Constants\IPProtocolVersion; | ||
use Buckaroo\Shopware6\Entity\Transaction\BuckarooTransactionEntity; | ||
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct; | ||
use Buckaroo\Shopware6\Entity\Transaction\BuckarooTransactionEntityRepository; | ||
|
||
class CancelPaymentService | ||
{ | ||
protected BuckarooTransactionEntityRepository $buckarooTransactionEntityRepository; | ||
|
||
protected UrlService $urlService; | ||
|
||
protected ClientService $clientService; | ||
|
||
public function __construct( | ||
BuckarooTransactionEntityRepository $buckarooTransactionEntityRepository, | ||
UrlService $urlService, | ||
ClientService $clientService | ||
) { | ||
$this->buckarooTransactionEntityRepository = $buckarooTransactionEntityRepository; | ||
$this->urlService = $urlService; | ||
$this->clientService = $clientService; | ||
} | ||
|
||
|
||
/** | ||
* Do a refund on hanging giftcards | ||
* | ||
* @param AsyncPaymentTransactionStruct $transactionStruct | ||
* | ||
* @return void | ||
*/ | ||
public function cancel( | ||
AsyncPaymentTransactionStruct $transactionStruct | ||
): void { | ||
|
||
$orderTransaction = $transactionStruct->getOrderTransaction(); | ||
$transactions = $this->buckarooTransactionEntityRepository->findByOrderId($orderTransaction->getOrderId()); | ||
|
||
foreach ($transactions as $transaction) { | ||
if ( | ||
$transaction === null || | ||
$transaction->get('statuscode') != ResponseStatus::BUCKAROO_STATUSCODE_SUCCESS | ||
) { | ||
continue; | ||
} | ||
|
||
$order = $transactionStruct->getOrder(); | ||
$amount = $transaction->get('amount'); | ||
$paymentCode = $transaction->get('transaction_method'); | ||
|
||
if ( | ||
!is_scalar($amount) || | ||
(float)$amount <= 0 || | ||
!is_string($paymentCode) | ||
) { | ||
continue; | ||
} | ||
|
||
|
||
$client = $this->getClient( | ||
'giftcards', | ||
$order->getSalesChannelId() | ||
) | ||
->setAction('refund') | ||
->setPayload( | ||
$this->getPayload( | ||
$order, | ||
$transaction->get('transactions'), | ||
$orderTransaction->getId(), | ||
(float)$amount, | ||
$paymentCode | ||
) | ||
); | ||
$this->handleResponse( | ||
$client->execute(), | ||
$transaction | ||
); | ||
} | ||
} | ||
|
||
protected function handleResponse( | ||
ClientResponseInterface $response, | ||
BuckarooTransactionEntity $transaction | ||
) { | ||
if (!$response->isSuccess()) { | ||
return; | ||
} | ||
|
||
$amountCredit = $transaction->get('amount_credit'); | ||
|
||
if (!is_scalar($amountCredit)) { | ||
$amountCredit = 0; | ||
} | ||
|
||
$transactionAmount = $response->get('brq_amount'); | ||
|
||
if (!is_scalar($transactionAmount)) { | ||
return; | ||
} | ||
|
||
$this->buckarooTransactionEntityRepository | ||
->save( | ||
$transaction->get('id'), | ||
[ | ||
'amount_credit' => (string)((float)$amountCredit + (float)$transactionAmount) | ||
], | ||
); | ||
} | ||
|
||
|
||
protected function getCurrencyIso(OrderEntity $order): string | ||
{ | ||
$currency = $order->getCurrency(); | ||
if ($currency === null) { | ||
throw new \InvalidArgumentException('Cannot find currency on order'); | ||
} | ||
return $currency->getIsoCode(); | ||
} | ||
|
||
/** | ||
* Get request parameters | ||
* | ||
* @param OrderEntity $order | ||
* @param mixed $transactionKey | ||
* @param string $orderTransactionId | ||
* @param float $amount | ||
* @param string $transactionKey | ||
* | ||
* @return array<mixed> | ||
*/ | ||
private function getPayload( | ||
OrderEntity $order, | ||
$transactionKey, | ||
string $orderTransactionId, | ||
float $amount, | ||
string $paymentCode | ||
): array { | ||
|
||
if (!is_scalar($transactionKey)) { | ||
$transactionKey = ''; | ||
} | ||
|
||
return [ | ||
'order' => $order->getOrderNumber(), | ||
'invoice' => $order->getOrderNumber(), | ||
'amountCredit' => $amount, | ||
'currency' => $this->getCurrencyIso($order), | ||
'pushURL' => $this->urlService->getReturnUrl('buckaroo.payment.push'), | ||
'pushURLFailure' => $this->urlService->getReturnUrl('buckaroo.payment.push'), | ||
'clientIP' => $this->getIp(), | ||
'originalTransactionKey' => (string)$transactionKey, | ||
'additionalParameters' => [ | ||
'orderTransactionId' => $orderTransactionId, | ||
'orderId' => $order->getId(), | ||
], | ||
"name" => $paymentCode, | ||
]; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get buckaroo client | ||
* | ||
* @param string $paymentCode | ||
* @param string $salesChannelId | ||
* | ||
* @return Client | ||
*/ | ||
private function getClient(string $paymentCode, string $salesChannelId): Client | ||
{ | ||
return $this->clientService | ||
->get($paymentCode, $salesChannelId); | ||
} | ||
|
||
|
||
/** | ||
* Get client ip | ||
* | ||
* @param Request $request | ||
* | ||
* @return array<mixed> | ||
*/ | ||
private function getIp(): array | ||
{ | ||
$remoteIp = (Request::createFromGlobals())->getClientIp(); | ||
|
||
return [ | ||
'address' => $remoteIp, | ||
'type' => IPProtocolVersion::getVersion($remoteIp) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.