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 6.73 KB
Newer Older
rikterbeek's avatar
rikterbeek committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Adyen\Payment\Model\Api;

class PaymentRequest extends \Magento\Framework\Object
{
    protected $_scopeConfig;
    protected $_code;
    protected $_logger;
    protected $_encryptor;
15
    protected $_adyenHelper;
rikterbeek's avatar
rikterbeek committed
16 17 18 19 20

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Encryption\EncryptorInterface $encryptor,
21
        \Adyen\Payment\Helper\Data $adyenHelper,
rikterbeek's avatar
rikterbeek committed
22 23 24 25 26 27
        array $data = []
    ) {
        $this->_scopeConfig = $scopeConfig;
        $this->_logger = $logger;
        $this->_encryptor = $encryptor;
        $this->_code = "adyen_cc";
28
        $this->_adyenHelper = $adyenHelper;
rikterbeek's avatar
rikterbeek committed
29 30 31 32 33 34 35 36 37 38
    }

    /**
     * Retrieve information from payment configuration
     *
     * @param string $field
     * @param int|string|null|\Magento\Store\Model\Store $storeId
     *
     * @return mixed
     */
39
    public function getConfigData($field, $paymentMethodCode = "adyen_abstract", $storeId = null)
rikterbeek's avatar
rikterbeek committed
40 41 42 43
    {
        if (null === $storeId) {
            $storeId = $this->getStore();
        }
44 45 46

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

50
    public function fullApiRequest($payment)
rikterbeek's avatar
rikterbeek committed
51 52
    {
        $order = $payment->getOrder();
53 54 55 56 57
        $amount = $order->getGrandTotal();
        $customerEmail = $order->getCustomerEmail();
        $shopperIp = $order->getRemoteIp();
        $orderCurrencyCode = $order->getOrderCurrencyCode();
        $merchantAccount = $this->getConfigData("merchant_account");
rikterbeek's avatar
rikterbeek committed
58

59
        $this->_logger->critical("fullApiRequest1 ");
rikterbeek's avatar
rikterbeek committed
60 61 62 63

        $request = array(
            "action" => "Payment.authorise",
            "paymentRequest.merchantAccount" => $merchantAccount,
64 65
            "paymentRequest.amount.currency" => $orderCurrencyCode,
            "paymentRequest.amount.value" => $this->_adyenHelper->formatAmount($amount, $orderCurrencyCode),
66
            "paymentRequest.reference" => $order->getIncrementId(),
67 68 69 70
            "paymentRequest.shopperIP" => $shopperIp,
            "paymentRequest.shopperEmail" => $customerEmail,
            "paymentRequest.shopperReference" => $order->getIncrementId(),
            "paymentRequest.fraudOffset" => "0"
rikterbeek's avatar
rikterbeek committed
71 72
        );

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 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
        $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
118 119 120 121 122 123 124 125 126
        $this->_logger->critical("fullApiRequest");
        $this->_logger->critical(print_r($request, true));

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


    protected function _apiRequest($request) {

127 128
        // TODO make differents between test and live
        $webserviceUsername = $this->getConfigData("ws_username_test");
rikterbeek's avatar
rikterbeek committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        $webservicePassword = $this->decryptPassword($this->getConfigData("webservice_password")); // DECODE!!

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://pal-test.adyen.com/pal/adapter/httppost");
        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));
        }


152 153 154 155 156 157

//        throw new \Magento\Framework\Exception\LocalizedException(__('HTTP Status code' . print_r($results, true)));

        parse_str($results, $results);

        $this->_logger->critical("result is" . print_r($results,true));
rikterbeek's avatar
rikterbeek committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

        curl_close($ch);

        return $results;
    }

    /**
     * Decrypt password
     *
     * @param   string $password
     * @return  string
     */
    public function decryptPassword($password)
    {
        return $this->_encryptor->decrypt($password);
    }
}