We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Data.php 54.6 KB
Newer Older
rikterbeek's avatar
rikterbeek committed
1 2
<?php
/**
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
rikterbeek's avatar
rikterbeek committed
22
 */
23

rikterbeek's avatar
rikterbeek committed
24 25 26
namespace Adyen\Payment\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
27
use Adyen\Payment\Model\ApplicationInfo;
rikterbeek's avatar
rikterbeek committed
28 29 30 31 32 33

/**
 * @SuppressWarnings(PHPMD.LongVariable)
 */
class Data extends AbstractHelper
{
34
    const MODULE_NAME = 'adyen-magento2';
35 36
    const TEST = 'test';
    const LIVE = 'live';
37 38
    const CHECKOUT_COMPONENT_JS_LIVE = 'https://checkoutshopper-live.adyen.com/checkoutshopper/sdk/3.2.0/adyen.js';
    const CHECKOUT_COMPONENT_JS_TEST = 'https://checkoutshopper-test.adyen.com/checkoutshopper/sdk/3.2.0/adyen.js';
39

40 41 42
    /**
     * @var \Magento\Framework\Encryption\EncryptorInterface
     */
43 44
    protected $_encryptor;

45 46 47 48
    /**
     * @var \Magento\Framework\Config\DataInterface
     */
    protected $_dataStorage;
49

rikterbeek's avatar
#40  
rikterbeek committed
50 51 52 53 54
    /**
     * @var \Magento\Directory\Model\Config\Source\Country
     */
    protected $_country;

rikterbeek's avatar
rikterbeek committed
55 56 57 58 59
    /**
     * @var \Magento\Framework\Module\ModuleListInterface
     */
    protected $_moduleList;

60
    /**
61
     * @var \Adyen\Payment\Model\ResourceModel\Billing\Agreement\CollectionFactory
62 63 64 65
     */
    protected $_billingAgreementCollectionFactory;

    /**
66
     * @var \Magento\Framework\View\Asset\Repository
67 68 69 70 71 72 73 74
     */
    protected $_assetRepo;

    /**
     * @var \Magento\Framework\View\Asset\Source
     */
    protected $_assetSource;

75 76 77 78 79
    /**
     * @var \Adyen\Payment\Model\ResourceModel\Notification\CollectionFactory
     */
    protected $_notificationFactory;

80 81 82 83 84 85 86 87 88 89
    /**
     * @var \Magento\Tax\Model\Config
     */
    protected $_taxConfig;

    /**
     * @var \Magento\Tax\Model\Calculation
     */
    protected $_taxCalculation;

90 91 92
    /**
     * @var \Magento\Framework\App\ProductMetadataInterface
     */
93 94
    protected $productMetadata;

95 96 97
    /**
     * @var \Adyen\Payment\Logger\AdyenLogger
     */
98 99
    protected $adyenLogger;

Rik ter Beek's avatar
Rik ter Beek committed
100 101 102
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
103 104
    protected $storeManager;

Rik ter Beek's avatar
Rik ter Beek committed
105 106 107
    /**
     * @var \Magento\Framework\App\CacheInterface
     */
108
    protected $cache;
109

Rik ter Beek's avatar
Rik ter Beek committed
110
    /**
111
     * @var \Adyen\Payment\Model\Billing\AgreementFactory
Rik ter Beek's avatar
Rik ter Beek committed
112
     */
113
    protected $billingAgreementFactory;
114

115 116 117 118 119
    /**
     * @var ResourceModel\Billing\Agreement
     */
    private $agreementResourceModel;

attilak's avatar
[WIP]  
attilak committed
120 121 122
    /**
     * @var \Magento\Framework\Locale\ResolverInterface
     */
attilak's avatar
[WIP]  
attilak committed
123 124
    private $localeResolver;

125 126 127 128 129
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $config;

130 131 132 133 134
    /**
     * @var \Magento\Backend\Helper\Data $helperBackend
     */
    private $helperBackend;

marcoss's avatar
marcoss committed
135 136 137 138
    /**
     * @var \Magento\Framework\Serialize\SerializerInterface
     */
    private $serializer;
139

140 141 142 143 144
    /**
     * @var \Magento\Framework\Component\ComponentRegistrarInterface
     */
    private $componentRegistrar;

Rik ter Beek's avatar
Rik ter Beek committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    /**
     * Data constructor.
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
     * @param \Magento\Framework\Config\DataInterface $dataStorage
     * @param \Magento\Directory\Model\Config\Source\Country $country
     * @param \Magento\Framework\Module\ModuleListInterface $moduleList
     * @param \Adyen\Payment\Model\ResourceModel\Billing\Agreement\CollectionFactory $billingAgreementCollectionFactory
     * @param \Magento\Framework\View\Asset\Repository $assetRepo
     * @param \Magento\Framework\View\Asset\Source $assetSource
     * @param \Adyen\Payment\Model\ResourceModel\Notification\CollectionFactory $notificationFactory
     * @param \Magento\Tax\Model\Config $taxConfig
     * @param \Magento\Tax\Model\Calculation $taxCalculation
     * @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
     * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\App\CacheInterface $cache
     * @param \Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory
163
     * @param \Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel
164 165 166
     * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     * @param \Magento\Backend\Helper\Data $helperBackend
marcoss's avatar
marcoss committed
167
     * @param \Magento\Framework\Serialize\SerializerInterface $serializer
Rik ter Beek's avatar
Rik ter Beek committed
168
     */
169 170
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
171
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
rikterbeek's avatar
#40  
rikterbeek committed
172
        \Magento\Framework\Config\DataInterface $dataStorage,
rikterbeek's avatar
rikterbeek committed
173
        \Magento\Directory\Model\Config\Source\Country $country,
174
        \Magento\Framework\Module\ModuleListInterface $moduleList,
175
        \Adyen\Payment\Model\ResourceModel\Billing\Agreement\CollectionFactory $billingAgreementCollectionFactory,
176
        \Magento\Framework\View\Asset\Repository $assetRepo,
177
        \Magento\Framework\View\Asset\Source $assetSource,
178 179
        \Adyen\Payment\Model\ResourceModel\Notification\CollectionFactory $notificationFactory,
        \Magento\Tax\Model\Config $taxConfig,
180
        \Magento\Tax\Model\Calculation $taxCalculation,
181 182 183 184
        \Magento\Framework\App\ProductMetadataInterface $productMetadata,
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\CacheInterface $cache,
185
        \Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory,
attilak's avatar
[WIP]  
attilak committed
186
        \Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel,
187
        \Magento\Framework\Locale\ResolverInterface $localeResolver,
188
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
marcoss's avatar
marcoss committed
189
        \Magento\Backend\Helper\Data $helperBackend,
190 191
        \Magento\Framework\Serialize\SerializerInterface $serializer,
        \Magento\Framework\Component\ComponentRegistrarInterface $componentRegistrar
192
    ) {
193
        parent::__construct($context);
194
        $this->_encryptor = $encryptor;
195
        $this->_dataStorage = $dataStorage;
rikterbeek's avatar
#40  
rikterbeek committed
196
        $this->_country = $country;
rikterbeek's avatar
rikterbeek committed
197
        $this->_moduleList = $moduleList;
198 199 200
        $this->_billingAgreementCollectionFactory = $billingAgreementCollectionFactory;
        $this->_assetRepo = $assetRepo;
        $this->_assetSource = $assetSource;
201
        $this->_notificationFactory = $notificationFactory;
202 203
        $this->_taxConfig = $taxConfig;
        $this->_taxCalculation = $taxCalculation;
204 205
        $this->productMetadata = $productMetadata;
        $this->adyenLogger = $adyenLogger;
206 207
        $this->storeManager = $storeManager;
        $this->cache = $cache;
208
        $this->billingAgreementFactory = $billingAgreementFactory;
209
        $this->agreementResourceModel = $agreementResourceModel;
attilak's avatar
[WIP]  
attilak committed
210
        $this->localeResolver = $localeResolver;
211
        $this->config = $config;
212
        $this->helperBackend = $helperBackend;
marcoss's avatar
marcoss committed
213
        $this->serializer = $serializer;
214
        $this->componentRegistrar = $componentRegistrar;
215 216
    }

