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

AdyenRequestMerchantSession.php 4.11 KB
Newer Older
Alessio Zampatti's avatar
Alessio Zampatti committed
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment Module
 *
 * Copyright (c) 2017 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\AdyenRequestMerchantSessionInterface;

class AdyenRequestMerchantSession implements AdyenRequestMerchantSessionInterface
{
    /**
     * @var \Adyen\Payment\Helper\Data
     */
    protected $_adyenHelper;

    /**
     * @var \Adyen\Payment\Logger\AdyenLogger
     */
    protected $_adyenLogger;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * AdyenRequestMerchantSession constructor.
     * @param \Adyen\Payment\Helper\Data $adyenHelper
     */
    public function __construct(
        \Adyen\Payment\Helper\Data $adyenHelper,
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_adyenHelper = $adyenHelper;
        $this->_adyenLogger = $adyenLogger;
        $this->_storeManager = $storeManager;
    }


    /**
     * Get the merchant Session from Apple to start Apple Pay transaction
     *
     * @return mixed
     */
    public function getMerchantSession()
    {
        // Works for test and live. Maybe we need to switch for validationUrl from callback event waiting for apple to respond
        $validationUrl = "https://apple-pay-gateway-cert.apple.com/paymentservices/startSession";

        // create a new cURL resource
        $ch = curl_init();

        $merchantIdentifier = $this->_adyenHelper->getAdyenApplePayMerchantIdentifier();
        $domainName = parse_url($this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))['host'];
        $displayName = $this->_storeManager->getStore()->getName();

        $data = '{
            "merchantIdentifier":"' . $merchantIdentifier . '",
            "domainName":"' . $domainName . '",
            "displayName":"' . $displayName . '"
        }';

84
        $this->_adyenLogger->addAdyenDebug("JSON Request is: " . print_r($data, true));
Alessio Zampatti's avatar
Alessio Zampatti committed
85 86 87 88 89 90 91 92 93 94

        curl_setopt($ch, CURLOPT_URL, $validationUrl);

        // location applepay certificates
        $fullPathLocationPEMFile = $this->_adyenHelper->getAdyenApplePayPemFileLocation();

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSLCERT, $fullPathLocationPEMFile);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
95
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
Alessio Zampatti's avatar
Alessio Zampatti committed
96 97
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data)
98
            ]);
Alessio Zampatti's avatar
Alessio Zampatti committed
99 100 101 102 103 104 105 106 107 108

        $result = curl_exec($ch);
        $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        // log the raw response
        $this->_adyenLogger->addAdyenDebug("JSON Response is: " . $result);

        // result not 200 throw error
        if ($httpStatus != 200 && $result) {
            $this->_adyenLogger->addAdyenDebug("Error Apple, API HTTP Status is: " . $httpStatus . " result is:" . $result);
109
        } elseif (!$result) {
Alessio Zampatti's avatar
Alessio Zampatti committed
110 111 112 113 114 115 116 117 118
            $errno = curl_errno($ch);
            $message = curl_error($ch);
            $msg = "(Network error [errno $errno]: $message)";
            $this->_adyenLogger->addAdyenDebug($msg);
        }

        curl_close($ch);
        return $result;
    }
119
}