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

CheckoutDataBuilder.php 10.9 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
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * 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>
 */

namespace Adyen\Payment\Gateway\Request;

use Magento\Payment\Gateway\Request\BuilderInterface;
use Adyen\Payment\Observer\AdyenHppDataAssignObserver;
28
use Adyen\Payment\Observer\AdyenBoletoDataAssignObserver;
29 30 31

class CheckoutDataBuilder implements BuilderInterface
{
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    /**
     * @var \Adyen\Payment\Helper\Data
     */
    private $adyenHelper;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    private $checkoutSession;

    /**
     * @var \Magento\Quote\Model\Quote
     */
    private $quote;
51 52 53 54 55 56

	/**
	 * @var \Magento\Tax\Model\Config
	 */
	protected $taxConfig;

57 58 59 60 61 62 63
    /**
     * CheckoutDataBuilder constructor.
     * @param \Adyen\Payment\Helper\Data $adyenHelper
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Tax\Model\Config $taxConfig
     */
64 65 66 67 68
	public function __construct(
		\Adyen\Payment\Helper\Data $adyenHelper,
		\Magento\Store\Model\StoreManagerInterface $storeManager,
		\Magento\Checkout\Model\Session $checkoutSession,
		\Magento\Tax\Model\Config $taxConfig
69 70 71 72 73 74 75 76
    )
    {
        $this->adyenHelper = $adyenHelper;
        $this->storeManager = $storeManager;
        $this->checkoutSession = $checkoutSession;
        $this->quote = $checkoutSession->getQuote();
        $this->taxConfig = $taxConfig;
    }
77 78 79 80 81 82 83 84 85 86 87

	/**
	 * @param array $buildSubject
	 * @return mixed
	 */
	public function build(array $buildSubject)
	{
		/** @var \Magento\Payment\Gateway\Data\PaymentDataObject $paymentDataObject */
		$paymentDataObject =\Magento\Payment\Gateway\Helper\SubjectReader::readPayment($buildSubject);
		$payment = $paymentDataObject->getPayment();
		$order = $payment->getOrder();
alexandros's avatar
alexandros committed
88
		$storeId = $order->getStoreId();
89 90
		$request = [];

91 92
        // do not send email
        $order->setCanSendNewEmailFlag(false);
93

94
        $request['paymentMethod']['type'] = $payment->getAdditionalInformation(AdyenHppDataAssignObserver::BRAND_CODE);
95

96 97 98 99
        // Additional data for payment methods with issuer list
        if ($payment->getAdditionalInformation(AdyenHppDataAssignObserver::ISSUER_ID)) {
            $request['paymentMethod']['issuer'] = $payment->getAdditionalInformation(AdyenHppDataAssignObserver::ISSUER_ID);
        }
100

101
        $request['returnUrl'] = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK) . 'adyen/process/result';
102

attilak's avatar
typo  
attilak committed
103
        // Additional data for ACH
attilak's avatar
attilak committed
104 105 106 107 108 109 110 111 112 113 114 115
        if ($payment->getAdditionalInformation("bankAccountNumber")) {
            $request['bankAccount']['bankAccountNumber'] = $payment->getAdditionalInformation("bankAccountNumber");
        }

        if ($payment->getAdditionalInformation("bankLocationId")) {
            $request['bankAccount']['bankLocationId'] = $payment->getAdditionalInformation("bankLocationId");
        }

        if ($payment->getAdditionalInformation("bankAccountOwnerName")) {
            $request['bankAccount']['ownerName'] = $payment->getAdditionalInformation("bankAccountOwnerName");
        }

116 117 118 119 120
		// Additional data for open invoice payment
		if ($payment->getAdditionalInformation("gender")) {
			$order->setCustomerGender(\Adyen\Payment\Model\Gender::getMagentoGenderFromAdyenGender(
				$payment->getAdditionalInformation("gender"))
			);
121
			$request['paymentMethod']['personalDetails']['gender'] = $payment->getAdditionalInformation("gender");
122 123
		}

124 125
        if ($payment->getAdditionalInformation("dob")) {
            $order->setCustomerDob($payment->getAdditionalInformation("dob"));
126

127
			$request['paymentMethod']['personalDetails']['dateOfBirth']= $this->adyenHelper->formatDate($payment->getAdditionalInformation("dob"), 'Y-m-d') ;
128 129 130 131
		}

		if ($payment->getAdditionalInformation("telephone")) {
			$order->getBillingAddress()->setTelephone($payment->getAdditionalInformation("telephone"));
132
			$request['paymentMethod']['personalDetails']['telephoneNumber']= $payment->getAdditionalInformation("telephone");
133 134
		}

135 136 137 138
        if ($payment->getAdditionalInformation("ssn")) {
            $request['paymentMethod']['personalDetails']['socialSecurityNumber']= $payment->getAdditionalInformation("ssn");
        }

139 140 141 142
        // Additional data for sepa direct debit
        if ($payment->getAdditionalInformation("ownerName")) {
            $request['paymentMethod']['sepa.ownerName'] = $payment->getAdditionalInformation("ownerName");
        }
143

144 145 146
        if ($payment->getAdditionalInformation("ibanNumber")) {
            $request['paymentMethod']['sepa.ibanNumber'] = $payment->getAdditionalInformation("ibanNumber");
        }
147 148

		if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod(
149
			$payment->getAdditionalInformation(AdyenHppDataAssignObserver::BRAND_CODE)
150 151 152
		) || $this->adyenHelper->isPaymentMethodAfterpayTouchMethod(
				$payment->getAdditionalInformation(AdyenHppDataAssignObserver::BRAND_CODE)
			)) {
153 154 155 156
			$openInvoiceFields = $this->getOpenInvoiceData($order);
			$request = array_merge($request, $openInvoiceFields);
		}