217
    /**
attilak's avatar
attilak committed
218
     * return recurring types for configuration setting
219 220
     * @return array
     */
221 222
    public function getRecurringTypes()
    {
rikterbeek's avatar
rikterbeek committed
223 224 225 226 227 228 229
        return [
            \Adyen\Payment\Model\RecurringType::ONECLICK => 'ONECLICK',
            \Adyen\Payment\Model\RecurringType::ONECLICK_RECURRING => 'ONECLICK,RECURRING',
            \Adyen\Payment\Model\RecurringType::RECURRING => 'RECURRING'
        ];
    }

230
    /**
attilak's avatar
attilak committed
231
     * return recurring types for configuration setting
232 233
     * @return array
     */
234 235
    public function getModes()
    {
236 237 238 239 240 241
        return [
            '1' => 'Test Mode',
            '0' => 'Production Mode'
        ];
    }

242
    /**
243
     * return recurring types for configuration setting
244 245
     * @return array
     */
246 247
    public function getCaptureModes()
    {
248 249 250 251 252 253
        return [
            'auto' => 'immediate',
            'manual' => 'manual'
        ];
    }

254
    /**
attilak's avatar
attilak committed
255
     * return recurring types for configuration setting
256 257
     * @return array
     */
258 259
    public function getPaymentRoutines()
    {
260 261 262 263 264 265
        return [
            'single' => 'Single Page Payment Routine',
            'multi' => 'Multi-page Payment Routine'
        ];
    }

266
    /**
267
     * Return the number of decimals for the specified currency
268
     * @param $currency
269
     * @return int
270
     */
271
    public function decimalNumbers($currency)
272
    {
273
        switch ($currency) {
274 275 276
            case "CVE":
            case "DJF":
            case "GNF":
alessio's avatar
alessio committed
277 278 279 280
            case "IDR":
            case "JPY":
            case "KMF":
            case "KRW":
281 282 283
            case "PYG":
            case "RWF":
            case "UGX":
alessio's avatar
alessio committed
284
            case "VND":
285 286 287 288 289 290 291
            case "VUV":
            case "XAF":
            case "XOF":
            case "XPF":
                $format = 0;
                break;
            case "BHD":
alessio's avatar
alessio committed
292
            case "IQD":
293 294 295
            case "JOD":
            case "KWD":
            case "LYD":
alessio's avatar
alessio committed
296
            case "OMR":
297 298 299 300 301 302
            case "TND":
                $format = 3;
                break;
            default:
                $format = 2;
        }
303 304
        return $format;
    }
305

306 307 308 309 310 311 312 313 314
    /**
     * Return the formatted amount. Adyen accepts the currency in multiple formats.
     * @param $amount
     * @param $currency
     * @return int
     */
    public function formatAmount($amount, $currency)
    {
        return (int)number_format($amount, $this->decimalNumbers($currency), '', '');
315 316
    }

317

318 319 320 321 322 323 324 325 326 327
    /**
     * Tax Percentage needs to be in minor units for Adyen
     *
     * @param float $taxPercent
     * @return int
     */
    public function getMinorUnitTaxPercent($taxPercent)
    {
        $taxPercent = $taxPercent * 100;
        return (int)$taxPercent;
328 329
    }

330 331 332 333 334
    /**
     * @param $amount
     * @param $currency
     * @return float
     */
335 336 337
    public function originalAmount($amount, $currency)
    {
        // check the format
338
        switch ($currency) {
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
            case "JPY":
            case "IDR":
            case "KRW":
            case "BYR":
            case "VND":
            case "CVE":
            case "DJF":
            case "GNF":
            case "PYG":
            case "RWF":
            case "UGX":
            case "VUV":
            case "XAF":
            case "XOF":
            case "XPF":
            case "GHC":
            case "KMF":
                $format = 1;
                break;
            case "MRO":
                $format = 10;
                break;
            case "BHD":
            case "JOD":
            case "KWD":
            case "OMR":
            case "LYD":
            case "TND":
                $format = 1000;
                break;
            default:
                $format = 100;
                break;
        }

        return ($amount / $format);
    }

377 378 379 380 381 382 383
    /**
     * Street format
     * @param type $address
     * @return array
     */
    public function getStreet($address)
    {
384 385 386 387
        if (empty($address)) {
            return false;
        }

388 389 390
        $street = self::formatStreet($address->getStreet());
        $streetName = $street['0'];
        unset($street['0']);
391
        $streetNr = implode(' ', $street);
rikterbeek's avatar
rikterbeek committed
392
        return (['name' => trim($streetName), 'house_number' => $streetNr]);
393 394
    }

395 396 397 398 399 400 401 402 403 404 405 406 407
    /**
     * Street format
     * @param string $streetLine
     * @return array
     */
    public function getStreetFromString($streetLine)
    {
        $street = self::formatStreet([$streetLine]);
        $streetName = $street['0'];
        unset($street['0']);
        $streetNr = implode(' ', $street);
        return (['name' => trim($streetName), 'house_number' => $streetNr]);
    }
408

409 410 411
    /**
     * Fix this one string street + number
     * @example street + number
412 413
     * @param array $street
     * @return array $street
414
     */
415
    public static function formatStreet($street)
416 417 418 419
    {
        if (count($street) != 1) {
            return $street;
        }
420 421 422

        $street['0'] = trim($street['0']);

423
        preg_match('/((\s\d{0,10})|(\s\d{0,10}\w{1,3}))$/i', $street['0'], $houseNumber, PREG_OFFSET_CAPTURE);
424
        if (!empty($houseNumber['0'])) {
425 426
            $_houseNumber = trim($houseNumber['0']['0']);
            $position = $houseNumber['0']['1'];
427 428
            $streetName = trim(substr($street['0'], 0, $position));
            $street = [$streetName, $_houseNumber];
429 430 431 432 433
        }
        return $street;
    }


434
    /**
attilak's avatar
attilak committed
435 436
     * gives back global configuration values
     *
437 438 439 440
     * @param $field
     * @param null $storeId
     * @return mixed
     */
441 442 443 444 445
    public function getAdyenAbstractConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_abstract', $storeId);
    }

446
    /**
attilak's avatar
attilak committed
447 448
     * gives back global configuration values as boolean
     *
449 450 451 452
     * @param $field
     * @param null $storeId
     * @return mixed
     */
453 454 455 456 457
    public function getAdyenAbstractConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_abstract', $storeId, true);
    }

458
    /**
attilak's avatar
attilak committed
459 460
     * Gives back adyen_cc configuration values
     *
461 462 463 464
     * @param $field
     * @param null $storeId
     * @return mixed
     */
465 466 467 468 469
    public function getAdyenCcConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_cc', $storeId);
    }

470
    /**
attilak's avatar
attilak committed
471 472
     * Gives back adyen_cc configuration values as flag
     *
473 474 475 476
     * @param $field
     * @param null $storeId
     * @return mixed
     */
