Recurring Payments In Magento

We would like to discuss one of the most interesting payment functionalities of Magento – recurring payments. It is not an easy task to find a complete guide showing how to set up a recurring profile step-by-step therefore we decided to create one.

Magento’s payment gateway sample for recurring profiles includes all the functionalities (but trials) that recurring profiles for products have: different billing period units, frequency, initial fee etc. A necessary functionality, autocharging, had to be created by us and it is contained in our sample which we will describe in details below. In Magento CE, there is only one recurring gateway installed – PayPal. However, there is no barrier to create another one, and that is why we are posting our sample.

1. Why you should care
As a WHMCS experts we know that recurring payments are often used in the hosting industry. Why do not use it in your Magento store to increase your sales? We are very surprised that this part is still a niche in the Magento market. For more info about recurring profiles, see official Magento knowledgebase.

2. Download the sample
You can test it in your development Magento installation. Do not install it on production Magento as this is just a demo of recurring profiles. Installation is very simple: Download zip package, upload it to your Magento, clear cache and new section should appear at System > Configuration > Payment Methods > Recurring Payments Sample. We tested it on Magento CE versions 1.8.1.0 and 1.9.0.0. Download link can be found in further part of the article.

ModulesGarden Magento - Ordering

3. How it works (step-by-step)
After installing the extension, administrator should go to the gateway configuration (System > Configuration > Payment Methods > Recurring Payments Sample) and enable it. API ID and API Key are just fake fields and there is no need to fill them (more about it in programming description below). After that, you should create product that will have a recurring profile enabled. When client buys this product there are two API calls which are sent to the real gateway (of course not in sample case): first for recurring initialization with initial fee, and second one for the first payment. That part could be different for other gateways. Extension adds cron job for sending request to the remote API when profile should be charged. Magento does not do that and we have to use very long SQL query for checking all of recurring profiles. On next profile’s time period our gateway is trying to charge it. After successful attempt, new order is created with transaction assigned.

ModulesGarden Magento - Backend Profile

4. Programming description
In this part we assume that you have a basic knowledge how to build Magento Extension. We will put attention to the gateway part and skip basic explanations.

4.1 Configuration
The first thing what we would like to do is to create configuration for our gateway. In extension’s etc/system.xml we will use:

This file is mostly self-explanatory. You can create as many fields as gateway requires. We added fake fields api_id and api_key. In real gateway they could be used. Fields ‘active’, ‘title’, ‘cctypes’, ‘allowspecific’, ‘specificcountry’, ‘sort_order’ are used by default Magento mechanisms and should be used in all of gateways.

ModulesGarden Magento - Configuration

4.2 Gateway
Magento should “know” that we would like to add payment gateway and we should define it in etc/config.xml file:

This part describes that main payment PHP class will be located in extensions’s Model/Payment.php and named Modulesgarden_Recurringpaymentssample_Model_Payment. The important fact is that class must extend Mage_Payment_Model_Method_Abstract and implement Mage_Payment_Model_Recurring_Profile_MethodInterface. Now Magento “knows” exactly what we want to do. Our class must implement these methods:

  • validateRecurringProfile
  • submitRecurringProfile (this is the most important method – it will send our profile to the gateway after order of recurring product)
  • getRecurringProfileDetails
  • canGetRecurringProfileDetails
  • updateRecurringProfile
  • updateRecurringProfileStatus

All of methods are logged in Magento log files in var/log/recurringpaymentssample.log file (enable logging in Magento configuration), so you can monitor what and when is called. Check out the screenshot of logs after placing an order.

ModulesGarden Magento - After Ordering

Source code for submitRecurringProfile method:

The problem with this gateway is that it will be shown while ordering of all products (non-recurring). To hide it from regular payments we have to create another method (overwrite from parent):

Methods checks whether the cart contains “regular” products. If so, then just disables it.

4.3 Cron
The first question is: where is the method for cyclical payments? There is no such method! PayPal (the only recurring gateway in Magento) does the job on its side and does not require it. We have to implement it by ourselves. In config.xml file we have to add cron job:

It will call Modulesgarden_Recurringpaymentssample_Model_Observer::chargeRecurringProfiles every minute (just for example). This method runs log SQL query for profiles that needs top be charged:

Finally, the method for charging a profile:

To get the full source code, just download the sample gateway from the link below.

Download ModulesGarden Recurring Profiles Sample For Magento

5. Amazing extension – GPN DATA For Magento

GPN DATA payment gateway offers reliable and secure online payment solutions for IT business all over the world. A big piece of cake of that business are online stores, such as Magento.

We have created a payment gateway for recurring profiles. It is named GPN DATA For Magento. It is using a similar logic as in this sample extension showed above but it is a little bit more complex. The real gateway is called in that case. The communication exists in both directions: magento-gateway and gateway-magento. These are GPN DATA requirements. Owing to GPN DATA For Magento module you will be able implement proven GPN DATA services directly to your sales platform.

For more information visit our website.

6. Summary
This is a great opportunity for your online store to earn more money. We could also create such a gateway for you – just ask us for a custom software development here.

VN:F [1.9.22_1171]
Rating: 4.9/5 (67 votes cast)
Recurring Payments In Magento, 4.9 out of 5 based on 67 ratings
Main , , ,

14 comments


  1. I am trying to find a recurring billing solution that can process recurring billing and one-time purchases in the same cart. Then run the necessary charges and generate the orders when they are due.

    Is this something you can do for us?

    • Certainly John, that will not be a problem for us. We have already created such solutions. Good example is extension GPN DATA For Magento mentioned in this post. Feel free to discuss the details here: modulesgarden.com/order/request

  2. amit bera

    Can you please tell ,is this extension create daily basic order on billing frequency= one day and billing period =1day?

  3. Yes it supports all combinations of billing period and frequency, including daily basic order in billing frequency & billing period = 1day. Just do not forget to set the cron correctly.

  4. I Read your blog you have mentioned almost many things about layaway payments and how it can be effective for Magento Store owners. But it can be more informative if you will emphasize more on GPN Data Magento and how it can be beneficial for customers, but over all nice way to cover recurring payments in Magento guidelines

  5. Does your extension give the ability to process more than one recurring payment at a time? The current Magneto recurring products only allows one recurring product to process at a time through the checkout.

  6. kj

    I set recurring_profile attribute as downloadable product enabled.
    And I could buy downloadable product with recurring profile ,but
    download link was not displayed at download list .

    Could you please teach me how can I display download link with recurring profile. ?

  7. Thanks for sharing information !!!

  8. Christie

    Thanks for this info.

    Please is there a way to set multiple recurring options for customers to select at checkout? From what I see, you can only set one option per product but not sure if I am correct.

  9. Christie, the code is based on a default Magento configuration therefore it is not possible to set multiple recurring options for customers to select at checkout with its help.

  10. Rajbir Singh

    Hello Piotr Dołęga,

    Thanks for such a beautiful information.

    am not able to download example code from given link on text “Download ModulesGarden Recurring Profiles Sample For Magento”.

    can you please help me with this ?

    Thank you

Leave a Reply to Piotr Dołęga Cancel reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">