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
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ Rails/NotNullColumn:
Layout/LineLength:
Exclude:
- db/migrate/**/*

RSpec/FilePath:
Exclude:
- spec/requests/solidus_stripe/webhooks_controller/**/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module SolidusStripe
module Webhook
# Handlers for Stripe payment_intent events.
class PaymentIntentSubscriber
include Omnes::Subscriber

handle :"stripe.payment_intent.succeeded", with: :complete_payment

# Captures a payment.
#
# Marks a Solidus payment associated to a Stripe payment intent as
# completed, adding a log entry about the event.
#
# @param event [SolidusStripe::Webhook::Event]
def complete_payment(event)
payment_intent_id = event.data.object.id
payment = Spree::Payment.find_by!(response_code: payment_intent_id)
return if payment.completed?

payment.complete!.tap do
SolidusStripe::LogEntries.payment_log(
payment,
success: true,
message: "Capture was successful after payment_intent.succeeded webhook"
)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/solidus_stripe/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Engine < Rails::Engine
user_events: SolidusStripe.configuration.webhook_events,
bus: Spree::Bus
)
SolidusStripe::Webhook::PaymentIntentSubscriber.new.subscribe_to(Spree::Bus)
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/solidus_stripe/webhook/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class Event
PREFIX = "stripe."
private_constant :PREFIX

# TBD
CORE_EVENTS = Set[*%i[]].freeze
CORE_EVENTS = Set[*%i[
payment_intent.succeeded
]].freeze
private_constant :CORE_EVENTS

# @api private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "solidus_stripe_spec_helper"

RSpec.describe SolidusStripe::WebhooksController, type: %i[request webhook_request] do
describe "POST /create payment_intent.succeeded" do
it "transitions the associated payment to completed" do
payment_method = create(:stripe_payment_method)
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
payment = create(:payment,
payment_method: payment_method,
response_code: stripe_payment_intent.id,
state: "pending")
context = SolidusStripe::Webhook::EventWithContextFactory.from_object(
payment_method: payment_method,
object: stripe_payment_intent,
type: "payment_intent.succeeded"
)

expect do
webhook_request(context)
end.to change { payment.reload.state }.from("pending").to("completed")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require "solidus_stripe_spec_helper"

RSpec.describe SolidusStripe::Webhook::PaymentIntentSubscriber do
describe "#complete_payment" do
it "completes a pending payment" do
payment_method = create(:stripe_payment_method)
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
payment = create(:payment,
payment_method: payment_method,
response_code: stripe_payment_intent.id,
state: "pending")
event = SolidusStripe::Webhook::EventWithContextFactory.from_object(
payment_method: payment_method,
object: stripe_payment_intent,
type: "payment_intent.succeeded"
).solidus_stripe_object

described_class.new.complete_payment(event)

expect(payment.reload.state).to eq "completed"
end

it "adds a log entry to the payment" do
payment_method = create(:stripe_payment_method)
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
payment = create(:payment,
payment_method: payment_method,
response_code: stripe_payment_intent.id,
state: "pending")
event = SolidusStripe::Webhook::EventWithContextFactory.from_object(
payment_method: payment_method,
object: stripe_payment_intent,
type: "payment_intent.succeeded"
).solidus_stripe_object

described_class.new.complete_payment(event)

expect(
payment.log_entries.last.parsed_details.message
).to eq "Capture was successful after payment_intent.succeeded webhook"
end

it "does nothing if the payment is already completed" do
payment_method = create(:stripe_payment_method)
stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
payment = create(:payment,
payment_method: payment_method,
response_code: stripe_payment_intent.id,
state: "completed")
event = SolidusStripe::Webhook::EventWithContextFactory.from_object(
payment_method: payment_method,
object: stripe_payment_intent,
type: "payment_intent.succeeded",
).solidus_stripe_object

described_class.new.complete_payment(event)

expect(payment.reload.state).to eq "completed"
expect(payment.log_entries.count).to be(0)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module Webhook
# Factory to create a webhook event along with its context.
#
# It allows to create Stripe webhook from different sources (hash, stripe
# object) in different representations (json, stripe event object, header).
# object) in different representations (json, stripe event object, solidus
# stripe object, header).
#
# The context for a event is composed by the timestamp and its secret, which
# in turn affect the header representation.
Expand Down Expand Up @@ -56,6 +57,11 @@ def stripe_object
@stripe_object ||= Stripe::Event.construct_from(data)
end

def solidus_stripe_object

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please help me understand why this method is called solidus_stripe_object and returns a Webhook Event?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EventWithContext class can return a Stripe event in different representations:

  • #json: A JSON string representation
  • #signature_header: How it looks signed in the request header
  • #stripe_object: As a Stripe::Event object.
  • #solidus_stripe_object: As a SolidusStripe::Webhook::Event object.

I know it's a bit cryptic, but I can't find better candidates. We can't call the #stripe_object method #stripe_event, because all those methods return representations of a Stripe event. Once we have that method, the most logical candidate for the new addition is #solidus_stripe_object.

Happy to change it if we find a better solution!!

@solidus_stripe_object = SolidusStripe::Webhook::Event.new(stripe_event: stripe_object,
spree_payment_method: @payment_method)
end

def json
@json ||= JSON.generate(data)
end
Expand Down