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

AdyenInitiateTerminalApi.php 7.06 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 24 25 26 27 28 29 30 31 32
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2018 Adyen B.V.
 * This file is open source and available under the MIT license.
 * See the LICENSE file for more info.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Model;

use Adyen\Payment\Api\AdyenInitiateTerminalApiInterface;
use Adyen\Payment\Model\Ui\AdyenPosCloudConfigProvider;
use Adyen\Util\Util;

class AdyenInitiateTerminalApi implements AdyenInitiateTerminalApiInterface
{
33 34 35 36
	/**
	 * @var \Adyen\Payment\Helper\Data 
	 */
    private $adyenHelper;
37

38 39 40 41 42 43 44 45 46
	/**
	 * @var \Adyen\Payment\Logger\AdyenLogger 
	 */
    private $adyenLogger;

	/**
	 * @var \Adyen\Client 
	 */
	protected $client;
47

48 49 50 51 52 53 54 55
    /**
     * @var int
     */
    protected $storeId;

    /**
     * @var \Magento\Checkout\Model\Session
     */
56
    protected $checkoutSession;
57 58 59 60 61

    /**
     * AdyenInitiateTerminalApi constructor.
     * @param \Adyen\Payment\Helper\Data $adyenHelper
     * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
stkams's avatar
stkams committed
62
     * @param \Magento\Checkout\Model\Session $_checkoutSession
63 64 65 66 67
     * @param array $data
     */
    public function __construct(
        \Adyen\Payment\Helper\Data $adyenHelper,
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
68
        \Magento\Checkout\Model\Session $checkoutSession,
69
        \Magento\Store\Model\StoreManagerInterface $storeManager,
70
        array $data = []
71 72
    )
    {
73 74 75
        $this->adyenHelper = $adyenHelper;
        $this->adyenLogger = $adyenLogger;
        $this->checkoutSession = $checkoutSession;
76
        $this->storeId = $storeManager->getStore()->getId();
77 78

        // initialize client
79 80
		$apiKey = $this->adyenHelper->getPosApiKey($this->storeId);
        $client = $this->adyenHelper->initializeAdyenClient($this->storeId, $apiKey);
81 82

        //Set configurable option in M2
83
        $posTimeout = $this->adyenHelper->getAdyenPosCloudConfigData('pos_timeout', $this->storeId);
84 85 86 87 88 89 90
        if (!empty($posTimeout)) {
            $client->setTimeout($posTimeout);
        }

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

91
        $this->client = $client;
92 93 94 95 96
    }

    /**
     * Trigger sync call on terminal
     * @return mixed
Aleffio's avatar
Aleffio committed
97
     * @throws \Exception
98 99 100
     */
    public function initiate()
    {
101
        $quote = $this->checkoutSession->getQuote();
102 103 104 105
        $payment = $quote->getPayment();
        $payment->setMethod(AdyenPosCloudConfigProvider::CODE);
        $reference = $quote->reserveOrderId()->getReservedOrderId();

106
        $service = $this->adyenHelper->createAdyenPosPaymentService($this->client);
107
        $transactionType = \Adyen\TransactionType::NORMAL;
108
        $poiId = $this->adyenHelper->getPoiId($this->storeId);
109 110
        $serviceID = date("dHis");
        $initiateDate = date("U");
111
        $timeStamper = date("Y-m-d") . "T" . date("H:i:s+00:00");
112 113
        $customerId = $quote->getCustomerId();

Rik ter Beek's avatar
Rik ter Beek committed
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
        $request = [
            'SaleToPOIRequest' =>
                [
                    'MessageHeader' =>
                        [
                            'MessageType' => 'Request',
                            'MessageClass' => 'Service',
                            'MessageCategory' => 'Payment',
                            'SaleID' => 'Magento2Cloud',
                            'POIID' => $poiId,
                            'ProtocolVersion' => '3.0',
                            'ServiceID' => $serviceID,
                        ],
                    'PaymentRequest' =>
                        [
                            'SaleData' =>
                                [
                                    'TokenRequestedType' => 'Customer',
                                    'SaleTransactionID' =>
                                        [
                                            'TransactionID' => $reference,
                                            'TimeStamp' => $timeStamper,
                                        ],
                                ],
                            'PaymentTransaction' =>
                                [
                                    'AmountsReq' =>
                                        [
                                            'Currency' => $quote->getCurrency()->getQuoteCurrencyCode(),
143
                                            'RequestedAmount' => doubleval($quote->getGrandTotal()),
Rik ter Beek's avatar
Rik ter Beek committed
144 145 146 147 148 149 150 151 152 153 154
                                        ],
                                ],
                            'PaymentData' =>
                                [
                                    'PaymentType' => $transactionType,
                                ],
                        ],
                ],
        ];

        // If customer exists add it into the request to store request
155 156
        if (!empty($customerId)) {
            $shopperEmail = $quote->getCustomerEmail();
157
            $recurringContract = $this->adyenHelper->getAdyenPosCloudConfigData('recurring_type', $this->storeId);
158

159
            if (!empty($recurringContract) && !empty($shopperEmail)) {
160
                $recurringDetails = [
161
                    'shopperEmail' => $shopperEmail,
162
                    'shopperReference' => strval($customerId),
163
                    'recurringContract' => $recurringContract
164
                ];
165
                $request['SaleToPOIRequest']['PaymentRequest']['SaleData']['SaleToAcquirerData'] = http_build_query($recurringDetails);
Rik ter Beek's avatar
Rik ter Beek committed
166
            }
167 168
        }

169 170
        $quote->getPayment()->getMethodInstance()->getInfoInstance()->setAdditionalInformation('serviceID',
            $serviceID);
171 172
        $quote->getPayment()->getMethodInstance()->getInfoInstance()->setAdditionalInformation('initiateDate',
            $initiateDate);
173 174

        try {
Rik ter Beek's avatar
Rik ter Beek committed
175
            $response = $service->runTenderSync($request);
176 177
        } catch (\Adyen\AdyenException $e) {
            //Not able to perform a payment
178
            $this->adyenLogger->addAdyenDebug("adyenexception");
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
            $response['error'] = $e->getMessage();
        } catch (\Exception $e) {
            //Probably timeout
            $quote->getPayment()->getMethodInstance()->getInfoInstance()->setAdditionalInformation('terminalResponse',
                null);
            $quote->save();
            $response['error'] = $e->getMessage();
            throw $e;
        }
        $quote->getPayment()->getMethodInstance()->getInfoInstance()->setAdditionalInformation('terminalResponse',
            $response);

        $quote->save();
        return $response;
    }
Aleffio's avatar
Aleffio committed
194
}