Reputation: 34884
I'm testing the web hooks in Stripe, I send an event from Stripe's website, here is my code:
require 'json'
class StripeWebHooksController < ApplicationController
protect_from_forgery except: :webhook
def webhook
data_json = JSON.parse(request.body.read)
event = Stripe::Event.retrieve(data_json['id'])
#....
It always throws an exception:
Stripe::InvalidRequestError (Could not determine which URL to request: Stripe::Event instance has invalid ID: nil)
On Stripes's website it says the id is 00000 for some reason, whereas in production.log it's not zeros, it has a real id.
Here is the data from Stripe.com:
{
"created": 1326853478,
"livemode": false,
"id": "evt_00000000000000",
"type": "charge.succeeded",
"object": "event",
"request": null,
"pending_webhooks": 1,
"api_version": "2015-02-10",
"data": { # and so on....
And here's from production.log:
[- 8b95acf0-96ab-0132-4be7-42010af08843] Parameters: {"id"=>"evt_15VG6eKl7JrIRpD6md9wpwUu", "created"=>1423721144, "livemode"=>false, "type"=>"invoice.created", "data"=>{"object"=>{"date"=>1423721144, "id"=>"in_15VG6eKl7JrIRpD6Nnd5hqm3", "period_start"=>1423721144, "period_end"=>1423721144, "lines"=>{"object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/invoices/in_15VG6eKl7JrIRpD6Nnd5hqm3/lines", "data"=>[{"id"=>"sub_5gdNIr16MzaeUe", "object"=>"line_item", "type"=>"subscription", "livemode"=>false, "amount"=>28000, "currency"=>"cad", "proration"=>false, "period"=>{"start"=>1423721144, "end"=>1426140344}, "subscription"=>nil, "quantity"=>1, "plan"=>{"interval"=>"month", "name"=>"accelerated_test", "created"=>1423705393, "amount"=>28000, "currency"=>"cad", "id"=>"accelerated_test", "object"=>"plan", "livemode"=>false, "interval_count"=>1, "trial_period_days"=>nil, "metadata"=>{}, "statement_descriptor"=>nil, "statement_description"=>nil}, "description"=>nil, "metadata"=>{}}]}, "subtotal"=>28000, "total"=>28000, "customer"=>"cus_5gdN3daWCJVNzg", "object"=>"invoice", "attempted"=>true, "closed"=>true, "forgiven"=>false, "paid"=>true, "livemode"=>false, "attempt_count"=>1, "amount_due"=>28000, "currency"=>"cad", "starting_balance"=>0, "ending_balance"=>0, "next_payment_attempt"=>nil, "webhooks_delivered_at"=>nil, "charge"=>"ch_15VG6eKl7JrIRpD6nHI4iIdx", "discount"=>nil, "application_fee"=>nil, "subscription"=>"sub_5gdNIr16MzaeUe", "tax_percent"=>nil, "metadata"=>{}, "statement_descriptor"=>nil, "description"=>nil, "receipt_number"=>nil, "statement_description"=>nil}}, "object"=>"event", "pending_webhooks"=>1, "request"=>"iar_5gdNTJThO2GckT", "api_version"=>"2014-11-20"}
Anyway, even if it's zeros, it doesn't have to raise an exception, does it? If it does, how do I test it after all?
Upvotes: 2
Views: 2785
Reputation: 25552
The issue here is that the Test Webhook button is sending a fake event so you can't retrieve it through the API. You need to add some specific code so that when the event id is "evt_00000000000000"
you don't call Stripe::Event.retrieve(data_json['id'])
If you want to test webhooks just create customers or charges in test mode in your account and events will be sent automatically.
Upvotes: 3