Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions app/models/solidus_stripe/create_intents_payment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,46 @@ 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
}
}]
}
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
Expand Down
16 changes: 16 additions & 0 deletions spec/models/solidus_stripe/create_intents_payment_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"id" => intent_id,
"charges" => {
"data" => [{
"billing_details" => {
"name" => "John Doe"
},
"payment_method_details" => {
"card" => {
"brand" => "visa",
Expand Down Expand Up @@ -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) }

Expand Down