{"id":3387,"date":"2021-03-08T21:27:16","date_gmt":"2021-03-08T21:27:16","guid":{"rendered":"https:\/\/documents.openpay.mx\/?p=3387"},"modified":"2022-05-23T16:37:49","modified_gmt":"2022-05-23T16:37:49","slug":"suscriptions","status":"publish","type":"post","link":"https:\/\/documents-mx-dev.opdevhm.com\/en\/docs\/suscriptions.html","title":{"rendered":"Subscriptions"},"content":{"rendered":"
\n\t\t\t\t\t\t
\n\t\t\t\t\t\t
\n\t\t\t\t\t
\n\t\t\t
\n\t\t\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\tPara esta gu\u00eda vamos a ver como crear una suscripci\u00f3n para cobrar un servicio a un cliente de manera peri\u00f3dica.\n\nDigamos que tienes un servicio en el cual ofreces 1 mes de prueba y despu\u00e9s cobras una renta mensual de $99.99 pesos indefinidamente (o hasta que tu cliente cancele el servicio).\n\nPara ello vamos a hacer lo siguiente:\n
    \n \t
  1. Create a plan<\/li>\n \t
  2. Create a token of debit or credit card using Openpay.js<\/a><\/li>\n \t
  3. Create customer<\/li>\n \t
  4. Card token association with customer<\/li>\n \t
  5. Subscribe customer<\/li>\n<\/ol>\n

    Create a plan<\/h2>\nUn plan es un plantilla para una suscripci\u00f3n que contiene el costo, frecuencia de cobro, n\u00famero de d\u00edas de prueba, etc.\n\nPara crear un plan tenemos dos opciones:\n\nOption 1.- Creation from the API<\/strong>\n
    \n
    \n
    <?<\/span>\n$openpay<\/span> =<\/span> Openpay<\/span>::<\/span>getInstance<\/span>(<\/span>'mzdtln0bmtms6o3kck8f'<\/span>,<\/span> 'sk_e568c42a6c384b7ab02cd47d2e407cab'<\/span>);<\/span>\n\n$planDataRequest<\/span> =<\/span> array<\/span><\/strong>(<\/span>\n    'amount'<\/span> =><\/span> 150.00<\/span>,<\/span>\n    'status_after_retry'<\/span> =><\/span> 'cancelled'<\/span>,<\/span>\n    'retry_times'<\/span> =><\/span> 2<\/span>,<\/span>\n    'name'<\/span> =><\/span> 'Plan Curso Verano'<\/span>,<\/span>\n    'repeat_unit'<\/span> =><\/span> 'month'<\/span>,<\/span>\n    'trial_days'<\/span> =><\/span> '30'<\/span>,<\/span>\n    'repeat_every'<\/span> =><\/span> '1'<\/span>,<\/span>\n    'currency'<\/span> =><\/span> 'MXN'<\/span>);<\/span>\n\n$plan<\/span> =<\/span> $openpay<\/span>-><\/span>plans<\/span>-><\/span>add<\/span>(<\/span>$planDataRequest<\/span>);<\/span>\n?><\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n
    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.<\/blockquote>\nIf the request is successful we will have a response that contains the id<\/code> of the plan\n\nResponse:<\/strong>\n
    \n
      {<\/span>\n     \"name\"<\/span>:<\/span>\"Servicio de TV\"<\/span>,<\/span>\n     \"status\"<\/span>:<\/span>\"active\"<\/span>,<\/span>\n     \"amount\"<\/span>:<\/span>99.99<\/span>,<\/span>\n     \"currency\"<\/span>:<\/span>\"MXN\"<\/span>,<\/span>\n     \"id\"<\/span>:<\/span>\"psjubnktzpofhakixfkp\"<\/span>,<\/span>\n     \"creation_date\"<\/span>:<\/span>\"2014-02-14T13:47:55-06:00\"<\/span>,<\/span>\n     \"repeat_every\"<\/span>:<\/span>1<\/span>,<\/span>\n     \"repeat_unit\"<\/span>:<\/span>\"month\"<\/span>,<\/span>\n     \"retry_times\"<\/span>:<\/span>2<\/span>,<\/span>\n     \"status_after_retry\"<\/span>:<\/span>\"cancelled\"<\/span>,<\/span>\n     \"trial_days\"<\/span>:<\/span>30<\/span>\n  }<\/span>\n  <\/code><\/pre>\n<\/figure>\n

    For more information about the plan parameters go to \"Add Plan\"<\/em><\/span><\/p>\nOption 2.- Creation from the dashboard<\/strong>\n\nYou can also create plans from the dashboard in Planes -> Agregar:\n\n

    \"Agregar<\/center>\n

    Card tokenization<\/h2>\nTo tokenize a customer card using the library Openpay.js<\/a>.\n\nPrimero agregamos al head<\/code> and Openpay.js<\/a> files to the head:\n
    \n
    <<\/span>script<\/span> type<\/span>=<\/span>\"text\/javascript\"<\/span> src<\/span>=<\/span>\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.0\/jquery.min.js\"<\/span>><\/<\/span><\/span>script<\/span>><\/span>\n<<\/span>script<\/span> type<\/span>=<\/span>\"text\/javascript\"<\/span> src<\/span>=<\/span>\"https:\/\/js.openpay.mx\/openpay.v1.min.js\"<\/span>><\/<\/span><\/span>script<\/span>><\/span><\/code><\/pre>\n<\/figure>\nNow let\u2019s set the merchant-id<\/code> and thepublic-key<\/code>):\n
    \n
    <<\/span>script <\/span>type<\/span>=<\/span>\"text\/javascript\"<\/span>><\/span>\n     $<\/span>(<\/span>document<\/span>).<\/span>ready<\/span>(<\/span>function<\/span><\/strong>()<\/span> {<\/span>\n            OpenPay<\/span>.<\/span>setId<\/span>(<\/span>'mzdtln0bmtms6o3kck8f'<\/span>);<\/span>\n            OpenPay<\/span>.<\/span>setApiKey<\/span>(<\/span>'pk_f0660ad5a39f4912872e24a7a660370c'<\/span>);<\/span>\n            OpenPay<\/span>.<\/span>setSandboxMode<\/span>(<\/span>true<\/span><\/strong>);<\/span>\n    });<\/span>\n<\/<\/span><\/span>script<\/span>><\/span><\/code><\/pre>\n<\/figure>\n

    Card token creation<\/h2>\nOnce you have installed and you set up the library, you\u2019ll need to call the OpenPay.token.create () method in order to create a token.\n
    \n
    OpenPay<\/span>.<\/span>token<\/span>.<\/span>create<\/span>(<\/span>CREATE_PARAMETERS_OBJECT<\/span>,<\/span> SUCCESS_CALLBACK<\/span>,<\/span> ERROR_CALLBACK<\/span>);<\/span><\/code><\/pre>\n<\/figure>\nThe parameters for this method are:\n