Skip to content

Commit

Permalink
Add specs for Stripe customer accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
brchristian committed Aug 15, 2020
1 parent 36d0f64 commit a7d01ca
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 14 deletions.
1 change: 1 addition & 0 deletions solidus_stripe.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'solidus_core', ['>= 2.3', '< 3']
spec.add_dependency 'solidus_support', '~> 0.5'
spec.add_dependency 'activemerchant', '>= 1.100'
spec.add_dependency 'stripe'

spec.add_development_dependency 'solidus_dev_support'
end
28 changes: 14 additions & 14 deletions spec/features/stripe_checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,63 +124,63 @@
end

it "shows an error with a missing credit card number", js: true do
fill_in_card({ number: "", code: "" })
fill_in_card(number: "", code: "")
click_button "Save and Continue"
expect(page).to have_content("Could not find payment information")
end

it "shows an error with a missing expiration date", js: true do
fill_in_card({ exp_month: "", exp_year: "" })
fill_in_card(exp_month: "", exp_year: "")
click_button "Save and Continue"
expect(page).to have_content("Your card's expiration year is invalid.")
end

it "shows an error with an invalid credit card number", js: true do
fill_in_card({ number: "1111 1111 1111 1111" })
fill_in_card(number: "1111 1111 1111 1111")
click_button "Save and Continue"
expect(page).to have_content("Your card number is incorrect.")
end

it "shows an error with invalid security fields", js: true do
fill_in_card({ code: "12" })
fill_in_card(code: "12")
click_button "Save and Continue"
expect(page).to have_content("Your card's security code is invalid.")
end

it "shows an error with invalid expiry fields", js: true do
fill_in_card({ exp_month: "00" })
fill_in_card(exp_month: "00")
click_button "Save and Continue"
expect(page).to have_content("Your card's expiration month is invalid.")
end
end

shared_examples "Stripe Elements invalid payments" do
it "shows an error with a missing credit card number" do
fill_in_card({ number: "" })
fill_in_card(number: "")
click_button "Save and Continue"
expect(page).to have_content("Your card number is incomplete.")
end

it "shows an error with a missing expiration date" do
fill_in_card({ exp_month: "", exp_year: "" })
fill_in_card(exp_month: "", exp_year: "")
click_button "Save and Continue"
expect(page).to have_content("Your card's expiration date is incomplete.")
end

it "shows an error with an invalid credit card number" do
fill_in_card({ number: "1111 1111 1111 1111" })
fill_in_card(number: "1111 1111 1111 1111")
click_button "Save and Continue"
expect(page).to have_content("Your card number is invalid.")
end

it "shows an error with invalid security fields" do
fill_in_card({ code: "12" })
fill_in_card(code: "12")
click_button "Save and Continue"
expect(page).to have_content("Your card's security code is incomplete.")
end

it "shows an error with invalid expiry fields" do
fill_in_card({ exp_month: "01", exp_year: "3" })
fill_in_card(exp_month: "01", exp_year: "3")
click_button "Save and Continue"
expect(page).to have_content("Your card's expiration date is incomplete.")
end
Expand Down Expand Up @@ -308,7 +308,7 @@

context "when using a card without enough money" do
it "fails the payment" do
fill_in_card({ number: "4000 0000 0000 9995" })
fill_in_card(number: "4000 0000 0000 9995")
click_button "Save and Continue"

expect(page).to have_content "Your card has insufficient funds."
Expand All @@ -317,7 +317,7 @@

context "when entering the wrong 3D verification code" do
it "fails the payment" do
fill_in_card({ number: "4000 0084 0000 1629" })
fill_in_card(number: "4000 0084 0000 1629")
click_button "Save and Continue"

within_3d_secure_modal do
Expand Down Expand Up @@ -414,7 +414,7 @@
let(:regular_card) { "4242 4242 4242 4242" }

it "voids the first stripe payment and successfully pays with 3DS card" do
fill_in_card({ number: regular_card })
fill_in_card(number: regular_card)
click_button "Save and Continue"

expect(page).to have_content "Ending in #{regular_card.last(4)}"
Expand Down Expand Up @@ -481,7 +481,7 @@ def within_3d_secure_modal
end

def authenticate_3d_secure_card(card_number)
fill_in_card({ number: card_number })
fill_in_card(number: card_number)
click_button "Save and Continue"

within_3d_secure_modal do
Expand Down
182 changes: 182 additions & 0 deletions spec/features/stripe_customer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit a7d01ca

Please sign in to comment.