Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIMAG-676 | Bugfix: Set the region for Apple Pay orders #833

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions Controller/ApplePay/PlaceOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Mollie\Payment\Api\Webapi\PaymentTokenRequestInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Service\PaymentToken\Generate;
use Mollie\Payment\Service\Quote\SetRegionFromApplePayAddress;

class PlaceOrder extends Action
{
Expand Down Expand Up @@ -56,6 +58,14 @@ class PlaceOrder extends Action
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* @var SetRegionFromApplePayAddress
*/
private $setRegionFromApplePayAddress;
/**
* @var Config
*/
private $config;

public function __construct(
Context $context,
Expand All @@ -64,7 +74,9 @@ public function __construct(
QuoteManagement $quoteManagement,
Session $checkoutSession,
Generate $paymentToken,
OrderRepositoryInterface $orderRepository
SetRegionFromApplePayAddress $setRegionFromApplePayAddress,
OrderRepositoryInterface $orderRepository,
Config $config
) {
parent::__construct($context);

Expand All @@ -74,6 +86,8 @@ public function __construct(
$this->checkoutSession = $checkoutSession;
$this->paymentToken = $paymentToken;
$this->orderRepository = $orderRepository;
$this->setRegionFromApplePayAddress = $setRegionFromApplePayAddress;
$this->config = $config;
}

public function execute()
Expand Down Expand Up @@ -104,8 +118,22 @@ public function execute()
$this->cartRepository->save($cart);
$cart->getPayment()->addData(['method' => 'mollie_methods_applepay']);

/** @var OrderInterface $order */
$order = $this->quoteManagement->submit($cart);
$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);

try {
/** @var OrderInterface $order */
$order = $this->quoteManagement->submit($cart);
} catch (\Exception $exception) {
$this->config->addToLog('error', [
'message' => 'Error while try place Apple Pay order',
'quote_id' => $cart->getId(),
'exception' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);

return $response->setData(['error' => true, 'error_message' => $exception->getMessage()]);
}

$order->getPayment()->setAdditionalInformation(
'applepay_payment_token',
$this->getRequest()->getParam('applePayPaymentToken')
Expand All @@ -126,8 +154,7 @@ public function execute()
->setLastRealOrderId($order->getIncrementId())
->setLastOrderId($order->getId());

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);
return $response->setData(['url' => $url]);
return $response->setData(['url' => $url, 'error' => false, 'error_message' => '']);
}

private function updateAddress(AddressInterface $address, array $input)
Expand All @@ -141,6 +168,8 @@ private function updateAddress(AddressInterface $address, array $input)
AddressInterface::KEY_POSTCODE => $input['postalCode'],
]);

$this->setRegionFromApplePayAddress->execute($address, $input);

if (isset($input['phoneNumber'])) {
$address->setTelephone($input['phoneNumber']);
}
Expand Down
49 changes: 49 additions & 0 deletions Service/Quote/SetRegionFromApplePayAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Mollie\Payment\Service\Quote;

use Magento\Directory\Api\CountryInformationAcquirerInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;

class SetRegionFromApplePayAddress
{
/**
* @var CountryInformationAcquirerInterface
*/
private $countryInformationAcquirer;

public function __construct(
CountryInformationAcquirerInterface $countryInformationAcquirer
) {
$this->countryInformationAcquirer = $countryInformationAcquirer;
}

public function execute(AddressInterface $address, array $input): void
{
if (!array_key_exists('administrativeArea', $input)) {
return;
}

try {
$information = $this->countryInformationAcquirer->getCountryInfo($input['countryCode']);
} catch (NoSuchEntityException $exception) {
return;
}

$regions = $information->getAvailableRegions();
if ($regions === null) {
$address->setRegion($input['administrativeArea']);
return;
}

foreach ($regions as $region) {
if ($region->getCode() === $input['administrativeArea']) {
$address->setRegionId($region->getId());
return;
}
}
}
}
25 changes: 25 additions & 0 deletions view/frontend/web/js/view/applepay/minicart.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ define([
return;
}

if (result.error) {
this.sendMessage(result.error_message);
this.session.abort()
return;
}

if (!result.url) {
this.sendMessage('Something went wrong, please try again later.');
this.session.abort()
return;
}

this.session.completePayment(ApplePaySession.STATUS_SUCCESS);

customerData.invalidate(['cart']);
Expand Down Expand Up @@ -174,6 +186,19 @@ define([
}.bind(this);

this.session.begin();
},

sendMessage: function (message) {
var customerMessages = customerData.get('messages')() || {},
messages = customerMessages.messages || [];

messages.push({
text: message,
type: 'error'
});

customerMessages.messages = messages;
customerData.set('messages', customerMessages);
}
});
});
Loading