Fix checkout Stripe: ricrea customer se ID test su ambiente live.
Evita errore «No such customer» dopo passaggio da sk_test a sk_live. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
29
backend/spec/services/billing/stripe/ensure_customer_spec.rb
Normal file
29
backend/spec/services/billing/stripe/ensure_customer_spec.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe Billing::Stripe::EnsureCustomer do
|
||||
let(:user) { User.create!(email: "ensure@test.it", name: "U", password: "password123", role: "coach") }
|
||||
let(:club) do
|
||||
Club.create!(name: "Ensure Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff")
|
||||
end
|
||||
|
||||
before do
|
||||
load Rails.root.join("db/seeds/plans.rb")
|
||||
Billing::AssignPlan.call(club: club, plan_slug: "free")
|
||||
club.subscription.update!(stripe_customer_id: "cus_stale_test_mode")
|
||||
end
|
||||
|
||||
it "ricrea il customer se l'id salvato non esiste su Stripe" do
|
||||
err = ::Stripe::InvalidRequestError.new("No such customer", 404, code: "resource_missing")
|
||||
allow(Stripe::Customer).to receive(:retrieve).with("cus_stale_test_mode").and_raise(err)
|
||||
allow(Stripe::Customer).to receive(:create).and_return(
|
||||
double(id: "cus_live_new", email: user.email)
|
||||
)
|
||||
allow(Stripe::Customer).to receive(:update)
|
||||
|
||||
id = described_class.call(club: club, user: user)
|
||||
|
||||
expect(id).to eq("cus_live_new")
|
||||
expect(club.subscription.reload.stripe_customer_id).to eq("cus_live_new")
|
||||
expect(club.subscription.stripe_subscription_id).to be_nil
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user