Skip to content

Commit c4158c0

Browse files
authored
Merge pull request #112 from payplug/qa
1.18.0
2 parents e1e45bd + a70f8db commit c4158c0

33 files changed

+800
-349
lines changed

Block/OneyInfo.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Magento\Framework\Exception\NoSuchEntityException;
66
use Magento\Sales\Model\Order;
77
use Payplug\Exception\PayplugException;
8+
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
89
use Payplug\Payments\Helper\Data;
910
use Payplug\Payments\Helper\Oney;
1011
use Payplug\Payments\Logger\Logger;
@@ -36,8 +37,13 @@ protected function buildPaymentDetails($payment, $order)
3637
$paymentDetails['Status'] = $status;
3738

3839
$oneyOption = str_replace('oney_', '', $payment->payment_method['type']);
39-
if (isset(Oney::ALLOWED_OPERATIONS_BY_PAYMENT[$order->getPayment()->getMethod()][$oneyOption])) {
40-
$paymentDetails['Oney option'] = __('Payment in %1', Oney::ALLOWED_OPERATIONS_BY_PAYMENT[$order->getPayment()->getMethod()][$oneyOption]);
40+
$paymentMethod = $order->getPayment()->getMethod();
41+
if (isset(Oney::ALLOWED_OPERATIONS_BY_PAYMENT[$paymentMethod][$oneyOption])) {
42+
$key = 'Payment in %1';
43+
if ($paymentMethod === OneyWithoutFees::METHOD_CODE) {
44+
$key = 'Payment in %1 without fees';
45+
}
46+
$paymentDetails['Oney option'] = __($key, Oney::ALLOWED_OPERATIONS_BY_PAYMENT[$paymentMethod][$oneyOption]);
4147
}
4248

4349
return $paymentDetails;

Gateway/Config/OneyWithoutFees.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Payplug\Payments\Gateway\Config;
4+
5+
class OneyWithoutFees extends PayplugPayment
6+
{
7+
const METHOD_CODE = 'payplug_payments_oney_without_fees';
8+
}

Helper/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Config extends AbstractHelper
2020
const PAYMENT_PAGE_REDIRECT = 'redirect';
2121
const PAYMENT_PAGE_EMBEDDED = 'embedded';
2222

23-
const MODULE_VERSION = '1.17.0';
23+
const MODULE_VERSION = '1.18.0';
2424

2525
/**
2626
* @var WriterInterface

Helper/Data.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Payplug\Payments\Gateway\Config\InstallmentPlan;
2525
use Payplug\Payments\Gateway\Config\Ondemand;
2626
use Payplug\Payments\Gateway\Config\Oney;
27+
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
2728
use Payplug\Payments\Gateway\Config\Standard;
2829
use Payplug\Payments\Model\Order\Payment;
2930
use Payplug\Payments\Model\Order\Processing;
@@ -290,6 +291,9 @@ public function canUpdatePayment($order)
290291
Oney::METHOD_CODE => [
291292
Order::STATE_PAYMENT_REVIEW
292293
],
294+
OneyWithoutFees::METHOD_CODE => [
295+
Order::STATE_PAYMENT_REVIEW
296+
],
293297
];
294298
if (!in_array($order->getState(), $allowedStates[$order->getPayment()->getMethod()])) {
295299
return false;
@@ -406,7 +410,7 @@ public function isOrderValidated($order)
406410
}
407411

408412
// If Oney payment is still being reviewed, order is validated but still in Payment Review state
409-
if ($order->getPayment()->getMethod() == Oney::METHOD_CODE && $order->getState() == Order::STATE_PAYMENT_REVIEW) {
413+
if (($order->getPayment()->getMethod() == Oney::METHOD_CODE || $order->getPayment()->getMethod() == OneyWithoutFees::METHOD_CODE) && $order->getState() == Order::STATE_PAYMENT_REVIEW) {
410414
return true;
411415
}
412416

@@ -531,7 +535,8 @@ public function isCodePayplugPayment($code)
531535
return $code == Standard::METHOD_CODE ||
532536
$code == \Payplug\Payments\Gateway\Config\InstallmentPlan::METHOD_CODE ||
533537
$code == Ondemand::METHOD_CODE ||
534-
$code == Oney::METHOD_CODE;
538+
$code == Oney::METHOD_CODE ||
539+
$code == OneyWithoutFees::METHOD_CODE;
535540
}
536541

537542
/**

Helper/Oney.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
1616
use Payplug\Exception\PayplugException;
1717
use Payplug\OneySimulation;
18+
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
1819
use Payplug\Payments\Logger\Logger;
1920
use Payplug\Payments\Model\OneySimulation\Option;
2021
use Payplug\Payments\Model\OneySimulation\Result;
@@ -27,6 +28,10 @@ class Oney extends AbstractHelper
2728
'x3_with_fees' => '3x',
2829
'x4_with_fees' => '4x',
2930
],
31+
\Payplug\Payments\Gateway\Config\OneyWithoutFees::METHOD_CODE => [
32+
'x3_without_fees' => '3x',
33+
'x4_without_fees' => '4x',
34+
],
3035
];
3136

3237
const MAX_ITEMS = 1000;
@@ -71,6 +76,16 @@ class Oney extends AbstractHelper
7176
*/
7277
private $customerSession;
7378

79+
/**
80+
* @var \Magento\Payment\Helper\Data
81+
*/
82+
private $paymentHelper;
83+
84+
/**
85+
* @var string
86+
*/
87+
private $oneyMethod;
88+
7489
/**
7590
* @param Context $context
7691
* @param Config $payplugConfig
@@ -81,6 +96,7 @@ class Oney extends AbstractHelper
8196
* @param Logger $logger
8297
* @param CheckoutSession $checkoutSession
8398
* @param CustomerSession $customerSession
99+
* @param \Magento\Payment\Helper\Data $paymentHelper
84100
*/
85101
public function __construct(
86102
Context $context,
@@ -91,7 +107,8 @@ public function __construct(
91107
Resolver $localeResolver,
92108
Logger $logger,
93109
CheckoutSession $checkoutSession,
94-
CustomerSession $customerSession
110+
CustomerSession $customerSession,
111+
\Magento\Payment\Helper\Data $paymentHelper
95112
) {
96113
parent::__construct($context);
97114

@@ -103,6 +120,7 @@ public function __construct(
103120
$this->logger = $logger;
104121
$this->checkoutSession = $checkoutSession;
105122
$this->customerSession = $customerSession;
123+
$this->paymentHelper = $paymentHelper;
106124
}
107125

108126
/**
@@ -118,7 +136,12 @@ public function canDisplayOney(): bool
118136
return false;
119137
}
120138

121-
$isActive = $this->scopeConfig->getValue('payment/payplug_payments_oney/active', ScopeInterface::SCOPE_STORE, $storeId);
139+
$oneyPaymentMethod = $this->getOneyMethod();
140+
if ($oneyPaymentMethod === '') {
141+
return false;
142+
}
143+
144+
$isActive = $this->scopeConfig->getValue('payment/' . $oneyPaymentMethod . '/active', ScopeInterface::SCOPE_STORE, $storeId);
122145
if (!$isActive) {
123146
return false;
124147
}
@@ -179,18 +202,22 @@ public function getOneyAmounts($storeId = null, $currency = null)
179202

180203
/**
181204
* @param string $countryCode
205+
* @param bool $throwException
182206
*
183207
* @return bool
184208
*
185209
* @throws \Exception
186210
*/
187-
private function validateCountry($countryCode): bool
211+
private function validateCountry($countryCode, $throwException = true): bool
188212
{
189213
$storeId = $this->storeManager->getStore()->getId();
190214
$oneyCountries = $this->scopeConfig->getValue(Config::CONFIG_PATH . 'oney_countries', ScopeInterface::SCOPE_STORE, $storeId);
191215
$oneyCountries = json_decode($oneyCountries, true);
192216

193217
if (!in_array($countryCode, $oneyCountries)) {
218+
if (!$throwException) {
219+
return false;
220+
}
194221
$countryNames = [];
195222
$locale = $this->localeResolver->getLocale();
196223
foreach ($oneyCountries as $oneyCountryCode) {
@@ -318,6 +345,9 @@ private function validateCheckoutCountries($billingCountry, $shippingCountry)
318345
if (!empty($billingCountry) && !empty($shippingCountry) && $billingCountry !== $shippingCountry) {
319346
throw new \Exception(__('Shipping and billing adresses must be both in the same country.'));
320347
}
348+
if (!$this->validateCountry($billingCountry, false)) {
349+
throw new \Exception(__('Unavailable for the specified country'));
350+
}
321351
}
322352

323353
/**
@@ -409,6 +439,7 @@ public function getOneySimulation($amount = null, $countryCode = null, $qty = nu
409439
if ($validationOnly) {
410440
$simulationResult = new Result();
411441
$simulationResult->setSuccess(true);
442+
$simulationResult->setMethod($paymentMethod);
412443

413444
return $simulationResult;
414445
} else {
@@ -422,6 +453,7 @@ public function getOneySimulation($amount = null, $countryCode = null, $qty = nu
422453
$simulationResult = new Result();
423454
$simulationResult->setSuccess(false);
424455
$simulationResult->setMessage($e->getMessage());
456+
$simulationResult->setMethod($paymentMethod);
425457

426458
return $simulationResult;
427459
}
@@ -437,6 +469,7 @@ private function getMockSimulationResult($paymentMethod)
437469
$simulationResult = new Result();
438470
$simulationResult->setSuccess(true);
439471
$simulationResult->setMessage(__('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.'));
472+
$simulationResult->setMethod($paymentMethod);
440473

441474
$operations = self::ALLOWED_OPERATIONS_BY_PAYMENT[$paymentMethod] ?? [];
442475

@@ -474,6 +507,7 @@ private function getSimulation($amount, $countryCode, $paymentMethod): Result
474507
$result = new Result();
475508
$result->setSuccess(true);
476509
$result->setAmount($amount);
510+
$result->setMethod($paymentMethod);
477511

478512
foreach ($operations as $operation => $type) {
479513
if (!isset($simulations[$operation])) {
@@ -511,8 +545,22 @@ private function getSimulation($amount, $countryCode, $paymentMethod): Result
511545
/**
512546
* @return string
513547
*/
514-
public function getOneyMethod()
548+
private function getOneyMethod()
515549
{
516-
return \Payplug\Payments\Gateway\Config\Oney::METHOD_CODE;
550+
if ($this->oneyMethod === null) {
551+
$oneyMethods = [
552+
\Payplug\Payments\Gateway\Config\Oney::METHOD_CODE,
553+
OneyWithoutFees::METHOD_CODE,
554+
];
555+
$this->oneyMethod = '';
556+
foreach ($oneyMethods as $oneyMethod) {
557+
if ($this->paymentHelper->getMethodInstance($oneyMethod)->isAvailable()) {
558+
$this->oneyMethod = $oneyMethod;
559+
break;
560+
}
561+
}
562+
}
563+
564+
return $this->oneyMethod;
517565
}
518566
}

Model/OneySimulation/Result.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Result extends DataObject
1010
const KEY_MESSAGE = 'message';
1111
const KEY_OPTIONS = 'options';
1212
const KEY_AMOUNT = 'amount';
13+
const KEY_METHOD = 'method';
1314

1415
/**
1516
* @return bool|null
@@ -88,4 +89,22 @@ public function setAmount($amount): self
8889
{
8990
return $this->setData(self::KEY_AMOUNT, $amount);
9091
}
92+
93+
/**
94+
* @return string|null
95+
*/
96+
public function getMethod()
97+
{
98+
return $this->_getData(self::KEY_METHOD);
99+
}
100+
101+
/**
102+
* @param string|null $method
103+
*
104+
* @return $this
105+
*/
106+
public function setMethod($method): self
107+
{
108+
return $this->setData(self::KEY_METHOD, $method);
109+
}
91110
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Payplug\Payments\Model\Payment\OneyWithoutFees;
4+
5+
use Magento\Checkout\Model\ConfigProviderInterface;
6+
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Framework\App\RequestInterface;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\View\Asset\Repository;
10+
use Magento\Payment\Helper\Data as PaymentHelper;
11+
use Magento\Payment\Model\MethodInterface;
12+
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
13+
14+
class ConfigProvider implements ConfigProviderInterface
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $methodCode = OneyWithoutFees::METHOD_CODE;
20+
21+
/**
22+
* @var MethodInterface
23+
*/
24+
private $method;
25+
26+
/**
27+
* @var ScopeConfigInterface
28+
*/
29+
private $scopeConfig;
30+
31+
/**
32+
* @var Repository
33+
*/
34+
private $assetRepo;
35+
36+
/**
37+
* @var RequestInterface
38+
*/
39+
private $request;
40+
41+
/**
42+
* @param ScopeConfigInterface $scopeConfig
43+
* @param Repository $assetRepo
44+
* @param RequestInterface $request
45+
* @param PaymentHelper $paymentHelper
46+
*/
47+
public function __construct(
48+
ScopeConfigInterface $scopeConfig,
49+
Repository $assetRepo,
50+
RequestInterface $request,
51+
PaymentHelper $paymentHelper
52+
) {
53+
$this->scopeConfig = $scopeConfig;
54+
$this->assetRepo = $assetRepo;
55+
$this->request = $request;
56+
$this->method = $paymentHelper->getMethodInstance($this->methodCode);
57+
}
58+
59+
/**
60+
* @return array
61+
*/
62+
public function getConfig()
63+
{
64+
return $this->method->isAvailable() ? [
65+
'payment' => [
66+
$this->methodCode => [
67+
'logo' => $this->getViewFileUrl('Payplug_Payments::images/oney_without_fees/3x4x.svg'),
68+
'logo_ko' => $this->getViewFileUrl('Payplug_Payments::images/oney_without_fees/3x4x-alt.svg'),
69+
],
70+
],
71+
] : [];
72+
}
73+
74+
/**
75+
* Retrieve url of a view file
76+
*
77+
* @param string $fileId
78+
* @param array $params
79+
*
80+
* @return string
81+
*/
82+
private function getViewFileUrl($fileId, array $params = [])
83+
{
84+
try {
85+
$params = array_merge(['_secure' => $this->request->isSecure()], $params);
86+
return $this->assetRepo->getUrlWithParams($fileId, $params);
87+
} catch (LocalizedException $e) {
88+
return null;
89+
}
90+
}
91+
}

Observer/CreditMemoCheckRefundedAmountObserver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Sales\Model\OrderRepository;
1212
use Payplug\Exception\PayplugException;
1313
use Payplug\Payments\Gateway\Config\Oney;
14+
use Payplug\Payments\Gateway\Config\OneyWithoutFees;
1415
use Payplug\Payments\Helper\Data;
1516
use Payplug\Payments\Logger\Logger;
1617

@@ -78,7 +79,7 @@ public function execute(EventObserver $observer)
7879
$payplugPayment = $this->helper->getOrderPayment($order->getIncrementId());
7980
$payment = $payplugPayment->retrieve($order->getStoreId());
8081

81-
if ($order->getPayment()->getMethod() === Oney::METHOD_CODE) {
82+
if ($order->getPayment()->getMethod() === Oney::METHOD_CODE || $order->getPayment()->getMethod() === OneyWithoutFees::METHOD_CODE) {
8283
if (time() < $payment->refundable_after) {
8384
$this->messageManager->addErrorMessage(
8485
__('The refund will be possible 48 hours after the last payment or refund transaction.')

0 commit comments

Comments
 (0)