Skip to content

Commit

Permalink
add specs for V3 Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
brchristian committed Jul 26, 2020
1 parent 59b1592 commit f1b4331
Showing 1 changed file with 159 additions and 0 deletions.
159 changes: 159 additions & 0 deletions spec/features/stripe_customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,163 @@
expect(stripe_customer_cards.data.map { |card| card.id }.sort).to eq(u.credit_cards.pluck(:gateway_payment_profile_id).sort)
end
end

context 'when using Stripe V3 API library with Elements' do
let(:preferred_v3_elements) { true }
let(:preferred_v3_intents) { false }

before do
click_on "Save and Continue"
expect(page).to have_current_path("/checkout/payment")
end

it "can re-use saved cards and maintain the same Stripe payment ID and customer ID", js: true do
within_frame find('#card_number iframe') do
'4242 4242 4242 4242'.split('').each { |n| find_field('cardnumber').native.send_keys(n) }
end
within_frame(find '#card_cvc iframe') { fill_in 'cvc', with: '123' }
within_frame(find '#card_expiry iframe') do
'0132'.split('').each { |n| find_field('exp-date').native.send_keys(n) }
end
click_button "Save and Continue"

expect(page).to have_current_path("/checkout/confirm")
click_button "Place Order"
expect(page).to have_content("Your order has been processed successfully")

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")
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")

u = Spree::User.find_by_email("[email protected]")
expect(u.wallet.wallet_payment_sources.size).to eq(1)

cc = u.wallet.wallet_payment_sources.first.payment_source
expect(cc.gateway_customer_profile_id).to start_with 'cus_'
expect(cc.gateway_payment_profile_id).to start_with 'card_'
stripe_customer = Stripe::Customer.retrieve(cc.gateway_customer_profile_id)
expect(stripe_customer[:sources][:total_count]).to eq(1)
expect(stripe_customer[:sources][:data].first[:customer]).to eq(cc.gateway_customer_profile_id)
expect(stripe_customer[:sources][:data].first[:id]).to eq(cc.gateway_payment_profile_id)

expect(u.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq.size).to eq(1)
expect(u.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
within_frame find('#card_number iframe') do
'4242 4242 4242 4242'.split('').each { |n| find_field('cardnumber').native.send_keys(n) }
end
within_frame(find '#card_cvc iframe') { fill_in 'cvc', with: '123' }
within_frame(find '#card_expiry iframe') do
'0132'.split('').each { |n| find_field('exp-date').native.send_keys(n) }
end
click_button "Save and Continue"

expect(page).to have_current_path("/checkout/confirm")
click_button "Place Order"
expect(page).to have_content("Your order has been processed successfully")

u = Spree::User.find_by_email("[email protected]")
expect(u.wallet.wallet_payment_sources.size).to eq(1)

cc = u.credit_cards.first
expect(cc.gateway_customer_profile_id).to start_with 'cus_'
expect(cc.gateway_payment_profile_id).to start_with 'card_'
stripe_customer = Stripe::Customer.retrieve(cc.gateway_customer_profile_id)
expect(stripe_customer[:sources][:total_count]).to eq(1)
expect(stripe_customer[:sources][:data].first[:customer]).to eq(cc.gateway_customer_profile_id)
expect(stripe_customer[:sources][:data].first[:id]).to eq(cc.gateway_payment_profile_id)

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")
choose "Use a new card / payment method"
within_frame find('#card_number iframe') do
'5555 5555 5555 4444'.split('').each { |n| find_field('cardnumber').native.send_keys(n) }
end
within_frame(find '#card_cvc iframe') { fill_in 'cvc', with: '123' }
within_frame(find '#card_expiry iframe') do
'0132'.split('').each { |n| find_field('exp-date').native.send_keys(n) }
end
click_button "Save and Continue"

# Confirm
expect(page).to have_current_path("/checkout/confirm")
u = Spree::User.find_by_email("[email protected]")
expect(u.credit_cards.size).to eq(2)
expect(u.credit_cards.last.gateway_customer_profile_id).to start_with 'cus_'
expect(u.credit_cards.last.gateway_payment_profile_id).to start_with 'card_'
expect(u.credit_cards.last.gateway_customer_profile_id).to eq(Spree::User.find_by_email("[email protected]").credit_cards.first.gateway_customer_profile_id)
expect(u.credit_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(u.wallet.wallet_payment_sources.size).to eq(2)
expect(u.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq.size).to eq(2)
expect(u.orders.map { |o| o.payments.valid.first.source.gateway_customer_profile_id }.uniq.size).to eq(1)

stripe_customer_cards = Stripe::PaymentMethod.list({ customer: stripe_customer.id, type: 'card' })
expect(stripe_customer_cards.count).to eq(2)
expect(stripe_customer_cards.data.map { |card| card.id }.sort).to eq(u.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq.sort)
expect(stripe_customer_cards.data.map { |card| card.id }.sort).to eq(u.credit_cards.pluck(:gateway_payment_profile_id).sort)
end
end
end

0 comments on commit f1b4331

Please sign in to comment.