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
23 changes: 14 additions & 9 deletions app/models/solidus_stripe/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ def initialize(options)
# We create and confirm the Stripe payment intent in two steps. That's to
# guarantee that we associate a Solidus payment on the creation, and we can
# fetch it after the webhook event is published on confirmation.
#
# The Stripe payment intent id is copied over the Solidus payment
# `response_code` field.
def authorize(amount_in_cents, source, options = {})
check_given_amount_matches_payment_intent(amount_in_cents, options)

stripe_payment_intent = create_confirmed_stripe_payment_intent(
source,
options[:originator].order,
source: source,
payment: options[:originator],
stripe_payment_intent_options: {
capture_method: "manual"
}
Expand Down Expand Up @@ -79,16 +82,15 @@ def capture(amount_in_cents, payment_intent_id, options = {})

# Authorizes and captures a certain amount on the provided payment source.
#
# See `#authorize` for why the payment intent is created and confirmed in
# two steps.
# See `#authorize` for how the confirmation step is performed.
#
# @todo add support for purchasing custom amounts
def purchase(amount_in_cents, source, options = {})
check_given_amount_matches_payment_intent(amount_in_cents, options)

stripe_payment_intent = create_confirmed_stripe_payment_intent(
source,
options[:originator].order,
source: source,
payment: options[:originator],
stripe_payment_intent_options: {
capture_method: "automatic"
}
Expand Down Expand Up @@ -163,18 +165,21 @@ def request(&block)

private

def create_confirmed_stripe_payment_intent(source, order, stripe_payment_intent_options:)
def create_confirmed_stripe_payment_intent(source:, payment:, stripe_payment_intent_options:)
stripe_payment_method = source.stripe_payment_method

SolidusStripe::PaymentIntent.create_stripe_intent(
payment_method: source.payment_method,
order: order,
order: payment.order,
stripe_intent_options: stripe_payment_intent_options.merge(
payment_method: stripe_payment_method.id,
customer: stripe_payment_method.customer,
confirm: false
)
).tap { confirm_stripe_payment_intent(_1.id) }
).tap do
payment.update_column(:response_code, _1.id) # rubocop:disable Rails/SkipsModelValidations
confirm_stripe_payment_intent(_1.id)
end
end

def confirm_stripe_payment_intent(stripe_payment_intent_id)
Expand Down
4 changes: 4 additions & 0 deletions spec/models/solidus_stripe/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
source = build(:stripe_payment_source, stripe_payment_method_id: "pi_123", payment_method: payment_method)
gateway = payment_method.gateway
order = create(:order_with_stripe_payment, amount: 123.45, payment_method: payment_method)
payment = order.payments.last

allow(source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
allow(Stripe::PaymentIntent).to receive(:create).and_return(stripe_payment_intent)
Expand All @@ -31,6 +32,7 @@
)
expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
expect(result.params).to eq("data" => '{"id":"pi_123"}')
expect(payment.reload.response_code).to eq("pi_123")
end

it "raises if the given amount doesn't match the order total" do
Expand Down Expand Up @@ -101,6 +103,7 @@
source = build(:stripe_payment_source, stripe_payment_method_id: "pi_123", payment_method: payment_method)
gateway = payment_method.gateway
order = create(:order_with_stripe_payment, amount: 123.45, payment_method: payment_method)
payment = order.payments.last

allow(source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
allow(Stripe::PaymentIntent).to receive(:create).and_return(stripe_payment_intent)
Expand All @@ -120,6 +123,7 @@
)
expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
expect(result.params).to eq("data" => '{"id":"pi_123"}')
expect(payment.reload.response_code).to eq("pi_123")
end

it "raises if the given amount doesn't match the order total" do
Expand Down