PaymentRequest.php 8.47 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 24 25
 */

namespace Adyen\Payment\Model\Api;

26 27 28
use Magento\Framework\DataObject;

class PaymentRequest extends DataObject
rikterbeek's avatar
rikterbeek committed
29
{
30 31 32 33

    /**
     * @var \Magento\Framework\Encryption\EncryptorInterface
     */
rikterbeek's avatar
rikterbeek committed
34
    protected $_encryptor;
35 36 37 38

    /**
     * @var \Adyen\Payment\Helper\Data
     */
39
    protected $_adyenHelper;
rikterbeek's avatar
rikterbeek committed
40

41 42 43 44 45
    /**
     * @var \Adyen\Payment\Logger\AdyenLogger
     */
    protected $_adyenLogger;

46 47 48 49 50
    /**
     * @var \Adyen\Client
     */
    protected $_client;

51 52 53 54 55
    /**
     * @var \Adyen\Payment\Model\RecurringType
     */
    protected $_recurringType;

56 57 58 59 60
    /**
     * @var \Magento\Framework\App\State
     */
    protected $_appState;

61
    /**
62 63 64
     * PaymentRequest constructor.
     *
     * @param \Magento\Framework\Model\Context $context
65 66 67
     * @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
     * @param \Adyen\Payment\Helper\Data $adyenHelper
     * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
68
     * @param \Adyen\Payment\Model\RecurringType $recurringType
69 70
     * @param array $data
     */
rikterbeek's avatar
rikterbeek committed
71
    public function __construct(
72
        \Magento\Framework\Model\Context $context,
rikterbeek's avatar
rikterbeek committed
73
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
74
        \Adyen\Payment\Helper\Data $adyenHelper,
75
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
76
        \Adyen\Payment\Model\RecurringType $recurringType,
rikterbeek's avatar
rikterbeek committed
77 78 79
        array $data = []
    ) {
        $this->_encryptor = $encryptor;
80
        $this->_adyenHelper = $adyenHelper;
81
        $this->_adyenLogger = $adyenLogger;
82
        $this->_recurringType = $recurringType;
83
        $this->_appState = $context->getAppState();
84 85 86 87 88

        // initialize client
        $webserviceUsername = $this->_adyenHelper->getWsUsername();
        $webservicePassword = $this->_adyenHelper->getWsPassword();

89
        $client = new \Adyen\Client();
90 91 92 93
        $client->setApplicationName("Magento 2 plugin");
        $client->setUsername($webserviceUsername);
        $client->setPassword($webservicePassword);

94
        if ($this->_adyenHelper->isDemoMode()) {
95
            $client->setEnvironment(\Adyen\Environment::TEST);
96
        } else {
97
            $client->setEnvironment(\Adyen\Environment::LIVE);
98 99 100 101 102 103
        }

        // assign magento log
        $client->setLogger($adyenLogger);

        $this->_client = $client;
rikterbeek's avatar
rikterbeek committed
104 105
    }

106 107 108 109 110
    /**
     * @param $payment
     * @return mixed
     * @throws \Magento\Framework\Exception\LocalizedException
     */
111 112 113
    public function authorise3d($payment)
    {
        $order = $payment->getOrder();
114 115
        $storeId = $order->getStoreId();
        $merchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData("merchant_account", $storeId);
116 117 118 119 120
        $shopperIp = $order->getRemoteIp();

        $md = $payment->getAdditionalInformation('md');
        $paResponse = $payment->getAdditionalInformation('paResponse');

121
        $browserInfo = ['userAgent' => $_SERVER['HTTP_USER_AGENT'], 'acceptHeader' => $_SERVER['HTTP_ACCEPT']];
122
        $request = [
123 124 125 126 127
            "merchantAccount" => $merchantAccount,
            "browserInfo" => $browserInfo,
            "md" => $md,
            "paResponse" => $paResponse,
            "shopperIP" => $shopperIp
128
        ];
129

130 131 132
        try {
            $service = new \Adyen\Service\Payment($this->_client);
            $result = $service->authorise3D($request);
133
        } catch(\Adyen\AdyenException $e) {
134
            throw new \Magento\Framework\Exception\LocalizedException(__('3D secure failed'));
135 136 137
        }

        return $result;
138 139
    }

140 141 142 143 144 145
    /**
     * @param $shopperReference
     * @param $storeId
     * @return array
     * @throws \Exception
     */
146 147
    public function getRecurringContractsForShopper($shopperReference, $storeId)
    {
148
        $recurringContracts = [];
149 150 151 152 153 154
        $recurringTypes = $this->_recurringType->getAllowedRecurringTypesForListRecurringCall();

        foreach ($recurringTypes as $recurringType) {

            try {
                // merge ONECLICK and RECURRING into one record with recurringType ONECLICK,RECURRING
155 156 157 158 159 160
                $listRecurringContractByType =
                    $this->listRecurringContractByType($shopperReference, $storeId, $recurringType);

                if (isset($listRecurringContractByType['details'])) {
                    foreach ($listRecurringContractByType['details'] as $recurringContractDetails) {
                        if (isset($recurringContractDetails['RecurringDetail'])) {
161 162
                            $recurringContract = $recurringContractDetails['RecurringDetail'];

163
                            if (isset($recurringContract['recurringDetailReference'])) {
164 165
                                $recurringDetailReference = $recurringContract['recurringDetailReference'];
                                // check if recurring reference is already in array
166 167 168 169 170 171 172
                                if (isset($recurringContracts[$recurringDetailReference])) {
                                    /*
                                     * recurring reference already exists so recurringType is possible
                                     * for ONECLICK and RECURRING
                                     */
                                    $recurringContracts[$recurringDetailReference]['recurring_type'] =
                                        "ONECLICK,RECURRING";
173 174 175 176 177 178 179 180
                                } else {
                                    $recurringContracts[$recurringDetailReference] = $recurringContract;
                                }
                            }
                        }
                    }
                }
            } catch (\Exception $exception) {
181 182 183
                // log exception
                $this->_adyenLogger->addError($exception);
                throw($exception);
184 185 186 187 188
            }
        }
        return $recurringContracts;
    }

189 190 191 192 193 194
    /**
     * @param $shopperReference
     * @param $storeId
     * @param $recurringType
     * @return mixed
     */
195 196 197 198
    public function listRecurringContractByType($shopperReference, $storeId, $recurringType)
    {
        // rest call to get list of recurring details
        $contract = ['contract' => $recurringType];
199
        $request = [
200 201 202
            "merchantAccount"    => $this->_adyenHelper->getAdyenAbstractConfigData('merchant_account', $storeId),
            "shopperReference"   => $shopperReference,
            "recurring" => $contract,
203
        ];
204 205 206 207 208 209 210

        // call lib
        $service = new \Adyen\Service\Recurring($this->_client);
        $result = $service->listRecurringDetails($request);

        return $result;
    }
211

212 213 214
    /**
     * Disable a recurring contract
     *
215 216
     * @param $recurringDetailReference
     * @param $shopperReference
217
     * @param $storeId
218
     * @return bool
219
     * @throws \Magento\Framework\Exception\LocalizedException
220
     */
221
    public function disableRecurringContract($recurringDetailReference, $shopperReference, $storeId)
222
    {
223
        $merchantAccount = $this->_adyenHelper->getAdyenAbstractConfigData("merchant_account", $storeId);
224

225
        $request = [
226 227 228
            "merchantAccount" => $merchantAccount,
            "shopperReference" => $shopperReference,
            "recurringDetailReference" => $recurringDetailReference
229
        ];
230 231 232 233 234 235 236 237 238 239

        // call lib
        $service = new \Adyen\Service\Recurring($this->_client);

        try {
            $result = $service->disable($request);
        } catch(\Exception $e) {
            $this->_adyenLogger->info($e->getMessage());
        }

240
        if (isset($result['response']) && $result['response'] == '[detail-successfully-disabled]') {
241 242 243 244 245
            return true;
        } else {
            throw new \Magento\Framework\Exception\LocalizedException(__('Failed to disable this contract'));
        }
    }
rikterbeek's avatar
rikterbeek committed
246
}