Merge pull request #5766 from mkllnk/5764-fix-pin-payments

5764 Provide credit card brand to Pin Payments
This commit is contained in:
Maikel
2020-07-16 10:28:18 +10:00
committed by GitHub
2 changed files with 44 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ module Checkout
move_payment_source_to_payment_attributes!
fill_in_card_type
set_amount_in_payments_attributes
construct_saved_card_attributes if @params[:order][:existing_card_id]
@@ -31,6 +33,28 @@ module Checkout
@params[:order][:payments_attributes].first[:source_attributes] = payment_source_params
end
# Ensures cc_type is always passed to the model by inferring the type when
# the frontend didn't provide it. This fixes Pin Payments specifically
# although it might be useful for future payment gateways.
#
# More details: app/assets/javascripts/darkswarm/services/checkout.js.coffee#L70-L98
def fill_in_card_type
return unless payment_source_attributes
return if payment_source_attributes.dig(:number).blank?
payment_source_attributes[:cc_type] ||= card_brand(payment_source_attributes[:number])
end
def payment_source_attributes
@payment_source_attributes ||=
params[:order][:payments_attributes]&.first&.dig(:source_attributes)
end
def card_brand(number)
ActiveMerchant::Billing::CreditCard.brand?(number)
end
def delete_payment_source_params!
@params.delete(:payment_source)[
@params[:order][:payments_attributes].first[:payment_method_id].underscore

View File

@@ -36,6 +36,26 @@ describe Checkout::FormDataAdapter do
end
end
describe "and a credit card is provided" do
before do
params[:order][:payments_attributes].first[:source_attributes] = {number: "4444333322221111"}
end
it "fills in missing credit card brand" do
expect(adapter.params[:order][:payments_attributes].first[:source_attributes][:cc_type]).to eq "visa"
end
it "leaves an existing credit card brand" do
params[:order][:payments_attributes].first[:source_attributes][:cc_type] = "test"
expect(adapter.params[:order][:payments_attributes].first[:source_attributes][:cc_type]).to eq "test"
end
it "doesn't touch the credit card brand without a number" do
params[:order][:payments_attributes].first[:source_attributes][:number] = ""
expect(adapter.params[:order][:payments_attributes].first[:source_attributes].key?(:cc_type)).to eq false
end
end
describe "and existing credit card is provided" do
before { params[:order][:existing_card_id] = credit_card.id }