From fa9f6432c3278d776cf3a5a3a7644908460585fa Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Fri, 6 Aug 2021 14:49:59 +0530 Subject: [PATCH] Fix profile_storer.rb and profile_storer_spec --- lib/stripe/profile_storer.rb | 8 ++++++-- spec/lib/stripe/profile_storer_spec.rb | 18 +++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/stripe/profile_storer.rb b/lib/stripe/profile_storer.rb index fb5b7d8ae9..c7fe1831f4 100644 --- a/lib/stripe/profile_storer.rb +++ b/lib/stripe/profile_storer.rb @@ -65,9 +65,13 @@ module Stripe def customer_profile_id(response) response.params['customer'] || response.params['id'] end - + def payment_profile_id(response) - response.params['default_source'] || response.params['default_card'] + if response.params['customer'] # Payment Intents API + response.params['id'] + else + response.params['default_source'] || response.params['default_card'] + end end end end diff --git a/spec/lib/stripe/profile_storer_spec.rb b/spec/lib/stripe/profile_storer_spec.rb index d39190e0d9..e7b69f8c8c 100644 --- a/spec/lib/stripe/profile_storer_spec.rb +++ b/spec/lib/stripe/profile_storer_spec.rb @@ -4,28 +4,28 @@ require 'spec_helper' module Stripe describe ProfileStorer do + include StripeStubs + describe "create_customer_from_token" do - let(:payment) { create(:payment) } let(:stripe_payment_method) { create(:stripe_sca_payment_method) } + let(:card) { create(:credit_card, gateway_payment_profile_id: card_id) } + let(:payment) { create(:payment, source: card, payment_method: stripe_payment_method) } let(:profile_storer) { Stripe::ProfileStorer.new(payment, stripe_payment_method.provider) } let(:customer_id) { "cus_A123" } let(:card_id) { "card_2342" } - let(:customer_response_mock) { { status: 200, body: customer_response_body } } + let(:customer_response_mock) { + { status: 200, body: JSON.generate(id: customer_id, sources: { data: [{ id: "1" }] }) } + } before do Stripe.api_key = "sk_test_12345" - stub_request(:post, "https://api.stripe.com/v1/customers") - .with(basic_auth: ["sk_test_12345", ""], body: { email: payment.order.email }) - .to_return(customer_response_mock) + stub_customers_post_request(email: payment.order.email, response: customer_response_mock) + stub_payment_method_attach_request(payment_method: card_id, customer: customer_id) end context "when called from Stripe SCA" do - let(:customer_response_body) { - JSON.generate(customer: customer_id, default_card: card_id, sources: { data: [{ id: "1" }] }) - } - it "fetches the customer id and the card id from the correct response fields" do profile_storer.create_customer_from_token