|
45 | 45 | class CheckoutSessionManagement implements \Amazon\Pay\Api\CheckoutSessionManagementInterface |
46 | 46 | { |
47 | 47 | protected const GENERIC_COMPLETE_CHECKOUT_ERROR_MESSAGE = 'Unable to complete Amazon Pay checkout.'; |
| 48 | + protected const ADDRESS_CHANGED_CHECKOUT_ERROR_MESSAGE = 'Shipping address mismatch.'; |
48 | 49 |
|
49 | 50 | /** |
50 | 51 | * @var \Magento\Store\Model\StoreManagerInterface |
@@ -731,9 +732,12 @@ public function cancelOrder($order, $quote = null, $reasonMessage = '') |
731 | 732 | protected function handleCompleteCheckoutSessionError($message, $logEntryDetails = '') |
732 | 733 | { |
733 | 734 | $this->logger->error($message . ' ' . $logEntryDetails); |
| 735 | + if ($message == $this::ADDRESS_CHANGED_CHECKOUT_ERROR_MESSAGE) { |
| 736 | + $message = $this::GENERIC_COMPLETE_CHECKOUT_ERROR_MESSAGE; |
| 737 | + } |
734 | 738 | $result = [ |
735 | 739 | 'success' => false, |
736 | | - 'message' => $this->getTranslationString($message), |
| 740 | + 'message' => $this->getTranslationString($message) |
737 | 741 | ]; |
738 | 742 | return $result; |
739 | 743 | } |
@@ -811,6 +815,20 @@ public function completeCheckoutSession($amazonSessionId, $cartId = null, $order |
811 | 815 | */ |
812 | 816 | public function placeOrder($amazonSessionId, $quoteId = null) |
813 | 817 | { |
| 818 | + |
| 819 | + // verify the shipping address has not been modified in Magento, it must match |
| 820 | + // the one selected in the Amazon checkout session (express checkout only) |
| 821 | + if ($amznShippingAddress = $this->getShippingAddress($amazonSessionId)) { |
| 822 | + $amazonAddress = $amznShippingAddress[0]; |
| 823 | + $magentoAddress = $this->session->getQuoteFromIdOrSession($quoteId)->getShippingAddress(); |
| 824 | + if (!$this->addressHelper->validateShippingIsSame($amazonAddress, $magentoAddress)) { |
| 825 | + return $this->handleCompleteCheckoutSessionError( |
| 826 | + self::ADDRESS_CHANGED_CHECKOUT_ERROR_MESSAGE, |
| 827 | + $this->getAddressMismatchDetails($amazonAddress, $magentoAddress) |
| 828 | + ); |
| 829 | + } |
| 830 | + } |
| 831 | + |
814 | 832 | if (!$quote = $this->session->getQuoteFromIdOrSession($quoteId)) { |
815 | 833 | $errorMsg = "Unable to complete Amazon Pay checkout. Quote not found."; |
816 | 834 | if ($quoteId) { |
@@ -881,7 +899,7 @@ public function placeOrder($amazonSessionId, $quoteId = null) |
881 | 899 | $this->logger->error($errorMsg . $quote->getId()); |
882 | 900 | return [ |
883 | 901 | 'success' => false, |
884 | | - 'message' => $this->getTranslationString(self::GENERIC_COMPLETE_CHECKOUT_ERROR_MESSAGE), |
| 902 | + 'message' => $this->getTranslationString(self::GENERIC_COMPLETE_CHECKOUT_ERROR_MESSAGE) |
885 | 903 | ]; |
886 | 904 | } |
887 | 905 |
|
@@ -910,6 +928,31 @@ protected function getCanceledMessage($amazonSession) |
910 | 928 | return $amazonSession['statusDetails']['reasonDescription']; |
911 | 929 | } |
912 | 930 |
|
| 931 | + /** |
| 932 | + * Get log-friendly details of disagreeing checkout session addresses |
| 933 | + * |
| 934 | + * @param mixed $amazonAddress |
| 935 | + * @param \Magento\Quote\Model\Quote\Address $magentoAddress |
| 936 | + * @return string |
| 937 | + */ |
| 938 | + protected function getAddressMismatchDetails($amazonAddress, $magentoAddress) |
| 939 | + { |
| 940 | + return 'Address from Amazon account: ' . json_encode($amazonAddress) . '; Address entered in Magento: ' . |
| 941 | + json_encode([ |
| 942 | + 'city' => $magentoAddress->getCity(), |
| 943 | + 'firstname' => $magentoAddress->getFirstName(), |
| 944 | + 'lastname' => $magentoAddress->getLastname(), |
| 945 | + 'country_id' => $magentoAddress->getCountryId(), |
| 946 | + 'street' => $magentoAddress->getStreet(), |
| 947 | + 'postcode' => $magentoAddress->getPostcode(), |
| 948 | + 'telephone' => $magentoAddress->getTelephone(), |
| 949 | + 'region' => $magentoAddress->getRegion(), |
| 950 | + 'region_id' => $magentoAddress->getRegionId(), |
| 951 | + 'region_code' => $magentoAddress->getRegionCode(), |
| 952 | + 'email' => $magentoAddress->getEmail() |
| 953 | + ]); |
| 954 | + } |
| 955 | + |
913 | 956 | /** |
914 | 957 | * Update vault token |
915 | 958 | * |
@@ -1365,7 +1408,7 @@ private function closeChargePermission($amazonSessionId, OrderInterface $order, |
1365 | 1408 | * @param mixed $orderId |
1366 | 1409 | * @return void |
1367 | 1410 | */ |
1368 | | - public function setOrderPendingPaymentReview(mixed $orderId) |
| 1411 | + public function setOrderPendingPaymentReview($orderId) |
1369 | 1412 | { |
1370 | 1413 | try { |
1371 | 1414 | if (!$orderId) { |
|
0 commit comments