477 478 479 480 481
    public function getAdyenCcConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_cc', $storeId, true);
    }

482
    /**
attilak's avatar
[WIP]  
attilak committed
483
     * Gives back adyen_cc_vault configuration values as flag
attilak's avatar
attilak committed
484
     *
485
     * @param $field
attilak's avatar
[WIP]  
attilak committed
486
     * @param int|null $storeId
487 488 489 490 491 492 493
     * @return mixed
     */
    public function getAdyenCcVaultConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_cc_vault', $storeId, true);
    }

494
    /**
attilak's avatar
attilak committed
495 496
     * Gives back adyen_hpp configuration values
     *
497 498 499 500
     * @param $field
     * @param null $storeId
     * @return mixed
     */
501 502 503 504 505
    public function getAdyenHppConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_hpp', $storeId);
    }

506
    /**
attilak's avatar
attilak committed
507 508
     * Gives back adyen_hpp configuration values as flag
     *
509 510 511 512
     * @param $field
     * @param null $storeId
     * @return mixed
     */
513 514 515 516 517
    public function getAdyenHppConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_hpp', $storeId, true);
    }

518
    /**
attilak's avatar
attilak committed
519 520
     * Gives back adyen_oneclick configuration values
     *
521 522 523 524 525 526 527 528 529 530
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenOneclickConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_oneclick', $storeId);
    }

    /**
attilak's avatar
attilak committed
531 532
     * Gives back adyen_oneclick configuration values as flag
     *
533 534 535 536 537 538 539 540 541
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenOneclickConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_oneclick', $storeId, true);
    }

542 543
    /**
     * @param $field
544
     * @param int|null $storeId
545 546 547 548 549 550 551 552 553
     * @return bool|mixed
     */
    public function getAdyenPosCloudConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pos_cloud', $storeId);
    }

    /**
     * @param $field
554
     * @param int|null $storeId
555 556 557 558 559 560 561
     * @return bool|mixed
     */
    public function getAdyenPosCloudConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pos_cloud', $storeId, true);
    }

562
    /**
attilak's avatar
attilak committed
563 564
     * Gives back adyen_pay_by_mail configuration values
     *
565
     * @param $field
566
     * @param int|null $storeId
567 568 569 570 571 572 573 574
     * @return mixed
     */
    public function getAdyenPayByMailConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pay_by_mail', $storeId);
    }

    /**
attilak's avatar
attilak committed
575 576
     * Gives back adyen_pay_by_mail configuration values as flag
     *
577
     * @param $field
578
     * @param int|null $storeId
579 580 581 582 583 584 585
     * @return mixed
     */
    public function getAdyenPayByMailConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pay_by_mail', $storeId, true);
    }

586
    /**
attilak's avatar
attilak committed
587 588
     * Gives back adyen_boleto configuration values
     *
589 590 591 592 593 594 595 596 597 598
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenBoletoConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_boleto', $storeId);
    }

    /**
attilak's avatar
attilak committed
599 600
     * Gives back adyen_boleto configuration values as flag
     *
601 602 603 604 605 606 607 608 609
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenBoletoConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_boleto', $storeId, true);
    }

Alessio Zampatti's avatar
Alessio Zampatti committed
610
    /**
attilak's avatar
attilak committed
611 612
     * Gives back adyen_apple_pay configuration values
     *
Alessio Zampatti's avatar
Alessio Zampatti committed
613 614 615 616 617 618 619 620
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenApplePayConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_apple_pay', $storeId);
    }
Rik ter Beek's avatar
Rik ter Beek committed
621

Alessio Zampatti's avatar
Alessio Zampatti committed
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
    /**
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenApplePayMerchantIdentifier($storeId = null)
    {
        $demoMode = $this->getAdyenAbstractConfigDataFlag('demo_mode');
        if ($demoMode) {
            return $this->getAdyenApplePayConfigData('merchant_identifier_test', $storeId);
        } else {
            return $this->getAdyenApplePayConfigData('merchant_identifier_live', $storeId);
        }
    }

    /**
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenApplePayPemFileLocation($storeId = null)
    {
        $demoMode = $this->getAdyenAbstractConfigDataFlag('demo_mode');
        if ($demoMode) {
            return $this->getAdyenApplePayConfigData('full_path_location_pem_file_test', $storeId);
        } else {
            return $this->getAdyenApplePayConfigData('full_path_location_pem_file_live', $storeId);
        }
    }

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
    /**
     * Gives back adyen_google_pay configuration values
     *
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenGooglePayConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_google_pay', $storeId);
    }

    /**
     * Gives back adyen_google_pay configuration values
     *
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function isAdyenGooglePayEnabled($storeId = null)
    {
        return $this->getAdyenGooglePayConfigData('active', $storeId);
    }

    /**
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenGooglePayMerchantIdentifier($storeId = null)
    {
        return $this->getAdyenGooglePayConfigData('merchant_identifier', $storeId);
    }

683
    /**
attilak's avatar
attilak committed
684 685
     * Retrieve decrypted hmac key
     *
686 687
     * @return string
     */
688 689 690 691
    public function getHmac()
    {
        switch ($this->isDemoMode()) {
            case true:
692
                $secretWord = $this->_encryptor->decrypt(trim($this->getAdyenHppConfigData('hmac_test')));
693 694 695
                break;
            default:
                $secretWord = $this->_encryptor->decrypt(trim($this->getAdyenHppConfigData('hmac_live')));
696 697 698 699 700 701 702 703 704
                break;
        }
        return $secretWord;
    }

    public function getHmacPayByMail()
    {
        switch ($this->isDemoMode()) {
            case true:
705
                $secretWord = $this->_encryptor->decrypt(trim($this->getAdyenPayByMailConfigData('hmac_test')));
706 707 708
                break;
            default:
                $secretWord = $this->_encryptor->decrypt(trim($this->getAdyenPayByMailConfigData('hmac_live')));
709 710 711 712 713
                break;
        }
        return $secretWord;
    }

Rik ter Beek's avatar
Rik ter Beek committed
714
    /**
attilak's avatar
attilak committed
715
     * Check if configuration is set to demo mode
Rik ter Beek's avatar
Rik ter Beek committed
716 717 718 719
     *
     * @param int|null $storeId
     * @return mixed
     */
720
    public function isDemoMode($storeId = null)
721
    {
722
        return $this->getAdyenAbstractConfigDataFlag('demo_mode', $storeId);
723 724
    }

725
    /**
attilak's avatar
attilak committed
726 727
     * Retrieve the decrypted notification password
     *
728 729
     * @return string
     */
730 731 732 733 734
    public function getNotificationPassword()
    {
        return $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData('notification_password')));
    }

Rik ter Beek's avatar
Rik ter Beek committed
735
    /**
attilak's avatar
attilak committed
736
     * Retrieve the API key
Rik ter Beek's avatar
Rik ter Beek committed
737 738 739 740 741 742 743
     *
     * @param int|null $storeId
     * @return string
     */
    public function getAPIKey($storeId = null)
    {
        if ($this->isDemoMode($storeId)) {
744 745 746 747
            $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData(
                'api_key_test',
                $storeId
            )));
Rik ter Beek's avatar
Rik ter Beek committed
748
        } else {
749 750 751 752
            $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData(
                'api_key_live',
                $storeId
            )));
Rik ter Beek's avatar
Rik ter Beek committed
753 754 755
        }
        return $apiKey;
    }
756

