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 6.64 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
<?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;

class CheckoutDataBuilder implements BuilderInterface
{
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	/**
	 * @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;
50 51 52 53 54 55

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

56 57 58 59 60 61 62 63 64

	/**
	 * 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
	 */
65 66 67 68 69
	public function __construct(
		\Adyen\Payment\Helper\Data $adyenHelper,
		\Magento\Store\Model\StoreManagerInterface $storeManager,
		\Magento\Checkout\Model\Session $checkoutSession,
		\Magento\Tax\Model\Config $taxConfig
70 71 72 73 74 75 76 77
	)
	{
		$this->adyenHelper = $adyenHelper;
		$this->storeManager = $storeManager;
		$this->checkoutSession = $checkoutSession;
		$this->quote = $checkoutSession->getQuote();
		$this->taxConfig = $taxConfig;
	}
78 79 80 81 82 83 84 85 86 87 88 89 90

	/**
	 * @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();
		$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 103 104 105 106 107

		// Additional data for open invoice payment
		if ($payment->getAdditionalInformation("gender")) {
			$order->setCustomerGender(\Adyen\Payment\Model\Gender::getMagentoGenderFromAdyenGender(
				$payment->getAdditionalInformation("gender"))
			);
108
			$request['shopperName']['gender'] = $payment->getAdditionalInformation("gender");
109 110
		}

111 112
		if ($payment->getAdditionalInformation("dob")) {
			$order->setCustomerDob($payment->getAdditionalInformation("dob"));
113

114
			$request['dateOfBirth']= $this->adyenHelper->formatDate($payment->getAdditionalInformation("dob"), 'Y-m-d') ;
115 116 117 118
		}

		if ($payment->getAdditionalInformation("telephone")) {
			$order->getBillingAddress()->setTelephone($payment->getAdditionalInformation("telephone"));
119
			$request['telephoneNumber']= $payment->getAdditionalInformation("telephone");
120 121
		}

122 123 124 125
		// Additional data for sepa direct debit
		if ($payment->getAdditionalInformation("ownerName")) {
			$request['paymentMethod']['sepa.ownerName']= $payment->getAdditionalInformation("ownerName");
		}
126

127 128 129
		if ($payment->getAdditionalInformation("ibanNumber")) {
			$request['paymentMethod']['sepa.ibanNumber']= $payment->getAdditionalInformation("ibanNumber");
		}
130 131

		if ($this->adyenHelper->isPaymentMethodOpenInvoiceMethod(
132 133
			$payment->getAdditionalInformation(AdyenHppDataAssignObserver::BRAND_CODE)
		)) {
134 135 136 137
			$openInvoiceFields = $this->getOpenInvoiceData($order);
			$request = array_merge($request, $openInvoiceFields);
		}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		return $request;
	}

	/**
	 * @param $formFields
	 * @return mixed
	 */
	protected function getOpenInvoiceData($order)
	{
		$formFields = [
			'lineItems' => []
		];

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

		$discountAmount = 0;

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

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

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

			$priceExcludingTax = $item->getPriceInclTax() - $item->getTaxAmount();

			$formFields['lineItems'][] = [
				'amountExcludingTax' => $priceExcludingTax,
				'taxAmount' => $item->getTaxAmount(),
				'description' => $item->getName(),
				'id' => $item->getId(),
				'quantity' => $item->getQty(),
				'taxCategory' => $item->getProduct()->getAttributeText('tax_class_id'),
				'taxPercentage' => $item->getTaxPercent()
			];
		}

		// 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
			];
		}

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

			$priceExcludingTax = $this->quote->getShippingAddress()->getShippingAmount() - $this->quote->getShippingAddress()->getShippingTaxAmount();

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

			$formFields['lineItems'][] = [
				'amountExcludingTax' => $priceExcludingTax,
				'taxAmount' => $this->quote->getShippingAddress()->getShippingTaxAmount(),
				'description' => $order->getShippingDescription(),
				'quantity' => 1,
				'taxPercentage' => $this->quote->getShippingAddress()->getShippingTaxAmount()
			];
		}

		return $formFields;
	}
212
}