Skip to content

Commit

Permalink
Add auto-capture functionality to Stripe integration
Browse files Browse the repository at this point in the history
With this commit, the Solidus auto-capture feature has been incorporated into
the Stripe integration through the use of the auto_capture flag in
the Spree::PaymentMethod base model.
Stripe `capture_method` is now auto configured during the intent creation with
 "manual" or "automatic" based on the auto_capture flag in the related
payment method.

More information can be found in the Stripe documentation:
https://stripe.com/docs/api/payment_intents/object#payment_intent_object-capture_method
  • Loading branch information
rainerdema committed Mar 29, 2023
1 parent 265aa6e commit d465a72
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/models/solidus_stripe/payment_intent.rb
Expand Up @@ -35,6 +35,9 @@ def process_payment
when 'requires_capture'
payment.pend! unless payment.pending?
successful = true
when 'succeeded'
payment.complete! unless payment.completed?
successful = true
else
payment.failure!
successful = false
Expand Down Expand Up @@ -81,7 +84,7 @@ def create_stripe_intent(**stripe_intent_options)
order.currency,
),
currency: order.currency,
capture_method: 'manual',
capture_method: payment_method.auto_capture? ? 'automatic' : 'manual',
setup_future_usage: payment_method.preferred_setup_future_usage.presence,
customer: stripe_customer_id,
metadata: { solidus_order_number: order.number },
Expand Down
26 changes: 22 additions & 4 deletions spec/support/solidus_stripe/checkout_test_helper.rb
Expand Up @@ -23,8 +23,12 @@ def assigns_guest_token(guest_token)
# rubocop:enable RSpec/AnyInstance
end

def creates_payment_method(setup_future_usage: 'off_session')
@payment_method = create(:stripe_payment_method, preferred_setup_future_usage: setup_future_usage)
def creates_payment_method(setup_future_usage: 'off_session', auto_capture: false)
@payment_method = create(
:stripe_payment_method,
preferred_setup_future_usage: setup_future_usage,
auto_capture: auto_capture
)
end

def payment_method
Expand Down Expand Up @@ -222,6 +226,15 @@ def payment_intent_is_created_with_required_capture
expect(intent.status).to eq('requires_capture')
end

def payment_intent_is_created_and_successfully_captured
order = Spree::Order.last
intent = SolidusStripe::PaymentIntent.where(
payment_method: payment_method,
order: order
).last.stripe_intent
expect(intent.status).to eq('succeeded')
end

def payment_intent_is_created_with_required_action
order = Spree::Order.last
intent = SolidusStripe::PaymentIntent.where(
Expand Down Expand Up @@ -295,14 +308,19 @@ def declined_cards_at_intent_creation_are_notified
end
end

def successfully_creates_a_payment_intent(user: nil)
def successfully_creates_a_payment_intent(user: nil, auto_capture: false)
visits_payment_step(user: user)
chooses_new_stripe_payment
fills_stripe_form

submits_payment

completes_order
payment_intent_is_created_with_required_capture

if auto_capture
payment_intent_is_created_and_successfully_captured
else
payment_intent_is_created_with_required_capture
end
end
end
8 changes: 8 additions & 0 deletions spec/system/frontend/solidus_stripe/checkout_spec.rb
Expand Up @@ -27,6 +27,14 @@
end
end

context 'when auto-capture is enabled' do
it 'creates a payment intent and automatically processes payment' do
creates_payment_method(auto_capture: true)

successfully_creates_a_payment_intent(user: create(:user), auto_capture: true)
end
end

context 'with a registered user' do
['on_session', 'off_session'].each do |setup_future_usage|
context "when setup_future_usage is set with '#{setup_future_usage}'" do
Expand Down

0 comments on commit d465a72

Please sign in to comment.