Skip to content

Commit 1554826

Browse files
Merge pull request #35 from Itonomy/feature/CMP-85_implement-elv-direct-flow
CMP-85 - Implement ELV direct flow
2 parents cf7b1a1 + 24d3001 commit 1554826

File tree

21 files changed

+912
-14
lines changed

21 files changed

+912
-14
lines changed

Api/Service/MethodServiceInterface.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ interface MethodServiceInterface
2121
ConfigProvider::CODE_CREDIT_CARD,
2222
ConfigProvider::CODE_IDEAL,
2323
ConfigProvider::CODE_PAYPAL,
24-
ConfigProvider::CODE_BANCONTACT
24+
ConfigProvider::CODE_BANCONTACT,
25+
ConfigProvider::CODE_ELV
2526
];
2627

2728
/**
@@ -33,15 +34,17 @@ interface MethodServiceInterface
3334
'MAESTRO' => ConfigProvider::CODE_CREDIT_CARD,
3435
'IDEAL' => ConfigProvider::CODE_IDEAL,
3536
'PAYPAL_EXPRESS_CHECKOUT' => ConfigProvider::CODE_PAYPAL,
36-
'BANCONTACT' => ConfigProvider::CODE_BANCONTACT
37+
'BANCONTACT' => ConfigProvider::CODE_BANCONTACT,
38+
'ELV' => ConfigProvider::CODE_ELV
3739
];
3840

3941
/**
4042
* Mapping of Magento Payment methods to CM Api Payment methods
4143
*/
4244
public const API_METHODS_MAPPING = [
4345
ConfigProvider::CODE_IDEAL => 'IDEAL',
44-
ConfigProvider::CODE_PAYPAL => 'PAYPAL'
46+
ConfigProvider::CODE_PAYPAL => 'PAYPAL',
47+
ConfigProvider::CODE_ELV => 'ELV'
4548
];
4649

4750
/**

Client/Model/Request/PaymentCreate.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ class PaymentCreate
2121
*/
2222
private $idealDetails;
2323

24+
/**
25+
* @var array
26+
*/
27+
private $elvDetails;
28+
2429
/**
2530
* Order constructor
2631
*
2732
* @param string $method
2833
* @param array $idealDetails
34+
* @param array $elvDetails
2935
*/
3036
public function __construct(
3137
string $method = '',
32-
array $idealDetails = []
38+
array $idealDetails = [],
39+
array $elvDetails = []
3340
) {
3441
$this->method = $method;
3542
$this->idealDetails = $idealDetails;
43+
$this->elvDetails = $elvDetails;
3644
}
3745

3846
/**
@@ -50,6 +58,10 @@ public function toArray(): array
5058
$data['ideal_details'] = $this->idealDetails;
5159
}
5260

61+
if ($this->elvDetails) {
62+
$data['elv_payment_input'] = $this->elvDetails;
63+
}
64+
5365
return $data;
5466
}
5567

@@ -84,4 +96,20 @@ public function setIdealDetails(array $idealDetails): void
8496
{
8597
$this->idealDetails = $idealDetails;
8698
}
99+
100+
/**
101+
* @return array
102+
*/
103+
public function getElvDetails(): array
104+
{
105+
return $this->elvDetails;
106+
}
107+
108+
/**
109+
* @param array $elvDetails
110+
*/
111+
public function setElvDetails(array $elvDetails): void
112+
{
113+
$this->elvDetails = $elvDetails;
114+
}
87115
}

Client/Request/PaymentCreateRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class PaymentCreateRequest implements RequestInterface
2929
private $orderKey;
3030

