Skip to content

process_webhook method for Stripe returning nil value for customer #26

@noleftsignal

Description

@noleftsignal

During rspec test for processing stripe webhook events, this method:

def self.process_webhook(type, data)
    account = Account.find_by!(stripe_customer_id: data["object"]["customer"])
    account.subscription_events.create(
      type: type,
      data: data,
    )

    if type == "customer.subscription.updated"
      handle_subscription_updated_event(account, data)
    end
  end

is returning this error:

Failure/Error: account = Account.find_by!( stripe_customer_id: data[:object][:customer] )
     
     NoMethodError:
       undefined method '[]' for nil:NilClass

Not sure if it is a result of not being able to parse the returned data from Stripe properly when converted to a hash with this line in the receive method of the stripe_webhooks_controller:
params[:data].permit!.to_h
or if it's an update to Stripe's API that's causing the issue now. I was able to nail it down to this line specifically in the subscription_event.rb model:
account = Account.find_by!(stripe_customer_id: data["object"]["customer"])
And by changing it to:
account = Account.find_by!( { 'stripe_customer_id': data['object']['customer'] } )
I was able to get the tests to pass.
Any info on why the lesser intuitive nature of the latter works and the former does not would be greatly appreciated to better grasp the concept!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions