Repository navigation
Replies: 10 comments 8 replies
|
Do not customize Pay’s docs describe this as “Custom Webhook Listeners”: subscribe a class to the Stripe event name in For example, if your own # app/webhooks/my_charge_succeeded_processor.rb
class MyChargeSucceededProcessor
def call(event)
stripe_charge = event.data.object
pay_charge = Pay::Stripe::Charge.sync(
stripe_charge.id,
stripe_account: event.try(:account)
)
Payment.find_or_create_by!(stripe_charge_id: stripe_charge.id) do |payment|
payment.amount_cents = stripe_charge.amount
payment.currency = stripe_charge.currency
payment.pay_charge = pay_charge if payment.respond_to?(:pay_charge=)
end
end
endThen subscribe it: # config/initializers/pay.rb
ActiveSupport.on_load(:pay) do
Pay::Webhooks.delegator.subscribe "stripe.charge.succeeded", MyChargeSucceededProcessor.new
endPick the event that matches your business moment. For one-time successful charges, Two caveats:
If this gives you the extension point you needed, please mark it as answered so others do not try to override the Stripe webhook controller directly. |
|
[ActiveJob] [Pay::Webhooks::ProcessJob] [95ae4cdd-0781-47a0-a418-8063c41c4ed0] Error performing Pay::Webhooks::ProcessJob (Job ID: 95ae4cdd-0781-47a0-a418-8063c41c4ed0) from Async(default) in 786.3ms: NoMethodError (undefined method `fetch' for #Stripe::StripeObject:0xb11c JSON: { |
|
This is to show how I'm initially passing my metadata As you can see I didn't wrap my hash in an array because I didn't see any instructions that said I should class CheckoutsController < ApplicationController ) |
|
Your metadata hash shape is fine. You do not need to wrap it in an array. There are two separate things to line up:
class FulfillCheckout
def call(event)
session = event.data.object
return unless session.payment_status == "paid"
post_id = session.metadata["post_id"]
user_id = session.metadata["user_id"]
end
endThat matches the Pay Checkout docs, which recommend
@checkout_session = current_user.payment_processor.checkout(
mode: "payment",
line_items: Rails.env.development? ? "price_1Tyv8xBTuznlCAVoImGv3rmS" : "price_1TyxlwBTuznlCAVo9UUMer4W",
metadata: {
post_id: params[:post_id].to_s,
user_id: current_user.id.to_s
},
payment_intent_data: {
metadata: {
post_id: params[:post_id].to_s,
user_id: current_user.id.to_s
}
}
)Pay passes extra checkout options through to The event.data.object.metadata.fetch("post_id")Change that access to bracket access: metadata = event.data.object.metadata || {}
post_id = metadata["post_id"]Also, the single string So the decision rule is: use |
|
I added --events checkout.session.completed to my CLI listener Am I supposed to leave it just listen |
|
Yes, leave the Stripe CLI process running while you make the test Checkout payment. It is the local forwarder; it needs to be listening when Stripe sends the event to your Rails app. For Pay, the forward target should be the Pay webhook endpoint: stripe listen --forward-to localhost:3000/pay/webhooks/stripe If you add this: --events checkout.session.completed then the CLI will only forward that event. That is OK if your custom listener is subscribed to
stripe listen --forward-to localhost:3000/pay/webhooks/stripe --events checkout.session.completed,checkout.session.async_payment_succeeded After starting that command, create a new Checkout session and complete it again. The CLI does not make an old event happen just by changing the On the Rails side, make sure your initializer is subscribed to the same Pay event name: Pay::Webhooks.delegator.subscribe "stripe.checkout.session.completed", FulfillCheckout.new Then inside the listener, read the Checkout Session object: session = event.data.object So the short answer is: yes, keep |
|
The class name resolves ok Of course it's the Pay gem webhook that I expect to in turn fire my custom class for additional tasks, maybe the gem fails to hand off processing to my class |
|
I may have found the problem, I had no config/initializers/stripe.rb What do I need to put in this file? signing_secret, test mode secret_key? |
|
I've been working this vanilla without Pay gem and so far although I receive webhooks, metadata isn't included in the webhooks although the request.body does include metadata as an empty hash |
|
Sorry for being confused I tried to redirect to Stripe from a controller but I wasn't having any luck so I'm making my request to checkout from a view I finally got this working but I don't know how to recall metadata->post_id from this webhook What's shown below results from JSON.parse(request.body) with the metadata at the very bottom because there was no need to paste beyond that I haven't considered whether or not to symbolize I'm looking to use dot notation in my code "=>"evt_1U3Wu1BTuznlCAVo6UUC64yj", |
Uh oh!
There was an error while loading. Please reload this page.
Great to see all these tables keeping track of customer payments but how do I add additional tasks, in my case add a record to my own Payment model
All reactions