From 4473b453b61b70b9534e75bb2093268855431ce2 Mon Sep 17 00:00:00 2001 From: Peter Berkenbosch Date: Mon, 17 Feb 2020 11:08:44 +0100 Subject: [PATCH 1/3] void previous pending payments we only have a hook to connect to when we render the order payload for Affirm, this commit adds additional logic to returning the order payload. Before returning the payload, we make sure there are no pending Affirm payments anymore. --- app/models/solidus_affirm/checkout_payload.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/models/solidus_affirm/checkout_payload.rb b/app/models/solidus_affirm/checkout_payload.rb index eb209de..50c29cf 100644 --- a/app/models/solidus_affirm/checkout_payload.rb +++ b/app/models/solidus_affirm/checkout_payload.rb @@ -29,6 +29,7 @@ def initialize(order, config, metadata = {}) @order = order @config = config @metadata = metadata + prepare_payments end def ship_address @@ -42,5 +43,10 @@ def bill_address def items order.line_items end + + private + def prepare_payments + @order.payments.where(source_type: 'SolidusAffirm::Checkout').pending.collect(&:void) + end end end From 80d2c7165db277f57510b3acc39525ea5f54b5ba Mon Sep 17 00:00:00 2001 From: Peter Berkenbosch Date: Mon, 17 Feb 2020 12:00:26 +0100 Subject: [PATCH 2/3] cancel! actually uses the gateway to void --- app/models/solidus_affirm/checkout_payload.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/solidus_affirm/checkout_payload.rb b/app/models/solidus_affirm/checkout_payload.rb index 50c29cf..1254cb9 100644 --- a/app/models/solidus_affirm/checkout_payload.rb +++ b/app/models/solidus_affirm/checkout_payload.rb @@ -46,7 +46,7 @@ def items private def prepare_payments - @order.payments.where(source_type: 'SolidusAffirm::Checkout').pending.collect(&:void) + @order.payments.where(source_type: 'SolidusAffirm::Checkout').pending.collect(&:cancel!) end end end From 9c2c9b6e363a9c63568c13cc7d508dd018955a2b Mon Sep 17 00:00:00 2001 From: Peter Berkenbosch Date: Mon, 17 Feb 2020 19:39:40 +0100 Subject: [PATCH 3/3] wip move cancelation of pending payments trying to find the best place to put this code --- app/helpers/affirm_helper.rb | 3 +++ app/models/solidus_affirm/checkout_payload.rb | 6 ------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/helpers/affirm_helper.rb b/app/helpers/affirm_helper.rb index 0c79ab2..cd92700 100644 --- a/app/helpers/affirm_helper.rb +++ b/app/helpers/affirm_helper.rb @@ -15,6 +15,9 @@ def affirm_payload_json(order, payment_method, metadata = {}) confirmation_url: spree.confirm_affirm_url(payment_method_id: payment_method.id, order_id: order.id), cancel_url: spree.cancel_affirm_url(payment_method_id: payment_method.id, order_id: order.id) } + + order.payments.where(source_type: 'SolidusAffirm::Checkout').pending.collect(&:cancel!) + payload = SolidusAffirm::CheckoutPayload.new(order, config, metadata) SolidusAffirm::Config.checkout_payload_serializer.new(payload, root: false).to_json end diff --git a/app/models/solidus_affirm/checkout_payload.rb b/app/models/solidus_affirm/checkout_payload.rb index 1254cb9..eb209de 100644 --- a/app/models/solidus_affirm/checkout_payload.rb +++ b/app/models/solidus_affirm/checkout_payload.rb @@ -29,7 +29,6 @@ def initialize(order, config, metadata = {}) @order = order @config = config @metadata = metadata - prepare_payments end def ship_address @@ -43,10 +42,5 @@ def bill_address def items order.line_items end - - private - def prepare_payments - @order.payments.where(source_type: 'SolidusAffirm::Checkout').pending.collect(&:cancel!) - end end end