-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathPaymentMethods.php
1064 lines (939 loc) · 36.1 KB
/
PaymentMethods.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2023 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Helper;
use Adyen\AdyenException;
use Adyen\Client;
use Adyen\ConnectionException;
use Adyen\Payment\Helper\Util\PaymentMethodUtil;
use Adyen\Model\Checkout\PaymentMethodsRequest;
use Adyen\Payment\Logger\AdyenLogger;
use Adyen\Payment\Model\Notification;
use Adyen\Payment\Model\Ui\Adminhtml\AdyenMotoConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPayByLinkConfigProvider;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\View\Asset\Source;
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
use Magento\Framework\View\DesignInterface;
use Magento\Payment\Helper\Data as MagentoDataHelper;
use Magento\Payment\Model\MethodInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Magento\Sales\Model\Order;
use Adyen\Payment\Helper\Data as AdyenDataHelper;
use Magento\Sales\Model\Order\Payment;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
class PaymentMethods extends AbstractHelper
{
const ADYEN_HPP = 'adyen_hpp';
const ADYEN_CC = 'adyen_cc';
const ADYEN_ONE_CLICK = 'adyen_oneclick';
const ADYEN_PAY_BY_LINK = 'adyen_pay_by_link';
const ADYEN_PREFIX = 'adyen_';
const ADYEN_CC_VAULT = 'adyen_cc_vault';
const METHODS_WITH_BRAND_LOGO = [
"giftcard"
];
const METHODS_WITH_LOGO_FILE_MAPPING = [
"scheme" => "card"
];
const FUNDING_SOURCE_DEBIT = 'debit';
const FUNDING_SOURCE_CREDIT = 'credit';
const ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS = 'adyen-alternative-payment-method';
const VALID_CHANNELS = ["iOS", "Android", "Web"];
/*
* Following payment methods should be enabled with their own configuration path.
*/
const EXCLUDED_PAYMENT_METHODS = [
AdyenPayByLinkConfigProvider::CODE,
AdyenPosCloudConfigProvider::CODE,
AdyenMotoConfigProvider::CODE
];
/**
* @var CartRepositoryInterface
*/
protected CartRepositoryInterface $quoteRepository;
/**
* @var ScopeConfigInterface
*/
protected ScopeConfigInterface $config;
/**
* @var Data
*/
protected Data $adyenHelper;
/**
* @var MagentoDataHelper
*/
private MagentoDataHelper $dataHelper;
/**
* @var ResolverInterface
*/
protected ResolverInterface $localeResolver;
/**
* @var AdyenLogger
*/
protected AdyenLogger $adyenLogger;
/**
* @var Data
*/
protected Data $adyenDataHelper;
/**
* @var Repository
*/
protected Repository $assetRepo;
/**
* @var RequestInterface
*/
protected RequestInterface $request;
/**
* @var Source
*/
protected Source $assetSource;
/**
* @var DesignInterface
*/
protected DesignInterface $design;
/**
* @var ThemeProviderInterface
*/
protected ThemeProviderInterface $themeProvider;
/**
* @var CartInterface
*/
protected CartInterface $quote;
/**
* @var ChargedCurrency
*/
private ChargedCurrency $chargedCurrency;
/**
* @var Config
*/
private Config $configHelper;
/**
* @var SerializerInterface
*/
private SerializerInterface $serializer;
/**
* @var PaymentTokenRepositoryInterface
*/
private PaymentTokenRepositoryInterface $paymentTokenRepository;
/**
* @var SearchCriteriaBuilder
*/
private SearchCriteriaBuilder $searchCriteriaBuilder;
/**
* @param Context $context
* @param CartRepositoryInterface $quoteRepository
* @param ScopeConfigInterface $config
* @param Data $adyenHelper
* @param ResolverInterface $localeResolver
* @param AdyenLogger $adyenLogger
* @param Repository $assetRepo
* @param RequestInterface $request
* @param Source $assetSource
* @param DesignInterface $design
* @param ThemeProviderInterface $themeProvider
* @param ChargedCurrency $chargedCurrency
* @param Config $configHelper
* @param MagentoDataHelper $dataHelper
* @param SerializerInterface $serializer
* @param Data $adyenDataHelper
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
Context $context,
CartRepositoryInterface $quoteRepository,
ScopeConfigInterface $config,
Data $adyenHelper,
ResolverInterface $localeResolver,
AdyenLogger $adyenLogger,
Repository $assetRepo,
RequestInterface $request,
Source $assetSource,
DesignInterface $design,
ThemeProviderInterface $themeProvider,
ChargedCurrency $chargedCurrency,
Config $configHelper,
MagentoDataHelper $dataHelper,
SerializerInterface $serializer,
AdyenDataHelper $adyenDataHelper,
PaymentTokenRepositoryInterface $paymentTokenRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
parent::__construct($context);
$this->quoteRepository = $quoteRepository;
$this->config = $config;
$this->adyenHelper = $adyenHelper;
$this->localeResolver = $localeResolver;
$this->adyenLogger = $adyenLogger;
$this->assetRepo = $assetRepo;
$this->request = $request;
$this->assetSource = $assetSource;
$this->design = $design;
$this->themeProvider = $themeProvider;
$this->chargedCurrency = $chargedCurrency;
$this->configHelper = $configHelper;
$this->dataHelper = $dataHelper;
$this->serializer = $serializer;
$this->adyenDataHelper = $adyenDataHelper;
$this->paymentTokenRepository = $paymentTokenRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* @param int $quoteId
* @param string|null $country
* @param string|null $shopperLocale
* @param string|null $channel
* @return string
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getPaymentMethods(
int $quoteId,
?string $country = null,
?string $shopperLocale = null,
?string $channel = null
): string
{
// get quote from quoteId
$quote = $this->quoteRepository->getActive($quoteId);
// If quote cannot be found early return the empty paymentMethods array
if (empty($quote)) {
return '';
}
$this->setQuote($quote);
return $this->fetchPaymentMethods($country, $shopperLocale, $channel);
}
/**
* @param string $methodCode
* @return bool
*/
public function isAdyenPayment(string $methodCode): bool
{
return in_array($methodCode, $this->getAdyenPaymentMethods(), true);
}
/**
* @return array
*/
public function getAdyenPaymentMethods() : array
{
$paymentMethods = $this->dataHelper->getPaymentMethodList();
$filtered = array_filter(
$paymentMethods,
function ($key) {
return strpos($key, self::ADYEN_PREFIX) === 0;
},
ARRAY_FILTER_USE_KEY
);
return array_keys($filtered);
}
/**
* @param bool|null $isActive
* @param string $scope
* @param int $scopeId
* @return array
*/
public function togglePaymentMethodsActivation(
?bool $isActive = null,
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
int $scopeId = 0
): array {
$enabledPaymentMethods = [];
if (is_null($isActive)) {
$isActive = $this->configHelper->getIsPaymentMethodsActive();
}
foreach ($this->getAdyenPaymentMethods() as $paymentMethod) {
if (in_array($paymentMethod, self::EXCLUDED_PAYMENT_METHODS)) {
continue;
}
$value = $isActive ? '1': '0';
$field = 'active';
$this->configHelper->setConfigData($value, $field, $paymentMethod, $scope, $scopeId);
$enabledPaymentMethods[] = $paymentMethod;
}
return $enabledPaymentMethods;
}
/**
* Remove activation config
* @param string $scope
* @param int $scopeId
* @return void
*/
public function removePaymentMethodsActivation(string $scope, int $scopeId): void
{
foreach ($this->getAdyenPaymentMethods() as $paymentMethod)
{
if (in_array($paymentMethod, self::EXCLUDED_PAYMENT_METHODS)) {
continue;
}
$this->configHelper->removeConfigData('active', $paymentMethod, $scope, $scopeId);
}
}
/**
* @param string|null $country
* @param string|null $shopperLocale
* @param string|null $channel
* @return string
* @throws AdyenException
* @throws LocalizedException
* @throws NoSuchEntityException
*/
protected function fetchPaymentMethods(
?string $country = null,
?string $shopperLocale = null,
?string $channel = null
): string
{
$quote = $this->getQuote();
$store = $quote->getStore();
$merchantAccount = $this->configHelper->getAdyenAbstractConfigData('merchant_account', $store->getId());
if (!$merchantAccount) {
return json_encode([]);
}
$requestData = $this->getPaymentMethodsRequest(
$merchantAccount,
$store,
$quote,
$shopperLocale,
$country,
$channel
);
$responseData = $this->getPaymentMethodsResponse($requestData, $store);
if (empty($responseData['paymentMethods'])) {
return json_encode([]);
}
$paymentMethods = $responseData['paymentMethods'];
$allowMultistoreTokens = $this->configHelper->getAllowMultistoreTokens($store->getId());
$customerId = $quote->getCustomerId();
$responseData = $this->filterStoredPaymentMethods($allowMultistoreTokens, $responseData, $customerId);
$response['paymentMethodsResponse'] = $responseData;
// Add extra details per payment method
$paymentMethodsExtraDetails = [];
$paymentMethodsExtraDetails = $this->showLogosPaymentMethods($paymentMethods, $paymentMethodsExtraDetails);
$paymentMethodsExtraDetails = $this->addExtraConfigurationToPaymentMethods(
$paymentMethods,
$paymentMethodsExtraDetails
);
$response['paymentMethodsExtraDetails'] = $paymentMethodsExtraDetails;
//TODO this should be the implemented with an interface
return json_encode($response);
}
/**
* @param $allowMultistoreTokens
* @param $responseData
* @param $customerId
* @return mixed
*/
protected function filterStoredPaymentMethods($allowMultistoreTokens, $responseData, $customerId): mixed
{
if (!$allowMultistoreTokens && isset($responseData['storedPaymentMethods'])) {
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('customer_id', $customerId)
->create();
$paymentTokens = $this->paymentTokenRepository->getList($searchCriteria)->getItems();
$gatewayTokens = array_map(function ($paymentToken) {
return $paymentToken->getGatewayToken();
}, $paymentTokens);
$storedPaymentMethods = $responseData['storedPaymentMethods'];
$responseData['storedPaymentMethods'] = array_filter(
$storedPaymentMethods,
function ($method) use ($gatewayTokens) {
return in_array($method['id'], $gatewayTokens);
});
}
return $responseData;
}
/**
* @return float
* @throws AdyenException
*/
protected function getCurrentPaymentAmount(): float
{
$total = $this->chargedCurrency->getQuoteAmountCurrency($this->getQuote())->getAmount();
if (!is_numeric($total)) {
$exceptionMessage =
sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a numeric value.',
$this->getQuote()->getEntityId()
);
throw new AdyenException($exceptionMessage);
}
$total = (float)$total;
if ($total >= 0) {
return $total;
}
$exceptionMessage =
sprintf(
'Cannot retrieve a valid grand total from quote ID: `%s`. Expected a float >= `0`, got `%f`.',
$this->getQuote()->getEntityId(),
$total
);
throw new AdyenException($exceptionMessage);
}
/**
* @param Store $store
* @return string
*/
protected function getCurrentCountryCode(Store $store): string
{
$quote = $this->getQuote();
$billingAddressCountry = $quote->getBillingAddress()->getCountryId();
// If customer is guest, billing address country might not be set yet
if (isset($billingAddressCountry)) {
return $billingAddressCountry;
}
$defaultCountry = $this->config->getValue(
\Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
ScopeInterface::SCOPE_STORES,
$store->getCode()
);
if ($defaultCountry) {
return $defaultCountry;
}
return "";
}
/**
* @param array $requestParams
* @param Store $store
* @return array
* @throws AdyenException
* @throws NoSuchEntityException
*/
protected function getPaymentMethodsResponse(array $requestParams, Store $store): array
{
// initialize the adyen client
$client = $this->adyenHelper->initializeAdyenClient($store->getId());
// initialize service
$service =$this->adyenHelper->initializePaymentsApi($client);
try {
$this->adyenHelper->logRequest($requestParams, Client::API_CHECKOUT_VERSION, '/paymentMethods');
$response = $service->paymentMethods(new PaymentMethodsRequest($requestParams));
$responseData = $response->toArray();
} catch (AdyenException $e) {
$this->adyenLogger->error(
"The Payment methods response is empty check your Adyen configuration in Magento."
);
// return empty result
return [];
}
catch (ConnectionException $e) {
$this->adyenLogger->error(
"Connection to the endpoint failed. Check the Adyen Live endpoint prefix configuration."
);
return [];
}
$this->adyenHelper->logResponse($responseData);
return $responseData;
}
/**
* @return CartInterface
*/
protected function getQuote(): CartInterface
{
return $this->quote;
}
/**
* @param CartInterface $quote
* @return void
*/
protected function setQuote(CartInterface $quote): void
{
$this->quote = $quote;
}
/**
* @return string|null
*/
protected function getCurrentShopperReference(): ?string
{
$customerId = $this->getQuote()->getCustomerId();
return $customerId ? (string)$customerId : null;
}
/**
* @param $merchantAccount
* @param Store $store
* @param Quote $quote
* @param string|null $shopperLocale
* @param string|null $country
* @param string|null $channel
* @return array
* @throws AdyenException
*/
protected function getPaymentMethodsRequest(
$merchantAccount,
Store $store,
Quote $quote,
?string $shopperLocale = null,
?string $country = null,
?string $channel = null
): array {
$currencyCode = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getCurrencyCode();
$channel = in_array($channel, self::VALID_CHANNELS, true) ? $channel : "Web";
$paymentMethodRequest = [
"channel" => $channel ?? "Web",
"merchantAccount" => $merchantAccount,
"countryCode" => $country ?? $this->getCurrentCountryCode($store),
"shopperLocale" => $shopperLocale ?? $this->adyenHelper->getCurrentLocaleCode($store->getId()),
"amount" => [
"currency" => $currencyCode
]
];
if (!empty($this->getCurrentShopperReference())) {
$paymentMethodRequest["shopperReference"] =
$this->adyenDataHelper->padShopperReference($this->getCurrentShopperReference());
}
$amountValue = $this->adyenHelper->formatAmount($this->getCurrentPaymentAmount(), $currencyCode);
if (!empty($amountValue)) {
$paymentMethodRequest["amount"]["value"] = $amountValue;
}
$billingAddress = $quote->getBillingAddress();
if (!empty($billingAddress) && !is_null($billingAddress->getTelephone())) {
$paymentMethodRequest['telephoneNumber'] = trim($billingAddress->getTelephone());
}
return $paymentMethodRequest;
}
/**
* @param array $paymentMethods
* @param array $paymentMethodsExtraDetails
* @return array
* @throws LocalizedException
*/
protected function showLogosPaymentMethods(array $paymentMethods, array $paymentMethodsExtraDetails): array
{
if (!$this->adyenHelper->showLogos()) {
return $paymentMethodsExtraDetails;
}
// Explicitly setting theme
$themeCode = "Magento/blank";
$themeId = $this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND);
if (!empty($themeId)) {
$theme = $this->themeProvider->getThemeById($themeId);
if ($theme && !empty($theme->getCode())) {
$themeCode = $theme->getCode();
}
}
$params = [];
$params = array_merge(
[
'area' => Area::AREA_FRONTEND,
'_secure' => $this->request->isSecure(),
'theme' => $themeCode
],
$params
);
foreach ($paymentMethods as $paymentMethod) {
$paymentMethodCode = in_array($paymentMethod['type'], self::METHODS_WITH_BRAND_LOGO)
? $paymentMethod['brand']
: $paymentMethod['type'];
$paymentMethodCode = !empty(self::METHODS_WITH_LOGO_FILE_MAPPING[$paymentMethod['type']])
? self::METHODS_WITH_LOGO_FILE_MAPPING[$paymentMethod['type']]
: $paymentMethodCode;
$icon = $this->buildPaymentMethodIcon($paymentMethodCode, $params);
$paymentMethodsExtraDetails[$paymentMethodCode]['icon'] = $icon;
//todo check if it is needed
// check if payment method is an open invoice method
$paymentMethodsExtraDetails[$paymentMethodCode]['isOpenInvoice'] =
$this->adyenHelper->isPaymentMethodOpenInvoiceMethod($paymentMethodCode);
}
return $paymentMethodsExtraDetails;
}
/**
* @param array $paymentMethods
* @param array $paymentMethodsExtraDetails
* @return array
* @throws AdyenException
*/
protected function addExtraConfigurationToPaymentMethods(
array $paymentMethods,
array $paymentMethodsExtraDetails
): array {
$quote = $this->getQuote();
$currencyCode = $this->chargedCurrency->getQuoteAmountCurrency($quote)->getCurrencyCode();
$amountValue = $this->adyenHelper->formatAmount($this->getCurrentPaymentAmount(), $currencyCode);
foreach ($paymentMethods as $paymentMethod) {
$paymentMethodCode = $paymentMethod['type'];
$paymentMethodsExtraDetails[$paymentMethodCode]['configuration'] = [
'amount' => [
'value' => $amountValue,
'currency' => $currencyCode
],
'currency' => $currencyCode,
];
}
return $paymentMethodsExtraDetails;
}
/**
* @param MethodInterface $paymentMethodInstance
* @return bool
*/
public function isWalletPaymentMethod(MethodInterface $paymentMethodInstance): bool
{
return boolval($paymentMethodInstance->getConfigData('is_wallet'));
}
/**
* @param MethodInterface $paymentMethodInstance
* @return bool
*/
public function isAlternativePaymentMethod(MethodInterface $paymentMethodInstance): bool
{
return $paymentMethodInstance->getConfigData('group') === self::ADYEN_GROUP_ALTERNATIVE_PAYMENT_METHODS;
}
/**
* @param MethodInterface $paymentMethodInstance
* @return string
* @throws AdyenException
*/
public function getAlternativePaymentMethodTxVariant(MethodInterface $paymentMethodInstance): string
{
if (!$this->isAlternativePaymentMethod($paymentMethodInstance)) {
throw new AdyenException('Given payment method is not an Adyen alternative payment method!');
}
return str_replace('adyen_', '', $paymentMethodInstance->getCode());
}
/**
* @param MethodInterface $paymentMethodInstance
* @return bool
*/
public function paymentMethodSupportsRecurring(MethodInterface $paymentMethodInstance): bool
{
return boolval($paymentMethodInstance->getConfigData('supports_recurring'));
}
/**
* @param Payment $payment
* @param string $method
* @return bool
*/
public function checkPaymentMethod(Order\Payment $payment, string $method): bool
{
return $payment->getMethod() === $method;
}
/**
* @return array
*/
public function getCcAvailableTypes(): array
{
$types = [];
$ccTypes = $this->adyenHelper->getAdyenCcTypes();
$availableTypes = $this->configHelper->getAdyenCcConfigData('cctypes');
if ($availableTypes) {
$availableTypes = explode(',', (string) $availableTypes);
foreach (array_keys($ccTypes) as $code) {
if (in_array($code, $availableTypes)) {
$types[$code] = $ccTypes[$code]['name'];
}
}
}
return $types;
}
/**
* @return array
*/
public function getCcAvailableTypesByAlt(): array
{
$types = [];
$ccTypes = $this->adyenHelper->getAdyenCcTypes();
$availableTypes = $this->configHelper->getAdyenCcConfigData('cctypes');
if ($availableTypes) {
$availableTypes = explode(',', (string) $availableTypes);
foreach (array_keys($ccTypes) as $code) {
if (in_array($code, $availableTypes)) {
$types[$ccTypes[$code]['code_alt']] = $code;
}
}
}
return $types;
}
/**
* @param Order $order
* @param string $notificationPaymentMethod
* @return bool
*/
public function isAutoCapture(Order $order, string $notificationPaymentMethod): bool
{
// validate if payment methods allows manual capture
if (PaymentMethodUtil::isManualCaptureSupported($notificationPaymentMethod)) {
$captureMode = trim(
(string) $this->configHelper->getConfigData(
'capture_mode',
'adyen_abstract',
$order->getStoreId()
)
);
$sepaFlow = trim(
(string) $this->configHelper->getConfigData(
'sepa_flow',
'adyen_abstract',
$order->getStoreId()
)
);
$paymentCode = $order->getPayment()->getMethod();
$autoCaptureOpenInvoice = $this->configHelper->getAutoCaptureOpenInvoice($order->getStoreId());
$manualCapturePayPal = trim(
(string) $this->configHelper->getConfigData(
'paypal_capture_mode',
'adyen_abstract',
$order->getStoreId()
)
);
/*
* if you are using authcap the payment method is manual.
* There will be a capture send to indicate if payment is successful
*/
if ($notificationPaymentMethod == "sepadirectdebit") {
if ($sepaFlow == "authcap") {
$this->adyenLogger->addAdyenNotification(
'Manual Capture is applied for sepa because it is in authcap flow',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return false;
} else {
// payment method ideal, cash adyen_boleto has direct capture
$this->adyenLogger->addAdyenNotification(
'This payment method does not allow manual capture.(2) paymentCode:' .
$paymentCode . ' paymentMethod:' . $notificationPaymentMethod . ' sepaFLow:' . $sepaFlow,
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
}
}
if ($paymentCode == "adyen_pos_cloud") {
$captureModePos = $this->configHelper->getAdyenPosCloudConfigData(
'capture_mode_pos',
$order->getStoreId()
);
if (strcmp((string) $captureModePos, 'auto') === 0) {
$this->adyenLogger->addAdyenNotification(
'This payment method is POS Cloud and configured to be working as auto capture ',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
} elseif (strcmp((string) $captureModePos, 'manual') === 0) {
$this->adyenLogger->addAdyenNotification(
'This payment method is POS Cloud and configured to be working as manual capture ',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return false;
}
}
// if auto capture mode for openinvoice is turned on then use auto capture
if ($autoCaptureOpenInvoice && $this->adyenHelper->isPaymentMethodOpenInvoiceMethod($notificationPaymentMethod)) {
$this->adyenLogger->addAdyenNotification(
'This payment method is configured to be working as auto capture ',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
}
// if PayPal capture modues is different from the default use this one
if (strcmp($notificationPaymentMethod, 'paypal') === 0) {
if ($manualCapturePayPal) {
$this->adyenLogger->addAdyenNotification(
'This payment method is paypal and configured to work as manual capture',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return false;
} else {
$this->adyenLogger->addAdyenNotification(
'This payment method is paypal and configured to work as auto capture',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
}
}
if (strcmp($captureMode, 'manual') === 0) {
$this->adyenLogger->addAdyenNotification(
'Capture mode for this payment is set to manual',
array_merge(
$this->adyenLogger->getOrderContext($order),
[
'paymentMethod' => $notificationPaymentMethod,
'pspReference' => $order->getPayment()->getData('adyen_psp_reference')
]
)
);
return false;
}
/*
* online capture after delivery, use Magento backend to online invoice
* (if the option auto capture mode for openinvoice is not set)
*/
if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod($notificationPaymentMethod)) {
$this->adyenLogger->addAdyenNotification(
'Capture mode for klarna is by default set to manual',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return false;
}
$this->adyenLogger->addAdyenNotification(
'Capture mode is set to auto capture',
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
} else {
// does not allow manual capture so is always immediate capture
$this->adyenLogger->addAdyenNotification(
sprintf('Payment method %s, does not allow manual capture', $notificationPaymentMethod),
array_merge(
$this->adyenLogger->getOrderContext($order),
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
)
);
return true;
}
}
/**
* @param Order $order
* @param Notification $notification
* @return bool
* @throws AdyenException
* @throws LocalizedException
*/
public function compareOrderAndWebhookPaymentMethods(Order $order, Notification $notification): bool
{
$paymentMethodInstance = $order->getPayment()->getMethodInstance();
if ($this->isAlternativePaymentMethod($paymentMethodInstance)) {
$orderPaymentMethod = $this->getAlternativePaymentMethodTxVariant($paymentMethodInstance);
} else {
$orderPaymentMethod = $order->getPayment()->getCcType();
}
$notificationPaymentMethod = $notification->getPaymentMethod();
// Returns if the payment method is wallet like wechatpayWeb, amazonpay, applepay, paywithgoogle
$isWalletPaymentMethod = $this->isWalletPaymentMethod($paymentMethodInstance);
$isCardPaymentMethod = $order->getPayment()->getMethod() === self::ADYEN_CC || $order->getPayment()->getMethod() === self::ADYEN_ONE_CLICK;
// If it is a wallet method OR a card OR the methods match exactly, return true
if ($isWalletPaymentMethod || $isCardPaymentMethod || strcmp($notificationPaymentMethod, $orderPaymentMethod) === 0) {
return true;
}
return false;
}
/**
* @param string $paymentMethod
* @return bool
*/
public function isBankTransfer(string $paymentMethod): bool
{
if (strlen($paymentMethod) >= 12 && substr($paymentMethod, 0, 12) == "bankTransfer") {
$isBankTransfer = true;
} else {
$isBankTransfer = false;
}
return $isBankTransfer;
}
/**
* @param Order $order
* @param Notification $notification
* @param string $status
* @return string|null
*/
public function getBoletoStatus(Order $order, Notification $notification, string $status): ?string
{
$additionalData = !empty($notification->getAdditionalData()) ? $this->serializer->unserialize(
$notification->getAdditionalData()
) : "";
$boletobancario = $additionalData['boletobancario'] ?? null;
if ($boletobancario && is_array($boletobancario)) {
// check if paid amount is the same as orginal amount