Skip to main content

Stripe Signed Webhooks Discussion

General • Asked by Chris Oliver

Hi Chris,

I am trying to test (with rspec) a webhook url with a particular event.type in stripe - "charge.dispute.created". I've simulated it on the stripe dashboard test site and the webhook works for my production site but I can't seem to get the spec to pass. I am using the stripe_event gem and strip-ruby-mock gem. I am also using a mailer. All other tests are passing using these gems.

I keep getting '400' instead of '200' for this line:
post '/stripe-web-hooks', params: { id: @event.id }

Here is my spec and my stripe.rb. I'd appreciate any advice.

Pam

require 'rails_helper'
require 'stripe_mock' 

describe "Customer Dispute Events" do

  describe "charge.dispute.created" do 

    before do
      StripeMock.start
      @event = StripeMock.mock_webhook_event('charge.dispute.created')
      #@event = Stripe::Event.retrieve("evt_1EEd1tKGa7MvTac7cvoKcpPk")
      ActionMailer::Base.deliveries.clear
    end

    after do 
      StripeMock.stop
      ActionMailer::Base.deliveries.clear
    end

    it "is successful" do 
      post '/stripe-web-hooks', params: { id: @event.id }
      expect(response.code).to eq "200"
    end

    it "sends an email to the admin" do
      post '/stripe-web-hooks', params: { id: @event.id }
      expect(ActionMailer::Base.deliveries.count).to eq 1
    end
  end
end
Stripe.api_key = ENV["stripe_secret_key"]
StripeEvent.signing_secret = ENV["stripe_signing_key"]

# StripeEvent.configure do |events|

#   events.subscribe 'charge.dispute.created' do |event|


StripeEvent.subscribe 'charge.dispute.created' do |event|
  event.class       #=> Stripe::Event
  event.type        #=> "charge.dispute.created"
  event.data.object #=> #<Stripe::Dispute:0x3fcb34c115f8>
  #end
CustomerDisputeMailer.send_admin_customer_dispute(event).deliver_now
end

I define @event in the customer_dispute_mailer.rb

class CustomerDisputeMailer < ApplicationMailer

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.customer_dispute_mailer.send_admin_customer_dispute.subject
  #
  def send_admin_customer_dispute(event)
    @event = event

    mail(to: "[email protected]", subject: "Uh oh, a customer has submitted a dispute", from: "[email protected]")

  end
end

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.