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

PaymentRequest.php 8.78 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 26 27 28 29 30 31
 */

namespace Adyen\Payment\Model\Api;

class PaymentRequest extends \Magento\Framework\Object
{
    protected $_scopeConfig;
    protected $_code;
    protected $_logger;
    protected $_encryptor;
32
    protected $_adyenHelper;
rikterbeek's avatar
rikterbeek committed
33 34 35 36 37

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
38
        \Adyen\Payment\Helper\Data $adyenHelper,
rikterbeek's avatar
rikterbeek committed
39 40 41 42 43 44
        array $data = []
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->_logger = $logger;
        $this->_encryptor = $encryptor;
        $this->_code = "adyen_cc";
45
        $this->_adyenHelper = $adyenHelper;
rikterbeek's avatar
rikterbeek committed
46 47 48 49 50 51 52 53 54 55
    }

    /**
     * Retrieve information from payment configuration
     *
     * @param string $field
     * @param int|string|null|\Magento\Store\Model\Store $storeId
     *
     * @return mixed
     */
56
    public function getConfigData($field, $paymentMethodCode = "adyen_abstract", $storeId = null)
rikterbeek's avatar
rikterbeek committed
57 58 59 60
    {
        if (null === $storeId) {
            $storeId = $this->getStore();
        }
61 62 63

        // $this->_code to get current methodcode
        $path = 'payment/' . $paymentMethodCode . '/' . $field;
rikterbeek's avatar
rikterbeek committed
64 65 66
        return $this->_scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId);
    }

67
    public function fullApiRequest($payment)
rikterbeek's avatar
rikterbeek committed
68 69
    {
        $order = $payment->getOrder();
70 71 72 73 74
        $amount = $order->getGrandTotal();
        $customerEmail = $order->getCustomerEmail();
        $shopperIp = $order->getRemoteIp();
        $orderCurrencyCode = $order->getOrderCurrencyCode();
        $merchantAccount = $this->getConfigData("merchant_account");
rikterbeek's avatar
rikterbeek committed
75

76
        $this->_logger->critical("fullApiRequest1 ");
rikterbeek's avatar
rikterbeek committed
77 78 79 80

        $request = array(
            "action" => "Payment.authorise",
            "paymentRequest.merchantAccount" => $merchantAccount,
81 82
            "paymentRequest.amount.currency" => $orderCurrencyCode,
            "paymentRequest.amount.value" => $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode),
83
            "paymentRequest.reference" => $order->getIncrementId(),
84 85 86
            "paymentRequest.shopperIP" => $shopperIp,
            "paymentRequest.shopperEmail" => $customerEmail,
            "paymentRequest.shopperReference" => $order->getIncrementId(),
87 88 89
            "paymentRequest.fraudOffset" => "0",
            "paymentRequest.browserInfo.userAgent" => $_SERVER['HTTP_USER_AGENT'],
            "paymentRequest.browserInfo.acceptHeader" => $_SERVER['HTTP_ACCEPT']
rikterbeek's avatar
rikterbeek committed
90 91
        );

92 93


