We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit 2944c2c7 authored by Rik ter Beek's avatar Rik ter Beek

#61 send in extra data for openinvoice and fix result hmac calculation so it...

 #61 send in extra data for openinvoice and fix result hmac calculation so it alllow to have dots in the result params
parent d721a8bc
......@@ -39,7 +39,7 @@ class Redirect extends \Magento\Payment\Block\Form
protected $_checkoutSession;
/**
* @var \Magento\Checkout\Model\Order
* @var \Magento\Sales\Model\Order
*/
protected $_order;
......@@ -114,18 +114,31 @@ class Redirect extends \Magento\Payment\Block\Form
if ($paymentRoutine == 'single' && $this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://test.adyen.com/hpp/pay.shtml';
} else {
$url = ($this->getPaymentMethodSelectionOnAdyen())
? 'https://test.adyen.com/hpp/select.shtml'
: "https://test.adyen.com/hpp/details.shtml";
if ($this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://test.adyen.com/hpp/select.shtml';
} else {
if ($this->_order->getPayment()->getAdditionalInformation('brand_code') == 'klarna') {
$url = "https://test.adyen.com/hpp/skipDetails.shtml";
} else {
$url = "https://test.adyen.com/hpp/details.shtml";
}
}
}
break;
default:
if ($paymentRoutine == 'single' && $this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://live.adyen.com/hpp/pay.shtml';
} else {
$url = ($this->getPaymentMethodSelectionOnAdyen())
? 'https://live.adyen.com/hpp/select.shtml'
: "https://live.adyen.com/hpp/details.shtml";
if ($this->getPaymentMethodSelectionOnAdyen()) {
$url = 'https://live.adyen.com/hpp/select.shtml';
} else {
if ($this->_order->getPayment()->getAdditionalInformation('brand_code') == 'klarna') {
$url = "https://live.adyen.com/hpp/skipDetails.shtml";
} else {
$url = "https://live.adyen.com/hpp/details.shtml";
}
}
}
break;
}
......@@ -231,6 +244,31 @@ class Redirect extends \Magento\Payment\Block\Form
$formFields['issuerId'] = $issuerId;
}
$formFields = $this->setBillingAddressData($formFields);
$formFields = $this->setShippingAddressData($formFields);
$formFields = $this->setOpenInvoiceData($formFields);
$formFields['shopper.gender'] = $this->getGenderText($this->_order->getCustomerGender());
$dob = $this->_order->getCustomerDob();
if ($dob) {
$formFields['shopper.dateOfBirthDayOfMonth'] = trim($this->_getDate($dob, 'd'));
$formFields['shopper.dateOfBirthMonth'] = trim($this->_getDate($dob, 'm'));
$formFields['shopper.dateOfBirthYear'] = trim($this->_getDate($dob, 'Y'));
}
if ($this->_order->getPayment()->getAdditionalInformation(
\Adyen\Payment\Observer\AdyenHppDataAssignObserver::BRAND_CODE) == "klarna"
) {
// don't allow editable shipping/delivery address
$formFields['billingAddressType'] = "1";
$formFields['deliveryAddressType'] = "1";
// make setting to make this optional
$adyFields['shopperType'] = "1";
}
// Sort the array by key using SORT_STRING order
ksort($formFields, SORT_STRING);
......@@ -243,7 +281,6 @@ class Redirect extends \Magento\Payment\Block\Form
$formFields['merchantSig'] = $merchantSig;
$this->_adyenLogger->addAdyenDebug(print_r($formFields, true));
}
} catch(Exception $e) {
......@@ -251,6 +288,202 @@ class Redirect extends \Magento\Payment\Block\Form
}
return $formFields;
}
/**
* Set Billing Address data
*
* @param $formFields
*/
protected function setBillingAddressData($formFields)
{
$billingAddress = $this->_order->getBillingAddress();
if ($billingAddress) {
$formFields['shopper.firstName'] = trim($billingAddress->getFirstname());
$middleName = trim($billingAddress->getMiddlename());
if ($middleName != "") {
$formFields['shopper.infix'] = trim($middleName);
}
$formFields['shopper.lastName'] = trim($billingAddress->getLastname());
$formFields['shopper.telephoneNumber'] = trim($billingAddress->getTelephone());
$street = $this->_adyenHelper->getStreet($billingAddress);
if (isset($street['name']) && $street['name'] != "") {
$formFields['billingAddress.street'] = $street['name'];
}
if (isset($street['house_number']) && $street['house_number'] != "") {
$formFields['billingAddress.houseNumberOrName'] = $street['house_number'];
} else {
$formFields['billingAddress.houseNumberOrName'] = "NA";
}
if (trim($billingAddress->getCity()) == "") {
$formFields['billingAddress.city'] = "NA";
} else {
$formFields['billingAddress.city'] = trim($billingAddress->getCity());
}
if (trim($billingAddress->getPostcode()) == "") {
$formFields['billingAddress.postalCode'] = "NA";
} else {
$formFields['billingAddress.postalCode'] = trim($billingAddress->getPostcode());
}
if (trim($billingAddress->getRegionCode()) == "") {
$formFields['billingAddress.stateOrProvince'] = "NA";
} else {
$formFields['billingAddress.stateOrProvince'] = trim($billingAddress->getRegionCode());
}
if (trim($billingAddress->getCountryId()) == "") {
$formFields['billingAddress.country'] = "NA";
} else {
$formFields['billingAddress.country'] = trim($billingAddress->getCountryId());
}
}
return $formFields;
}
/**
* Set Shipping Address data
*
* @param $formFields
*/
protected function setShippingAddressData($formFields)
{
$shippingAddress = $this->_order->getShippingAddress();
if ($shippingAddress) {
$street = $this->_adyenHelper->getStreet($shippingAddress);
if (isset($street['name']) && $street['name'] != "") {
$formFields['deliveryAddress.street'] = $street['name'];
}
if (isset($street['house_number']) && $street['house_number'] != "") {
$formFields['deliveryAddress.houseNumberOrName'] = $street['house_number'];
} else {
$formFields['deliveryAddress.houseNumberOrName'] = "NA";
}
if (trim($shippingAddress->getCity()) == "") {
$formFields['deliveryAddress.city'] = "NA";
} else {
$formFields['deliveryAddress.city'] = trim($shippingAddress->getCity());
}
if (trim($shippingAddress->getPostcode()) == "") {
$formFields['deliveryAddress.postalCode'] = "NA";
} else {
$formFields['deliveryAddress.postalCode'] = trim($shippingAddress->getPostcode());
}
if (trim($shippingAddress->getRegionCode()) == "") {
$formFields['deliveryAddress.stateOrProvince'] = "NA";
} else {
$formFields['deliveryAddress.stateOrProvince'] = trim($shippingAddress->getRegionCode());
}
if (trim($shippingAddress->getCountryId()) == "") {
$formFields['deliveryAddress.country'] = "NA";
} else {
$formFields['deliveryAddress.country'] = trim($shippingAddress->getCountryId());
}
}
return $formFields;
}
/**
* @param $formFields
* @return mixed
*/
protected function setOpenInvoiceData($formFields)
{
$count = 0;
$currency = $this->_order->getOrderCurrencyCode();
foreach ($this->_order->getAllVisibleItems() as $item) {
++$count;
$linename = "line".$count;
$formFields['openinvoicedata.' . $linename . '.currencyCode'] = $currency;
$formFields['openinvoicedata.' . $linename . '.description'] =
str_replace("\n", '', trim($item->getName()));
$formFields['openinvoicedata.' . $linename . '.itemAmount'] =
$this->_adyenHelper->formatAmount($item->getPrice(), $currency);
$formFields['openinvoicedata.' . $linename . '.itemVatAmount'] =
($item->getTaxAmount() > 0 && $item->getPriceInclTax() > 0) ?
$this->_adyenHelper->formatAmount(
$item->getPriceInclTax(),
$currency
) - $this->_adyenHelper->formatAmount(
$item->getPrice(),
$currency
) : $this->_adyenHelper->formatAmount($item->getTaxAmount(), $currency);
// $product = $item->getProduct();
// Calculate vat percentage
$percentageMinorUnits = $this->_adyenHelper->getMinorUnitTaxPercent($item->getTaxPercent());
$formFields['openinvoicedata.' . $linename . '.itemVatPercentage'] = $percentageMinorUnits;
$formFields['openinvoicedata.' . $linename . '.numberOfItems'] = (int) $item->getQtyOrdered();
if ($this->_order->getPayment()->getAdditionalInformation(
\Adyen\Payment\Observer\AdyenHppDataAssignObserver::BRAND_CODE) == "klarna"
) {
$formFields['openinvoicedata.' . $linename . '.vatCategory'] = "High";
} else {
$formFields['openinvoicedata.' . $linename . '.vatCategory'] = "None";
}
}
$formFields['openinvoicedata.refundDescription'] = "Refund / Correction for ".$formFields['merchantReference'];
$formFields['openinvoicedata.numberOfLines'] = $count;
return $formFields;
}
/**
* @param $genderId
* @return string
*/
protected function getGenderText($genderId)
{
$result = "";
if ($genderId == '1') {
$result = 'MALE';
} elseif ($genderId == '2') {
$result = 'FEMALE';
}
return $result;
}
/**
* @param null $date
* @param string $format
* @return mixed
*/
protected function _getDate($date = null, $format = 'Y-m-d H:i:s')
{
if (strlen($date) < 0) {
$date = date('d-m-Y H:i:s');
}
$timeStamp = new \DateTime($date);
return $timeStamp->format($format);
}
/**
* The character escape function is called from the array_map function in _signRequestParams
*
......
......@@ -262,17 +262,29 @@ class Result extends \Magento\Framework\App\Action\Action
protected function _authenticate($response) {
$hmacKey = $this->_adyenHelper->getHmac();
$merchantSigNotification = $response['merchantSig'];
// do it like this because $_GET is converting dot to underscore
$queryString = $_SERVER['QUERY_STRING'];
$result = [];
$pairs = explode("&", $queryString);
foreach ($pairs as $pair) {
$nv = explode("=", $pair);
$name = urldecode($nv[0]);
$value = urldecode($nv[1]);
$result[$name] = $value;
}
// do not include the merchantSig in the merchantSig calculation
$merchantSigNotification = $response['merchantSig'];
unset($response['merchantSig']);
unset($result['merchantSig']);
// Sort the array by key using SORT_STRING order
ksort($response, SORT_STRING);
ksort($result, SORT_STRING);
// Generate the signing data string
$signData = implode(":", array_map([$this, 'escapeString'],
array_merge(array_keys($response), array_values($response))));
array_merge(array_keys($result), array_values($result))));
$merchantSig = base64_encode(hash_hmac('sha256', $signData, pack("H*", $hmacKey), true));
......
......@@ -190,7 +190,19 @@ class Data extends AbstractHelper
break;
}
return number_format($amount, $format, '', '');
return (int)number_format($amount, $format, '', '');
}
/**
* Tax Percentage needs to be in minor units for Adyen
*
* @param float $taxPercent
* @return int
*/
public function getMinorUnitTaxPercent($taxPercent)
{
$taxPercent = $taxPercent * 100;
return (int)$taxPercent;
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment