1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-28 09:24:24 +01:00

(feat) humanize gateway error messages

This commit is contained in:
Sylvain 2022-09-28 12:54:29 +02:00
parent f784a4d8a0
commit a30eac10de
5 changed files with 54 additions and 9 deletions

View File

@ -1,5 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'stripe/helper'
require 'pay_zen/helper'
# API Controller for cart checkout # API Controller for cart checkout
class API::CheckoutController < API::ApiController class API::CheckoutController < API::ApiController
include ::API::OrderConcern include ::API::OrderConcern
@ -16,6 +19,10 @@ class API::CheckoutController < API::ApiController
res = Checkout::PaymentService.new.payment(@current_order, current_user, params[:coupon_code], res = Checkout::PaymentService.new.payment(@current_order, current_user, params[:coupon_code],
params[:payment_id]) params[:payment_id])
render json: res 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 rescue StandardError => e
render json: e, status: :unprocessable_entity render json: e, status: :unprocessable_entity
end end

View File

@ -43,6 +43,9 @@ en:
must_be_in_the_past: "The period must be strictly prior to today's date." must_be_in_the_past: "The period must be strictly prior to today's date."
registration_disabled: "Registration is disabled" registration_disabled: "Registration is disabled"
undefined_in_store: "must be defined to make the product available in the store" 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: apipie:
api_documentation: "API Documentation" api_documentation: "API Documentation"
code: "HTTP code" code: "HTTP code"

View File

@ -1,10 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'payment/helper'
# PayZen payement gateway # PayZen payement gateway
module PayZen; end module PayZen; end
## Provides various methods around the PayZen payment gateway ## Provides various methods around the PayZen payment gateway
class PayZen::Helper class PayZen::Helper < Payment::Helper
class << self class << self
## Is the PayZen gateway enabled? ## Is the PayZen gateway enabled?
def enabled? def enabled?
@ -13,11 +15,15 @@ class PayZen::Helper
res = true res = true
%w[payzen_username payzen_password payzen_endpoint payzen_public_key payzen_hmac payzen_currency].each do |pz_setting| %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 end
res res
end 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 ## generate an unique string reference for the content of a cart
def generate_ref(cart_items, customer) def generate_ref(cart_items, customer)
require 'sha3' require 'sha3'
@ -57,9 +63,8 @@ class PayZen::Helper
## Generate a hash map compatible with PayZen 'V4/Customer/ShoppingCart' ## Generate a hash map compatible with PayZen 'V4/Customer/ShoppingCart'
def generate_shopping_cart(cart_items, customer, operator) def generate_shopping_cart(cart_items, customer, operator)
cart = if cart_items.is_a? ShoppingCart cart = case cart_items
cart_items when ShoppingCart, Order
elsif cart_items.is_a? Order
cart_items cart_items
else else
cs = CartService.new(operator) 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 is not defined, we use kr-hash-key parameter to choose it
if key.nil? if key.nil?
if hash_key == 'sha256_hmac' case hash_key
when 'sha256_hmac'
key = Setting.get('payzen_hmac') key = Setting.get('payzen_hmac')
elsif hash_key == 'password' when 'password'
key = Setting.get('payzen_password') key = Setting.get('payzen_password')
else else
raise ::PayzenError, 'invalid hash-key parameter' raise ::PayzenError, 'invalid hash-key parameter'

10
lib/payment/helper.rb Normal file
View File

@ -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

View File

@ -1,10 +1,12 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'payment/helper'
# Stripe payement gateway # Stripe payement gateway
module Stripe; end module Stripe; end
## Provides various methods around the Stripe payment gateway ## Provides various methods around the Stripe payment gateway
class Stripe::Helper class Stripe::Helper < Payment::Helper
class << self class << self
## Is the Stripe gateway enabled? ## Is the Stripe gateway enabled?
def enabled? def enabled?
@ -13,9 +15,26 @@ class Stripe::Helper
res = true res = true
%w[stripe_public_key stripe_secret_key stripe_currency].each do |pz_setting| %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 end
res res
end 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
end end