From a30eac10defb634fb9821389c543e59f33c5a880 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 28 Sep 2022 12:54:29 +0200 Subject: [PATCH] (feat) humanize gateway error messages --- app/controllers/api/checkout_controller.rb | 7 +++++++ config/locales/en.yml | 3 +++ lib/pay_zen/helper.rb | 20 ++++++++++++------- lib/payment/helper.rb | 10 ++++++++++ lib/stripe/helper.rb | 23 ++++++++++++++++++++-- 5 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 lib/payment/helper.rb diff --git a/app/controllers/api/checkout_controller.rb b/app/controllers/api/checkout_controller.rb index 05e5faa84..f76c78759 100644 --- a/app/controllers/api/checkout_controller.rb +++ b/app/controllers/api/checkout_controller.rb @@ -1,5 +1,8 @@ # frozen_string_literal: true +require 'stripe/helper' +require 'pay_zen/helper' + # API Controller for cart checkout class API::CheckoutController < API::ApiController include ::API::OrderConcern @@ -16,6 +19,10 @@ class API::CheckoutController < API::ApiController res = Checkout::PaymentService.new.payment(@current_order, current_user, params[:coupon_code], params[:payment_id]) render json: res + rescue Stripe::StripeError => e + render json: Stripe::Helper.human_error(e), status: :unprocessable_entity + rescue PayzenError => e + render json: PayZen::Helper.human_error(e), status: :unprocessable_entity rescue StandardError => e render json: e, status: :unprocessable_entity end diff --git a/config/locales/en.yml b/config/locales/en.yml index de51ebeb0..37a79a5fd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -43,6 +43,9 @@ en: must_be_in_the_past: "The period must be strictly prior to today's date." registration_disabled: "Registration is disabled" undefined_in_store: "must be defined to make the product available in the store" + gateway_error: "Payement gateway error: %{MESSAGE}" + gateway_amount_too_small: "Payments under %{AMOUNT} are not supported. Please contact reception directly." + gateway_amount_too_large: "Payments above %{AMOUNT} are not supported. Please contact reception directly." apipie: api_documentation: "API Documentation" code: "HTTP code" diff --git a/lib/pay_zen/helper.rb b/lib/pay_zen/helper.rb index 05a377ca2..feaef549a 100644 --- a/lib/pay_zen/helper.rb +++ b/lib/pay_zen/helper.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true +require 'payment/helper' + # PayZen payement gateway module PayZen; end ## Provides various methods around the PayZen payment gateway -class PayZen::Helper +class PayZen::Helper < Payment::Helper class << self ## Is the PayZen gateway enabled? def enabled? @@ -13,11 +15,15 @@ class PayZen::Helper res = true %w[payzen_username payzen_password payzen_endpoint payzen_public_key payzen_hmac payzen_currency].each do |pz_setting| - res = false unless Setting.get(pz_setting).present? + res = false if Setting.get(pz_setting).blank? end res end + def human_error(error) + I18n.t('errors.messages.gateway_error', { MESSAGE: error.message }) + end + ## generate an unique string reference for the content of a cart def generate_ref(cart_items, customer) require 'sha3' @@ -57,9 +63,8 @@ class PayZen::Helper ## Generate a hash map compatible with PayZen 'V4/Customer/ShoppingCart' def generate_shopping_cart(cart_items, customer, operator) - cart = if cart_items.is_a? ShoppingCart - cart_items - elsif cart_items.is_a? Order + cart = case cart_items + when ShoppingCart, Order cart_items else cs = CartService.new(operator) @@ -98,9 +103,10 @@ class PayZen::Helper # if key is not defined, we use kr-hash-key parameter to choose it if key.nil? - if hash_key == 'sha256_hmac' + case hash_key + when 'sha256_hmac' key = Setting.get('payzen_hmac') - elsif hash_key == 'password' + when 'password' key = Setting.get('payzen_password') else raise ::PayzenError, 'invalid hash-key parameter' diff --git a/lib/payment/helper.rb b/lib/payment/helper.rb new file mode 100644 index 000000000..79d68f0b5 --- /dev/null +++ b/lib/payment/helper.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# Payments module +module Payment; end + +# Generic gateway helpers +class Payment::Helper + def self.enabled?; end + def self.human_error(_error); end +end diff --git a/lib/stripe/helper.rb b/lib/stripe/helper.rb index 3959d5278..507ba8ba7 100644 --- a/lib/stripe/helper.rb +++ b/lib/stripe/helper.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true +require 'payment/helper' + # Stripe payement gateway module Stripe; end ## Provides various methods around the Stripe payment gateway -class Stripe::Helper +class Stripe::Helper < Payment::Helper class << self ## Is the Stripe gateway enabled? def enabled? @@ -13,9 +15,26 @@ class Stripe::Helper res = true %w[stripe_public_key stripe_secret_key stripe_currency].each do |pz_setting| - res = false unless Setting.get(pz_setting).present? + res = false if Setting.get(pz_setting).blank? end res end + + def human_error(error) + message = error.message + case error.code + when 'amount_too_small' + message.match(/\d+\.\d+\s\w+/) do |res| + message = I18n.t('errors.messages.gateway_amount_too_small', { AMOUNT: res }) + end + when 'amount_too_large' + message.match(/\d+\.\d+\s\w+/) do |res| + message = I18n.t('errors.messages.gateway_amount_too_large', { AMOUNT: res }) + end + else + message = I18n.t('errors.messages.gateway_error', { MESSAGE: message }) + end + message + end end end