diff --git a/Controller/ApplePay/PlaceOrder.php b/Controller/ApplePay/PlaceOrder.php index 835705e62a9..cc37ab51ae0 100644 --- a/Controller/ApplePay/PlaceOrder.php +++ b/Controller/ApplePay/PlaceOrder.php @@ -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 { @@ -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, @@ -64,7 +74,9 @@ public function __construct( QuoteManagement $quoteManagement, Session $checkoutSession, Generate $paymentToken, - OrderRepositoryInterface $orderRepository + SetRegionFromApplePayAddress $setRegionFromApplePayAddress, + OrderRepositoryInterface $orderRepository, + Config $config ) { parent::__construct($context); @@ -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() @@ -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') @@ -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) @@ -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']); } diff --git a/Service/Quote/SetRegionFromApplePayAddress.php b/Service/Quote/SetRegionFromApplePayAddress.php new file mode 100644 index 00000000000..4171a3af2c0 --- /dev/null +++ b/Service/Quote/SetRegionFromApplePayAddress.php @@ -0,0 +1,49 @@ +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; + } + } + } +} diff --git a/view/frontend/web/js/view/applepay/minicart.js b/view/frontend/web/js/view/applepay/minicart.js index a42d16c211d..17d245186f9 100644 --- a/view/frontend/web/js/view/applepay/minicart.js +++ b/view/frontend/web/js/view/applepay/minicart.js @@ -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']); @@ -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); } }); });