From cb29d050695ceed429c4a4e9c5424556c46e957d Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Mon, 14 Dec 2020 11:54:03 -0800 Subject: [PATCH 1/4] use `purchase` to charge offline for Stripe Connect --- app/models/spree/gateway/stripe_connect.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/spree/gateway/stripe_connect.rb b/app/models/spree/gateway/stripe_connect.rb index 3484dc3e4f..493b9bd74e 100644 --- a/app/models/spree/gateway/stripe_connect.rb +++ b/app/models/spree/gateway/stripe_connect.rb @@ -31,6 +31,10 @@ module Spree failed_activemerchant_billing_response(e.message) end + def charge_offline(money, creditcard, gateway_options) + purchase(money, creditcard, gateway_options) + end + # NOTE: the name of this method is determined by Spree::Payment::Processing def void(response_code, _creditcard, gateway_options) gateway_options[:stripe_account] = stripe_account_id From 484576d1ace215e1d8db1664feb53205234e7fbc Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Tue, 15 Dec 2020 10:30:05 -0800 Subject: [PATCH 2/4] add unit test for stripe connect --- spec/models/spree/gateway/stripe_connect_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spec/models/spree/gateway/stripe_connect_spec.rb b/spec/models/spree/gateway/stripe_connect_spec.rb index cc4cb8f6b9..c6621b9343 100644 --- a/spec/models/spree/gateway/stripe_connect_spec.rb +++ b/spec/models/spree/gateway/stripe_connect_spec.rb @@ -91,4 +91,15 @@ describe Spree::Gateway::StripeConnect, type: :model do expect(provider).to have_received(:refund).with(money, response_code, hash_including(stripe_account: stripe_account_id)) end end + + describe "#charging offline" do + let(:gateway_options) { { some: 'option' } } + let(:money) { double(:money) } + let(:card) { double(:creditcard) } + + it "uses #purchase to charge offline" do + subject.charge_offline(money, card, gateway_options) + expect(provider).to have_received(:purchase).with('money', 'cc', 'opts') + end + end end From 934f4f278e8f7acd253b7461fcedfb8385d52808 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Tue, 15 Dec 2020 12:24:38 -0800 Subject: [PATCH 3/4] add spec to subscription confirm job --- spec/jobs/subscription_confirm_job_spec.rb | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/spec/jobs/subscription_confirm_job_spec.rb b/spec/jobs/subscription_confirm_job_spec.rb index 78c0e9cda8..4eff7c9b61 100644 --- a/spec/jobs/subscription_confirm_job_spec.rb +++ b/spec/jobs/subscription_confirm_job_spec.rb @@ -134,6 +134,48 @@ describe SubscriptionConfirmJob do expect(job).to receive(:record_order) end + context "when Stripe payments need to be processed" do + let(:charge_response_mock) do + { status: 200, body: JSON.generate(id: "ch_1234", object: "charge", amount: 2000) } + end + + before do + allow(order).to receive(:payment_required?) { true } + expect(job).to receive(:setup_payment!) { true } + stub_request(:post, "https://api.stripe.com/v1/charges") + .with(body: /amount/) + .to_return(charge_response_mock) + end + + context "Stripe SCA" do + let(:stripe_sca_payment_method) { create(:stripe_sca_payment_method) } + let(:stripe_sca_payment) { create(:payment, amount: 10, payment_method: stripe_sca_payment_method) } + + before do + allow(order).to receive(:pending_payments) { [stripe_sca_payment] } + expect(stripe_sca_payment_method).to receive(:charge_offline) + end + + it "runs the charges in offline mode" do + job.send(:confirm_order!, order) + end + end + + context "Stripe Connect" do + let(:stripe_connect_payment_method) { create(:stripe_connect_payment_method) } + let(:stripe_connect_payment) { create(:payment, amount: 10, payment_method: stripe_connect_payment_method) } + + before do + allow(order).to receive(:pending_payments) { [stripe_connect_payment] } + expect(stripe_connect_payment_method).to receive(:charge_offline) + end + + it "runs the charges in offline mode" do + job.send(:confirm_order!, order) + end + end + end + context "when payments need to be processed" do let(:payment_method) { create(:payment_method) } let(:payment) { create(:payment, amount: 10) } From 7fc9cc9f31a310d5c7790e8aa4015b47f3de8d36 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Wed, 16 Dec 2020 08:08:03 -0800 Subject: [PATCH 4/4] test that purchase is called by SCA and Connect providers --- spec/jobs/subscription_confirm_job_spec.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/spec/jobs/subscription_confirm_job_spec.rb b/spec/jobs/subscription_confirm_job_spec.rb index 4eff7c9b61..cb34a16a99 100644 --- a/spec/jobs/subscription_confirm_job_spec.rb +++ b/spec/jobs/subscription_confirm_job_spec.rb @@ -150,14 +150,18 @@ describe SubscriptionConfirmJob do context "Stripe SCA" do let(:stripe_sca_payment_method) { create(:stripe_sca_payment_method) } let(:stripe_sca_payment) { create(:payment, amount: 10, payment_method: stripe_sca_payment_method) } + let(:provider) { double } before do + allow_any_instance_of(Stripe::CreditCardCloner).to receive(:find_or_clone) { ["cus_123", "pm_1234"] } allow(order).to receive(:pending_payments) { [stripe_sca_payment] } - expect(stripe_sca_payment_method).to receive(:charge_offline) + allow(stripe_sca_payment_method).to receive(:provider) { provider } + allow(stripe_sca_payment_method.provider).to receive(:purchase) { true } end it "runs the charges in offline mode" do job.send(:confirm_order!, order) + expect(stripe_sca_payment_method.provider).to have_received(:purchase) end end @@ -167,11 +171,12 @@ describe SubscriptionConfirmJob do before do allow(order).to receive(:pending_payments) { [stripe_connect_payment] } - expect(stripe_connect_payment_method).to receive(:charge_offline) + allow(stripe_connect_payment_method).to receive(:purchase) { true } end it "runs the charges in offline mode" do job.send(:confirm_order!, order) + expect(stripe_connect_payment_method).to have_received(:purchase) end end end