Make StripeSCA store cards (and delete them) on the Stripe platform account and not the Stripe Connected account (the sellers accounts)

This is important so that cards can be re-used across sellers in OFN
This commit is contained in:
luisramos0
2020-01-27 16:40:42 +00:00
committed by Luis Ramos
parent 699110258b
commit 1afd712ff4
7 changed files with 8 additions and 37 deletions

View File

@@ -56,15 +56,9 @@ module Spree
private
# Currently can only destroy the whole customer object
# It destroys the whole customer object
def destroy_at_stripe
if @credit_card.payment_method &&
@credit_card.payment_method.type == "Spree::Gateway::StripeSCA"
options = { stripe_account: stripe_account_id }
end
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id,
options || {})
stripe_customer = Stripe::Customer.retrieve(@credit_card.gateway_customer_profile_id, {})
stripe_customer.delete if stripe_customer
end

View File

@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'stripe/profile_storer'
require 'stripe/credit_card_cloner'
require 'active_merchant/billing/gateways/stripe_payment_intents'
require 'active_merchant/billing/gateways/stripe_decorator'
@@ -52,7 +53,7 @@ module Spree
def create_profile(payment)
return unless payment.source.gateway_customer_profile_id.nil?
profile_storer = Stripe::ProfileStorer.new(payment, provider, stripe_account_id)
profile_storer = Stripe::ProfileStorer.new(payment, provider)
profile_storer.create_customer_from_token
end

View File

@@ -2,7 +2,7 @@
%script{:src => "https://js.stripe.com/v3/", :type => "text/javascript"}
- if Stripe.publishable_key
:javascript
angular.module('admin.payments').value("stripeObject", Stripe("#{Stripe.publishable_key}", { stripeAccount: "#{StripeAccount.find_by_enterprise_id(payment_method.preferred_enterprise_id).andand.stripe_user_id}" }))
angular.module('admin.payments').value("stripeObject", Stripe("#{Stripe.publishable_key}"))
.row
.three.columns

View File

@@ -1,7 +1,7 @@
- content_for :injection_data do
- if Stripe.publishable_key
:javascript
angular.module('Darkswarm').value("stripeObject", Stripe("#{Stripe.publishable_key}", { stripeAccount: "#{StripeAccount.find_by_enterprise_id(payment_method.preferred_enterprise_id).andand.stripe_user_id}" }))
angular.module('Darkswarm').value("stripeObject", Stripe("#{Stripe.publishable_key}"))
.row{ "ng-show" => "savedCreditCards.length > 0" }
.small-12.columns

View File

@@ -4,10 +4,9 @@
module Stripe
class ProfileStorer
def initialize(payment, provider, stripe_account_id = nil)
def initialize(payment, provider)
@payment = payment
@provider = provider
@stripe_account_id = stripe_account_id
end
def create_customer_from_token
@@ -29,13 +28,7 @@ module Stripe
email: @payment.order.email,
login: Stripe.api_key,
address: address_for(@payment)
}.merge(stripe_account_option)
end
def stripe_account_option
return {} if @stripe_account_id.blank?
{ stripe_account: @stripe_account_id }
}
end
def address_for(payment)

View File

@@ -172,22 +172,6 @@ describe Spree::CreditCardsController, type: :controller do
expect(response).to redirect_to spree.account_path(anchor: 'cards')
end
end
context "where the payment method is StripeSCA" do
let(:stripe_payment_method) { create(:stripe_sca_payment_method) }
let!(:card) { create(:credit_card, gateway_customer_profile_id: 'cus_AZNMJ', payment_method: stripe_payment_method) }
before do
stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ").
to_return(status: 200, body: JSON.generate(deleted: true, id: "cus_AZNMJ"))
end
it "the request to destroy the Stripe customer includes the stripe_account_id" do
expect(Stripe::Customer).to receive(:retrieve).with(card.gateway_customer_profile_id, { stripe_account: "abc123" })
expect{ delete :destroy, params }.to change(Spree::CreditCard, :count).by(-1)
end
end
end
end
end

View File

@@ -7,7 +7,6 @@ module Stripe
describe "create_customer_from_token" do
let(:payment) { create(:payment) }
let(:stripe_payment_method) { create(:stripe_payment_method) }
let(:stripe_account_id) { "12312" }
let(:profile_storer) { Stripe::ProfileStorer.new(payment, stripe_payment_method.provider) }
let(:customer_id) { "cus_A123" }