Suscripciones
Para esta guía vamos a ver como crear una suscripción para cobrar un servicio a un cliente de manera periódica.
Digamos que tienes un servicio en el cual ofreces 1 mes de prueba y después cobras una renta mensual de $99.99 pesos indefinidamente (o hasta que tu cliente cancele el servicio).
Para ello vamos a hacer lo siguiente:
Crear plan
Crear cliente
Guardar la tarjeta
Suscribir al cliente
Crear plan
Un plan es un plantilla para una suscripción que contiene el costo, frecuencia de cobro, número de días de prueba, etc.
Para crear un plan tenemos dos opciones:
Opción 1.- Creación desde la API
curl -u sk_e568c42a6c384b7ab02cd47d2e407cab \
-H "Content-type: application/json" \
-X POST -d '{
"amount":99.99,
"status_after_retry":"cancelled",
"retry_times":2,
"name":"Servicio de TV",
"repeat_unit":"month",
"trial_days":30,
"repeat_every":1
}' https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/plans
<?
$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 );
?>
OpenpayAPI api = new OpenpayAPI ( "https://sandbox-api.openpay.mx" , "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Plan request = new Plan ();
request . name ( "Plan Curso Verano" );
request . amount ( new BigDecimal ( "150.00" ));
request . repeatEvery ( 1 , PlanRepeatUnit . MONTH );
request . retryTimes ( 2 );
request . statusAfterRetry ( PlanStatusAfterRetry . CANCELLED );
request . trialDays ( 30 );
request = api . plans (). create ( request );
OpenpayAPI api = new OpenpayAPI ( "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Plan request = new Plan ();
request . Name = "Plan Curso Verano" ;
request . Amount = new Decimal ( 150.00 );
request . RepeatEvery = 1 ;
request . RepeatUnit = "month" ;
request . RetryTimes = 2 ;
request . StatusAfterRetry = "cancelled" ;
request . TrialDays = 30 ;
request = api . PlanService . Create ( request );
var planRequest = {
'amount' : 150.00 ,
'status_after_retry' : 'cancelled' ,
'retry_times' : 2 ,
'name' : 'Plan Curso Verano' ,
'repeat_unit' : 'month' ,
'trial_days' : '30' ,
'repeat_every' : '1'
};
openpay . plans . create ( planRequest , function ( error , plan ){
// ...
});
@openpay = OpenpayApi . new ( "mzdtln0bmtms6o3kck8f" , "sk_e568c42a6c384b7ab02cd47d2e407cab" )
@plans = @openpay . create ( :plans )
request_hash = {
"name" => "Plan Curso Verano" ,
"amount" => 150 . 00 ,
"repeat_every" => "1" ,
"repeat_unit" => "month" ,
"retry_times" => 2 ,
"status_after_retry" => "cancelled" ,
"trial_days" => "30"
}
response_hash = @plans . create ( request_hash . to_hash )
Puedes tener cuantos planes desees. Por ejemplo puedes tener un plan Oro, Platino y Bronce para diferentes niveles de servicio.
Si la llamada es correcta tendremos una respuesta que contiene el id
del plan
Respuesta:
{
"name" : "Servicio de TV" ,
"status" : "active" ,
"amount" : 99.99 ,
"currency" : "MXN" ,
"id" : "psjubnktzpofhakixfkp" ,
"creation_date" : "2014-02-14T13:47:55-06:00" ,
"repeat_every" : 1 ,
"repeat_unit" : "month" ,
"retry_times" : 2 ,
"status_after_retry" : "cancelled" ,
"trial_days" : 30
}
Para más información de los parámetros del plan dirígete a "Agregar Plan".
Opción 2.- Creacion desde el dashboard
Puedes crear planes también desde el dashboard en Planes -> Agregar:
Crear cliente
Bien, ahora crearemos al cliente que vamos a suscribir.
curl -u sk_e568c42a6c384b7ab02cd47d2e407cab \
-H "Content-type: application/json" \
-X POST -d '{
"name":"Mi cliente uno",
"email":"micliente@gmail.com"
}' https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers
<?
$openpay = Openpay :: getInstance ( 'mzdtln0bmtms6o3kck8f' , 'sk_e568c42a6c384b7ab02cd47d2e407cab' );
$customerData = array (
'name' => 'Mi cliente uno' ,
'email' => 'micliente@gmail.com'
)
);
$customer = $openpay -> customers -> add ( $customerData );
?>
OpenpayAPI api = new OpenpayAPI ( "https://sandbox-api.openpay.mx" , "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Customer request = new Customer ();
request . name ( "Mi cliente uno" );
request . email ( "micliente@gmail.com" );
request = api . customers (). create ( request );
OpenpayAPI api = new OpenpayAPI ( "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Customer request = new Customer ();
request . Name = "Mi cliente uno" ;
request . Email = "micliente@gmail.com" ;
request = api . CustomerService . Create ( request );
var customerRequest = {
'name' : 'Mi cliente uno' ,
'email' : 'micliente@gmail.com'
};
openpay . customers . create ( customerRequest , function ( error , customer ) {
// ...
});
@openpay = OpenpayApi . new ( "mzdtln0bmtms6o3kck8f" , "sk_e568c42a6c384b7ab02cd47d2e407cab" )
@customers = @openpay . create ( :customers )
request_hash = {
"name" => "Mi cliente uno" ,
"email" => "micliente@gmail.com"
}
response_hash = @customers . create ( request_hash . to_hash )
Respuesta:
{
"id" : "axapgwwolofnckfui2wx" ,
"name" : "Mi cliente uno" ,
"last_name" : null ,
"email" : "micliente@gmail.com" ,
"phone_number" : null ,
"status" : "active" ,
"balance" : 0 ,
"clabe" : "646180109400138692" ,
"address" : null ,
"creation_date" : "2014-02-14T12:30:09-06:00"
}
Guardar tarjeta
Lo siguiente es guardar la tarjeta que el cliente va a usar para la suscripción. Para ello vamos a ocupar el id
del cliente (axapgwwolofnckfui2wx) creado anteriormente.
curl -u sk_e568c42a6c384b7ab02cd47d2e407cab \
-H "Content-type: application/json" \
-X POST -d '{
"card_number":"4111111111111111",
"holder_name":"Mi cliente uno",
"expiration_year":"20",
"expiration_month":"12",
"cvv2":"110"
}' https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/axapgwwolofnckfui2wx/cards
<?
$openpay = Openpay :: getInstance ( 'mzdtln0bmtms6o3kck8f' , 'sk_e568c42a6c384b7ab02cd47d2e407cab' );
$cardDataRequest = array (
'holder_name' => 'Mi cliente uno' ,
'card_number' => '4111111111111111' ,
'cvv2' => '123' ,
'expiration_month' => '12' ,
'expiration_year' => '15' );
$customer = $openpay -> customers -> get ( 'axapgwwolofnckfui2wx' );
$card = $customer -> cards -> add ( $cardDataRequest );
?>
OpenpayAPI api = new OpenpayAPI ( "https://sandbox-api.openpay.mx" , "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Card request = new Card ();
request . holderName ( "Mi cliente uno" );
request . cardNumber ( "4111111111111111" );
request . cvv2 ( "110" );
request . expirationMonth ( 12 );
request . expirationYear ( 20 );
request = api . cards (). create ( "axapgwwolofnckfui2wx" , request );
OpenpayAPI api = new OpenpayAPI ( "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Card request = new Card ();
request . HolderName = "Mi cliente uno" ;
request . CardNumber = "4111111111111111" ;
request . Cvv2 = "110" ;
request . ExpirationMonth = "12" ;
request . ExpirationYear = "20" ;
request = api . CardService . Create ( "axapgwwolofnckfui2wx" , request );
var cardRequest = {
'card_number' : '4111111111111111' ,
'holder_name' : 'JMi cliente uno' ,
'expiration_year' : '20' ,
'expiration_month' : '12' ,
'cvv2' : '110'
};
openpay . customers . cards . create ( 'axapgwwolofnckfui2wx' , cardRequest , function ( error , card ) {
// ...
});
@openpay = OpenpayApi . new ( "mzdtln0bmtms6o3kck8f" , "sk_e568c42a6c384b7ab02cd47d2e407cab" )
@cards = @openpay . create ( :cards )
request_hash = {
"holder_name" => "Mi cliente uno" ,
"card_number" => "4111111111111111" ,
"cvv2" => "110" ,
"expiration_month" => "12" ,
"expiration_year" => "20"
}
response_hash = @cards . create ( request_hash . to_hash , "axapgwwolofnckfui2wx" )
Respuesta:
{
"type" : "debit" ,
"brand" : "visa" ,
"address" : null ,
"id" : "kokzmiiwephcdmq1h2qr" ,
"card_number" : "1111" ,
"holder_name" : "Mi cliente uno" ,
"expiration_year" : "20" ,
"expiration_month" : "12" ,
"allows_charges" : true ,
"allows_payouts" : true ,
"creation_date" : "2014-02-14T13:42:25-06:00" ,
"bank_name" : "Banamex" ,
"customer_id" : "axapgwwolofnckfui2wx" ,
"bank_code" : "002"
}
Suscribir al cliente
Por ultimo crearemos la suscripción con el id
de plan (psjubnktzpofhakixfkp) , el id
del cliente (axapgwwolofnckfui2wx) y el id
de tarjeta (kokzmiiwephcdmq1h2qr) , con lo cual la petición quedaría así:
curl -u sk_e568c42a6c384b7ab02cd47d2e407cab \
-H "Content-type: application/json" \
-X POST -d '{
"card_id":"kokzmiiwephcdmq1h2qr",
"plan_id":"psjubnktzpofhakixfkp"
}' https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/axapgwwolofnckfui2wx/subscriptions
<?
$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 );
?>
final Calendar trialEndDate = Calendar . getInstance ();
trialEndDate . set ( 2014 , 1 , 1 , 0 , 0 , 0 );
OpenpayAPI api = new OpenpayAPI ( "https://sandbox-api.openpay.mx" , "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Subscription request = new Subscription ();
request . planId ( "psjubnktzpofhakixfkp" );
request . trialEndDate ( trialEndDate . getTime ());
request . sourceId ( "kokzmiiwephcdmq1h2qr" );
request = api . subscriptions (). create ( "axapgwwolofnckfui2wx" , request );
OpenpayAPI api = new OpenpayAPI ( "sk_e568c42a6c384b7ab02cd47d2e407cab" , "mzdtln0bmtms6o3kck8f" );
Subscription request = new Subscription ();
request . PlanId = "psjubnktzpofhakixfkp" ;
request . TrialEndDate = new Datetime ( 2014 , 1 , 1 );;
request . CardId = "kokzmiiwephcdmq1h2qr" ;
request = api . SubscriptionService . Create ( "axapgwwolofnckfui2wx" , request );
var subscriptionRequest = {
'plan_id' : 'psjubnktzpofhakixfkp' ,
'source_id' : 'kokzmiiwephcdmq1h2qr' ,
'trial_end_date' : '2014-01-01' ,
};
openpay . customers . subscriptions . create ( "axapgwwolofnckfui2wx" , subscriptionRequest , function ( error , subscription ){
// ...
});
@openpay = OpenpayApi . new ( "mzdtln0bmtms6o3kck8f" , "sk_e568c42a6c384b7ab02cd47d2e407cab" )
@subscriptions = @openpay . create ( :subscriptions )
request_hash = {
"plan_id" => "psjubnktzpofhakixfkp" ,
"trial_end_date" => "2014-01-01" ,
"source_id" => "kokzmiiwephcdmq1h2qr"
}
response_hash = @subscriptions . create ( request_hash . to_hash , "axapgwwolofnckfui2wx" )
Respuesta:
{
"status" : "trial" ,
"card" :{
"type" : "debit" ,
"brand" : "visa" ,
"address" : null ,
"id" : "kokzmiiwephcdmq1h2qr" ,
"card_number" : "1111" ,
"holder_name" : "Mi cliente uno" ,
"expiration_year" : "20" ,
"expiration_month" : "12" ,
"allows_charges" : true ,
"allows_payouts" : true ,
"creation_date" : "2014-02-14T13:42:25-06:00" ,
"bank_name" : "Banamex" ,
"customer_id" : "axapgwwolofnckfui2wx" ,
"bank_code" : "002"
},
"id" : "sfquvei5ya0lwdrd5blo" ,
"cancel_at_period_end" : false ,
"charge_date" : "2014-03-15" ,
"creation_date" : "2014-02-14T13:48:59-06:00" ,
"current_period_number" : 0 ,
"period_end_date" : "2014-03-14" ,
"trial_end_date" : "2014-03-14" ,
"plan_id" : "psjubnktzpofhakixfkp" ,
"customer_id" : "axapgwwolofnckfui2wx"
}
Listo ya tenemos nuestra suscripción creada, la cual se va a cobrar automáticamente cada mes por $99.99 después de que los días de pruebas transcurran.
Notas:
Puedes simular diferentes resultados usando las tarjetas de Pruebas
Implementa las Notificaciones para conocer el estado de los pagos en tiempo real