Skip to content

Commit

Permalink
Merge pull request #738 from mollie/release/2.34.0
Browse files Browse the repository at this point in the history
Release/2.34.0
  • Loading branch information
Marvin-Magmodules authored Jan 30, 2024
2 parents 0f727ba + 8e27289 commit ac30d84
Show file tree
Hide file tree
Showing 24 changed files with 824 additions and 79 deletions.
2 changes: 1 addition & 1 deletion Api/Data/IssuerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getName(): string;
public function getImage(): string;

/**
* @return array
* @return string[]
*/
public function getImages(): array;

Expand Down
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
26 changes: 10 additions & 16 deletions Controller/Checkout/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session;
use Mollie\Payment\Service\Mollie\ValidateProcessRequest;
use Mollie\Payment\Service\Order\RedirectOnError;

/**
Expand Down Expand Up @@ -58,6 +59,10 @@ class Process extends Action
* @var RedirectOnError
*/
private $redirectOnError;
/**
* @var ValidateProcessRequest
*/
private $validateProcessRequest;

public function __construct(
Context $context,
Expand All @@ -67,7 +72,8 @@ public function __construct(
MollieHelper $mollieHelper,
OrderRepositoryInterface $orderRepository,
RedirectOnError $redirectOnError,
ManagerInterface $eventManager
ManagerInterface $eventManager,
ValidateProcessRequest $validateProcessRequest
) {
$this->checkoutSession = $checkoutSession;
$this->paymentHelper = $paymentHelper;
Expand All @@ -76,6 +82,7 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->redirectOnError = $redirectOnError;
$this->eventManager = $eventManager;
$this->validateProcessRequest = $validateProcessRequest;

parent::__construct($context);
}
Expand All @@ -85,7 +92,7 @@ public function __construct(
*/
public function execute()
{
$orderIds = $this->getOrderIds();
$orderIds = $this->validateProcessRequest->execute();
if (!$orderIds) {
$this->mollieHelper->addTolog('error', __('Invalid return, missing order id.'));
$this->messageManager->addNoticeMessage(__('Invalid return from Mollie.'));
Expand All @@ -94,8 +101,7 @@ public function execute()

try {
$result = [];
$paymentToken = $this->getRequest()->getParam('payment_token');
foreach ($orderIds as $orderId) {
foreach ($orderIds as $orderId => $paymentToken) {
$result = $this->mollieModel->processTransaction($orderId, 'success', $paymentToken);
}
} catch (\Exception $e) {
Expand Down Expand Up @@ -134,18 +140,6 @@ public function execute()
return $this->handleNonSuccessResult($result, $orderIds);
}

/**
* @return array
*/
protected function getOrderIds(): array
{
if ($orderId = $this->getRequest()->getParam('order_id')) {
return [$orderId];
}

return $this->getRequest()->getParam('order_ids') ?? [];
}

protected function handleNonSuccessResult(array $result, array $orderIds): ResponseInterface
{
$this->checkIfLastRealOrder($orderIds);
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,
]);
}
}
Loading

0 comments on commit ac30d84

Please sign in to comment.