From a962102b53ec6368d98c5ec3be20ea240847d029 Mon Sep 17 00:00:00 2001 From: hilrob Date: Mon, 4 Dec 2017 15:38:06 +0100 Subject: [PATCH 1/3] US address validation error handling. --- Model/Api.php | 69 +++++++++++++------ .../GuestPPPPaymentInformationManagement.php | 11 ++- .../PPPPaymentInformationManagement.php | 11 ++- RELEASE_NOTES.txt | 3 + .../web/js/action/patch-ppp-payment.js | 2 +- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/Model/Api.php b/Model/Api.php index d98e80c..77a9904 100755 --- a/Model/Api.php +++ b/Model/Api.php @@ -56,6 +56,8 @@ class Api const PATCH_ADD = 'add'; const PATCH_REPLACE = 'replace'; + const VALIDATION_ERROR = 'VALIDATION_ERROR'; + /** * @var null|ApiContext */ @@ -128,7 +130,7 @@ class Api protected $encryptor; /** - * @var Repository + * @var \Magento\Framework\View\Asset\Repository */ protected $assetRepo; @@ -261,11 +263,11 @@ public function getPayment($paymentId) } /** - * Create payment for curretn quote + * Create payment for current quote * * @param WebProfile $webProfile * @param \Magento\Quote\Model\Quote $quote - * @return boolean + * @return boolean|PayPalPayment */ public function createPayment($webProfile, $quote, $taxFailure = false) { @@ -300,7 +302,7 @@ public function createPayment($webProfile, $quote, $taxFailure = false) } $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } @@ -342,11 +344,37 @@ public function patchPayment($quote) $amountPatch->setValue($amount); $patchRequest->addPatch($amountPatch); - $response = $payment->update( - $patchRequest, - $this->_apiContext - ); - return $response; + + try { + $response = $payment->update( + $patchRequest, + $this->_apiContext + ); + return $response; + } catch (\PayPal\Exception\PayPalConnectionException $ex) { + $message = json_decode($ex->getData()); + if ( + isset($message->name) + && isset($message->details) + && $message->name == self::VALIDATION_ERROR + ) { + $validationMessage = __('Your address is invalid. Please check following errors: '); + foreach ($message->details as $detail) { + if (isset($detail->field) && isset($detail->issue)) { + $validationMessage .= + __( + 'Field: "%1" - %2. ', + [ + $detail->field, + $detail->issue + ] + ); + } + } + throw new \Exception($validationMessage); + } + } + } return false; } @@ -395,11 +423,10 @@ public function executePayment($paymentId, $payerId) } catch (PayPalConnectionException $ex) { $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } - return false; } /** @@ -436,11 +463,10 @@ public function getWebhooks() } catch (PayPalConnectionException $ex) { $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } - return false; } /** @@ -457,11 +483,10 @@ public function getWebhookEvent($webhookEventId) } catch (PayPalConnectionException $ex) { $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } - return false; } /** @@ -477,7 +502,7 @@ public function getWebhooksEventTypes() } catch (PayPalConnectionException $ex) { $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } @@ -513,7 +538,7 @@ public function createWebhook() } $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } @@ -535,7 +560,7 @@ public function deleteWebhook($webhookId) } catch (PayPalConnectionException $ex) { $this->payPalPlusHelper->handleException($ex); return false; - } catch (Exception $e) { + } catch (\Exception $e) { $this->logger->critical($e); return false; } @@ -553,7 +578,7 @@ public function validateWebhook($rawBody) try { $webhookEvent = new \PayPal\Api\WebhookEvent(); return $webhookEvent->validateAndGetReceivedEvent($rawBody, $this->_apiContext); - } catch (Exception $ex) { + } catch (\Exception $ex) { $this->logger->critical($ex); return false; } @@ -593,7 +618,7 @@ protected function buildShippingAddress($quote) * Build BillingAddress from quote * * @param \Magento\Quote\Model\Quote $quote - * @return ShippingAddress + * @return ShippingAddress|boolean */ protected function buildBillingAddress($quote) { @@ -742,7 +767,7 @@ protected function buildAmount($quote) } $total = $quote->getBaseGrandTotal(); - if((float)$quote->getShippingAddress()->getBaseShippingAmount() == 0 && (float)$quote->getShippingAddress()->getBaseShippingInclTax() >= 0) { + if ((float)$quote->getShippingAddress()->getBaseShippingAmount() == 0 && (float)$quote->getShippingAddress()->getBaseShippingInclTax() >= 0) { $total = (float)$total - (float)$quote->getShippingAddress()->getBaseShippingInclTax(); } @@ -834,7 +859,7 @@ protected function getHeaderImage() /** * Reset web profile id * - * @return type + * @return boolean */ public function resetWebProfileId() { diff --git a/Model/PaymentInformationManagement/GuestPPPPaymentInformationManagement.php b/Model/PaymentInformationManagement/GuestPPPPaymentInformationManagement.php index 716d7d1..e3ab024 100755 --- a/Model/PaymentInformationManagement/GuestPPPPaymentInformationManagement.php +++ b/Model/PaymentInformationManagement/GuestPPPPaymentInformationManagement.php @@ -15,6 +15,7 @@ namespace Iways\PayPalPlus\Model\PaymentInformationManagement; use Iways\PayPalPlus\Model\PaymentInformationManagement; +use Magento\Framework\Exception\CouldNotSaveException; use Magento\Quote\Api\CartRepositoryInterface; class GuestPPPPaymentInformationManagement extends PaymentInformationManagement implements \Iways\PayPalPlus\Api\GuestPPPPaymentInformationManagementInterface @@ -100,7 +101,15 @@ public function savePaymentInformation( } $paymentMethod = $this->handleComment($paymentMethod); $this->paymentMethodManagement->set($cartId, $paymentMethod); - $this->patchPayment($quoteIdMask->getQuoteId()); + + try { + $this->patchPayment($quoteIdMask->getQuoteId()); + } catch (\Exception $e) { + throw new CouldNotSaveException( + __($e->getMessage()), + $e + ); + } return true; } diff --git a/Model/PaymentInformationManagement/PPPPaymentInformationManagement.php b/Model/PaymentInformationManagement/PPPPaymentInformationManagement.php index d3de599..25b1d80 100755 --- a/Model/PaymentInformationManagement/PPPPaymentInformationManagement.php +++ b/Model/PaymentInformationManagement/PPPPaymentInformationManagement.php @@ -15,6 +15,7 @@ namespace Iways\PayPalPlus\Model\PaymentInformationManagement; use Iways\PayPalPlus\Model\PaymentInformationManagement; +use Magento\Framework\Exception\CouldNotSaveException; class PPPPaymentInformationManagement extends PaymentInformationManagement implements \Iways\PayPalPlus\Api\PPPPaymentInformationManagementInterface { @@ -88,7 +89,15 @@ public function savePaymentInformation( } $paymentMethod = $this->handleComment($paymentMethod); $this->paymentMethodManagement->set($cartId, $paymentMethod); - $this->patchPayment($cartId); + + try { + $this->patchPayment($cartId); + } catch (\Exception $e) { + throw new CouldNotSaveException( + __($e->getMessage()), + $e + ); + } return true; } diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index a012430..a437d80 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,3 +1,6 @@ +==== 1.1.5 ==== +US address validation error handling. + ==== 1.1.4 ==== Changed locale_code parsing Added workaround for broken shipping cost calculation in checkout diff --git a/view/frontend/web/js/action/patch-ppp-payment.js b/view/frontend/web/js/action/patch-ppp-payment.js index 190673a..2d21ef6 100755 --- a/view/frontend/web/js/action/patch-ppp-payment.js +++ b/view/frontend/web/js/action/patch-ppp-payment.js @@ -78,7 +78,7 @@ define( } ).fail( function (response) { - errorProcessor.process(response, messageContainer); + errorProcessor.process(response); } ).always( function () { From d725f3e698421e731fbabb86b35eedb00b9c15e0 Mon Sep 17 00:00:00 2001 From: hilrob Date: Fri, 8 Dec 2017 17:52:03 +0100 Subject: [PATCH 2/3] Hide Paypal --- Model/MethodList.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Model/MethodList.php b/Model/MethodList.php index 63579be..4eaa052 100755 --- a/Model/MethodList.php +++ b/Model/MethodList.php @@ -52,6 +52,11 @@ public function __construct( */ public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote = null, $checkPPP = true) { + + $pppEnabled = $this->scopeConfig->getValue( + 'payment/iways_paypalplus_payment/active', + \Magento\Store\Model\ScopeInterface::SCOPE_STORE); + if ($checkPPP) { $allowedPPPMethods = explode( ',', @@ -67,11 +72,16 @@ public function getAvailableMethods(\Magento\Quote\Api\Data\CartInterface $quote foreach ($this->paymentHelper->getStoreMethods($store, $quote) as $method) { if ($this->_canUseMethod($method, $quote)) { $method->setInfoInstance($quote->getPayment()); + + if ($pppEnabled && strpos($method->getCode(), 'paypal_') === 0) { + continue; + } + if ($checkPPP) { if ( - $method->getCode() == Payment::CODE + ($method->getCode() == Payment::CODE || $method->getCode() == self::AMAZON_PAYMENT - || !in_array($method->getCode(), $allowedPPPMethods) + || !in_array($method->getCode(), $allowedPPPMethods)) ) { $methods[] = $method; } From aebca461fb0a2c15f52021ac16f9195b60d87e5a Mon Sep 17 00:00:00 2001 From: hilrob Date: Wed, 13 Dec 2017 10:47:35 +0100 Subject: [PATCH 3/3] Composer.json 1.1.5 --- composer.json | 2 +- marketplace.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4a8ec15..02c38df 100755 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "type": "magento2-module", - "version": "1.1.4", + "version": "1.1.5", "license": [ "OSL-3.0" ], diff --git a/marketplace.json b/marketplace.json index b662300..76f9f6b 100755 --- a/marketplace.json +++ b/marketplace.json @@ -18,7 +18,7 @@ } ], "type": "magento2-module", - "version": "1.1.4", + "version": "1.1.5", "license": [ "OSL-3.0" ],