From 41471ec60f2be8906ad33b6af1c5191ebf750679 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Wed, 21 Jul 2021 13:52:08 +0200 Subject: [PATCH] Merge pull request #4292 from overleaf/tm-handle-recurly-pricing-errors Handle Recurly pricing API errors and add coupon code error display GitOrigin-RevId: b86a42a059984a7efa596db85bbcedb93c0e7376 --- services/web/app/views/subscriptions/new.pug | 3 ++ .../web/frontend/js/main/new-subscription.js | 43 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/services/web/app/views/subscriptions/new.pug b/services/web/app/views/subscriptions/new.pug index 00785c2912..e9921bc1a6 100644 --- a/services/web/app/views/subscriptions/new.pug +++ b/services/web/app/views/subscriptions/new.pug @@ -98,6 +98,9 @@ block content .alert.alert-warning.small(ng-show="genericError") strong {{genericError}} + .alert.alert-warning.small(ng-show="couponError") + strong {{couponError}} + div(ng-show="paymentMethod.value === 'credit_card'") .row .col-xs-6 diff --git a/services/web/frontend/js/main/new-subscription.js b/services/web/frontend/js/main/new-subscription.js index 46a1ae6125..07f481e70a 100644 --- a/services/web/frontend/js/main/new-subscription.js +++ b/services/web/frontend/js/main/new-subscription.js @@ -119,7 +119,16 @@ export default App.controller( ) { $scope.currencyCode = 'USD' setupPricing() + } else if (err.name === 'api-error' && err.code === 'not-found') { + // not-found here should refer to the coupon code, plan_code should be valid + $scope.$applyAsync(() => { + $scope.couponError = 'Coupon code is not valid for selected plan' + }) } else { + // Bail out on other errors, form state will not be correct + $scope.$applyAsync(() => { + $scope.recurlyLoadError = true + }) throw err } }) @@ -175,7 +184,25 @@ export default App.controller( $scope.$apply() }) - $scope.applyCoupon = () => pricing.coupon($scope.data.coupon).done() + $scope.applyCoupon = () => { + $scope.couponError = '' + pricing + .coupon($scope.data.coupon) + .catch(err => { + if (err.name === 'api-error' && err.code === 'not-found') { + $scope.$applyAsync(() => { + $scope.couponError = 'Coupon code is not valid for selected plan' + }) + } else { + $scope.$applyAsync(() => { + $scope.couponError = + 'An error occured when verifying the coupon code' + }) + throw err + } + }) + .done() + } $scope.applyVatNumber = () => pricing @@ -184,7 +211,19 @@ export default App.controller( $scope.changeCurrency = function (newCurrency) { $scope.currencyCode = newCurrency - return pricing.currency(newCurrency).done() + return pricing + .currency(newCurrency) + .catch(function (err) { + if ( + $scope.currencyCode !== 'USD' && + err.name === 'invalid-currency' + ) { + $scope.changeCurrency('USD') + } else { + throw err + } + }) + .done() } $scope.inputHasError = function (formItem) {