Data.php 13.7 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 27 28 29 30 31 32 33
namespace Adyen\Payment\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

/**
 * @SuppressWarnings(PHPMD.LongVariable)
 */
class Data extends AbstractHelper
{

34 35 36
    /**
     * @var \Magento\Framework\Encryption\EncryptorInterface
     */
37 38
    protected $_encryptor;

39 40 41 42
    /**
     * @var \Magento\Framework\Config\DataInterface
     */
    protected $_dataStorage;
43 44

    /**
45 46 47 48 49
     * Data constructor.
     *
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
     * @param \Magento\Framework\Config\DataInterface $dataStorage
50 51 52
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
53 54
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
        \Magento\Framework\Config\DataInterface $dataStorage
55
    ) {
56
        parent::__construct($context);
57
        $this->_encryptor = $encryptor;
58
        $this->_dataStorage = $dataStorage;
59 60
    }

61 62 63 64
    /**
     * @desc return recurring types for configuration setting
     * @return array
     */
65 66
    public function getRecurringTypes()
    {
rikterbeek's avatar
rikterbeek committed
67 68 69 70 71 72 73
        return [
            \Adyen\Payment\Model\RecurringType::ONECLICK => 'ONECLICK',
            \Adyen\Payment\Model\RecurringType::ONECLICK_RECURRING => 'ONECLICK,RECURRING',
            \Adyen\Payment\Model\RecurringType::RECURRING => 'RECURRING'
        ];
    }

74 75 76 77
    /**
     * @desc return recurring types for configuration setting
     * @return array
     */
78 79
    public function getModes()
    {
80 81 82 83 84 85
        return [
            '1' => 'Test Mode',
            '0' => 'Production Mode'
        ];
    }

86 87 88 89
    /**
     * @desc return recurring types for configuration setting
     * @return array
     */
90 91 92 93 94 95 96
    public function getCaptureModes() {
        return [
            'auto' => 'immediate',
            'manual' => 'manual'
        ];
    }

97 98 99 100
    /**
     * @desc return recurring types for configuration setting
     * @return array
     */
101 102 103 104 105 106 107
    public function getPaymentRoutines() {
        return [
            'single' => 'Single Page Payment Routine',
            'multi' => 'Multi-page Payment Routine'
        ];
    }

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
    /**
     * Return the formatted currency. Adyen accepts the currency in multiple formats.
     * @param $amount
     * @param $currency
     * @return string
     */
    public function formatAmount($amount, $currency)
    {
        switch($currency) {
            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 = 0;
                break;
            case "MRO":
                $format = 1;
                break;
            case "BHD":
            case "JOD":
            case "KWD":
            case "OMR":
            case "LYD":
            case "TND":
                $format = 3;
                break;
            default:
                $format = 2;
                break;
        }

        return number_format($amount, $format, '', '');
    }

156 157 158 159 160
    /**
     * @param $amount
     * @param $currency
     * @return float
     */
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
    public function originalAmount($amount, $currency)
    {
        // check the format
        switch($currency) {
            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);
    }

203 204 205 206 207 208 209
    /**
     * Street format
     * @param type $address
     * @return array
     */
    public function getStreet($address)
    {
210 211 212 213
        if (empty($address)) {
            return false;
        }

214 215 216
        $street = self::formatStreet($address->getStreet());
        $streetName = $street['0'];
        unset($street['0']);
217 218
        $streetNr = implode(' ', $street);
        return (['name' => $streetName, 'house_number' => $streetNr]);
219 220 221 222 223 224 225 226 227 228 229 230 231 232
    }

    /**
     * Fix this one string street + number
     * @example street + number
     * @param type $street
     * @return type $street
     */
    static public function formatStreet($street)
    {
        if (count($street) != 1) {
            return $street;
        }
        preg_match('/((\s\d{0,10})|(\s\d{0,10}\w{1,3}))$/i', $street['0'], $houseNumber, PREG_OFFSET_CAPTURE);
233
        if (!empty($houseNumber['0'])) {
234 235
            $_houseNumber = trim($houseNumber['0']['0']);
            $position = $houseNumber['0']['1'];
236 237
            $streetName = trim(substr($street['0'], 0, $position));
            $street = [$streetName, $_houseNumber];
238 239 240 241 242
        }
        return $street;
    }


243 244 245 246 247 248
    /**
     * @desc gives back global configuration values
     * @param $field
     * @param null $storeId
     * @return mixed
     */
249 250 251 252 253
    public function getAdyenAbstractConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_abstract', $storeId);
    }

254 255 256 257 258 259
    /**
     * @desc gives back global configuration values as boolean
     * @param $field
     * @param null $storeId
     * @return mixed
     */
260 261 262 263 264
    public function getAdyenAbstractConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_abstract', $storeId, true);
    }

265 266 267 268 269 270
    /**
     * @desc Gives back adyen_cc configuration values
     * @param $field
     * @param null $storeId
     * @return mixed
     */
271 272 273 274 275
    public function getAdyenCcConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_cc', $storeId);
    }

276 277 278 279 280 281
    /**
     * @desc Gives back adyen_cc configuration values as flag
     * @param $field
     * @param null $storeId
     * @return mixed
     */
282 283 284 285 286
    public function getAdyenCcConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_cc', $storeId, true);
    }

287 288 289 290 291 292
    /**
     * @desc Gives back adyen_hpp configuration values
     * @param $field
     * @param null $storeId
     * @return mixed
     */
