Skip to content

Commit

Permalink
fix(payment): Fix Adyen invoice payment (#1689)
Browse files Browse the repository at this point in the history
* fix(payment): Fix Adyen invoice payment

* fix(payment): Fix Adyen invoice payment, add error tracking back
  • Loading branch information
ivannovosad authored Feb 13, 2024
1 parent 94a8893 commit 53eb0ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
12 changes: 6 additions & 6 deletions app/services/invoices/payments/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def create
res = create_adyen_payment
return result unless res

handle_adyen_response(res)
return result unless result.success?
adyen_success, _adyen_error = handle_adyen_response(res)
return result unless adyen_success

payment = Payment.new(
invoice:,
Expand Down Expand Up @@ -77,7 +77,8 @@ def generate_payment_url
return result unless should_process_payment?

res = client.checkout.payment_links_api.payment_links(Lago::Adyen::Params.new(payment_url_params).to_h)
handle_adyen_response(res)
adyen_success, adyen_error = handle_adyen_response(res)
result.service_failure!(code: adyen_error.code, message: adyen_error.msg) unless adyen_success

return result unless result.success?

Expand All @@ -87,7 +88,7 @@ def generate_payment_url
rescue Adyen::AdyenError => e
deliver_error_webhook(e)

result.single_validation_failure!(error_code: 'payment_provider_error')
result.service_failure!(code: e.code, message: e.msg)
end

private
Expand Down Expand Up @@ -154,8 +155,7 @@ def create_adyen_payment
rescue Adyen::AdyenError => e
deliver_error_webhook(e)
update_invoice_payment_status(payment_status: :failed, deliver_webhook: false)

raise
raise e
end

def payment_method_params
Expand Down
4 changes: 2 additions & 2 deletions app/services/payment_provider_customers/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def generate_checkout_url(send_webhook: true)
return result.not_found_failure!(resource: 'adyen_payment_provider') unless adyen_payment_provider

res = client.checkout.payment_links_api.payment_links(Lago::Adyen::Params.new(payment_link_params).to_h)
handle_adyen_response(res)

adyen_success, adyen_error = handle_adyen_response(res)
result.service_failure!(code: adyen_error.code, message: adyen_error.msg) unless adyen_success
return result unless result.success?

result.checkout_url = res.response['url']
Expand Down
8 changes: 5 additions & 3 deletions lib/lago/adyen/error_handlable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ module Lago
module Adyen
module ErrorHandlable
def handle_adyen_response(res)
return if res.status < 400
return [true, nil] if res.status < 400

code = res.response['errorType']
message = res.response['message']

deliver_error_webhook(::Adyen::AdyenError.new(nil, nil, message, code))
result.service_failure!(code:, message:)
error = ::Adyen::AdyenError.new(nil, nil, message, code)
deliver_error_webhook(error)

[false, error]
end
end
end
Expand Down

0 comments on commit 53eb0ae

Please sign in to comment.