-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathstripe_customer_spec.rb
182 lines (145 loc) · 6.95 KB
/
stripe_customer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
require 'spec_helper'
require 'stripe'
Stripe.api_key = "sk_test_VCZnDv3GLU15TRvn8i2EsaAN"
RSpec.describe "Stripe checkout", type: :feature do
let(:zone) { FactoryBot.create(:zone) }
let(:country) { FactoryBot.create(:country) }
before do
FactoryBot.create(:store)
zone.members << Spree::ZoneMember.create!(zoneable: country)
FactoryBot.create(:free_shipping_method)
Spree::PaymentMethod::StripeCreditCard.create!(
name: "Stripe",
preferred_secret_key: "sk_test_VCZnDv3GLU15TRvn8i2EsaAN",
preferred_publishable_key: "pk_test_Cuf0PNtiAkkMpTVC2gwYDMIg",
preferred_v3_elements: preferred_v3_elements,
preferred_v3_intents: preferred_v3_intents
)
FactoryBot.create(:product, name: "DL-44")
visit spree.root_path
click_link "DL-44"
click_button "Add To Cart"
expect(page).to have_current_path("/cart")
click_button "Checkout"
expect(page).to have_current_path("/checkout/registration")
click_link "Create a new account"
within("#new_spree_user") do
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "superStrongPassword"
fill_in "Password Confirmation", with: "superStrongPassword"
end
click_button "Create"
# Address
expect(page).to have_current_path("/checkout/address")
within("#billing") do
fill_in_name
fill_in "Street Address", with: "YT-1300"
fill_in "City", with: "Mos Eisley"
select "United States of America", from: "Country"
select country.states.first.name, from: "order_bill_address_attributes_state_id"
fill_in "Zip", with: "12010"
fill_in "Phone", with: "(555) 555-5555"
end
click_on "Save and Continue"
# Delivery
expect(page).to have_current_path("/checkout/delivery")
expect(page).to have_content("UPS Ground")
click_on "Save and Continue"
# Payment
expect(page).to have_current_path("/checkout/payment")
fill_in_card
click_button "Save and Continue"
# Confirmation
expect(page).to have_current_path("/checkout/confirm")
click_button "Place Order"
expect(page).to have_content("Your order has been processed successfully")
# Begin Second Purchase
visit spree.root_path
click_link "DL-44"
click_button "Add To Cart"
expect(page).to have_current_path("/cart")
click_button "Checkout"
# Address
expect(page).to have_current_path("/checkout/address")
within("#billing") do
fill_in_name
fill_in "Street Address", with: "YT-1300"
fill_in "City", with: "Mos Eisley"
select "United States of America", from: "Country"
select country.states.first.name, from: "order_bill_address_attributes_state_id"
fill_in "Zip", with: "12010"
fill_in "Phone", with: "(555) 555-5555"
end
click_on "Save and Continue"
# Delivery
expect(page).to have_current_path("/checkout/delivery")
expect(page).to have_content("UPS Ground")
click_on "Save and Continue"
# Payment
expect(page).to have_current_path("/checkout/payment")
end
shared_examples "Maintain Consistent Stripe Customer Across Purchases" do
it "can re-use saved cards and maintain the same Stripe payment ID and customer ID", js: true do
choose "Use an existing card on file"
click_button "Save and Continue"
# Confirm
expect(page).to have_current_path("/checkout/confirm")
click_button "Place Order"
expect(page).to have_content("Your order has been processed successfully")
user = Spree::User.find_by(email: "[email protected]")
user_sources = user.wallet.wallet_payment_sources
expect(user_sources.size).to eq(1)
user_card = user_sources.first.payment_source
expect(user_card.gateway_customer_profile_id).to start_with 'cus_'
expect(user_card.gateway_payment_profile_id).to start_with (preferred_v3_intents ? 'pm_' : 'card_')
stripe_customer = Stripe::Customer.retrieve(user_card.gateway_customer_profile_id)
expect(stripe_customer[:email]).to eq(user.email)
stripe_customer_cards = Stripe::PaymentMethod.list(customer: stripe_customer, type: 'card')
expect(stripe_customer_cards.count).to eq(1)
expect(stripe_customer_cards.first.customer).to eq(user_card.gateway_customer_profile_id)
expect(stripe_customer_cards.first.id).to eq(user_card.gateway_payment_profile_id)
expect(user.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq.size).to eq(1)
expect(user.orders.map { |o| o.payments.valid.first.source.gateway_customer_profile_id }.uniq.size).to eq(1)
end
it "can use a new card and maintain the same Stripe customer ID", js: true do
choose "Use a new card / payment method"
fill_in_card(number: '5555 5555 5555 4444')
click_button "Save and Continue"
# Confirm
expect(page).to have_current_path("/checkout/confirm")
user = Spree::User.find_by(email: "[email protected]")
user_cards = user.credit_cards
expect(user_cards.size).to eq(2)
expect(user_cards.pluck(:gateway_customer_profile_id)).to all( start_with 'cus_' )
expect(user_cards.pluck(:gateway_payment_profile_id)).to all( start_with (preferred_v3_intents ? 'pm_' : 'card_'))
expect(user_cards.last.gateway_customer_profile_id).to eq(user_cards.first.gateway_customer_profile_id)
expect(user_cards.pluck(:gateway_customer_profile_id).uniq.size).to eq(1)
click_button "Place Order"
expect(page).to have_content("Your order has been processed successfully")
expect(user.wallet.wallet_payment_sources.size).to eq(2)
expect(user.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq.size).to eq(2)
expect(user.orders.map { |o| o.payments.valid.first.source.gateway_customer_profile_id }.uniq.size).to eq(1)
stripe_customer = Stripe::Customer.retrieve(user_cards.last.gateway_customer_profile_id)
stripe_customer_cards = Stripe::PaymentMethod.list(customer: stripe_customer, type: 'card')
expect(stripe_customer_cards.count).to eq(2)
expect(stripe_customer_cards.data.map(&:id)).to match_array(user.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq)
expect(stripe_customer_cards.data.map(&:id)).to match_array(user_cards.pluck(:gateway_payment_profile_id))
end
end
context 'when using Stripe V2 API library' do
let(:preferred_v3_elements) { false }
let(:preferred_v3_intents) { false }
it_behaves_like "Maintain Consistent Stripe Customer Across Purchases"
end
context 'when using Stripe V3 API library with Elements' do
let(:preferred_v3_elements) { true }
let(:preferred_v3_intents) { false }
it_behaves_like "Maintain Consistent Stripe Customer Across Purchases"
end
context 'when using Stripe V3 API library with Intents' do
let(:preferred_v3_elements) { false }
let(:preferred_v3_intents) { true }
it_behaves_like "Maintain Consistent Stripe Customer Across Purchases"
end
end