Search code examples
ruby-on-railsstripe-connect

Stripe managed account webhooks and rails


I'm building a marketplace in rails 5. I've been able to use the 'stripe gem' to set up a 'custom managed account' using stripe connect.

I'm also using the 'stripe_event gem' to catch the webhooks coming from stripe. I can do this without any problem using a standard stripe account.

However, based on the stripe docs when using stripe connect I have to add another account attribute somewhere. As the event being hooked doesn't exist in the primary stripe account but that in the connected account.

It makes total sense I just don't know how to update my class to do so.

Stripe.api_key = Rails.configuration.stripe[:secret_key]


class RecordAccount
    def call(event)
        myevent = event.data.object

        #Look up StripeAccount in our database
        stripe_account = StripeAccount.where(stripe_id: myevent.account).last

        #Record Verification details and status in StripeAccount
        u = stripe_account.update_attributes(
            verification_status: myevent.legal_entity.verification.status,
            verification_details: myevent.legal_entity.verification.details,
            )
        u.save

    end
end

StripeEvent.configure do |events|
  events.subscribe 'account.updated', RecordAccount.new
end

The response I am getting from the above is a 404 - event XXXX not found. Which makes sense, but how do I fix it. I know it's a simple answer, I've just been looking at the screen for too long.


Solution

  • I haven't used the gem you're mentioning but I had the same problem for connected account events. In my case, I've just setup an endpoint in my app to receive the Stripe webhook events.

    The important part is that for connected accounts, you have to use the "account" property in the payload together with the event id, whereas for events that concern your account you just use the event id.

    Here's a code sample of what I mean

      begin
        payload = JSON.parse(request.body.read)
        # Verify the event by fetching it from Stripe
        if payload['account'].present?
          # This is for connected accounts
          Stripe::Event.retrieve(payload['id'], stripe_account: payload['account'])
        else
          # Normal events
          Stripe::Event.retrieve(payload['id'])
        end
      rescue Stripe::StripeError => e
        # Handle this however you prefer
        raise ActiveRecord::RecordNotFound.new(e)
      end
    

    Hope it helps!