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
29 changes: 19 additions & 10 deletions app/models/solidus_stripe/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ def initialize(options)

# Authorizes a certain amount on the provided payment source.
#
# @see #purchase
# 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.
def authorize(amount_in_cents, source, options = {})
check_given_amount_matches_payment_intent(amount_in_cents, options)

stripe_payment_intent = create_stripe_payment_intent(
stripe_payment_intent = create_confirmed_stripe_payment_intent(
source,
options[:originator].order,
stripe_payment_intent_options: {
capture_method: "manual",
confirm: true
capture_method: "manual"
}
)
build_payment_log(
Expand Down Expand Up @@ -77,16 +78,19 @@ def capture(amount_in_cents, payment_intent_id, options = {})
end

# 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.
#
# @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_stripe_payment_intent(
stripe_payment_intent = create_confirmed_stripe_payment_intent(
source,
options[:originator].order,
stripe_payment_intent_options: {
capture_method: "automatic",
confirm: true
capture_method: "automatic"
}
)
build_payment_log(
Expand Down Expand Up @@ -159,17 +163,22 @@ def request(&block)

private

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

SolidusStripe::PaymentIntent.create_stripe_intent(
payment_method: source.payment_method,
order: order,
stripe_intent_options: stripe_payment_intent_options.merge(
payment_method: stripe_payment_method.id,
customer: stripe_payment_method.customer
customer: stripe_payment_method.customer,
confirm: false
)
)
).tap { confirm_stripe_payment_intent(_1.id) }
end

def confirm_stripe_payment_intent(stripe_payment_intent_id)
request { Stripe::PaymentIntent.confirm(stripe_payment_intent_id) }
end

def capture_stripe_payment_intent(stripe_payment_intent_id)
Expand Down
8 changes: 6 additions & 2 deletions spec/models/solidus_stripe/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@

allow(source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
allow(Stripe::PaymentIntent).to receive(:create).and_return(stripe_payment_intent)
allow(Stripe::PaymentIntent).to receive(:confirm).with("pi_123").and_return(stripe_payment_intent)

result = gateway.authorize(123_45, source, currency: 'USD', originator: order.payments.first)

expect(Stripe::PaymentIntent).to have_received(:create).with(
amount: 123_45,
currency: 'USD',
capture_method: 'manual',
confirm: true,
confirm: false,
metadata: { solidus_order_number: order.number },
customer: "cus_123",
payment_method: "pm_123",
setup_future_usage: nil
)
expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
expect(result.params).to eq("data" => '{"id":"pi_123"}')
end

Expand Down Expand Up @@ -102,19 +104,21 @@

allow(source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
allow(Stripe::PaymentIntent).to receive(:create).and_return(stripe_payment_intent)
allow(Stripe::PaymentIntent).to receive(:confirm).with("pi_123").and_return(stripe_payment_intent)

result = gateway.purchase(123_45, source, currency: 'USD', originator: order.payments.first)

expect(Stripe::PaymentIntent).to have_received(:create).with(
amount: 123_45,
currency: 'USD',
capture_method: 'automatic',
confirm: true,
confirm: false,
metadata: { solidus_order_number: order.number },
customer: "cus_123",
payment_method: "pm_123",
setup_future_usage: nil
)
expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
expect(result.params).to eq("data" => '{"id":"pi_123"}')
end

Expand Down