-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor create_profile to use Stripe gem
- Loading branch information
1 parent
5d43fed
commit 49ab060
Showing
8 changed files
with
158 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module CreditCardDecorator | ||
def cc_type=(type) | ||
# See https://stripe.com/docs/api/cards/object#card_object-brand, | ||
# active_merchant/lib/active_merchant/billing/credit_card.rb, | ||
# and active_merchant/lib/active_merchant/billing/credit_card_methods.rb | ||
# (And see also the Solidus docs at core/app/models/spree/credit_card.rb, | ||
# which indicate that Solidus uses ActiveMerchant conventions by default.) | ||
self[:cc_type] = case type | ||
when 'American Express' | ||
'american_express' | ||
when 'Diners Club' | ||
'diners_club' | ||
when 'Discover' | ||
'discover' | ||
when 'JCB' | ||
'jcb' | ||
when 'MasterCard' | ||
'master' | ||
when 'UnionPay' | ||
'unionpay' | ||
when 'Visa' | ||
'visa' | ||
when 'Unknown' | ||
super('') | ||
else | ||
super(type) | ||
end | ||
end | ||
|
||
::Spree::CreditCard.prepend(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,6 @@ | |
|
||
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" | ||
|
||
|
@@ -110,38 +109,38 @@ | |
click_button "Place Order" | ||
expect(page).to have_content("Your order has been processed successfully") | ||
|
||
user = Spree::User.find_by_email("[email protected]") | ||
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 'card_' | ||
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) | ||
expect(stripe_customer[:sources][:total_count]).to eq(1) | ||
expect(stripe_customer[:sources][:data].first[:customer]).to eq(user_card.gateway_customer_profile_id) | ||
expect(stripe_customer[:sources][:data].first[:id]).to eq(user_card.gateway_payment_profile_id) | ||
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' }) | ||
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 = 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 'card_' ) | ||
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) | ||
|
||
|
@@ -153,10 +152,10 @@ | |
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.id, type: 'card' }) | ||
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 { |card| card.id }).to match_array(user.orders.map { |o| o.payments.valid.first.source.gateway_payment_profile_id }.uniq) | ||
expect(stripe_customer_cards.data.map { |card| card.id }).to match_array(user_cards.pluck(:gateway_payment_profile_id)) | ||
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 | ||
|
||
|
@@ -173,4 +172,11 @@ | |
|
||
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 |
Oops, something went wrong.