MercadoPago Custom Subscription Checkout

Official Content

Checkout Steps

  • Create Local Subscription
  • Search for Customer
    • Create Customer if Customer not Found
  • Create New Plan (MercadoPago)
  • Save Plan to Local DataBase
  • Create Subscription in MercadoPago

 

When using the custom subscription method, the user will not leave the seller’s website.
You need a standard token, seller id and a transaction to store information about the subscriptions and one to store information about plans.
First, you need to save a subscription (same as Basic Checkout), which will be edited later, after the user makes the payment.

Create Local Subscription

&Suscription.SusName = 'GX Suscription Custom'
&Suscription.SusCurrency_id = currency_id.Mexico
&Suscription.SusStartDate = now()    
&Suscription.SusDate = now()
&Suscription.SusEndDate = &Suscription.SusStartDate.AddDays(10)
&Suscription.SusAmount = 10
&Suscription.SusFrequency = 1
&Suscription.SusFrequencyType = frequency_type.days
&Suscription.SusPayer_email = !'test_user_email@testuser.com'
&Suscription.SusMPCollector = &Collector_Id

Search For Customer

You must search for the user (in MercadoPago) using the SearchUser procedure where &Email is the user’s you want to search for e-mail.:

MercadoPago.Users.SearchCustomer(&Email,&SearchUserSDT,&Message,&ErrorCode)

//If &ErrorCode = 0 you load the user
if &SearchUserSDT.paging.total=1 
    for &s in &SearchUserSDT.results                                                          
        &user.Load(&s.id.Trim())
        &user.UserId = &s.id.Trim()
        &user.UserMPId = &s.id.Trim()                                                              
        &user.UserEmail = &s.email.Trim()
        &user.UserForTest = false
        &user.UserType = UserType.Buyer
        &user.Save()     
        commit
    endfor
endif

&SearchUserSDT is a SearchUserSDT variable.

Note: &user is a User type variable (transaction in which users are stored in your DataBase).
Note: User is an example transaction, each developer may create the user with the attributes it prefers.
 

Create Customer

//If &ErrorCode is not 0 (this means the user was not found), then you have to create a user (in MercadoPago):
MercadoPago.Users.CreateCustomer(&Email, &ClientSDT,&Message,&ErrorCode)
if &ErrorCode = 0
    &user = new()
    &user.UserId = &ClientSDT.id.Trim()
    &user.UserMPId = &ClientSDT.id.Trim()
    &user.UserNickName = &ClientSDT.nickname.Trim()
    &user.UserPassword = &ClientSDT.password.Trim()              
    &user.UserEmail = &ClientSDT.email.Trim()
    &user.UserForTest = false
    &user.UserType = UserType.Buyer
    &user.Save()                                    
    commit                                               
endif

Create New Plan

A subscription plan indicates how payments are going to be made (ex: monthly, once every month, xxx price, etc.)

&CreatePlanSDT = new()
&CreatePlanSDT.external_reference = 'GX Plan'             
&CreatePlanSDT.status = 'active'
&CreatePlanSDT.description = &Suscription.SusName.Trim()
&CreatePlanSDT.auto_recurring.currency_id = &Suscription.SusCurrency_id  
&CreatePlanSDT.auto_recurring.frequency = &Suscription.SusFrequency
&CreatePlanSDT.auto_recurring.frequency_type = &Suscription.SusFrequencyType
&CreatePlanSDT.auto_recurring.transaction_amount = &Suscription.SusAmount
&CreatePlanSDT.auto_recurring.repetitions = 5
&CreatePlanSDT.auto_recurring.debit_date = &Today.Day()
MercadoPago.Suscriptions.CreatePlan(&CreatePlanSDT,&planSDT,&Message,&errorCode)

Information is returned in &planSDT

Save Plan to Local DataBase

Now a new plan must be created in the local DataBase using the returned information

&plan = new()
&plan.Plan_debit_date = &planSdt.auto_recurring.debit_date
&plan.PlanCurrency = &planSdt.auto_recurring.currency_id
&plan.PlanDsc = &planSdt.description
&plan.PlanFrec_type = &planSdt.auto_recurring.frequency_type
&plan.PlanFrecuency = &planSdt.auto_recurring.frequency
&plan.PlanMPId = &planSdt.id
&plan.PlanRepetitions = &planSdt.auto_recurring.repetitions
&plan.PlanStatus = &planSdt.status
&plan.PlanTrn_amount = &planSdt.auto_recurring.transaction_amount
&plan.Save()

Create Subscription in MercadoPago

Finally, create the subscription in MercadoPago

 
&CreateSubscriptionSDT = new()
&CreateSubscriptionSDT.description = &Suscription.SusName
&CreateSubscriptionSDT.plan_id = &plan.PlanMPId                      
&CreateSubscriptionSDT.external_reference = &Suscription.SuscId.ToString().Trim()
&CreateSubscriptionSDT.payer.id = &user.UserMPId
                               
MercadoPago.Suscriptions.CreateSubscription.Call(&CreateSubscriptionSDT,&subscriptionsdt,&Message,&ErrorCode)

If success (ErrorCode = 0), update your subscription status, date, planId, etc.


Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.