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

AdyenOneclickConfigProvider.php 8.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * 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>
 */

24
namespace Adyen\Payment\Model\Ui;
25

26
use Magento\Checkout\Model\ConfigProviderInterface;
27 28
use Magento\Payment\Helper\Data as PaymentHelper;

29
class AdyenOneclickConfigProvider implements ConfigProviderInterface
30 31
{

32 33
    const CODE = 'adyen_oneclick';

34 35 36 37 38 39 40
    /**
     * @var Config
     */
    protected $config;


    /**
41
     * @var \Adyen\Payment\Helper\Data
42
     */
43
    protected $_adyenHelper;
44 45

    /**
46
     * @var \Magento\Framework\App\RequestInterface
47
     */
48
    protected $_request;
49 50 51 52 53 54 55 56 57 58 59 60

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $_session;

    /**
61
     * @var \Magento\Store\Model\StoreManagerInterface
62
     */
63
    protected $_storeManager;
64 65

    /**
66
     * @var \Magento\Framework\UrlInterface
67
     */
68 69 70 71 72 73
    protected $_urlBuilder;

    /**
     * @var \Magento\Payment\Model\CcConfig
     */
    private $ccConfig;
74 75

    /**
76
     * AdyenOneclickConfigProvider constructor.
77
     *
78
     * @param \Adyen\Payment\Helper\Data $adyenHelper
79
     * @param \Magento\Framework\App\RequestInterface $request
80 81 82
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magento\Checkout\Model\Session $session
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
83
     * @param \Magento\Framework\UrlInterface $urlBuilder
84 85 86
     */
    public function __construct(
        \Adyen\Payment\Helper\Data $adyenHelper,
87
        \Magento\Framework\App\RequestInterface $request,
88 89
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $session,
90 91 92
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Payment\Model\CcConfig $ccConfig
93 94
    ) {
        $this->_adyenHelper = $adyenHelper;
95
        $this->_request = $request;
96 97 98
        $this->_customerSession = $customerSession;
        $this->_session = $session;
        $this->_storeManager = $storeManager;
99 100
        $this->_urlBuilder = $urlBuilder;
        $this->ccConfig = $ccConfig;
101 102
    }

103 104 105
    /**
     * @return array
     */
106 107
    public function getConfig()
    {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        // set to active
        $config = [
            'payment' => [
                self::CODE => [
                    'isActive' => true,
                    'redirectUrl' => $this->_urlBuilder->getUrl(
                        'adyen/process/validate3d/', ['_secure' => $this->_getRequest()->isSecure()])
                ]
            ]
        ];

        $methodCode = self::CODE;

        $config = array_merge_recursive($config, [
            'payment' => [
                'ccform' => [
                    'availableTypes' => [$methodCode => $this->getCcAvailableTypes($methodCode)],
                    'months' => [$methodCode => $this->getCcMonths()],
                    'years' => [$methodCode => $this->getCcYears()],
                    'hasVerification' => [$methodCode => $this->hasVerification($methodCode)],
                    'cvvImageUrl' => [$methodCode => $this->getCvvImageUrl()]
                ]
            ]
        ]);
132 133 134

        $demoMode = $this->_adyenHelper->getAdyenAbstractConfigDataFlag('demo_mode');

135
        if ($demoMode) {
136 137 138 139 140 141 142 143 144
            $cseKey = $this->_adyenHelper->getAdyenCcConfigData('cse_publickey_test');
        } else {
            $cseKey = $this->_adyenHelper->getAdyenCcConfigData('cse_publickey_live');
        }

        $cseEnabled = $this->_adyenHelper->getAdyenCcConfigDataFlag('cse_enabled');

        $recurringType = $this->_adyenHelper->getAdyenAbstractConfigData('recurring_type');
        $canCreateBillingAgreement = false;
145
        if ($recurringType == "ONECLICK" || $recurringType == "ONECLICK,RECURRING") {
146 147 148 149 150 151 152 153 154
            $canCreateBillingAgreement = true;
        }

        $config['payment'] ['adyenOneclick']['cseKey'] = $cseKey;
        $config['payment'] ['adyenOneclick']['cseEnabled'] = $cseEnabled;
        $config['payment'] ['adyenOneclick']['cseEnabled'] = $cseEnabled;
        $config['payment']['adyenOneclick']['generationTime'] = date("c");
        $config['payment']['adyenOneclick']['canCreateBillingAgreement'] = $canCreateBillingAgreement;

155
        $recurringContractType = $this->_getRecurringContractType();
156

157 158 159 160 161
        $config['payment'] ['adyenOneclick']['billingAgreements'] = $this->getAdyenOneclickPaymentMethods();
        if ($recurringContractType == \Adyen\Payment\Model\RecurringType::ONECLICK) {
            $config['payment'] ['adyenOneclick']['hasCustomerInteraction'] = true;
        } else {
            $config['payment'] ['adyenOneclick']['hasCustomerInteraction'] = false;
162
        }
163

164 165 166
        return $config;
    }

167 168 169 170
    /**
     * @return array
     * @throws \Magento\Framework\Exception\LocalizedException
     */
171 172 173 174 175
    public function getAdyenOneclickPaymentMethods()
    {
        $billingAgreements = [];
        if ($this->_customerSession->isLoggedIn()) {
            $customerId = $this->_customerSession->getCustomerId();
176 177 178
            $storeId = $this->_storeManager->getStore()->getId();
            $grandTotal = $this->_getQuote()->getGrandTotal();
            $recurringType = $this->_getRecurringContractType();
179

180 181 182 183 184 185
            $billingAgreements = $this->_adyenHelper->getOneClickPaymentMethods(
                $customerId, 
                $storeId, 
                $grandTotal, 
                $recurringType
            );
186 187 188 189
        }
        return $billingAgreements;
    }

190 191 192
    /**
     * @return mixed
     */
193 194 195 196
    protected function _getRecurringContractType()
    {
        return $this->_adyenHelper->getAdyenOneclickConfigData('recurring_payment_type');
    }
197 198 199 200 201 202 203 204

    /**
     * @return \Magento\Quote\Model\Quote
     */
    protected function _getQuote()
    {
        return $this->_session->getQuote();
    }
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

    /**
     * Retrieve availables credit card types
     *
     * @param string $methodCode
     * @return array
     */
    protected function getCcAvailableTypes($methodCode)
    {
        $types = [];
        $ccTypes = $this->_adyenHelper->getAdyenCcTypes();
        $availableTypes = $this->_adyenHelper->getAdyenCcConfigData('cctypes');
        if ($availableTypes) {
            $availableTypes = explode(',', $availableTypes);
            foreach (array_keys($ccTypes) as $code) {
                if (in_array($code, $availableTypes)) {
                    $types[$code] = $ccTypes[$code]['name'];
                }
            }
        }

        return $types;
    }



    /**
     * Retrieve credit card expire months
     *
     * @return array
     */
    protected function getCcMonths()
    {
        return $this->ccConfig->getCcMonths();
    }

    /**
     * Retrieve credit card expire years
     *
     * @return array
     */
    protected function getCcYears()
    {
        return $this->ccConfig->getCcYears();
    }

    /**
     * Has verification is always true
     *
     * @return bool
     */
    protected function hasVerification()
    {
        return $this->_adyenHelper->getAdyenCcConfigData('useccv');
    }

    /**
     * Retrieve CVV tooltip image url
     *
     * @return string
     */
    protected function getCvvImageUrl()
    {
        return $this->ccConfig->getCvvImageUrl();
    }


    /**
     * Retrieve request object
     *
     * @return \Magento\Framework\App\RequestInterface
     */
    protected function _getRequest()
    {
        return $this->_request;
    }
281
}