Skip to content

Commit

Permalink
Merge branch 'feature/paymentlink-pwa' into release-week-04
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Jan 29, 2024
2 parents 517cb6e + 1800f8f commit c497efc
Show file tree
Hide file tree
Showing 14 changed files with 471 additions and 59 deletions.
21 changes: 6 additions & 15 deletions Block/Info/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

namespace Mollie\Payment\Block\Info;

use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Registry;
use Magento\Framework\UrlInterface;
use Magento\Payment\Block\Info;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\Stdlib\DateTime;
Expand All @@ -21,6 +19,7 @@
use Mollie\Payment\Model\Methods\Klarnapaylater;
use Mollie\Payment\Model\Methods\Klarnapaynow;
use Mollie\Payment\Model\Methods\Klarnasliceit;
use Mollie\Payment\Service\Magento\PaymentLinkUrl;

class Base extends Info
{
Expand All @@ -44,36 +43,30 @@ class Base extends Info
* @var PriceCurrencyInterface
*/
private $price;
/**
* @var EncryptorInterface
*/
private $encryptor;
/**
* @var Config
*/
private $config;
/**
* @var UrlInterface
* @var PaymentLinkUrl
*/
private $urlBuilder;
private $paymentLinkUrl;

public function __construct(
Context $context,
Config $config,
MollieHelper $mollieHelper,
Registry $registry,
PriceCurrencyInterface $price,
EncryptorInterface $encryptor,
UrlInterface $urlBuilder
PaymentLinkUrl $paymentLinkUrl
) {
parent::__construct($context);
$this->mollieHelper = $mollieHelper;
$this->timezone = $context->getLocaleDate();
$this->registry = $registry;
$this->price = $price;
$this->encryptor = $encryptor;
$this->config = $config;
$this->urlBuilder = $urlBuilder;
$this->paymentLinkUrl = $paymentLinkUrl;
}

public function getCheckoutType(): ?string
Expand Down Expand Up @@ -114,9 +107,7 @@ public function getPaymentLink($storeId = null): ?string

public function getPaymentLinkUrl(): string
{
return $this->urlBuilder->getUrl('mollie/checkout/paymentlink', [
'order' => base64_encode($this->encryptor->encrypt($this->getInfo()->getParentId())),
]);
return $this->paymentLinkUrl->execute((int)$this->getInfo()->getParentId());
}

public function getCheckoutUrl(): ?string
Expand Down
15 changes: 15 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Config
const PAYMENT_PAYMENTLINK_NEW_STATUS = 'payment/mollie_methods_paymentlink/order_status_new';
const PAYMENT_PAYMENTLINK_ADD_MESSAGE = 'payment/mollie_methods_paymentlink/add_message';
const PAYMENT_PAYMENTLINK_MESSAGE = 'payment/mollie_methods_paymentlink/message';
const PAYMENT_USE_CUSTOM_PAYMENTLINK_URL = 'payment/mollie_general/use_custom_paymentlink_url';
const PAYMENT_CUSTOM_PAYMENTLINK_URL = 'payment/mollie_general/custom_paymentlink_url';
const PAYMENT_POINTOFSALE_ALLOWED_CUSTOMER_GROUPS = 'payment/mollie_methods_pointofsale/allowed_customer_groups';
const PAYMENT_VOUCHER_CATEGORY = 'payment/mollie_methods_voucher/category';
const PAYMENT_VOUCHER_CUSTOM_ATTRIBUTE = 'payment/mollie_methods_voucher/custom_attribute';
Expand Down Expand Up @@ -513,6 +515,19 @@ public function paymentLinkMessage($storeId = null): string
);
}

public function useCustomPaymentLinkUrl($storeId = null): bool
{
return $this->isSetFlag(static::PAYMENT_USE_CUSTOM_PAYMENTLINK_URL, $storeId);
}

public function customPaymentLinkUrl($storeId = null): string
{
return (string)$this->getPath(
static::PAYMENT_CUSTOM_PAYMENTLINK_URL,
$storeId
);
}

/**
* @param string $method
* @param int|null $storeId
Expand Down
39 changes: 8 additions & 31 deletions Controller/Checkout/PaymentLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Service\Magento\PaymentLinkRedirect;

class PaymentLink implements HttpGetActionInterface
{
/**
* @var RequestInterface
*/
private $request;
/**
* @var EncryptorInterface
*/
private $encryptor;
/**
* @var ResultFactory
*/
Expand All @@ -38,28 +31,20 @@ class PaymentLink implements HttpGetActionInterface
*/
private $messageManager;
/**
* @var OrderRepositoryInterface
* @var PaymentLinkRedirect
*/
private $orderRepository;
/**
* @var Mollie
*/
private $mollie;
private $paymentLinkRedirect;

