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

Commit 232244ad authored by cyattilakiss's avatar cyattilakiss Committed by GitHub

Merge pull request #446 from Adyen/PW-1281

PW-1256 Fix new Klarna integration for checkout api
parents a1b699b8 5e0c764b
...@@ -173,36 +173,35 @@ class Result extends \Magento\Framework\App\Action\Action ...@@ -173,36 +173,35 @@ class Result extends \Magento\Framework\App\Action\Action
$response = $this->validatePayloadAndReturnResponse($response); $response = $this->validatePayloadAndReturnResponse($response);
} }
$incrementId = $response['merchantReference']; $incrementId = null;
if ($incrementId) {
$order = $this->_getOrder($incrementId);
if ($order->getId()) {
$this->_eventManager->dispatch('adyen_payment_process_resulturl_before', [
'order' => $order,
'adyen_response' => $response
]);
if (isset($response['handled'])) {
return $response['handled_response'];
}
// update the order if (!empty($response['merchantReference'])) {
$result = $this->_validateUpdateOrder($order, $response); $incrementId = $response['merchantReference'];
}
$this->_eventManager->dispatch('adyen_payment_process_resulturl_after', [ $order = $this->_getOrder($incrementId);
'order' => $order, if ($order->getId()) {
'adyen_response' => $response $this->_eventManager->dispatch('adyen_payment_process_resulturl_before', [
]); 'order' => $order,
} else { 'adyen_response' => $response
throw new \Magento\Framework\Exception\LocalizedException( ]);
__('Order does not exists with increment_id: %1', $incrementId) if (isset($response['handled'])) {
); return $response['handled_response'];
} }
// update the order
$result = $this->_validateUpdateOrder($order, $response);
$this->_eventManager->dispatch('adyen_payment_process_resulturl_after', [
'order' => $order,
'adyen_response' => $response
]);
} else { } else {
throw new \Magento\Framework\Exception\LocalizedException( throw new \Magento\Framework\Exception\LocalizedException(
__('Empty merchantReference') __('Order does not exists with increment_id: %1', $incrementId)
); );
} }
return $result; return $result;
} }
...@@ -351,11 +350,16 @@ class Result extends \Magento\Framework\App\Action\Action ...@@ -351,11 +350,16 @@ class Result extends \Magento\Framework\App\Action\Action
* @param $incrementId * @param $incrementId
* @return \Magento\Sales\Model\Order * @return \Magento\Sales\Model\Order
*/ */
protected function _getOrder($incrementId) protected function _getOrder($incrementId = null)
{ {
if (!$this->_order) { if (!$this->_order) {
$this->_order = $this->_orderFactory->create()->loadByIncrementId($incrementId); if (!is_null($incrementId)) {
$this->_order = $this->_orderFactory->create()->loadByIncrementId($incrementId);
} else {
$this->_order = $this->_session->getLastRealOrder();
}
} }
return $this->_order; return $this->_order;
} }
...@@ -371,11 +375,13 @@ class Result extends \Magento\Framework\App\Action\Action ...@@ -371,11 +375,13 @@ class Result extends \Magento\Framework\App\Action\Action
$client = $this->_adyenHelper->initializeAdyenClient($this->storeManager->getStore()->getId()); $client = $this->_adyenHelper->initializeAdyenClient($this->storeManager->getStore()->getId());
$service = $this->_adyenHelper->createAdyenCheckoutService($client); $service = $this->_adyenHelper->createAdyenCheckoutService($client);
$request = array( $request = [];
"details" => array(
"payload" => $response["payload"] if (!empty($this->_session->getLastRealOrder()->getPayment()->getAdditionalInformation("paymentData"))) {
) $request['paymentData'] = $this->_session->getLastRealOrder()->getPayment()->getAdditionalInformation("paymentData");
); }
$request["details"] = $response;
try { try {
$response = $service->paymentsDetails($request); $response = $service->paymentsDetails($request);
...@@ -385,4 +391,4 @@ class Result extends \Magento\Framework\App\Action\Action ...@@ -385,4 +391,4 @@ class Result extends \Magento\Framework\App\Action\Action
return $response; return $response;
} }
} }
\ No newline at end of file
...@@ -228,6 +228,7 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -228,6 +228,7 @@ class CheckoutDataBuilder implements BuilderInterface
$formattedPriceExcludingTax = $this->adyenHelper->formatAmount($priceExcludingTax, $currency); $formattedPriceExcludingTax = $this->adyenHelper->formatAmount($priceExcludingTax, $currency);
$formattedTaxAmount = $this->adyenHelper->formatAmount($item->getTaxAmount(), $currency); $formattedTaxAmount = $this->adyenHelper->formatAmount($item->getTaxAmount(), $currency);
$formattedTaxPercentage = $item->getTaxPercent() * 100;
$formFields['lineItems'][] = [ $formFields['lineItems'][] = [
'id' => $item->getId(), 'id' => $item->getId(),
...@@ -237,7 +238,7 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -237,7 +238,7 @@ class CheckoutDataBuilder implements BuilderInterface
'description' => $item->getName(), 'description' => $item->getName(),
'quantity' => $item->getQty(), 'quantity' => $item->getQty(),
'taxCategory' => $item->getProduct()->getAttributeText('tax_class_id'), 'taxCategory' => $item->getProduct()->getAttributeText('tax_class_id'),
'taxPercentage' => $item->getTaxPercent() 'taxPercentage' => $formattedTaxPercentage
]; ];
} }
...@@ -272,13 +273,19 @@ class CheckoutDataBuilder implements BuilderInterface ...@@ -272,13 +273,19 @@ class CheckoutDataBuilder implements BuilderInterface
$taxClassId = $this->taxConfig->getShippingTaxClass($this->storeManager->getStore()->getId()); $taxClassId = $this->taxConfig->getShippingTaxClass($this->storeManager->getStore()->getId());
$formattedTaxPercentage = 0;
if ($priceExcludingTax !== 0) {
$formattedTaxPercentage = $this->quote->getShippingAddress()->getShippingTaxAmount() / $priceExcludingTax * 100 * 100;
}
$formFields['lineItems'][] = [ $formFields['lineItems'][] = [
'itemId' => 'shippingCost', 'itemId' => 'shippingCost',
'amountExcludingTax' => $formattedPriceExcludingTax, 'amountExcludingTax' => $formattedPriceExcludingTax,
'taxAmount' => $formattedTaxAmount, 'taxAmount' => $formattedTaxAmount,
'description' => $order->getShippingDescription(), 'description' => $order->getShippingDescription(),
'quantity' => 1, 'quantity' => 1,
'taxPercentage' => $this->quote->getShippingAddress()->getShippingTaxAmount() 'taxPercentage' => $formattedTaxPercentage
]; ];
} }
......
...@@ -111,6 +111,11 @@ class CustomerDataBuilder implements BuilderInterface ...@@ -111,6 +111,11 @@ class CustomerDataBuilder implements BuilderInterface
if ($countryId = $billingAddress->getCountryId()) { if ($countryId = $billingAddress->getCountryId()) {
$result['countryCode'] = $countryId; $result['countryCode'] = $countryId;
} }
if ($shopperLocale = $this->adyenHelper->getCurrentLocaleCode($order->getStoreId())) {
$result['shopperLocale'] = $shopperLocale;
}
} }
return $result; return $result;
......
...@@ -118,6 +118,16 @@ class Data extends AbstractHelper ...@@ -118,6 +118,16 @@ class Data extends AbstractHelper
*/ */
private $agreementResourceModel; private $agreementResourceModel;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
private $localeResolver;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $config;
/** /**
* Data constructor. * Data constructor.
* @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\App\Helper\Context $context
...@@ -155,7 +165,9 @@ class Data extends AbstractHelper ...@@ -155,7 +165,9 @@ class Data extends AbstractHelper
\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\CacheInterface $cache, \Magento\Framework\App\CacheInterface $cache,
\Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory, \Adyen\Payment\Model\Billing\AgreementFactory $billingAgreementFactory,
\Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel \Adyen\Payment\Model\ResourceModel\Billing\Agreement $agreementResourceModel,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\Magento\Framework\App\Config\ScopeConfigInterface $config
) { ) {
parent::__construct($context); parent::__construct($context);
$this->_encryptor = $encryptor; $this->_encryptor = $encryptor;
...@@ -174,6 +186,8 @@ class Data extends AbstractHelper ...@@ -174,6 +186,8 @@ class Data extends AbstractHelper
$this->cache = $cache; $this->cache = $cache;
$this->billingAgreementFactory = $billingAgreementFactory; $this->billingAgreementFactory = $billingAgreementFactory;
$this->agreementResourceModel = $agreementResourceModel; $this->agreementResourceModel = $agreementResourceModel;
$this->localeResolver = $localeResolver;
$this->config = $config;
} }
/** /**
...@@ -1701,4 +1715,30 @@ class Data extends AbstractHelper ...@@ -1701,4 +1715,30 @@ class Data extends AbstractHelper
$timeStamp = new \DateTime($date); $timeStamp = new \DateTime($date);
return $timeStamp->format($format); return $timeStamp->format($format);
} }
/**
* @param int $storeId
* @return mixed|string
*/
public function getCurrentLocaleCode($storeId)
{
$localeCode = $this->getAdyenAbstractConfigData('shopper_locale', $storeId);
if ($localeCode != "") {
return $localeCode;
}
$locale = $this->localeResolver->getLocale();
if ($locale) {
return $locale;
}
// should have the value if not fall back to default
$localeCode = $this->config->getValue(
\Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
return $localeCode;
}
} }
...@@ -198,7 +198,7 @@ class PaymentMethods extends AbstractHelper ...@@ -198,7 +198,7 @@ class PaymentMethods extends AbstractHelper
), ),
], ],
"shopperReference" => $this->getCurrentShopperReference(), "shopperReference" => $this->getCurrentShopperReference(),
"shopperLocale" => $this->getCurrentLocaleCode($store) "shopperLocale" => $this->adyenHelper->getCurrentLocaleCode($store->getId())
]; ];
$billingAddress = $this->getQuote()->getBillingAddress(); $billingAddress = $this->getQuote()->getBillingAddress();
...@@ -325,32 +325,6 @@ class PaymentMethods extends AbstractHelper ...@@ -325,32 +325,6 @@ class PaymentMethods extends AbstractHelper
return ""; return "";
} }
/**
* @param $store
* @return mixed|string
*/
protected function getCurrentLocaleCode($store)
{
$localeCode = $this->adyenHelper->getAdyenAbstractConfigData('shopper_locale', $store->getId());
if ($localeCode != "") {
return $localeCode;
}
$locale = $this->localeResolver->getLocale();
if ($locale) {
return $locale;
}
// should have the value if not fall back to default
$localeCode = $this->config->getValue(
\Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORES,
$store->getCode()
);
return $localeCode;
}
/** /**
* @var array * @var array
*/ */
......
...@@ -412,6 +412,11 @@ define( ...@@ -412,6 +412,11 @@ define(
*/ */
result.renderKlarnaComponent = function () { result.renderKlarnaComponent = function () {
/* The new Klarna integration doesn't return details and the component does not handle it */
if (!value.details) {
return;
}
var klarnaNode = document.getElementById('klarnaContainer'); var klarnaNode = document.getElementById('klarnaContainer');
var klarna = self.checkoutComponent.create('klarna', { var klarna = self.checkoutComponent.create('klarna', {
......
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