Rik ter Beek's avatar
Rik ter Beek committed
757
    /**
attilak's avatar
attilak committed
758
     * Retrieve the webserver username
Rik ter Beek's avatar
Rik ter Beek committed
759 760 761 762 763 764 765 766 767 768 769 770 771
     *
     * @param int|null $storeId
     * @return string
     */
    public function getWsUsername($storeId = null)
    {
        if ($this->isDemoMode($storeId)) {
            $wsUsername = trim($this->getAdyenAbstractConfigData('ws_username_test', $storeId));
        } else {
            $wsUsername = trim($this->getAdyenAbstractConfigData('ws_username_live', $storeId));
        }
        return $wsUsername;
    }
772

Rik ter Beek's avatar
Rik ter Beek committed
773
    /**
attilak's avatar
attilak committed
774
     * Retrieve the Live endpoint prefix key
Rik ter Beek's avatar
Rik ter Beek committed
775 776 777 778 779 780 781 782 783
     *
     * @param int|null $storeId
     * @return string
     */
    public function getLiveEndpointPrefix($storeId = null)
    {
        $prefix = trim($this->getAdyenAbstractConfigData('live_endpoint_url_prefix', $storeId));
        return $prefix;
    }
784

785
    /**
attilak's avatar
attilak committed
786 787
     * Cancels the order
     *
788 789
     * @param $order
     */
790 791 792 793 794 795 796 797 798 799 800 801
    public function cancelOrder($order)
    {
        $orderStatus = $this->getAdyenAbstractConfigData('payment_cancelled');
        $order->setActionFlag($orderStatus, true);

        switch ($orderStatus) {
            case \Magento\Sales\Model\Order::STATE_HOLDED:
                if ($order->canHold()) {
                    $order->hold()->save();
                }
                break;
            default:
802
                if ($order->canCancel()) {
803 804 805 806 807 808
                    $order->cancel()->save();
                }
                break;
        }
    }

809 810 811 812 813 814 815 816 817 818
    /**
     * Creditcard type that is selected is different from creditcard type that we get back from the request this
     * function get the magento creditcard type this is needed for getting settings like installments
     * @param $ccType
     * @return mixed
     */
    public function getMagentoCreditCartType($ccType)
    {
        $ccTypesMapper = $this->getCcTypesAltData();

819
        if (isset($ccTypesMapper[$ccType])) {
820 821 822 823 824 825
            $ccType = $ccTypesMapper[$ccType]['code'];
        }

        return $ccType;
    }

826 827 828
    /**
     * @return array
     */
829 830
    public function getCcTypesAltData()
    {
831
        $adyenCcTypes = $this->getAdyenCcTypes();
832
        $types = [];
833 834
        foreach ($adyenCcTypes as $key => $data) {
            $types[$data['code_alt']] = $data;
835
            $types[$data['code_alt']]['code'] = $key;
836 837 838 839
        }
        return $types;
    }

840 841 842
    /**
     * @return mixed
     */
843 844 845 846 847
    public function getAdyenCcTypes()
    {
        return $this->_dataStorage->get('adyen_credit_cards');
    }