public function __construct(
RequestInterface $request,
EncryptorInterface $encryptor,
ResultFactory $resultFactory,
ManagerInterface $messageManager,
OrderRepositoryInterface $orderRepository,
Mollie $mollie
PaymentLinkRedirect $paymentLinkRedirect
) {
$this->request = $request;
$this->encryptor = $encryptor;
$this->resultFactory = $resultFactory;
$this->messageManager = $messageManager;
$this->orderRepository = $orderRepository;
$this->mollie = $mollie;
$this->paymentLinkRedirect = $paymentLinkRedirect;
}

public function execute()
Expand All @@ -69,27 +54,19 @@ public function execute()
return $this->returnStatusCode(400);
}

$id = $this->encryptor->decrypt(base64_decode($orderKey));

if (empty($id)) {
return $this->returnStatusCode(404);
}

try {
$order = $this->orderRepository->get($id);
$result = $this->paymentLinkRedirect->execute($orderKey);
} catch (NoSuchEntityException $exception) {
return $this->returnStatusCode(404);
}

if (in_array($order->getState(), [Order::STATE_PROCESSING, Order::STATE_COMPLETE])) {
if ($result->isAlreadyPaid()) {
$this->messageManager->addSuccessMessage(__('Your order has already been paid.'));

return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl('/');
}

$url = $this->mollie->startTransaction($order);

return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl($url);
return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setUrl($result->getRedirectUrl());
}

public function returnStatusCode(int $code): ResultInterface
Expand Down
46 changes: 46 additions & 0 deletions GraphQL/Resolver/Checkout/PaymentLinkRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\GraphQL\Resolver\Checkout;

use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Mollie\Payment\Service\Magento\PaymentLinkRedirect as PaymentLinkRedirectService;

class PaymentLinkRedirect implements ResolverInterface
{
/**
* @var PaymentLinkRedirectService
*/
private $paymentLinkRedirect;

public function __construct(
PaymentLinkRedirectService $paymentLinkRedirect
) {
$this->paymentLinkRedirect = $paymentLinkRedirect;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$order = $args['order'];

try {
$result = $this->paymentLinkRedirect->execute($order);
} catch (NotFoundException $exception) {
throw new GraphQlNoSuchEntityException(__('Order not found'));
}

return [
'already_paid' => $result->isAlreadyPaid(),
'redirect_url' => $result->getRedirectUrl(),
];
}
}
75 changes: 75 additions & 0 deletions Service/Magento/PaymentLinkRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Magento;

use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\NotFoundException;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Mollie\Payment\Model\Mollie;

class PaymentLinkRedirect
{
/**
* @var EncryptorInterface
*/
private $encryptor;
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var Mollie
*/
private $mollie;
/**
* @var PaymentLinkRedirectResultFactory
*/
private $paymentLinkRedirectResultFactory;

public function __construct(
EncryptorInterface $encryptor,
OrderRepositoryInterface $orderRepository,
Mollie $mollie,
PaymentLinkRedirectResultFactory $paymentLinkRedirectResultFactory
) {
$this->encryptor = $encryptor;
$this->orderRepository = $orderRepository;
$this->mollie = $mollie;
$this->paymentLinkRedirectResultFactory = $paymentLinkRedirectResultFactory;
}

public function execute(string $orderId): PaymentLinkRedirectResult
{
$id = $this->encryptor->decrypt(base64_decode($orderId));

if (empty($id)) {
throw new NotFoundException(__('Order not found'));
}

try {
$order = $this->orderRepository->get($id);
} catch (NoSuchEntityException $exception) {
throw new NotFoundException(__('Order not found'));
}

if (in_array($order->getState(), [Order::STATE_PROCESSING, Order::STATE_COMPLETE])) {
return $this->paymentLinkRedirectResultFactory->create([
'redirectUrl' => null,
'alreadyPaid' => true,
]);
}

return $this->paymentLinkRedirectResultFactory->create([
'redirectUrl' => $this->mollie->startTransaction($order),
'alreadyPaid' => false,
]);
}
}
39 changes: 39 additions & 0 deletions Service/Magento/PaymentLinkRedirectResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Magento;

class PaymentLinkRedirectResult
{
/**
* @var bool
*/
private $alreadyPaid;
/**
* @var string|null
*/
private $redirectUrl;

public function __construct(
bool $alreadyPaid,
string $redirectUrl = null
) {
$this->alreadyPaid = $alreadyPaid;
$this->redirectUrl = $redirectUrl;
}

public function isAlreadyPaid(): bool
{
return $this->alreadyPaid;
}

public function getRedirectUrl(): ?string
{
return $this->redirectUrl;
}
}
Loading

0 comments on commit c497efc

Please sign in to comment.