Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add ability to store/read Credit Card wallet type, eg. Apple Pay (#12985)
Damian Legawiec95f1446312b7
diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rbindex 4a368c6ccc..d084a490d0 100644--- a/core/app/models/spree/credit_card.rb+++ b/core/app/models/spree/credit_card.rb@@ -51,6 +51,14 @@ module Spree# needed for some of the ActiveMerchant gateways (eg. SagePay)alias_attribute :brand, :cc_type+ store_accessor :private_metadata, :wallet++ # Returns the type of wallet the card is associated with, eg. "apple_pay", "google_pay", etc.+ # @return [String]+ def wallet_type+ wallet&.[]('type')+ end+# ActiveMerchant::Billing::CreditCard added this accessor used by some gateways.# More info: https://github.com/spree/spree/issues/6209#@@ -116,6 +124,8 @@ module Spree@verification_value = value.to_s.gsub(/\s/, '')end+ # Returns the last 4 digits of the card number.+ # @return [String], eg. "4338"def set_last_digitsself.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)end@@ -130,10 +140,13 @@ module Spreeend# Show the card number, with all but last 4 numbers replace with "X". (XXXX-XXXX-XXXX-4338)+ # @return [String]def display_number"XXXX-XXXX-XXXX-#{last_digits}"end+ # Show the card brand, eg. "VISA", "MASTERCARD", etc.+ # @return [String]def display_brandbrand.present? ? brand.upcase : Spree.t(:no_cc_type)end@@ -141,14 +154,21 @@ module Spree# ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.# Looking at the ActiveMerchant source code we should probably be calling #to_active_merchant before passing# the object to ActiveMerchant but this should do for now.+ #+ # Returns the first name of the cardholder.+ # @return [String]def first_namename.to_s.split(/[[:space:]]/, 2)[0]end+ # Returns the last name of the cardholder.+ # @return [String]def last_namename.to_s.split(/[[:space:]]/, 2)[1]end+ # Returns an ActiveMerchant::Billing::CreditCard object.+ # @return [ActiveMerchant::Billing::CreditCard]def to_active_merchantActiveMerchant::Billing::CreditCard.new(number: number,diff --git a/core/spec/models/spree/credit_card_spec.rb b/core/spec/models/spree/credit_card_spec.rbindex 0be83f105b..8fa35d3b85 100644--- a/core/spec/models/spree/credit_card_spec.rb+++ b/core/spec/models/spree/credit_card_spec.rb@@ -26,7 +26,7 @@ describe Spree::CreditCard, type: :model doexpect(credit_card.respond_to?(:track_data)).to be trueend- context '#can_capture?' do+ describe '#can_capture?' doshared_examples 'can be captured' doit 'can be captured' doexpect(credit_card.can_capture?(payment)).to be true@@ -54,7 +54,7 @@ describe Spree::CreditCard, type: :model doendend- context '#can_void?' do+ describe '#can_void?' docontext 'when payment is not voided' dobefore { allow(payment).to receive_messages(failed?: false, void?: false) }@@ -72,7 +72,7 @@ describe Spree::CreditCard, type: :model doendend- context '#can_credit?' do+ describe '#can_credit?' doshared_examples 'can not credit' doit 'can not credit' doexpect(credit_card.can_credit?(payment)).to be false@@ -106,7 +106,7 @@ describe Spree::CreditCard, type: :model doendend- context '#valid?' do+ describe '#valid?' doit 'validates presence of number' docredit_card.attributes = valid_credit_card_attributes.except(:number)expect(credit_card).not_to be_valid@@ -150,7 +150,7 @@ describe Spree::CreditCard, type: :model doendend- context '#save' do+ describe '#save' dobefore docredit_card.attributes = valid_credit_card_attributescredit_card.save!@@ -167,7 +167,7 @@ describe Spree::CreditCard, type: :model doendend- context '#number=' do+ describe '#number=' doit 'strips non-numeric characters from card input' docredit_card.number = '6011000990139424'expect(credit_card.number).to eq('6011000990139424')@@ -205,7 +205,7 @@ describe Spree::CreditCard, type: :model doend# Regression test for #3847 & #3896- context '#expiry=' do+ describe '#expiry=' doit 'can set with a 2-digit month and year' docredit_card.expiry = '04 / 14'expect(credit_card.month).to eq(4)@@ -246,7 +246,7 @@ describe Spree::CreditCard, type: :model doendend- context '#cc_type=' do+ describe '#cc_type=' doit 'converts between the different types' docredit_card.cc_type = 'mastercard'expect(credit_card.cc_type).to eq('master')@@ -299,13 +299,13 @@ describe Spree::CreditCard, type: :model doendend- context '#associations' do+ context 'Associations' doit 'is able to access its payments' doexpect { credit_card.payments.to_a }.not_to raise_errorendend- context '#first_name' do+ describe '#first_name' dobefore docredit_card.name = 'Ludwig van Beethoven'end@@ -315,7 +315,7 @@ describe Spree::CreditCard, type: :model doendend- context '#last_name' do+ describe '#last_name' dobefore docredit_card.name = 'Ludwig van Beethoven'end@@ -325,7 +325,7 @@ describe Spree::CreditCard, type: :model doendend- context '#to_active_merchant' do+ describe '#to_active_merchant' dobefore docredit_card.number = '4111111111111111'credit_card.year = Time.current.year@@ -377,7 +377,7 @@ describe Spree::CreditCard, type: :model doexpect { second.update!(default: true) }.not_to raise_errorend- describe 'scopes' do+ context 'Scopes' dodescribe '#not_expired' dolet(:previous_year) { DateTime.now.year - 1 }let(:current_year) { DateTime.now.year }@@ -422,8 +422,8 @@ describe Spree::CreditCard, type: :model doendend- context '#display_brand' do- let(:credit_card) { create(:credit_card, cc_type: cc_type, user_id: order.user_id) }+ describe '#display_brand' do+ let(:credit_card) { build(:credit_card, cc_type: cc_type, user_id: order.user_id) }context 'when the cc_type does not exist' dolet(:cc_type) { nil }@@ -437,4 +437,19 @@ describe Spree::CreditCard, type: :model doit { expect(credit_card.display_brand).to eq(cc_type.upcase) }endend++ describe '#wallet_type' do+ let(:credit_card) { build(:credit_card, private_metadata: metadata) }+ let(:metadata) { {} }++ context 'when the wallet_type does not exist' do+ it { expect(credit_card.wallet_type).to be_nil }+ end++ context 'when the wallet_type exists' do+ let(:metadata) { { wallet: { type: 'apple_pay' } } }++ it { expect(credit_card.wallet_type).to eq('apple_pay') }+ end+ endend