848
    /**
attilak's avatar
attilak committed
849 850
     * Retrieve information from payment configuration
     *
851 852 853 854 855
     * @param $field
     * @param $paymentMethodCode
     * @param $storeId
     * @param bool|false $flag
     * @return bool|mixed
856 857 858 859 860
     */
    public function getConfigData($field, $paymentMethodCode, $storeId, $flag = false)
    {
        $path = 'payment/' . $paymentMethodCode . '/' . $field;

861
        if (!$flag) {
862
            return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
863
        } else {
864
            return $this->scopeConfig->isSetFlag($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
865 866
        }
    }
rikterbeek's avatar
#40  
rikterbeek committed
867

868 869 870 871 872 873 874 875 876 877 878
    /**
     * Get adyen magento module's name sent to Adyen
     *
     * @return string
     */
    public function getModuleName()
    {
        return (string)self::MODULE_NAME;
    }

    /**
879
     * Get adyen magento module's version from composer.json
880 881 882
     *
     * @return string
     */
rikterbeek's avatar
rikterbeek committed
883 884
    public function getModuleVersion()
    {
885 886
        $moduleDir = $this->componentRegistrar->getPath(\Magento\Framework\Component\ComponentRegistrar::MODULE,
            'Adyen_Payment');
887 888 889 890 891 892 893 894 895

        $composerJson = file_get_contents($moduleDir . '/composer.json');
        $composerJson = json_decode($composerJson, true);

        if (empty($composerJson['version'])) {
            return "Version is not available in composer.json";
        }

        return $composerJson['version'];
rikterbeek's avatar
rikterbeek committed
896
    }
897 898 899 900 901 902 903 904 905 906 907 908 909

    public function getBoletoTypes()
    {
        return [
            [
                'value' => 'boletobancario_itau',
                'label' => __('boletobancario_itau'),
            ],
            [
                'value' => 'boletobancario_santander',
                'label' => __('boletobancario_santander'),
            ],
            [
910 911 912
                'value' => 'primeiropay_boleto',
                'label' => __('primeiropay_boleto'),
            ]
913 914
        ];
    }
915 916 917 918 919 920 921 922 923 924 925 926 927 928

    /**
     * @param $customerId
     * @param $storeId
     * @param $grandTotal
     * @param $recurringType
     * @return array
     */
    public function getOneClickPaymentMethods($customerId, $storeId, $grandTotal, $recurringType)
    {
        $billingAgreements = [];

        $baCollection = $this->_billingAgreementCollectionFactory->create();
        $baCollection->addFieldToFilter('customer_id', $customerId);
929 930 931
        if ($this->isPerStoreBillingAgreement($storeId)) {
            $baCollection->addFieldToFilter('store_id', $storeId);
        }
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
        $baCollection->addFieldToFilter('method_code', 'adyen_oneclick');
        $baCollection->addActiveFilter();

        foreach ($baCollection as $billingAgreement) {
            $agreementData = $billingAgreement->getAgreementData();

            // no agreementData and contractType then ignore
            if ((!is_array($agreementData)) || (!isset($agreementData['contractTypes']))) {
                continue;
            }

            // check if contractType is supporting the selected contractType for OneClick payments
            $allowedContractTypes = $agreementData['contractTypes'];
            if (in_array($recurringType, $allowedContractTypes)) {
                // check if AgreementLabel is set and if contract has an recurringType
                if ($billingAgreement->getAgreementLabel()) {
                    // for Ideal use sepadirectdebit because it is
                    if ($agreementData['variant'] == 'ideal') {
                        $agreementData['variant'] = 'sepadirectdebit';
                    }

953 954
                    $data = [
                        'reference_id' => $billingAgreement->getReferenceId(),
955 956 957 958 959 960 961 962 963 964
                        'agreement_label' => $billingAgreement->getAgreementLabel(),
                        'agreement_data' => $agreementData
                    ];

                    if ($this->showLogos()) {
                        $logoName = $agreementData['variant'];

                        $asset = $this->createAsset(
                            'Adyen_Payment::images/logos/' . $logoName . '.png'
                        );
965

966
                        $icon = null;
Rik ter Beek's avatar
Rik ter Beek committed
967
                        $placeholder = $this->_assetSource->findSource($asset);
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
                        if ($placeholder) {
                            list($width, $height) = getimagesize($asset->getSourceFile());
                            $icon = [
                                'url' => $asset->getUrl(),
                                'width' => $width,
                                'height' => $height
                            ];
                        }
                        $data['logo'] = $icon;
                    }

                    /**
                     * Check if there are installments for this creditcard type defined
                     */
                    $data['number_of_installments'] = 0;
                    $ccType = $this->getMagentoCreditCartType($agreementData['variant']);
                    $installments = null;
                    $installmentsValue = $this->getAdyenCcConfigData('installments');
                    if ($installmentsValue) {
marcoss's avatar
marcoss committed
987
                        $installments = $this->serializer->unserialize($installmentsValue);
988 989 990
                    }

                    if ($installments) {
Alessio Zampatti's avatar
Alessio Zampatti committed
991
                        $numberOfInstallments = [];
992 993 994 995

                        foreach ($installments as $ccTypeInstallment => $installment) {
                            if ($ccTypeInstallment == $ccType) {
                                foreach ($installment as $amount => $installments) {
Alessio Zampatti's avatar
Alessio Zampatti committed
996 997
                                    if ($grandTotal >= $amount) {
                                        array_push($numberOfInstallments, $installments);
998 999 1000 1001 1002
                                    }
                                }
                            }
                        }
                        if ($numberOfInstallments) {
Alessio Zampatti's avatar
Alessio Zampatti committed
1003
                            sort($numberOfInstallments);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
                            $data['number_of_installments'] = $numberOfInstallments;
                        }
                    }
                    $billingAgreements[] = $data;
                }
            }
        }
        return $billingAgreements;
    }

1014 1015
    public function isPerStoreBillingAgreement($storeId)
    {
1016 1017
        return !$this->getAdyenOneclickConfigDataFlag('share_billing_agreement', $storeId);
    }
1018 1019 1020 1021 1022 1023 1024

    /**
     * @param $paymentMethod
     * @return bool
     */
    public function isPaymentMethodOpenInvoiceMethod($paymentMethod)
    {
1025 1026 1027 1028
        if (strpos($paymentMethod, 'afterpay') !== false ||
            strpos($paymentMethod, 'klarna') !== false ||
            strpos($paymentMethod, 'ratepay') !== false
        ) {
1029 1030
            return true;
        }
Rik ter Beek's avatar
Rik ter Beek committed
1031 1032

        return false;
1033 1034
    }

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
    /**
     * @param $paymentMethod
     * @return bool
     */
    public function isPaymentMethodRatepayMethod($paymentMethod)
    {
        if (strpos($paymentMethod, 'ratepay') !== false) {
            return true;
        }

        return false;
    }

1048 1049 1050 1051 1052 1053 1054 1055 1056
    /**
     * @param $paymentMethod
     * @return bool
     */
    public function isPaymentMethodAfterpayTouchMethod($paymentMethod)
    {
        if (strpos($paymentMethod, 'afterpaytouch') !== false) {
            return true;
        }
1057

1058 1059
        return false;
    }
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069
    /**
     * @param $paymentMethod
     * @return bool
     */
    public function isPaymentMethodMolpayMethod($paymentMethod)
    {
        if (strpos($paymentMethod, 'molpay_') !== false) {
            return true;
        }
1070

1071 1072
        return false;
    }
1073

attilak's avatar
attilak committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    /**
     * @param $paymentMethod
     * @return bool
     */
    public function isPaymentMethodOneyMethod($paymentMethod)
    {
        if (strpos($paymentMethod, 'facilypay_') !== false) {
            return true;
        }

        return false;
    }

    /**
     * @param $paymentMethod
     * @return bool
     */
    public function doesPaymentMethodSkipDetails($paymentMethod)
    {
        if ($this->isPaymentMethodOpenInvoiceMethod($paymentMethod) ||
            $this->isPaymentMethodMolpayMethod($paymentMethod) ||
            $this->isPaymentMethodOneyMethod($paymentMethod)
        ) {
            return true;
        }

        return false;
    }

1103 1104 1105 1106 1107
    public function getRatePayId()
    {
        return $this->getAdyenHppConfigData("ratepay_id");
    }

1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
    /**
     * For Klarna And AfterPay use VatCategory High others use none
     *
     * @param $paymentMethod
     * @return bool
     */
    public function isVatCategoryHigh($paymentMethod)
    {
        if ($paymentMethod == "klarna" ||
            strlen($paymentMethod) >= 9 && substr($paymentMethod, 0, 9) == 'afterpay_'
        ) {
            return true;
        }
        return false;
    }
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
    /**
     * @return bool
     */
    public function showLogos()
    {
        $showLogos = $this->getAdyenAbstractConfigData('title_renderer');
        if ($showLogos == \Adyen\Payment\Model\Config\Source\RenderMode::MODE_TITLE_IMAGE) {
            return true;
        }
        return false;
    }

    /**
     * Create a file asset that's subject of fallback system
     *
     * @param string $fileId
     * @param array $params
     * @return \Magento\Framework\View\Asset\File
     */
    public function createAsset($fileId, array $params = [])
    {
        $params = array_merge(['_secure' => $this->_request->isSecure()], $params);
        return $this->_assetRepo->createAsset($fileId, $params);
    }

1149 1150
    public function getStoreLocale($storeId)
    {
1151 1152 1153 1154
        $path = \Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE;
        return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
    }

attilak's avatar
[WIP]  
attilak committed
1155 1156 1157 1158 1159 1160 1161
    /**
     * Format Magento locale codes with undersocre to ISO locale codes with dash
     * @param $localeCode
     */
    public function formatLocaleCode($localeCode)
    {
        return str_replace("_", "-", $localeCode);
attilak's avatar
[WIP]  
attilak committed
1162 1163
    }

Alessio Zampatti's avatar
Alessio Zampatti committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    public function getApplePayShippingTypes()
    {
        return [
            [
                'value' => 'shipping',
                'label' => __('Shipping Method')
            ],
            [
                'value' => 'delivery',
                'label' => __('Delivery Method')
            ],
            [
                'value' => 'storePickup',
                'label' => __('Store Pickup Method')
            ],
            [
                'value' => 'servicePickup',
                'label' => __('Service Pickup Method')
            ]
        ];
    }

1186 1187 1188 1189
    public function getUnprocessedNotifications()
    {
        $notifications = $this->_notificationFactory->create();
        $notifications->unprocessedNotificationsFilter();
1190
        return $notifications->getSize();
1191 1192
    }

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    /**
     * @param $formFields
     * @param $count
     * @param $name
     * @param $price
     * @param $currency
     * @param $taxAmount
     * @param $priceInclTax
     * @param $taxPercent
     * @param $numberOfItems
     * @param $payment
1204
     * @param null $itemId
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
     * @return mixed
     */
    public function createOpenInvoiceLineItem(
        $formFields,
        $count,
        $name,
        $price,
        $currency,
        $taxAmount,
        $priceInclTax,
        $taxPercent,
        $numberOfItems,
1217 1218
        $payment,
        $itemId = null
1219 1220 1221 1222
    ) {
        $description = str_replace("\n", '', trim($name));
        $itemAmount = $this->formatAmount($price, $currency);

1223 1224 1225 1226 1227 1228
        $itemVatAmount = $this->getItemVatAmount(
            $taxAmount,
            $priceInclTax,
            $price,
            $currency
        );
1229 1230 1231 1232

        // Calculate vat percentage
        $itemVatPercentage = $this->getMinorUnitTaxPercent($taxPercent);

1233 1234 1235 1236 1237
        return $this->getOpenInvoiceLineData(
            $formFields,
            $count,
            $currency,
            $description,
1238
            $itemAmount,
1239 1240 1241
            $itemVatAmount,
            $itemVatPercentage,
            $numberOfItems,
1242 1243
            $payment,
            $itemId
1244
        );
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    }

    /**
     * @param $formFields
     * @param $count
     * @param $order
     * @param $shippingAmount
     * @param $shippingTaxAmount
     * @param $currency
     * @param $payment
     * @return mixed
     */
    public function createOpenInvoiceLineShipping(
        $formFields,
        $count,
        $order,
        $shippingAmount,
        $shippingTaxAmount,
        $currency,
        $payment
    ) {
        $description = $order->getShippingDescription();
        $itemAmount = $this->formatAmount($shippingAmount, $currency);
        $itemVatAmount = $this->formatAmount($shippingTaxAmount, $currency);

        // Create RateRequest to calculate the Tax class rate for the shipping method
        $rateRequest = $this->_taxCalculation->getRateRequest(
            $order->getShippingAddress(),
            $order->getBillingAddress(),
            null,
            $order->getStoreId(),
            $order->getCustomerId()
        );

        $taxClassId = $this->_taxConfig->getShippingTaxClass($order->getStoreId());
        $rateRequest->setProductClassId($taxClassId);
        $rate = $this->_taxCalculation->getRate($rateRequest);

        $itemVatPercentage = $this->getMinorUnitTaxPercent($rate);
        $numberOfItems = 1;

1286 1287 1288 1289 1290
        return $this->getOpenInvoiceLineData(
            $formFields,
            $count,
            $currency,
            $description,
1291
            $itemAmount,
1292 1293 1294
            $itemVatAmount,
            $itemVatPercentage,
            $numberOfItems,
1295
            $payment,
1296
            "shippingCost"
1297
        );
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    }

    /**
     * @param $taxAmount
     * @param $priceInclTax
     * @param $price
     * @param $currency
     * @return string
     */
    public function getItemVatAmount(
        $taxAmount,
        $priceInclTax,
        $price,
        $currency
    ) {
        if ($taxAmount > 0 && $priceInclTax > 0) {
            return $this->formatAmount($priceInclTax, $currency) - $this->formatAmount($price, $currency);
        }
        return $this->formatAmount($taxAmount, $currency);
    }

    /**
     * Set the openinvoice line
     *
     * @param $formFields
     * @param $count
     * @param $currencyCode
     * @param $description
     * @param $itemAmount
     * @param $itemVatAmount
     * @param $itemVatPercentage
     * @param $numberOfItems
     * @param $payment
1331 1332
     * @param null|int $itemId optional
     * @return mixed
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
     */
    public function getOpenInvoiceLineData(
        $formFields,
        $count,
        $currencyCode,
        $description,
        $itemAmount,
        $itemVatAmount,
        $itemVatPercentage,
        $numberOfItems,
1343 1344
        $payment,
        $itemId = null
1345 1346
    ) {
        $linename = "line" . $count;
1347 1348 1349

        // item id is optional
        if ($itemId) {
attilak's avatar
attilak committed
1350
            $formFields['openinvoicedata.' . $linename . '.itemId'] = $itemId;
1351 1352
        }

1353 1354 1355 1356 1357 1358 1359 1360
        $formFields['openinvoicedata.' . $linename . '.currencyCode'] = $currencyCode;
        $formFields['openinvoicedata.' . $linename . '.description'] = $description;
        $formFields['openinvoicedata.' . $linename . '.itemAmount'] = $itemAmount;
        $formFields['openinvoicedata.' . $linename . '.itemVatAmount'] = $itemVatAmount;
        $formFields['openinvoicedata.' . $linename . '.itemVatPercentage'] = $itemVatPercentage;
        $formFields['openinvoicedata.' . $linename . '.numberOfItems'] = $numberOfItems;

        if ($this->isVatCategoryHigh($payment->getAdditionalInformation(
1361 1362
            \Adyen\Payment\Observer\AdyenHppDataAssignObserver::BRAND_CODE
        ))
1363 1364 1365 1366 1367 1368 1369
        ) {
            $formFields['openinvoicedata.' . $linename . '.vatCategory'] = "High";
        } else {
            $formFields['openinvoicedata.' . $linename . '.vatCategory'] = "None";
        }
        return $formFields;
    }
1370

stkams's avatar
stkams committed
1371
    /**
1372
     * @param int|null $storeId
stkams's avatar
stkams committed
1373 1374
     * @return string the X API Key for the specified or current store
     */
1375
    public function getPosApiKey($storeId = null)
1376
    {
stkams's avatar
stkams committed
1377
        if ($this->isDemoMode($storeId)) {
1378
            $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_test', $storeId)));
stkams's avatar
stkams committed
1379
        } else {
1380
            $apiKey = $this->_encryptor->decrypt(trim($this->getAdyenPosCloudConfigData('api_key_live', $storeId)));
1381 1382 1383 1384
        }
        return $apiKey;
    }

1385
    /**
attilak's avatar
attilak committed
1386
     * Return the Store ID for the current store/mode
1387 1388 1389 1390
     *
     * @param int|null $storeId
     * @return mixed
     */
attilak's avatar
attilak committed
1391
    public function getPosStoreId($storeId = null)
1392
    {
attilak's avatar
attilak committed
1393
        return $this->getAdyenPosCloudConfigData('pos_store_id', $storeId);
1394 1395
    }

1396 1397 1398 1399 1400 1401
    /**
     * Return the merchant account name configured for the proper payment method.
     * If it is not configured for the specific payment method,
     * return the merchant account name defined in required settings.
     *
     * @param $paymentMethod
attilak's avatar
[WIP]  
attilak committed
1402
     * @param int|null $storeId
1403 1404
     * @return string
     */
attilak's avatar
[WIP]  
attilak committed
1405
    public function getAdyenMerchantAccount($paymentMethod, $storeId = null)
1406
    {
attilak's avatar
[WIP]  
attilak committed
1407 1408 1409 1410
        if (!$storeId) {
            $storeId = $this->storeManager->getStore()->getId();
        }

1411 1412 1413
        $merchantAccount = $this->getAdyenAbstractConfigData("merchant_account", $storeId);
        $merchantAccountPos = $this->getAdyenPosCloudConfigData('pos_merchant_account', $storeId);

1414
        if ($paymentMethod == 'adyen_pos_cloud' && !empty($merchantAccountPos)) {
1415 1416 1417 1418 1419
            return $merchantAccountPos;
        }
        return $merchantAccount;
    }

1420 1421 1422 1423 1424 1425 1426
    /**
     * Format the Receipt sent in the Terminal API response in HTML
     * so that it can be easily shown to the shopper
     *
     * @param $paymentReceipt
     * @return string
     */
1427 1428
    public function formatTerminalAPIReceipt($paymentReceipt)
    {
1429
        $formattedHtml = "<table class='terminal-api-receipt'>";
1430
        foreach ($paymentReceipt as $receipt) {
1431 1432 1433 1434
            if ($receipt['DocumentQualifier'] == "CustomerReceipt") {
                foreach ($receipt['OutputContent']['OutputText'] as $item) {
                    parse_str($item['Text'], $textParts);
                    $formattedHtml .= "<tr class='terminal-api-receipt'>";
1435
                    if (!empty($textParts['name'])) {
1436
                        $formattedHtml .= "<td class='terminal-api-receipt-name'>" . $textParts['name'] . "</td>";
1437
                    } else {
1438
                        $formattedHtml .= "<td class='terminal-api-receipt-name'>&nbsp;</td>";
1439 1440
                    }
                    if (!empty($textParts['value'])) {
1441
                        $formattedHtml .= "<td class='terminal-api-receipt-value' align='right'>" . $textParts['value'] . "</td>";
1442
                    } else {
1443
                        $formattedHtml .= "<td class='terminal-api-receipt-value' align='right'>&nbsp;</td>";
1444
                    }
1445
                    $formattedHtml .= "</tr>";
1446 1447 1448
                }
            }
        }
1449 1450
        $formattedHtml .= "</table>";
        return $formattedHtml;
1451 1452
    }

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    /**
     * Initializes and returns Adyen Client and sets the required parameters of it
     *
     * @param int|null $storeId
     * @param string|null $apiKey
     * @return \Adyen\Client
     * @throws \Adyen\AdyenException
     */
    public function initializeAdyenClient($storeId = null, $apiKey = null)
    {
        // initialize client
cyattilakiss's avatar
cyattilakiss committed
1464
        if ($storeId === null) {
alessio's avatar
alessio committed
1465 1466 1467
            $storeId = $this->storeManager->getStore()->getId();
        }

1468 1469 1470
        if (empty($apiKey)) {
            $apiKey = $this->getAPIKey($storeId);
        }
1471

1472
        $client = $this->createAdyenClient();
Rik ter Beek's avatar
Rik ter Beek committed
1473
        $client->setApplicationName("Magento 2 plugin");
1474
        $client->setXApiKey($apiKey);
1475
        $moduleVersion = $this->getModuleVersion();
1476

1477
        $client->setAdyenPaymentSource($this->getModuleName(), $moduleVersion);
Rik ter Beek's avatar
Rik ter Beek committed
1478 1479 1480 1481
        $client->setExternalPlatform($this->productMetadata->getName(), $this->productMetadata->getVersion());
        if ($this->isDemoMode($storeId)) {
            $client->setEnvironment(\Adyen\Environment::TEST);
        } else {
1482
            $client->setEnvironment(\Adyen\Environment::LIVE, $this->getLiveEndpointPrefix($storeId));
Rik ter Beek's avatar
Rik ter Beek committed
1483
        }
1484

Rik ter Beek's avatar
Rik ter Beek committed
1485
        $client->setLogger($this->adyenLogger);
1486 1487 1488 1489 1490

        return $client;
    }

    /**
1491
     * @param \Adyen\Client $client
1492 1493 1494 1495 1496 1497 1498
     * @return \Adyen\Service\PosPayment
     * @throws \Adyen\AdyenException
     */
    public function createAdyenPosPaymentService($client)
    {
        return new \Adyen\Service\PosPayment($client);
    }
1499

1500 1501 1502 1503 1504 1505 1506 1507 1508
    /**
     * @return \Adyen\Client
     * @throws \Adyen\AdyenException
     */
    private function createAdyenClient()
    {
        return new \Adyen\Client();
    }

attilak's avatar
[WIP]  
attilak committed
1509 1510 1511
    /**
     * @return string
     */
1512 1513
    public function getOrigin()
    {
1514 1515
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $state = $objectManager->get('Magento\Framework\App\State');
attilak's avatar
[WIP]  
attilak committed
1516
        $baseUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
1517
        if ('adminhtml' === $state->getAreaCode()) {
1518
            $baseUrl = $this->helperBackend->getHomePageUrl();
1519
        }
attilak's avatar
[WIP]  
attilak committed
1520 1521
        $parsed = parse_url($baseUrl);
        $origin = $parsed['scheme'] . "://" . $parsed['host'];
1522 1523 1524
        if (!empty($parsed['port'])) {
            $origin .= ":" . $parsed['port'];
        }
attilak's avatar
[WIP]  
attilak committed
1525 1526 1527
        return $origin;
    }

1528 1529 1530 1531 1532 1533 1534 1535
    /**
     * Retrieve origin keys for platform's base url
     *
     * @return string
     * @throws \Adyen\AdyenException
     */
    public function getOriginKeyForBaseUrl()
    {
attilak's avatar
[WIP]  
attilak committed
1536
        $origin = $this->getOrigin();
1537
        $storeId = $this->storeManager->getStore()->getId();
attilak's avatar
[WIP]  
attilak committed
1538
        $cacheKey = 'Adyen_origin_key_for_' . $origin . '_' . $storeId;
1539

1540
        if (!$originKey = $this->cache->load($cacheKey)) {
attilak's avatar
[WIP]  
attilak committed
1541
            if ($originKey = $this->getOriginKeyForOrigin($origin, $storeId)) {
attilak's avatar
attilak committed
1542
                $this->cache->save($originKey, $cacheKey, [], 60 * 60 * 24);
1543 1544 1545 1546 1547 1548 1549
            }
        }

        return $originKey;
    }

    /**
attilak's avatar
[WIP]  
attilak committed
1550
     * Get origin key for a specific origin using the adyen api library client
1551
     *
attilak's avatar
[WIP]  
attilak committed
1552
     * @param $origin
1553 1554 1555 1556
     * @param int|null $storeId
     * @return string
     * @throws \Adyen\AdyenException
     */
attilak's avatar
[WIP]  
attilak committed
1557
    private function getOriginKeyForOrigin($origin, $storeId = null)
1558
    {
attilak's avatar
attilak committed
1559 1560
        $params = [
            "originDomains" => [
attilak's avatar
[WIP]  
attilak committed
1561
                $origin
attilak's avatar
attilak committed
1562 1563
            ]
        ];
1564 1565

        $client = $this->initializeAdyenClient($storeId);
1566

1567
        try {
1568
            $service = $this->createAdyenCheckoutUtilityService($client);
1569
            $response = $service->originKeys($params);
1570
        } catch (\Exception $e) {
1571 1572
            $this->adyenLogger->error($e->getMessage());
        }
1573

1574
        $originKey = "";
attilak's avatar
attilak committed
1575

attilak's avatar
[WIP]  
attilak committed
1576 1577
        if (!empty($response['originKeys'][$origin])) {
            $originKey = $response['originKeys'][$origin];
1578 1579 1580 1581 1582 1583 1584 1585 1586
        }

        return $originKey;
    }

    /**
     * @param int|null $storeId
     * @return string
     */
1587
    public function getCheckoutEnvironment($storeId = null)
1588 1589
    {
        if ($this->isDemoMode($storeId)) {
1590
            return self::TEST;
1591 1592
        }

1593
        return self::LIVE;
1594 1595 1596
    }

    /**
1597
     * @param \Adyen\Client $client
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
     * @return \Adyen\Service\CheckoutUtility
     * @throws \Adyen\AdyenException
     */
    private function createAdyenCheckoutUtilityService($client)
    {
        return new \Adyen\Service\CheckoutUtility($client);
    }

    /**
     * @param int|null $storeId
     * @return string
     */
    public function getCheckoutCardComponentJs($storeId = null)
    {
        if ($this->isDemoMode($storeId)) {
            return self::CHECKOUT_COMPONENT_JS_TEST;
        }

        return self::CHECKOUT_COMPONENT_JS_LIVE;
    }

1619 1620 1621 1622
    /**
     * @param $order
     * @param $additionalData
     */
1623 1624 1625 1626 1627 1628
    public function createAdyenBillingAgreement($order, $additionalData)
    {
        if (!empty($additionalData['recurring.recurringDetailReference'])) {
            $listRecurringContracts = null;
            try {
                // Get or create billing agreement
1629
                /** @var \Adyen\Payment\Model\Billing\Agreement $billingAgreement */
1630 1631 1632 1633 1634 1635 1636 1637 1638
                $billingAgreement = $this->billingAgreementFactory->create();
                $billingAgreement->load($additionalData['recurring.recurringDetailReference'], 'reference_id');

                // check if BA exists
                if (!($billingAgreement && $billingAgreement->getAgreementId() > 0 && $billingAgreement->isValid())) {
                    // create new BA
                    $billingAgreement = $this->billingAgreementFactory->create();
                    $billingAgreement->setStoreId($order->getStoreId());
                    $billingAgreement->importOrderPayment($order->getPayment());
1639 1640 1641
                    if ($billingAgreement->getCustomerId() === null) {
                        $billingAgreement->setCustomerId($this->getCustomerId($order));
                    }
1642 1643 1644 1645
                    $message = __(
                        'Created billing agreement #%1.',
                        $additionalData['recurring.recurringDetailReference']
                    );
1646 1647 1648
                } else {

                    $billingAgreement->setIsObjectChanged(true);
1649 1650 1651 1652
                    $message = __(
                        'Updated billing agreement #%1.',
                        $additionalData['recurring.recurringDetailReference']
                    );
1653 1654 1655
                }

                // Populate billing agreement data
1656
                $storeOneClick = $order->getPayment()->getAdditionalInformation('store_cc');
1657 1658

                $billingAgreement->setCcBillingAgreement($additionalData, $storeOneClick, $order->getStoreId());
1659 1660 1661
                $billingAgreementErrors = $billingAgreement->getErrors();

                if ($billingAgreement->isValid() && empty($billingAgreementErrors)) {
1662

1663 1664 1665 1666
                    if (!$this->agreementResourceModel->getOrderRelation(
                        $billingAgreement->getAgreementId(),
                        $order->getId()
                    )) {
1667 1668 1669 1670

                        // save into sales_billing_agreement_order
                        $billingAgreement->addOrderRelation($order);
                    }
1671 1672 1673
                    // add to order to save agreement
                    $order->addRelatedObject($billingAgreement);
                } else {
1674
                    $message = __('Failed to create billing agreement for this order. Reason(s): ') . join(
1675 1676 1677
                            ', ',
                            $billingAgreementErrors
                        );
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
                    throw new \Exception($message);
                }

            } catch (\Exception $exception) {
                $message = $exception->getMessage();
                $this->adyenLogger->error("exception: " . $message);
            }

            $comment = $order->addStatusHistoryComment($message);

            $order->addRelatedObject($comment);
        }
    }

1692 1693 1694 1695 1696
    /**
     * Method can be used by interceptors to provide the customer ID in a different way.
     * @param \Magento\Sales\Model\Order $order
     * @return int|null
     */
1697 1698
    public function getCustomerId(\Magento\Sales\Model\Order $order)
    {
1699 1700 1701
        return $order->getCustomerId();
    }

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    /**
     * For backwards compatibility get the recurringType used for HPP + current billing agreements
     *
     * @param null $storeId
     * @return null|string
     */
    public function getRecurringTypeFromOneclickRecurringSetting($storeId = null)
    {
        $enableOneclick = $this->getAdyenAbstractConfigData('enable_oneclick', $storeId);
        $enableRecurring = $this->getAdyenAbstractConfigData('enable_recurring', $storeId);

        if ($enableOneclick && $enableRecurring) {
            return \Adyen\Payment\Model\RecurringType::ONECLICK_RECURRING;
        } elseif ($enableOneclick && !$enableRecurring) {
            return \Adyen\Payment\Model\RecurringType::ONECLICK;
        } elseif (!$enableOneclick && $enableRecurring) {
            return \Adyen\Payment\Model\RecurringType::RECURRING;
        } else {
            return \Adyen\Payment\Model\RecurringType::NONE;
        }
    }
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742

    /**
     * Get icon from variant
     *
     * @param $variant
     * @return array
     */
    public function getVariantIcon($variant)
    {
        $asset = $this->createAsset(sprintf("Adyen_Payment::images/logos/%s_small.png", $variant));
        list($width, $height) = getimagesize($asset->getSourceFile());
        $icon = [
            'url' => $asset->getUrl(),
            'width' => $width,
            'height' => $height
        ];
        return $icon;
    }

    /**
attilak's avatar
attilak committed
1743 1744
     * Check if CreditCard vault is enabled
     *
1745 1746 1747 1748 1749 1750 1751 1752
     * @param int|null $storeId
     * @return mixed
     */
    public function isCreditCardVaultEnabled($storeId = null)
    {
        return $this->getAdyenCcVaultConfigDataFlag('active', $storeId);
    }

1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    /**
     * Checks if the house number needs to be sent to the Adyen API separately or as it is in the street field
     *
     * @param $country
     * @return bool
     */
    public function isSeparateHouseNumberRequired($country)
    {
        $countryList = ["nl", "de", "se", "no", "at", "fi", "dk"];

        return in_array(strtolower($country), $countryList);
    }
1765

attilak's avatar
[WIP]  
attilak committed
1766 1767 1768 1769 1770 1771 1772 1773
    /**
     * Check if 3DS2.0 is enabled for credit cards
     *
     * @param int|null $storeId
     * @return mixed
     */
    public function isCreditCardThreeDS2Enabled($storeId = null)
    {
1774
        return $this->getAdyenCcConfigDataFlag('threeds2_enabled', $storeId);
attilak's avatar
[WIP]  
attilak committed
1775
    }
1776

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808
    /**
     * @param $client
     * @return \Adyen\Service\Checkout
     */
    public function createAdyenCheckoutService($client)
    {
        return new \Adyen\Service\Checkout($client);
    }

    /**
     * @param $client
     * @return \Adyen\Service\Recurring
     * @throws \Adyen\AdyenException
     */
    public function createAdyenRecurringService($client)
    {
        return new \Adyen\Service\Recurring($client);
    }

    /**
     * @param string $date
     * @param string $format
     * @return mixed
     */
    public function formatDate($date = null, $format = 'Y-m-d H:i:s')
    {
        if (strlen($date) < 0) {
            $date = date('d-m-Y H:i:s');
        }
        $timeStamp = new \DateTime($date);
        return $timeStamp->format($format);
    }
attilak's avatar
[WIP]  
attilak committed
1809 1810 1811 1812 1813 1814 1815 1816

    /**
     * @param string|null $type
     * @param string|null $token
     * @return string
     */
    public function buildThreeDS2ProcessResponseJson($type = null, $token = null)
    {
attilak's avatar
attilak committed
1817
        $response = ['threeDS2' => false];
attilak's avatar
[WIP]  
attilak committed
1818

1819
        if (!empty($type)) {
1820
            $response['type'] = $type;
1821 1822
        }

attilak's avatar
[WIP]  
attilak committed
1823
        if ($type && $token) {
1824 1825
            $response['threeDS2'] = true;
            $response['token'] = $token;
attilak's avatar
[WIP]  
attilak committed
1826 1827
        }

attilak's avatar
attilak committed
1828 1829
        return json_encode($response);
    }
1830

attilak's avatar
attilak committed
1831
    /**
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
     * @param int $storeId
     * @return mixed|string
     */
    public function getCurrentLocaleCode($storeId)
    {
        $localeCode = $this->getAdyenAbstractConfigData('shopper_locale', $storeId);
        if ($localeCode != "") {
            return $localeCode;
        }

        $locale = $this->localeResolver->getLocale();
        if ($locale) {
            return $locale;
        }

        // should have the value if not fall back to default
        $localeCode = $this->config->getValue(
            \Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
1851
            $this->storeManager->getStore($storeId)->getCode()
1852 1853 1854
        );

        return $localeCode;
attilak's avatar
[WIP]  
attilak committed
1855
    }
1856
}