Following Stripe’s documentation, it invites us to declare the functions directly into the DOM but, in this case, I decided to create an Object to handle everything from there so, at the end of our new view, I create a new instance:

I left out a lot of code to reduce the length of the post, however, you can see the full code in the Payment file.

In the code provided by Stripe, we bind the submit and trigger the actions directly, here, I’m binding it in the initializer of the class (line: 8), then, I’m retrieving the token (as dictated by Stripe code) and sending it to a callback function:

$.proxy(@responseHandler, @)

I’m using the $.proxy() because I want to trigger that function inside the context of our Payment instance, if I remove the $.proxy, it will look for a globally declared function (like in the Stripe code).

Stripe Event

As you may know, Stripe offers us the functionality of automatically sending a payment ticket to the registered customer email, you only need to upload a logo, select some colors and voilà. But, what happens if you aren’t registering customers on Stripe? What if you’re just making charges to the card? or what happens if you want to have your own email template? or log the email sent action?.

Well, this could be done by using Stripe Webhooks and ‘hooking’ on to them it’s pretty easy with stripe_event.

OK, let’s walk through a simple implementation for sending the tickets to our beloved customers:

config/routes.rb

And last but not least, you’ll need to go to your Stripe Account and setup your webhooks:

And that’s all, we’re listening for any succeed charge. I’m not showing here the PurchasesController or the PurchasesService as you can go and take a look at them and your implementation will vary depending on your requirements.

Ah, I almost forgot, what happens if you want to receive the hooks on your local environment? Well, there are many options but one that is easy to setup is Ultrahook

Ultrahook

I could guide you through the installation and so, but it’s pretty straightforward, you only need to create an account and follow the steps Ultrahook shows.

After you have everything working, you would only need to run:

$ ultrahook stripe http://localhost:3000/stripe-webhooks

This will authenticate you and start forwarding any POST request to Ultrahook to your local environment:

In my case, Ultrahook will be listening to stripe.mumoc.ultrahook.com for any requests and it will be redirecting them to localhost:3000/stripe-webhooks, because I mounted the StripeEvent::Engine at ‘stripe-webhooks’.

All that is left is to register a webhook on Stripe pointing to the Ultrahook URL and that’s it.

Conclusion

As Tapia said in his post, Stripe is a simple and fast way to receive payments and, having a way to control the webhooks on our app, we can easily make interesting implementations.