-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from PAYONE-GmbH/feature/PAYONE-143-Przelewy24
[PAYONE-143] Add payment method Przelewy24
- Loading branch information
Showing
26 changed files
with
1,135 additions
and
9 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
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
77 changes: 77 additions & 0 deletions
77
src/EventListener/CheckoutConfirmPrzelewy24EventListener.php
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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\EventListener; | ||
|
||
use PayonePayment\PaymentHandler\PayonePrzelewy24PaymentHandler; | ||
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity; | ||
use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity; | ||
use Shopware\Core\Checkout\Payment\PaymentMethodCollection; | ||
use Shopware\Core\Checkout\Payment\PaymentMethodEntity; | ||
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent; | ||
use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent; | ||
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent; | ||
use Shopware\Storefront\Page\PageLoadedEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class CheckoutConfirmPrzelewy24EventListener implements EventSubscriberInterface | ||
{ | ||
private const ALLOWED_COUNTRIES = ['PL']; | ||
private const ALLOWED_CURRENCIES = ['PLN']; | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
CheckoutConfirmPageLoadedEvent::class => 'hidePaymentMethod', | ||
AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethod', | ||
AccountEditOrderPageLoadedEvent::class => 'hidePaymentMethod', | ||
]; | ||
} | ||
|
||
/** | ||
* @param AccountEditOrderPageLoadedEvent|AccountPaymentMethodPageLoadedEvent|CheckoutConfirmPageLoadedEvent $event | ||
*/ | ||
public function hidePaymentMethod(PageLoadedEvent $event): void | ||
{ | ||
$context = $event->getSalesChannelContext(); | ||
$customer = $context->getCustomer(); | ||
$page = $event->getPage(); | ||
|
||
$billingAddress = $customer ? $customer->getActiveBillingAddress() : null; | ||
|
||
if ($this->isCurrencyAllowed($context->getCurrency()->getIsoCode()) | ||
&& (!$billingAddress || $this->isAddressAllowed($billingAddress)) | ||
) { | ||
return; | ||
} | ||
|
||
$paymentMethods = $this->removePaymentMethods($page->getPaymentMethods(), [PayonePrzelewy24PaymentHandler::class]); | ||
|
||
$event->getPage()->setPaymentMethods($paymentMethods); | ||
} | ||
|
||
/** | ||
* @param CustomerAddressEntity|OrderAddressEntity $address | ||
*/ | ||
private function isAddressAllowed($address): bool | ||
{ | ||
$country = $address->getCountry(); | ||
|
||
return $country && \in_array($country->getIso(), self::ALLOWED_COUNTRIES, true); | ||
} | ||
|
||
private function isCurrencyAllowed(string $currencyCode): bool | ||
{ | ||
return \in_array($currencyCode, self::ALLOWED_CURRENCIES, true); | ||
} | ||
|
||
private function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $paymentHandler): PaymentMethodCollection | ||
{ | ||
return $paymentMethods->filter( | ||
static function (PaymentMethodEntity $paymentMethod) use ($paymentHandler) { | ||
return !\in_array($paymentMethod->getHandlerIdentifier(), $paymentHandler, true); | ||
} | ||
); | ||
} | ||
} |
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,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\PaymentHandler; | ||
|
||
use PayonePayment\Components\ConfigReader\ConfigReaderInterface; | ||
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface; | ||
use PayonePayment\Components\PaymentStateHandler\PaymentStateHandlerInterface; | ||
use PayonePayment\Payone\Client\Exception\PayoneRequestException; | ||
use PayonePayment\Payone\Client\PayoneClientInterface; | ||
use PayonePayment\Payone\RequestParameter\RequestParameterFactory; | ||
use PayonePayment\Payone\RequestParameter\Struct\PaymentTransactionStruct; | ||
use PayonePayment\Struct\PaymentTransaction; | ||
use Shopware\Core\Checkout\Payment\Cart\AsyncPaymentTransactionStruct; | ||
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\AsynchronousPaymentHandlerInterface; | ||
use Shopware\Core\Checkout\Payment\Exception\AsyncPaymentProcessException; | ||
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; | ||
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag; | ||
use Shopware\Core\System\SalesChannel\SalesChannelContext; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class PayonePrzelewy24PaymentHandler extends AbstractPayonePaymentHandler implements AsynchronousPaymentHandlerInterface | ||
{ | ||
private PayoneClientInterface $client; | ||
|
||
private TranslatorInterface $translator; | ||
|
||
private TransactionDataHandlerInterface $dataHandler; | ||
|
||
private PaymentStateHandlerInterface $stateHandler; | ||
|
||
private RequestParameterFactory $requestParameterFactory; | ||
|
||
public function __construct( | ||
ConfigReaderInterface $configReader, | ||
EntityRepositoryInterface $lineItemRepository, | ||
PayoneClientInterface $client, | ||
TranslatorInterface $translator, | ||
TransactionDataHandlerInterface $dataHandler, | ||
PaymentStateHandlerInterface $stateHandler, | ||
RequestStack $requestStack, | ||
RequestParameterFactory $requestParameterFactory | ||
) { | ||
parent::__construct($configReader, $lineItemRepository, $requestStack); | ||
|
||
$this->client = $client; | ||
$this->translator = $translator; | ||
$this->dataHandler = $dataHandler; | ||
$this->stateHandler = $stateHandler; | ||
$this->requestParameterFactory = $requestParameterFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse | ||
{ | ||
$requestData = $this->fetchRequestData(); | ||
|
||
// Get configured authorization method | ||
$authorizationMethod = $this->getAuthorizationMethod( | ||
$transaction->getOrder()->getSalesChannelId(), | ||
'przelewy24AuthorizationMethod', | ||
'authorization' | ||
); | ||
|
||
$paymentTransaction = PaymentTransaction::fromAsyncPaymentTransactionStruct($transaction, $transaction->getOrder()); | ||
|
||
$request = $this->requestParameterFactory->getRequestParameter( | ||
new PaymentTransactionStruct( | ||
$paymentTransaction, | ||
$requestData, | ||
$salesChannelContext, | ||
__CLASS__, | ||
$authorizationMethod | ||
) | ||
); | ||
|
||
try { | ||
$response = $this->client->request($request); | ||
} catch (PayoneRequestException $exception) { | ||
throw new AsyncPaymentProcessException( | ||
$transaction->getOrderTransaction()->getId(), | ||
$exception->getResponse()['error']['CustomerMessage'] | ||
); | ||
} catch (\Throwable $exception) { | ||
throw new AsyncPaymentProcessException( | ||
$transaction->getOrderTransaction()->getId(), | ||
$this->translator->trans('PayonePayment.errorMessages.genericError') | ||
); | ||
} | ||
|
||
if (empty($response['status']) && $response['status'] !== 'REDIRECT') { | ||
throw new AsyncPaymentProcessException( | ||
$transaction->getOrderTransaction()->getId(), | ||
$this->translator->trans('PayonePayment.errorMessages.genericError') | ||
); | ||
} | ||
|
||
$data = $this->preparePayoneOrderTransactionData($request, $response, [ | ||
'transactionState' => $response['status'], | ||
'allowCapture' => false, | ||
'allowRefund' => false, | ||
]); | ||
|
||
$this->dataHandler->saveTransactionData($paymentTransaction, $salesChannelContext->getContext(), $data); | ||
|
||
return new RedirectResponse($response['redirecturl']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function finalize(AsyncPaymentTransactionStruct $transaction, Request $request, SalesChannelContext $salesChannelContext): void | ||
{ | ||
$this->stateHandler->handleStateResponse($transaction, (string) $request->query->get('state')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function isCapturable(array $transactionData, array $payoneTransActionData): bool | ||
{ | ||
if (static::isNeverCapturable($payoneTransActionData)) { | ||
return false; | ||
} | ||
|
||
return static::isTransactionAppointedAndCompleted($transactionData) || static::matchesIsCapturableDefaults($transactionData); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function isRefundable(array $transactionData): bool | ||
{ | ||
if (static::isNeverRefundable($transactionData)) { | ||
return false; | ||
} | ||
|
||
return static::matchesIsRefundableDefaults($transactionData); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PayonePayment\PaymentMethod; | ||
|
||
use PayonePayment\Installer\PaymentMethodInstaller; | ||
use PayonePayment\PaymentHandler\PayonePrzelewy24PaymentHandler; | ||
|
||
class PayonePrzelewy24 extends AbstractPaymentMethod | ||
{ | ||
public const UUID = PaymentMethodInstaller::PAYMENT_METHOD_IDS[self::class]; | ||
|
||
protected string $id = self::UUID; | ||
|
||
protected string $name = 'PAYONE Przelewy24'; | ||
|
||
protected string $description = 'Pay save and secured with P24'; | ||
|
||
protected string $paymentHandler = PayonePrzelewy24PaymentHandler::class; | ||
|
||
protected ?string $template = null; | ||
|
||
protected array $translations = [ | ||
'de-DE' => [ | ||
'name' => 'PAYONE Przelewy24', | ||
'description' => 'Zahle sicher und geschützt mit P24', | ||
], | ||
'en-GB' => [ | ||
'name' => 'PAYONE Przelewy24', | ||
'description' => 'Pay save and secured with P24', | ||
], | ||
]; | ||
|
||
protected int $position = 160; | ||
} |
Oops, something went wrong.