3131
/**
32-
* PaymentCreateRequest constructor.
32+
* PaymentCreateRequest constructor
3333
*
3434
* @param string $orderKey
3535
* @param PaymentCreate $orderCreate
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Copyright © CM.com. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace CM\Payments\Controller\Payment;
10+
11+
use CM\Payments\Api\Service\OrderServiceInterface;
12+
use CM\Payments\Api\Service\PaymentServiceInterface;
13+
use Exception;
14+
use Magento\Checkout\Model\Session;
15+
use Magento\Framework\App\Action\Action;
16+
use Magento\Framework\App\Action\Context;
17+
use Magento\Framework\App\Action\HttpGetActionInterface;
18+
use Magento\Framework\Controller\Result\Redirect;
19+
use Magento\Framework\Controller\Result\RedirectFactory;
20+
use Magento\Framework\Exception\LocalizedException;
21+
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
22+
use Magento\Framework\Phrase;
23+
use Psr\Log\LoggerInterface;
24+
25+
class ElvConfirmation extends Action implements HttpGetActionInterface
26+
{
27+
/**
28+
* @var MessageManagerInterface
29+
*/
30+
protected $messageManager;
31+
32+
/**
33+
* @var Session
34+
*/
35+
private $checkoutSession;
36+
37+
/**
38+
* @var RedirectFactory
39+
*/
40+
private $redirectFactory;
41+
42+
/**
43+
* @var OrderServiceInterface
44+
*/
45+
private $orderService;
46+
47+
/**
48+
* @var PaymentServiceInterface
49+
*/
50+
private $paymentService;
51+
52+
/**
53+
* @var LoggerInterface
54+
*/
55+
private $logger;
56+
57+
/**
58+
* Elv constructor
59+
*
60+
* @param Context $context
61+
* @param MessageManagerInterface $messageManager
62+
* @param Session $checkoutSession
63+
* @param RedirectFactory $redirectFactory
64+
* @param OrderServiceInterface $orderService
65+
* @param PaymentServiceInterface $paymentService
66+
* @param LoggerInterface $logger
67+
*/
68+
public function __construct(
69+
Context $context,
70+
MessageManagerInterface $messageManager,
71+
Session $checkoutSession,
72+
RedirectFactory $redirectFactory,
73+
OrderServiceInterface $orderService,
74+
PaymentServiceInterface $paymentService,
75+
LoggerInterface $logger
76+
) {
77+
$this->messageManager = $messageManager;
78+
$this->checkoutSession = $checkoutSession;
79+
$this->redirectFactory = $redirectFactory;
80+
$this->orderService = $orderService;
81+
$this->paymentService = $paymentService;
82+
$this->logger = $logger;
83+
84+
parent::__construct($context);
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
public function execute()
91+
{
92+
try {
93+
$order = $this->checkoutSession->getLastRealOrder();
94+
$orderId = $order->getRealOrderId();
95+
96+
if (!$orderId) {
97+
return $this->redirectToCheckoutCart(__('No order id found.'));
98+
}
99+
100+
$cmOrder = $this->orderService->create($order->getEntityId());
101+
if (!$cmOrder->getOrderReference()) {
102+
throw new LocalizedException(__('The order was not placed properly.'));
103+
}
104+
105+
$cmPayment = $this->paymentService->create($order->getEntityId());
106+
if (!$cmPayment->getId()) {
107+
throw new LocalizedException(__('The Payment was not finished properly!'));
108+
}
109+
110+
return $this->redirectFactory->create()
111+
->setPath('checkout/onepage/success');
112+
} catch (Exception $exception) {
113+
$this->logger->critical($exception->getMessage());
114+
115+
return $this->redirectToCheckoutCart(__('Something went wrong while creating the order.'));
116+
}
117+
}
118+
119+
/**
120+
* Return to checkout cart with error message
121+
*
122+
* @param Phrase $message
123+
* @return Redirect
124+
*/
125+
public function redirectToCheckoutCart(Phrase $message): Redirect
126+
{
127+
$this->checkoutSession->restoreQuote();
128+
129+
$this->messageManager->addErrorMessage($message);
130+
131+
return $this->redirectFactory->create()
132+
->setPath('checkout/cart');
133+
}
134+
}

Model/ConfigProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ConfigProvider implements ConfigProviderInterface
2929
public const CODE_IDEAL = 'cm_payments_ideal';
3030
public const CODE_PAYPAL = 'cm_payments_paypal';
3131
public const CODE_BANCONTACT = 'cm_payments_bancontact';
32+
public const CODE_ELV = 'cm_payments_elv';
3233
public const CODE_CM_PAYMENTS_MENU = 'cm_payments';
3334

3435
/**

Observer/AdditionalDataAssignObserver.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See LICENSE.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace CM\Payments\Observer;
810

911
use Magento\Framework\Event\Observer;
@@ -12,13 +14,18 @@
1214

1315
class AdditionalDataAssignObserver extends AbstractDataAssignObserver
1416
{
15-
const SELECTED_ISSUER = 'selected_issuer';
17+
/**
18+
* Additional data keys
19+
*/
20+
public const SELECTED_ISSUER = 'selected_issuer';
21+
public const IBAN = 'iban';
1622

