This is the first article in the “Process Payments with Paypal in Rails” tutorial series:
- Process Payments with Paypal in Rails, Part 1 – Basic Checkout (this article)
- Process Payments with Paypal in Rails, Part 2 – Charge Credit Cards
- Process Payments with Paypal in Rails, Part 3 – Recurring Payments
Integrating Paypal to your Rails application is quite simple. In this tutorial, we will walk you through a basic checkout process to have Paypal process payments and notify our Rails app after payment is made.
As an example we will have an imaginary “Cookery School” application to sell online cooking courses. You can check it out here:
1 2 3 4 5 6
Step 1. Creating your Paypal Developer account
You can create yours at your account. Those will be valid while you are logged in at Paypal Developer site. I have created two accounts:
- ‘firstname.lastname@example.org’ : will be our seller account in sandbox environment
- ‘email@example.com’ : will be our buyer account for testing
Step 2. Redirecting to Paypal url
At ‘registrations_controller.rb’ we will redirect to Paypal URL after the registration is successfully saved:
1 2 3 4 5 6 7 8 9
This URL is produced by registration and course params. Next method is added to our ‘registration.rb’ model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
You can manage your applications parameter from ‘config/secrets.yml’ file. We will add Paypal URLs and our local URLs for both environments development and production at ‘config/secrets.yml’ file:
1 2 3 4 5 6
After that we can sell, filling registration data at our desired course, and clicking “Registration Payment”:
We can see how we are redirected to Paypal standard form, with our course details and price. All prepared just to be purchased.
Step 3. Creating a web hook to manage payment notifications
At this point we can receive payments at our merchant account. But do we know if those payments are really done? No, right now our application does not know if a registration was paid or not. For that purpose we need to create a webservice where Paypal can connect to notify when the payment is done.
In ‘config/routes.rb’ create the routes:
First for ‘return_url’ (show invoice), and second for the ‘notification_url’ service.
The controller should answer these requests. Add to your ‘registrations_controller.rb’:
1 2 3 4 5 6 7 8 9 10
Removing protect_from_forgery process in order to turn off csrf checking when Paypal send a plain HTTP POST.
Here we need to add some attributes to our Regstration model, in order to catch notifications details. So that we will create a migration:
Now we need to inform to Paypal where to transfer the notification. Then we need to add notify_url parameter to our paypal_url creation, in ‘registration.rb’ model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Step 4. Testing
Now is the moment to start your app (remember, 3000 is the default port):
Expose your service to be accessed by Paypal
If we think a while we can realize that notification_url and return_url can not be accessed from Paypal because they are not exposed in the public internet, they are in our localhost.
We will use the Ngrok Service for tunnelling our local application to a Ngrok subdomain. So we will be able to allow Paypal to send a HTTP POST to our localhost and test our payment process locally.
First install Ngrok downloading it for your system. Or just run your package manager if you are on Linux:
Now you can publish your application just running the command:
ngrok 3000, since the rails port is 3000.
It will respond with a URL,
http://3c99b47d.ngrok.com in my case. This is the app_host you should configure in your ‘config/secrets.yml’ file. Replace
app_host: http://our_ngrok_url by
Testing from Paypal Developer site
Now you should access your ngrok URL, select a course and fill in your registration details:
Clicking registration button you will be redirected to Paypal checkout form (paypal_url), with all course details and price:
Login with your buyer paypal account, and you will be redirected to payment confirmation page:
After you confirm the payment, you will see the success page:
Here you can click the return link (“Return to SandboxTest Account’s Test Store”) to return to our Academy page. Then you will be redirected to your invoice page:
Here you can see notification time, status and identifier. You can also login with your buyer or merchant accounts in Sandbox Paypal site where you can see payments received:
This is a basic solution to process payment with Paypal. The advantages are:
- Pretty easy and quick way to implement a payments gateway on your site
- No need to store credit card numbers locally and no need to worry about PCI Compliance
- You get notified by Paypal when payments are processed.
The disadvantage is that you have to send your customers away from your website to go to Paypal to finish their payment process, which can be disruptive for the user experience.
For further information you can visit the following links: