Using Stripe webhooks with Rails - ruby-on-rails

I have successfully built out a rails app as per Ryan Bates' railscast (http://railscasts.com/episodes/288-billing-with-stripe) - However, in the railscast he doesn't cover webhooks. I was wondering if there are any good examples in the public domain of implementing and using webhooks to manage a subscription billing that has been created.
Any thoughts / direction would be super helpful?
Thanks!

There's a nice gem for this: https://github.com/integrallis/stripe_event.
It looks to be well written and maintained.
In your gemfile you can simply add the line -
gem 'stripe_event'
There's clear doc in the readme on github.

Most all payment gateways, including stripe, have some way of telling the client (your webapp) whether the charge went through or not.
For stripe, their docs show how to receive a webhook notification from them.
The flow is:
Create a controller and method in your app to receive the webhook calls from stripe. Note that the calls will come in as POSTs, not GETs
Register the url with stripe using their dashboard. This is a manual step that you do once.
When your method is called from stripe, use the event info to update your models which track the status of your users' subscriptions.
Any questions?

I'd like to use this gem for testing stripe webhooks https://github.com/stripe-ruby-mock/stripe-ruby-mock
Features
No stripe server access required
Easily test against stripe errors
Mock and customize stripe webhooks
Flip a switch to run your tests against Stripe's live test servers

Related

Rails and Razorpay Webhook setup

I am trying to setup Razorpay Webhook in rails. Have done other developments in rails but pricing is new to me. i already setup and tested Webhook in razorpay dashboard and used requestbin.com to get the payload results(JSON) as mentioned in this doc https://razorpay.com/docs/webhooks/
Now, should i create a new POST route and capture the payload results(JSON) in controller and update the database accordingly?! Or is there any other way for handling Webhooks in Rails?
If it is so simple like above what is this gem https://github.com/razorpay/razorpay-ruby used for?
https://github.com/razorpay/razorpay-ruby is simply a ruby wrapper razorpay provides for all create/fetch methods.
In razorpay you can set up a web hook by first creating a handler path on your controller and expose it to them. You can add this route on their web hooks/add under settings on your razorpay merchant dashboard.
To make this callback more secure, razorpay allows you to setup a web hook signature from the dashboard and the above mentioned gem has a wrapper method that helps you verify the signature as well.
Handling Webhooks require 3 things,
Create a POST endpoint in your application
Add the URL in the callback settings in the 3rd party dashboard, in your case it's Razorpay dashboard
Since the Webhook endpoint is public, you need to verify whether the callback is actually from the authentic source or now. The details are usually provided in the api documentation of the 3rd party service.
After doing this, you can handle the callback in your own way according to the requirement. Preferebly Webhook callbacks params should be moved to a background service and return 204 request everytime.
Hope that helps!

Consuming webhooks shopify-api