1723
/**
1824
* @var array
1925
*/
2026
protected $additionalInformationList = [
2127
self::SELECTED_ISSUER,
28+
self::IBAN,
2229
];
2330

2431
/**

Service/MethodService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function addMethodAdditionalData(
128128
}
129129

130130
foreach ($availablePaymentMethods as $id => $paymentMethod) {
131-
if (! $this->isCmPaymentsMethod($paymentMethod->getCode())) {
131+
if (!$this->isCmPaymentsMethod($paymentMethod->getCode())) {
132132
continue;
133133
}
134134

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © CM.com. All rights reserved.
4+
* See LICENSE.txt for license details.
5+
*/
6+
7+
namespace CM\Payments\Service\Payment\Request\Part;
8+
9+
use CM\Payments\Api\Service\Payment\Request\RequestPartInterface;
10+
use CM\Payments\Client\Model\Request\PaymentCreate;
11+
use CM\Payments\Model\ConfigProvider;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
14+
class ElvDetails implements RequestPartInterface
15+
{
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function process(OrderInterface $order, PaymentCreate $paymentCreate): PaymentCreate
20+
{
21+
if ($order->getPayment()->getMethod() !== ConfigProvider::CODE_ELV) {
22+
return $paymentCreate;
23+
}
24+
25+
$value = $this->getElvPaymentData($order);
26+
$paymentCreate->setElvDetails(
27+
[
28+
'iban' => $value
29+
]
30+
);
31+
32+
return $paymentCreate;
33+
}
34+
35+
/**
36+
* @param OrderInterface $order
37+
* @return string
38+
*/
39+
private function getElvPaymentData(OrderInterface $order): string
40+
{
41+
$additionalData = $order->getPayment()->getAdditionalInformation();
42+
43+
if (isset($additionalData['iban'])) {
44+
return $additionalData['iban'];
45+
}
46+
47+
return '';
48+
}
49+
}

Service/PaymentService.php

+25-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
use CM\Payments\Client\Model\CMPaymentFactory;
2020
use CM\Payments\Exception\EmptyPaymentIdException;
2121
use CM\Payments\Logger\CMPaymentsLogger;
22+
use CM\Payments\Model\ConfigProvider;
2223
use Magento\Framework\Event\ManagerInterface;
2324
use Magento\Sales\Api\OrderRepositoryInterface;
25+
use GuzzleHttp\Exception\GuzzleException;
2426

2527
class PaymentService implements PaymentServiceInterface
2628
{
@@ -126,12 +128,31 @@ public function create(string $orderId): CMPaymentInterface
126128
'paymentCreateRequest' => $paymentCreateRequest,
127129
]);
128130

129-
$paymentCreateResponse = $this->paymentClient->create(
130-
$paymentCreateRequest
131-
);
131+
$paymentCreateResponse = null;
132+
try {
133+
$paymentCreateResponse = $this->paymentClient->create(
134+
$paymentCreateRequest
135+
);
136+
} catch (GuzzleException $e) {
137+
$this->logger->info(
138+
'CM Create payment request error',
139+
[
140+
'orderId' => $orderId,
141+
'exceptionMessage' => $e->getMessage()
142+
]
143+
);
144+
}
145+
146+
// Cleaning of ELV iban from payment information
147+
$additionalInformation = $order->getPayment()->getAdditionalInformation();
148+
if ($order->getPayment()->getMethod() == ConfigProvider::CODE_ELV) {
149+
unset($additionalInformation['iban']);
150+
$order->getPayment()->setAdditionalInformation($additionalInformation);
151+
$this->orderRepository->save($order);
152+
}
132153

133154
// Todo: validate and handle response status
134-
if (!$paymentCreateResponse->getId()) {
155+
if (!$paymentCreateResponse || !$paymentCreateResponse->getId()) {
135156
throw new EmptyPaymentIdException(__('Empty payment id'));
136157
}
137158

@@ -144,9 +165,7 @@ public function create(string $orderId): CMPaymentInterface
144165

145166
$this->cmPaymentRepository->save($cmPayment);
146167

147-
$additionalInformation = $order->getPayment()->getAdditionalInformation();
148168
$additionalInformation['cm_payment_id'] = $paymentCreateResponse->getId();
149-
150169
$order->getPayment()->setAdditionalInformation($additionalInformation);
151170
$this->orderRepository->save($order);
152171

0 commit comments

Comments
 (0)