Skip to content

Commit 9d1ffff

Browse files
author
Ivan Tagil
committed
CMP-85 - Bug fixing, code improvements, tests
1 parent 557f37b commit 9d1ffff

14 files changed

Lines changed: 265 additions & 20 deletions

File tree

Api/Service/MethodServiceInterface.php

Lines changed: 6 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ public function getElvDetails(): array
110110
*/
111111
public function setElvDetails(array $elvDetails): void
112112
{
113-
$this->idealDetails = $elvDetails;
113+
$this->elvDetails = $elvDetails;
114114
}
115115
}
Lines changed: 134 additions & 0 deletions
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+
}

Observer/AdditionalDataAssignObserver.php

Lines changed: 9 additions & 2 deletions
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
*/
20-
protected $additionalInformationList = [
26+
protected array $additionalInformationList = [
2127
self::SELECTED_ISSUER,
28+
self::IBAN,
2229
];
2330

2431
/**

Service/MethodService.php

Lines changed: 1 addition & 1 deletion
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

Service/PaymentService.php

Lines changed: 25 additions & 6 deletions
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

Test/Integration/Service/PaymentServiceTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,35 @@ public function testCreatePaypalPayment()
115115
);
116116
}
117117

118+
/**
119+
* @magentoDataFixture Magento/Sales/_files/order.php
120+
*/
121+
public function testCreateElvPayment()
122+
{
123+
$magentoOrder = $this->loadOrderById('100000001');
124+
$magentoOrder = $this->addCurrencyToOrder($magentoOrder);
125+
126+
$cmOrderFactory = $this->objectManager->create(OrderInterfaceFactory::class);
127+
$cmOrderOrder = $cmOrderFactory->create();
128+
$cmOrderRepository = $this->objectManager->get(CMOrderRepositoryInterface::class);
129+
$cmOrderOrder->setIncrementId($magentoOrder->getIncrementId());
130+
$cmOrderOrder->setOrderId((int)$magentoOrder->getEntityId());
131+
$cmOrderOrder->setOrderKey('0287A1617D93780EF28044B98438BF2M');
132+
$cmOrderRepository->save($cmOrderOrder);
133+
134+
$this->clientMock->expects($this->once())->method('execute')->willReturn(
135+
[
136+
'id' => 'pid4911261022t',
137+
'status' => 'AUTHORIZED'
138+
]
139+
);
140+
141+
$payment = $this->paymentService->create($magentoOrder->getId());
142+
$this->assertNotNull(
143+
$payment->getId()
144+
);
145+
}
146+
118147
/**
119148
* @param string $orderId
120149
* @return OrderInterface

Test/Unit/Service/PaymentRequestBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ public function testCreatePaypalPaymentRequestBuilder()
6363
);
6464
}
6565

66+
public function testCreateElvPaymentRequestBuilder()
67+
{
68+
$orderMock = $this->getOrderMock(ConfigProvider::CODE_ELV);
69+
$orderKey = '0287A1617D93780EF28044B98438BF2F';
70+
$paymentRequest = $this->paymentRequestBuilder->create($orderMock, $orderKey);
71+
72+
$this->assertSame(
73+
MethodServiceInterface::API_METHODS_MAPPING[ConfigProvider::CODE_ELV],
74+
$paymentRequest->getPayload()['method']
75+
);
76+
}
77+
6678
/**
6779
* @param string $paymentMethod
6880
* @return OrderInterface

Test/Unit/Service/PaymentServiceTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ public function testCreatePaypalPayment()
141141
);
142142
}
143143

144+
public function testCreateElvPayment()
145+
{
146+
$this->paymentClientMock->expects($this->once())->method('create')->willReturn(
147+
new \CM\Payments\Client\Model\Response\PaymentCreate(
148+
[
149+
'id' => 'pid4911261022t',
150+
'status' => 'AUTHORIZED'
151+
]
152+
)
153+
);
154+
155+
$order = $this->getOrderMock();
156+
$payment = $this->paymentService->create((string)$order->getEntityId());
157+
158+
$this->assertNotNull(
159+
$payment->getId()
160+
);
161+
}
162+
144163
public function testEventDispatch()
145164
{
146165
$paymentCreateResponse = new \CM\Payments\Client\Model\Response\PaymentCreate(

etc/adminhtml/system.xml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,24 @@
703703
<field id="active">1</field>
704704
</depends>
705705
</field>
706-
<field id="sort_order" translate="label" type="text" sortOrder="90" showInDefault="1" showInWebsite="1"
706+
<field id="order_expiry_unit" translate="label" type="select" sortOrder="90" showInDefault="1"
707+
showInWebsite="1" showInStore="1">
708+
<label>Order Expiry Unit</label>
709+
<source_model>CM\Payments\Model\Adminhtml\Source\OrderExpiryUnit</source_model>
710+
<config_path>payment/cm_payments_elv/order_expiry_unit</config_path>
711+
<depends>
712+
<field id="active">1</field>
713+
</depends>
714+
</field>
715+
<field id="order_expiry_duration" translate="label" type="text" sortOrder="100" showInDefault="1"
716+
showInWebsite="1" showInStore="1">
717+
<label>Order Expiry Duration</label>
718+
<config_path>payment/cm_payments_elv/order_expiry_duration</config_path>
719+
<depends>
720+
<field id="active">1</field>
721+
</depends>
722+
</field>
723+
<field id="sort_order" translate="label" type="text" sortOrder="110" showInDefault="1" showInWebsite="1"
707724
showInStore="1">
708725
<label>Sort Order</label>
709726
<config_path>payment/cm_payments_elv/sort_order</config_path>

0 commit comments

Comments
 (0)