-
-
Notifications
You must be signed in to change notification settings - Fork 66
Handle payment_intent.succeeded event #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
elia
merged 1 commit into
master
from
waiting-for-dev/handle_payment_itent_succeeded_event
Mar 17, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/subscribers/solidus_stripe/webhook/payment_intent_subscriber.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
spec/requests/solidus_stripe/webhooks_controller/payment_intent/succeeded_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
64 changes: 64 additions & 0 deletions
64
spec/subscribers/solidus_stripe/webhook/payment_intent_subscriber_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_objectand returns a Webhook Event?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
EventWithContextclass 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 aStripe::Eventobject.#solidus_stripe_object: As aSolidusStripe::Webhook::Eventobject.I know it's a bit cryptic, but I can't find better candidates. We can't call the
#stripe_objectmethod#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!!