Reputation: 347
I've set up my webhook in a hooks_controller and I'm successfully receiving the webhook.
However for some reason the changes from 'invoice.payment_succeeded' doesn't want to work.
class HooksController < ApplicationController
protect_from_forgery :except => :receiver
require 'json'
Stripe.api_key = "key"
def receiver
data_json = JSON.parse request.body.read
p data_json['data']['object']['customer']
if data_json[:type] == "invoice.payment_succeeded"
make_active(data_event)
end
if data_json[:type] == "invoice.payment_failed"
make_inactive(data_event)
end
end
def make_active(data_event)
@user = User.find_by_stripe_customer_id(data['data']['object']['customer'])
if @user.service_id.nil?
@user.service_id = "test"
@user.save!
end
end
end
I'm getting, and the entire webhook seems to work fine.
[2015-01-13 16:47:39] POST http://localhost:3000/hooks/receiver - 200
I've also recreated the make_active method in my rails console, where I input the User.find_by_stripe_customer_id("value") and it saves successfully.
Response log: Started POST "/hooks/receiver" for 127.0.0.1 at 2015-01-13 16:47:39 -0800 Processing by HooksController#receiver as XML Parameters: {"id"=>"evt_15KfJsKSH0PjiOYJ1MSTQxdi", "created"=>1421196456, "livemode"=>false, "type"=>"invoice.payment_succeeded", "data"=>{"object"=>{"date"=>1421196455, "id"=>"in_15KfJrKSH0PjiOYJJKOb0p1F", "period_start"=>1421196455, "period_end"=>1421196455, "lines"=>{"object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/invoices/in_15KfJrKSH0PjiOYJJKOb0p1F/lines", "data"=>[{"id"=>"sub_5VghThK2pwN5kM", "object"=>"line_item", "type"=>"subscription", "livemode"=>false, "amount"=>500, "currency"=>"usd", "proration"=>false, "period"=>{"start"=>1421196455, "end"=>1423874855}, "subscription"=>nil, "quantity"=>1, "plan"=>{"interval"=>"month", "name"=>"monkeyjr", "created"=>1421185125, "amount"=>500, "currency"=>"usd", "id"=>"1", "object"=>"plan", "livemode"=>false, "interval_count"=>1, "trial_period_days"=>nil, "metadata"=>{}, "statement_descriptor"=>"monkeywork - monkeyjr"}, "description"=>nil, "metadata"=>{}}]}, "subtotal"=>500, "total"=>500, "customer"=>"cus_5VghMM74h7yUFG", "object"=>"invoice", "attempted"=>true, "closed"=>true, "forgiven"=>false, "paid"=>true, "livemode"=>false, "attempt_count"=>1, "amount_due"=>500, "currency"=>"usd", "starting_balance"=>0, "ending_balance"=>0, "next_payment_attempt"=>nil, "webhooks_delivered_at"=>nil, "charge"=>"ch_15KfJrKSH0PjiOYJ03y6dXP1", "discount"=>nil, "application_fee"=>nil, "subscription"=>"sub_5VghThK2pwN5kM", "tax_percent"=>nil, "metadata"=>{}, "statement_descriptor"=>nil, "description"=>nil, "receipt_number"=>nil}}, "object"=>"event", "pending_webhooks"=>1, "request"=>"iar_5VghmqLO5Fy6Rt", "api_version"=>"2014-12-22", "hook"=>{"id"=>"evt_15KfJsKSH0PjiOYJ1MSTQxdi", "created"=>1421196456, "livemode"=>false, "type"=>"invoice.payment_succeeded", "data"=>{"object"=>{"date"=>1421196455, "id"=>"in_15KfJrKSH0PjiOYJJKOb0p1F", "period_start"=>1421196455, "period_end"=>1421196455, "lines"=>{"object"=>"list", "total_count"=>1, "has_more"=>false, "url"=>"/v1/invoices/in_15KfJrKSH0PjiOYJJKOb0p1F/lines", "data"=>[{"id"=>"sub_5FghThK2pwN5kM", "object"=>"line_item", "type"=>"subscription", "livemode"=>false, "amount"=>500, "currency"=>"usd", "proration"=>false, "period"=>{"start"=>1421196455, "end"=>1423874855}, "subscription"=>nil, "quantity"=>1, "plan"=>{"interval"=>"month", "name"=>"monkeyjr", "created"=>1421185125, "amount"=>500, "currency"=>"usd", "id"=>"1", "object"=>"plan", "livemode"=>false, "interval_count"=>1, "trial_period_days"=>nil, "metadata"=>{}, "statement_descriptor"=>"monkeywork - monkeyjr"}, "description"=>nil, "metadata"=>{}}]}, "subtotal"=>500, "total"=>500, "customer"=>"cus_5VghMF74h7yUFG", "object"=>"invoice", "attempted"=>true, "closed"=>true, "forgiven"=>false, "paid"=>true, "livemode"=>false, "attempt_count"=>1, "amount_due"=>500, "currency"=>"usd", "starting_balance"=>0, "ending_balance"=>0, "next_payment_attempt"=>nil, "webhooks_delivered_at"=>nil, "charge"=>"ch_15KfJrKSHPjiOYJ03y6dXP1", "discount"=>nil, "application_fee"=>nil, "subscription"=>"sub_5VghhK2pwN5kM", "tax_percent"=>nil, "metadata"=>{}, "statement_descriptor"=>nil, "description"=>nil, "receipt_number"=>nil}}, "object"=>"event", "pending_webhooks"=>1, "request"=>"iar_5VghmqLO5Fy6Rt", "api_version"=>"2014-12-22"}} "cus_5VghMF74h7yUFG" Rendered hooks/receiver.html.erb within layouts/application (0.1ms) Rendered layouts/nav/_default.html.erb (0.8ms) Rendered layouts/_header.html.erb (12.2ms) Rendered layouts/_alerts.html.erb (0.1ms) Rendered layouts/_footer.html.erb (0.1ms) Completed 200 OK in 183ms (Views: 182.4ms | ActiveRecord: 0.0ms)
Upvotes: 0
Views: 207
Reputation: 4251
1. Here, you were trying to fetch data_json[:type]. instead use data_json['type']. Use below mentioned receiver action.
def receiver
data_json = JSON.parse(request.body.read)
data_event = data_json['data']['object']['customer']
if data_json['type'] == "invoice.payment_succeeded"
make_active(data_event)
end
if data_json['type'] == "invoice.payment_failed"
make_inactive(data_event)
end
end
2: You were trying to use the data hash(which is local to receiver only) in make_active method. Instead just assign that value to a variable data_event and pass it to make_active method. Check following code:
def make_active(data_event)
user = User.find_by_stripe_customer_id(data_event)
if user.service_id.nil?
user.service_id = "test"
user.save!
end
end
Check my console:
I have created my sample app & connected it to stripe to test the webhook. You can refer my code in this gist link.
Upvotes: 0