I'm in the process of building my first RoR webapp and I'm currently trying to set up an integration with Shopify. I want to create an order in my web application whenever one is created in Shopify. For this I want to use a Shopify webhook. Step two will be to set it up so that after processing the order I'll use the Shopify API to update some records. For now my main concern is receiving the webhooks. I've been looking at the documentation for the Shopify gem but with my limited RoR skills I can't seem to figure it out. I've been searching the web for a few hours but can't find any clear examples / explanation.
I'm confused about this gem; shopify-api (https://docs.shopify.com/api/authentication/using-api-gem-with-private-app-credentials). I've created a private app in shopify, but after that I'm lost. Where am I supposed to place this code and how is this invoked after receiving a webhook?
It seems to me that this gem is mainly used to access the Shopify API instead of consuming webhooks. Am I going at this all wrong?
For receiving the changes (especially Order Creation event) from Shopify, you don't need to use any gem or third party to integrate!
Here are the steps that I did before in my app:
1. Provide an api in your app to receive a webhook event
Here is an example:
Your route:
post '/shopify/create_order', to: 'shopify#create_order'
Your controller:
class ShopifyController < ApplicationController
def create_order
# process shopify order here: all info in `params`
end
end
2. Config to call webhook from your Shopify Settings. The configured url will be:
http://your_production_url/shopify/create_order
If you want to test from localhost, go to step 3
3. (Optional) Test from your localhost
Download this free tool: ngrok and extract it to your machine.
Run ngrok in your terminal to generate a forwarding url to your localhost by this command:
ngrok http 3000
So you will get the output from console like this:
Copy the generated url (http://fbc5cf88.ngrok.io for example) above and add to your Shopify settings in step 2
4. (Optional but critical) Set privacy in your app, to make sure only Shopify can call your api. Follow this documentation
Shopify also provides api to integrate with Shopify resources: create/update product, collection, collect, metafield,... You can directly use it. All was described at Shopify API documentation. But the easiest way to work with Shopify is shopify_api gem which provides an interface to work with Shopify (via ActiceResource)
Probably reading up on the documentation on Webhooks would help you figure out a lot of what you need to do
In essence, here are the overview of what you need to start consuming web hooks:
1. Create your web hooks
You can do it using the Shopify API (POST /admin/webhooks.json)
Or you can do it through store admin
The params that you need to take note of:
topic: the type of hook you want (for e.g orders/create)
address: the URL of the endpoint that you would be consuming the hook at (for e.g: https://myapp.domain.com/hooks/order_create)
2. Create your endpoint that will respond to hooks
You then need to setup your ROR app to expose an endpoint that will respond to a POST request.
This endpoint is the same URL that you previously specified when creating the hook
More info at https://docs.shopify.com/api/webhooks/using-webhooks#respond-to-webhook
Notes: You also need to implement a way to verify that requests that you receive through the endpoints came from Shopify, detailed here.

Where to add 3DS parameters in the payment request

We are using spree e-commerce platform to build our application which in ruby on rails framework. We wanted to just implement the 3D secure payment method into the existing payment option available with us. The adyen payment gateway is already implemented in the application, just wanted to know how to achieve 3d secure payment processing.
I know this is a later response but for those who end up here this may help.
I'm currently developing an integration for Mangopay in Spree so I don't have the full answer, but in order to make 3Ds to work you'll have to make some changes:
change the update method on checkout controller to redirect to 3ds url once the payment is created
change the payment processing logic in spree to start_processing instead of complete the payment once you get a successful response from adyen (first stage of 3DS)
change the adyen gateway to support 3DS (as far as I know the implementation in ActiveMerchant does not add anything for 3Ds, but I just made a quick look)
add some route, controller and action to process the callback from 3DS and update spree payment and order acordingly
Here you can find useful information:
Spree Mollie Gateway Extension they implement Mollie Gateway with 3DS support
Active Merchant Gem
Spree Extension Tutorial
Feel free to contact me if you have further questions

sendgrid_webapi event notification sendgrid

I'm having difficulty retrieving the event notifications for my SendGrid account and I'm not sure how to proceed.
I'm utilizing the sendgrid_webapi gem (https://github.com/kylejginavan/sendgrid_webapi) and I'm attempting to retrieve the event notifications for the client using the barebones implementation that the README.rdoc provides.
Very simple:
client = SendGridWebApi::Client.new(*user*, *password*)
notifications = client.event_notification.get()
return notifications
Each of the other calls (bounces, unsubscribes, etc.) work as expected, but for some reason I've been unable to retrieve anything other than the URL that I've specified within the SendGrid interface (I've enabled all events and the event notification app).
The documentation for the gem states that the options available can be found at http://sendgrid.com/docs/API_Reference/Webhooks/event.html but I've been unable to take that information and turn it into something tangible that I can use when making the call.
I feel like this is just something simple that I'm missing, but any help / guidance would be greatly appreciated.
Thanks in advance!
The API call you are trying to use is for configuring the Event Webhook, not for consuming events.
A webhook is a "push" API rather than one that you query for information. You need to setup a URL with a script that can handle HTTP posts from SendGrid in order to use the webhook.
Here is a gem called gridhook that can do what you need.

Charge user per request - rails application

I'm working on a project that aims to charge user by request. He will have an account, and monthly a receipt should be sent to him describing how much of the service he used on the month. Is there some gem or documentaion about this?
My concern is about just registering as a legitm use, when the request completes correctly, so as to avoid charging him error requests. I believe its a very careful part of the application. Have anyone seen anything about this?
I don't think you'll find a gem for that. Probably you'll end up creating a Rack middleware and add it to the end of the middlewares chain, check if the status code is 200 and increment the user's usage counter.

Resources