This is the third article in the “Process Payments with Paypal in Rails” tutorial series:
- Process Payments with Paypal in Rails, Part 1 – Basic Checkout
- Process Payments with Paypal in Rails, Part 2 – Charge Credit Cards
- Process Payments with Paypal in Rails, Part 3 – Recurring Payments (this article)
In this article we will adapt these two payment processes to accept recurring payments. We will sell an on-line “Cookery Master in Oriental Food” which has one year duration and is paid monthly.
For this post, we will work on the
1 2 3 4 5 6 7 8
There are two previous steps needed, which were covered in the Process Payments With Paypal in Rails, Part 2 – Charge Credit Cards post:
- Step 1. Create Paypal Pro account: create a merchant Pro account to test in Sandbox
- Step 3. Configure Active Merchant: Configure the application to accept credit cards
Now let’s see how to set up recurring payments!
Step 1. Modify our model to offer recurring courses
To process our Master we need the following parameters:
recurring?: a boolean value to determine if the course has recurring payments or not
period: possible values are: [“Day”, “Week”, “SemiMonth”, “Month”, “Year”]
cycles: number of charges
We will add our new Master in our ‘db/seeds.rb’ file:
You can reload the courses executing
And we need to fix our view in order to announce our recurring courses properly. Just add the course period to the price if this is recurring. In
app/views/courses/_index.html.haml put the condition in the button:
This will show our catalog properly to our clients:
Step 2. Modify Paypal method to pay recurring courses
Here we just need to notify to Paypal through the URL to make recurring charges. Modifying paypal_url method in Registration model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
The code is pretty self-explicative, just add recurring values if the course need them. The other values such as notify hook, return url, business account … are the same.
If your use case needs to include more parameters, you can take a look at HTML Variables for PayPal Payments Standard
Test Paypal method
As we described in Process Payments with Paypal in Rails, Part 1 – Basic Checkout post, we need a HTTP tunnel published on the web in order to be properly returned and notified of the payment.
Now we can go to this direction (having our app running in localhost, port 3000):
Here you should fill the form and choose Paypal payment method:
You will be redirected to the Paypal authentication page, and here, you can see the recurring payment detail:
You should login and Accept the payment. Then you go to Successful purchase page:
When you click in the “Return To SandboxTest Account’s Test Store” you will return to Invoice page:
Where you can see that is Completed, so the /hook is also running with revurring registrations.
firstname.lastname@example.org account you can see the Subscription created towards our
Step 3. Modify Credit Card payment method to pay recurring courses
You just need to modify the Card model,
- Access directly to the course from the card model, to simplify our code:
- The purchase option should call to one or another method of the Gateway:
1 2 3 4 5 6 7 8 9 10
- And the options are different for recurring payments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
Finally, just for seen the Profile ID (this is a subscription, not a transaction), we will show the profile in the invoice:
1 2 3 4 5 6
Test Credit Card method
Just select the Master and fill the form including Credit card:
Directly your invoice is created:
You can see at the merchant account how the recurring payment has been created:
Recurring payments make good business sense for many applications. In this tutorial we have shown that you can manage recurring payments, and receive all of them in your Paypal Pro account.