For this tutorial we will see how to create a subscription to charge periodically a service to a customer.

Say you have a service where you offer a one month trial and then you charge a monthly fee of $ 99.99 pesos indefinitely (or until your customer cancels the service).

You have to do the following:

  1. Create a plan
  2. Create a token of debit or credit card using Openpay.js
  3. Create customer
  4. Card token association with customer
  5. Subscribe customer

Create a plan

A plan is a template for a subscription that contains the cost, charge frequency, trial period, etc.

To create a plan we have two options:

Option 1.- Creation from the API

$openpay = Openpay::getInstance('mzdtln0bmtms6o3kck8f', 'sk_e568c42a6c384b7ab02cd47d2e407cab');

$planDataRequest = array(
    'amount' => 150.00,
    'status_after_retry' => 'cancelled',
    'retry_times' => 2,
    'name' => 'Plan Curso Verano',
    'repeat_unit' => 'month',
    'trial_days' => '30',
    'repeat_every' => '1',
    'currency' => 'MXN');

$plan = $openpay->plans->add($planDataRequest);

You can have as many plans as you want. For example you can have a Gold, Platinum and Bronze plan for different levels of service.

If the request is successful we will have a response that contains the id of the plan


     "name":"Servicio de TV",

For more information about the plan parameters go to "Add Plan"

Option 2.- Creation from the dashboard

You can also create plans from the dashboard in Planes -> Agregar:

Agregar plan

Card tokenization

To tokenize a customer card using the library Openpay.js.

First add the head and Openpay.js files to the head:

<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>

Now let’s set the merchant-id and the public-key to the Openpay library:

<script type="text/javascript">
     $(document).ready(function() {

Card token creation

Once you have installed and you set up the library, you’ll need to call the OpenPay.token.create () method in order to create a token.


The parameters for this method are:

  • Parameter CREATE_PARAMETERS_OBJECT is a Javascript object containing the card information.
  • Parameter SUCCESS_CALLBACK defines the function to be called if the operation was successful.
  • Parameter ERROR_CALLBACK defines the function to be called if the operation failed.

Example of creating a token:

      "holder_name":"Juan Perez Ramirez",
         "line1":"Av 5 de Febrero",
         "line2":"Roble 207",
}, onSuccess, onError);

The method returns an object type token with an id which you’ll need later. You’ll find the definition of the token object here.

For further reference on the use of the library, see the Openpay.js

Create customer

Now we’ll create the customer we want to subscribe:

$openpay = Openpay::getInstance('mzdtln0bmtms6o3kck8f', 'sk_e568c42a6c384b7ab02cd47d2e407cab');
$customerData = array(
     'name' => 'Mi cliente uno',
     'email' => ''

$customer = $openpay->customers->add($customerData);


   "name":"Mi cliente uno",

Asociación del token de tarjeta al cliente

Now this only requires to use the token_id that comes with the request to save and assign the card to the customer.

The card object has an id which has to be saved in your server in order to make future charges to that card. For more information go to Create Card with a Token. ​

Response example

   "holder_name":"Juan Perez Ramirez",

Notes: You can simulate different results using the cards Testing

Subscribe customer

Finally we create the subscription with the plan id (psjubnktzpofhakixfkp), the customer id (axapgwwolofnckfui2wx) and id de tarjeta (kokzmiiwephcdmq1h2qr), con lo cual la petición quedaría así:

$openpay = Openpay::getInstance('mzdtln0bmtms6o3kck8f', 'sk_e568c42a6c384b7ab02cd47d2e407cab');

$subscriptionDataRequest = array(
    "trial_end_date" => "2014-01-01",
    'plan_id' => 'psjubnktzpofhakixfkp',
    'card_id' => 'kokzmiiwephcdmq1h2qr');

$customer = $openpay->customers->get('axapgwwolofnckfui2wx');
$subscription = $customer->subscriptions->add($subscriptionDataRequest);


      "holder_name":"Mi cliente uno",

Ready, we have created the subscription, which will be automatically charged each month for $ 99.99 after trial period.


  • You can simulate different results using the cards Testing
  • Implement the Notifications to know the status of payments in real time