diff --git a/app/models/solidus_stripe/create_intents_payment_service.rb b/app/models/solidus_stripe/create_intents_payment_service.rb index 4c6b750a..38183dbd 100644 --- a/app/models/solidus_stripe/create_intents_payment_service.rb +++ b/app/models/solidus_stripe/create_intents_payment_service.rb @@ -61,7 +61,7 @@ def payment_params cc_type: intent_card['brand'], last_digits: intent_card['last4'], gateway_payment_profile_id: intent_customer_profile, - name: address_full_name, + name: card_holder_name || address_full_name, address_attributes: address_attributes } }] @@ -69,26 +69,38 @@ def payment_params end def intent_card - intent.params['charges']['data'][0]['payment_method_details']['card'] + intent_data['payment_method_details']['card'] end def intent_customer_profile intent.params['payment_method'] end + def card_holder_name + (html_payment_source_data['name'] || intent_data['billing_details']['name']).presence + end + + def intent_data + intent.params['charges']['data'][0] + end + def form_data params[:form_data] end - def address_attributes + def html_payment_source_data if form_data.is_a?(String) data = Rack::Utils.parse_nested_query(form_data) - data['payment_source'][stripe.id.to_s]['address_attributes'] + data['payment_source'][stripe.id.to_s] else - SolidusStripe::AddressFromParamsService.new(form_data).call.attributes + {} end end + def address_attributes + html_payment_source_data['address_attributes'] || SolidusStripe::AddressFromParamsService.new(form_data).call.attributes + end + def address_full_name current_order.bill_address&.full_name || form_data[:recipient] end diff --git a/spec/models/solidus_stripe/create_intents_payment_service_spec.rb b/spec/models/solidus_stripe/create_intents_payment_service_spec.rb index 17713c63..cdc697ee 100644 --- a/spec/models/solidus_stripe/create_intents_payment_service_spec.rb +++ b/spec/models/solidus_stripe/create_intents_payment_service_spec.rb @@ -41,6 +41,9 @@ "id" => intent_id, "charges" => { "data" => [{ + "billing_details" => { + "name" => "John Doe" + }, "payment_method_details" => { "card" => { "brand" => "visa", @@ -70,6 +73,19 @@ expect(order.payments.last.reload).to be_pending end + it "creates a credit card with the correct information" do + expect { subject }.to change { Spree::CreditCard.count } + card = Spree::CreditCard.last + + aggregate_failures do + expect(card.name).to eq "John Doe" + expect(card.cc_type).to eq "visa" + expect(card.month).to eq "1" + expect(card.year).to eq "2022" + expect(card.last_digits).to eq "4242" + end + end + context "when for any reason the payment could not be created" do before { params[:form_data].delete(:city) }