alexandros's avatar
alexandros committed
157 158 159 160 161 162 163 164 165 166 167 168 169
        //Boleto data
        if ($payment->getAdditionalInformation("social_security_number")) {
            $request['socialSecurityNumber'] = $payment->getAdditionalInformation("social_security_number");
        }

        if ($payment->getAdditionalInformation("firstname")) {
            $request['shopperName']['firstName'] = $payment->getAdditionalInformation("firstname");
        }

        if ($payment->getAdditionalInformation("lastName")) {
            $request['shopperName']['lastName'] = $payment->getAdditionalInformation("lastName");
        }

170
        if ($payment->getMethod() == \Adyen\Payment\Model\Ui\AdyenBoletoConfigProvider::CODE) {
alexandros's avatar
alexandros committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
            $boletoTypes = $this->adyenHelper->getAdyenBoletoConfigData('boletotypes');
            $boletoTypes = explode(',', $boletoTypes);

            if (count($boletoTypes) == 1) {
                $request['selectedBrand'] = $boletoTypes[0];
                $request['paymentMethod']['type'] = $boletoTypes[0];
            } else {
                $request['selectedBrand'] = $payment->getAdditionalInformation("boleto_type");
                $request['paymentMethod']['type'] = $payment->getAdditionalInformation("boleto_type");
            }

            $deliveryDays = (int)$this->adyenHelper->getAdyenBoletoConfigData("delivery_days", $storeId);
            $deliveryDays = (!empty($deliveryDays)) ? $deliveryDays : 5;
            $deliveryDate = date(
                "Y-m-d\TH:i:s ",
                mktime(
                    date("H"),
                    date("i"),
                    date("s"),
                    date("m"),
                    date("j") + $deliveryDays,
                    date("Y")
                )
            );

            $request['deliveryDate'] = $deliveryDate;

198 199
            $order->setCanSendNewEmailFlag(true);
        }
alexandros's avatar
alexandros committed
200 201
        return $request;
    }
202

203 204 205 206 207 208 209 210 211
    /**
     * @param $formFields
     * @return mixed
     */
    protected function getOpenInvoiceData($order)
    {
        $formFields = [
            'lineItems' => []
        ];
212

213
        $currency = $this->quote->getCurrency();
214

215
        $discountAmount = 0;
216

217
        foreach ($this->quote->getAllVisibleItems() as $item) {
218

219
            $numberOfItems = (int)$item->getQtyOrdered();
220

221 222
            // Summarize the discount amount item by item
            $discountAmount += $item->getDiscountAmount();
223

224
            $priceExcludingTax = $item->getPriceInclTax() - $item->getTaxAmount();
attilak's avatar
attilak committed
225
            $formattedPriceExcludingTax = $this->adyenHelper->formatAmount($priceExcludingTax, $currency);
226

227
            $formattedTaxAmount = $this->adyenHelper->formatAmount($item->getTaxAmount(), $currency);
228
            $formattedTaxPercentage = $item->getTaxPercent() * 100;
229

230
            $formFields['lineItems'][] = [
attilak's avatar
attilak committed
231
                'amountExcludingTax' => $formattedPriceExcludingTax,
232
                'taxAmount' => $formattedTaxAmount,
233 234 235 236
                'description' => $item->getName(),
                'id' => $item->getId(),
                'quantity' => $item->getQty(),
                'taxCategory' => $item->getProduct()->getAttributeText('tax_class_id'),
237
                'taxPercentage' => $formattedTaxPercentage
238 239
            ];
        }
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        // Discount cost
        if ($discountAmount != 0) {

            $description = __('Total Discount');
            $itemAmount = $this->adyenHelper->formatAmount($discountAmount, $currency);
            $itemVatAmount = "0";
            $itemVatPercentage = "0";
            $numberOfItems = 1;

            $formFields['lineItems'][] = [
                'amountExcludingTax' => $itemAmount,
                'taxAmount' => $itemVatAmount,
                'description' => $description,
                'quantity' => $numberOfItems,
                'taxCategory' => 'None',
                'taxPercentage' => $itemVatPercentage
            ];
        }
259

260 261
        // Shipping cost
        if ($this->quote->getShippingAddress()->getShippingAmount() > 0 || $this->quote->getShippingAddress()->getShippingTaxAmount() > 0) {
262

263
            $priceExcludingTax = $this->quote->getShippingAddress()->getShippingAmount() - $this->quote->getShippingAddress()->getShippingTaxAmount();
264 265 266

            $formattedTaxAmount = $this->adyenHelper->formatAmount($this->quote->getShippingAddress()->getShippingTaxAmount(), $currency);

attilak's avatar
attilak committed
267
            $formattedPriceExcludingTax = $this->adyenHelper->formatAmount($priceExcludingTax, $currency);
268

269
            $taxClassId = $this->taxConfig->getShippingTaxClass($this->storeManager->getStore()->getId());
270

271 272 273 274 275 276
            $formattedTaxPercentage = 0;

            if ($priceExcludingTax !== 0) {
                $formattedTaxPercentage = $this->quote->getShippingAddress()->getShippingTaxAmount() / $priceExcludingTax * 100 * 100;
            }
            
277
            $formFields['lineItems'][] = [
attilak's avatar
attilak committed
278
                'amountExcludingTax' => $formattedPriceExcludingTax,
279
                'taxAmount' => $formattedTaxAmount,
280 281
                'description' => $order->getShippingDescription(),
                'quantity' => 1,
282
                'taxPercentage' => $formattedTaxPercentage
283 284
            ];
        }
285

286 287
        return $formFields;
    }
288
}