94 95 96 97 98 99 100 101 102 103 104 105 106 107 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
        $billingAddress = $order->getBillingAddress();

        if($billingAddress)
        {
            $addressArray = $this->_adyenHelper->getStreet($billingAddress);
            $requestBilling = array("paymentRequest.card.billingAddress.street" => $addressArray['name'],
                "paymentRequest.card.billingAddress.postalCode" => $billingAddress->getPostcode(),
                "paymentRequest.card.billingAddress.city" => $billingAddress->getCity(),
                "paymentRequest.card.billingAddress.houseNumberOrName" => $addressArray['house_number'],
                "paymentRequest.card.billingAddress.stateOrProvince" => $billingAddress->getRegionCode(),
                "paymentRequest.card.billingAddress.country" => $billingAddress->getCountryId()
            );
            $request = array_merge($request, $requestBilling);
        }

        $deliveryAddress = $order->getDeliveryAddress();
        if($deliveryAddress)
        {
            $addressArray = $this->_adyenHelper->getStreet($deliveryAddress);

            $requestDelivery = array("paymentRequest.card.billingAddress.street" => $addressArray['name'],
                "paymentRequest.card.billingAddress.postalCode" => $deliveryAddress->getPostcode(),
                "paymentRequest.card.billingAddress.city" => $deliveryAddress->getCity(),
                "paymentRequest.card.billingAddress.houseNumberOrName" => $addressArray['house_number'],
                "paymentRequest.card.billingAddress.stateOrProvince" => $deliveryAddress->getRegionCode(),
                "paymentRequest.card.billingAddress.country" => $deliveryAddress->getCountryId()
            );
            $request = array_merge($request, $requestDelivery);
        }


        // TODO get CSE setting
        $cseEnabled = true;
        if($cseEnabled) {
            $request['paymentRequest.additionalData.card.encrypted.json'] = $payment->getAdditionalInformation("encrypted_data");
        } else {
            $requestCreditCardDetails = array("paymentRequest.card.expiryMonth" => $payment->getCcExpMonth(),
                "paymentRequest.card.expiryYear" => $payment->getCcExpYear(),
                "paymentRequest.card.holderName" => $payment->getCcOwner(),
                "paymentRequest.card.number" => $payment->getCcNumber(),
                "paymentRequest.card.cvc" => $payment->getCcCid(),
            );
            $request = array_merge($request, $requestCreditCardDetails);
        }

rikterbeek's avatar
rikterbeek committed
139 140 141 142 143 144 145
        $this->_logger->critical("fullApiRequest");
        $this->_logger->critical(print_r($request, true));

        return $this->_apiRequest($request);
    }


146 147
    protected function _apiRequest($request)
    {
rikterbeek's avatar
rikterbeek committed
148

149 150 151 152 153 154 155 156 157 158
        // Use test or live credentials depends on demo mode
        if($this->_adyenHelper->isDemoMode()) {
            $webserviceUsername = $this->getConfigData("ws_username_test");
            $webservicePassword = $this->decryptPassword($this->getConfigData("ws_password_test"));
            $url = "https://pal-test.adyen.com/pal/adapter/httppost";
        } else {
            $webserviceUsername = $this->getConfigData("ws_username_live");
            $webservicePassword = $this->decryptPassword($this->getConfigData("ws_password_live"));
            $url = "https://pal-live.adyen.com/pal/adapter/httppost";
        }
rikterbeek's avatar
rikterbeek committed
159 160

        $ch = curl_init();
161
        curl_setopt($ch, CURLOPT_URL, $url);
rikterbeek's avatar
rikterbeek committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC  );
        curl_setopt($ch, CURLOPT_USERPWD, $webserviceUsername.":".$webservicePassword);
        curl_setopt($ch, CURLOPT_POST,count($request));
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($request));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

        if ($httpStatus != 200) {
            throw new \Magento\Framework\Exception\LocalizedException(__('HTTP Status code' . $httpStatus . " " . $webserviceUsername . ":" . $webservicePassword));
        }

        if ($results === false) {
            throw new \Magento\Framework\Exception\LocalizedException(__('HTTP Status code' . $results));
        }

180 181 182
        parse_str($results, $results);

        $this->_logger->critical("result is" . print_r($results,true));
rikterbeek's avatar
rikterbeek committed
183 184 185 186 187 188

        curl_close($ch);

        return $results;
    }

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    public function authorise3d($payment)
    {
        $order = $payment->getOrder();
        $merchantAccount = $this->getConfigData("merchant_account");
        $shopperIp = $order->getRemoteIp();

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

        $request = array(
            "action" => "Payment.authorise3d",
            "paymentRequest3d.merchantAccount" => $merchantAccount,
            "paymentRequest3d.browserInfo.userAgent" => $_SERVER['HTTP_USER_AGENT'],
            "paymentRequest3d.browserInfo.acceptHeader" => $_SERVER['HTTP_ACCEPT'],
            "paymentRequest3d.md" => $md,
            "paymentRequest3d.paResponse" => $paResponse,
            "paymentRequest3d.shopperIP" => $shopperIp
        );

        return $this->_apiRequest($request);
    }

rikterbeek's avatar
rikterbeek committed
211 212 213 214 215 216 217 218 219 220 221
    /**
     * Decrypt password
     *
     * @param   string $password
     * @return  string
     */
    public function decryptPassword($password)
    {
        return $this->_encryptor->decrypt($password);
    }
}