293 294 295 296 297
    public function getAdyenHppConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_hpp', $storeId);
    }

298 299 300 301 302 303
    /**
     * @desc Gives back adyen_hpp configuration values as flag
     * @param $field
     * @param null $storeId
     * @return mixed
     */
304 305 306 307 308
    public function getAdyenHppConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_hpp', $storeId, true);
    }

309
    /**
rikterbeek's avatar
rikterbeek committed
310
     * @desc Gives back adyen_oneclick configuration values
311 312 313 314 315 316 317 318 319 320
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenOneclickConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_oneclick', $storeId);
    }

    /**
rikterbeek's avatar
rikterbeek committed
321
     * @desc Gives back adyen_oneclick configuration values as flag
322 323 324 325 326 327 328 329 330
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenOneclickConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_oneclick', $storeId, true);
    }

rikterbeek's avatar
rikterbeek committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    /**
     * @desc Gives back adyen_pos configuration values
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenPosConfigData($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pos', $storeId);
    }

    /**
     * @desc Gives back adyen_pos configuration values as flag
     * @param $field
     * @param null $storeId
     * @return mixed
     */
    public function getAdyenPosConfigDataFlag($field, $storeId = null)
    {
        return $this->getConfigData($field, 'adyen_pos', $storeId, true);
    }

353 354 355 356
    /**
     * @desc Retrieve decrypted hmac key
     * @return string
     */
357 358 359 360 361 362 363 364 365 366 367 368 369
    public function getHmac()
    {
        switch ($this->isDemoMode()) {
            case true:
                $secretWord =  $this->_encryptor->decrypt(trim($this->getAdyenHppConfigData('hmac_test')));
                break;
            default:
                $secretWord = $this->_encryptor->decrypt(trim($this->getAdyenHppConfigData('hmac_live')));
                break;
        }
        return $secretWord;
    }

370 371 372 373
    /**
     * @desc Check if configuration is set to demo mode
     * @return mixed
     */
374 375 376 377 378
    public function isDemoMode()
    {
        return $this->getAdyenAbstractConfigDataFlag('demo_mode');
    }

379 380 381 382
    /**
     * @desc Retrieve the decrypted notification password
     * @return string
     */
383 384 385 386 387
    public function getNotificationPassword()
    {
        return $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData('notification_password')));
    }

388 389 390 391 392 393
    /**
     * @desc Retrieve the webserver username
     * @return string
     */
    public function getWsUsername()
    {
394
        if ($this->isDemoMode()) {
395 396 397 398 399 400 401 402 403 404 405 406 407
            $wsUsername =  trim($this->getAdyenAbstractConfigData('ws_username_test'));
        } else {
            $wsUsername = trim($this->getAdyenAbstractConfigData('ws_username_live'));
        }
        return $wsUsername;
    }

    /**
     * @desc Retrieve the webserver password
     * @return string
     */
    public function getWsPassword()
    {
408
        if ($this->isDemoMode()) {
409 410 411 412 413 414 415 416 417 418 419 420 421
            $wsPassword = $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData('ws_password_test')));
        } else {
            $wsPassword = $this->_encryptor->decrypt(trim($this->getAdyenAbstractConfigData('ws_password_live')));
        }
        return $wsPassword;
    }

    /**
     * @desc Retrieve the webserver url defined in the config.xlm only
     * @return string
     */
    public function getWsUrl()
    {
422
        if ($this->isDemoMode()) {
423 424 425 426 427 428 429 430 431 432 433
            $url = $this->getAdyenAbstractConfigData('ws_url_test');
        } else {
            $url =  $this->getAdyenAbstractConfigData('ws_url_live');
        }
        return $url;
    }

    /**
     * @desc Cancels the order
     * @param $order
     */
434 435 436 437 438 439 440 441 442 443 444 445
    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:
446
                if ($order->canCancel()) {
447 448 449 450 451 452
                    $order->cancel()->save();
                }
                break;
        }
    }

453 454 455 456 457 458 459 460 461 462
    /**
     * 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();

463
        if (isset($ccTypesMapper[$ccType])) {
464 465 466 467 468 469
            $ccType = $ccTypesMapper[$ccType]['code'];
        }

        return $ccType;
    }

470 471 472
    /**
     * @return array
     */
473 474 475
    public function getCcTypesAltData()
    {
        $adyenCcTypes =  $this->getAdyenCcTypes();
476
        $types = [];
477 478
        foreach ($adyenCcTypes as $key => $data) {
            $types[$data['code_alt']] = $data;
479
            $types[$data['code_alt']]['code'] = $key;
480 481 482 483
        }
        return $types;
    }

484 485 486
    /**
     * @return mixed
     */
487 488 489 490 491
    public function getAdyenCcTypes()
    {
        return $this->_dataStorage->get('adyen_credit_cards');
    }

492
    /**
493 494 495 496 497 498
     * @desc Retrieve information from payment configuration
     * @param $field
     * @param $paymentMethodCode
     * @param $storeId
     * @param bool|false $flag
     * @return bool|mixed
499 500 501 502 503 504
     */
    public function getConfigData($field, $paymentMethodCode, $storeId, $flag = false)
    {
        $path = 'payment/' . $paymentMethodCode . '/' . $field;

        if(!$flag) {
505
            return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
506
        } else {
507
            return $this->scopeConfig->isSetFlag($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
508 509
        }
    }
rikterbeek's avatar
rikterbeek committed
510
}