{"id":3508,"date":"2021-03-10T16:50:22","date_gmt":"2021-03-10T16:50:22","guid":{"rendered":"https:\/\/documents.openpay.mx\/?p=3508"},"modified":"2022-02-18T16:35:13","modified_gmt":"2022-02-18T16:35:13","slug":"openpay-js","status":"publish","type":"post","link":"https:\/\/documents-mx-dev.opdevhm.com\/en\/docs\/openpay-js.html","title":{"rendered":"Openpay.js"},"content":{"rendered":"
What is Openpay.js?<\/strong><\/p> Openpay.js is a Javascript library designed to facilitate the creation of tokens based on credit and debit cards' data retrieved from a web page without passing any information through the source server (the server of the business).<\/p> Advantages:<\/strong><\/p> The first step is to add the Openpay.js library to your page as shown below:<\/p> Configuration<\/strong><\/p> To use Openpay.js you must configure both the ID of the business and the public key that were assigned to you when you created your account on the sandbox dashboard. With these data, Openpay can identify your operations and can assign your cards and charges.<\/p> To configure both parameters use the methods Note:<\/strong><\/span> You can obtain both MERCHANT-ID and the PUBLIC_API_KEY in the dashboard when you log in on your openpay account, in the home page or in your profile page.<\/em><\/span><\/p> Recuerda:<\/strong><\/span> You should never use your private key with this library, because the key is visible on the client side.<\/em><\/span><\/p> Enable sandbox mode<\/strong><\/p> There is a sandbox environment for you to try on your implementation, which is enabled with the method: OpenPay.setSandboxMode ().<\/p> Parameter If necessary, you can use the Note:<\/strong><\/span> The sandbox environment has the same features as production, but it only allows the use of certain card numbers, chosen with testing purposes more information on the test section.<\/em><\/span><\/p> Once 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.<\/p> The parameters for this method are:<\/p> Example of creating a token:<\/p> The method returns an object type token with an id which you\u2019ll need later. You\u2019ll find the definition of the token object here.<\/p> The Openpay.js library facilitates the extraction of the card information using a form, so you can send it later through the The parameters for this method are:<\/p> To start creating tokens, you must have a form like this:<\/p>First Steps<\/h2>
<<\/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><\/figure>
OpenPay.setId()<\/span><\/code> and
OpenPay.setApiKey()<\/span><\/code>, respectively:<\/p>
OpenPay<\/span>.<\/span>setId<\/span>(<\/span>'MERCHANT_ID'<\/span>);<\/span>\n\u200b<\/span>OpenPay<\/span>.<\/span>setApiKey<\/span>(<\/span>'PUBLIC_API_KEY'<\/span>);<\/span><\/code><\/pre><\/figure>
OpenPay<\/span>.<\/span>setSandboxMode<\/span>(<\/span>FLAG<\/span>);<\/span><\/code><\/pre><\/figure>
FLAG<\/span><\/code> <\/span>parameter is a true \/ false flag to enable or disable test mode.<\/p>
OpenPay.getSandboxMode()<\/span><\/code> method to determine the status of the sandbox mode at any time:<\/p>
OpenPay<\/span>.<\/span>getSandboxMode<\/span>();<\/span> \/\/ TRUE\/FALSE, dependiendo si el modo est\u00e1 activado o no.<\/span><\/code><\/pre><\/figure>
Creating tokens without a form.<\/h2>
OpenPay<\/span>.<\/span>token<\/span>.<\/span>create<\/span>(<\/span>CREATE_PARAMETERS_OBJECT<\/span>,<\/span> SUCCESS_CALLBACK<\/span>,<\/span> ERROR_CALLBACK<\/span>);<\/span><\/code><\/pre><\/figure>
CREATE_PARAMETERS_OBJECT<\/span><\/code> is a Javascript object containing the card information.<\/li>
SUCCESS_CALLBACK<\/span><\/code> defines the function to be called if the operation was successful.<\/li>
ERROR_CALLBACK<\/span><\/code> defines the function to be called if the operation failed.<\/li><\/ul>
OpenPay<\/span>.<\/span>token<\/span>.<\/span>create<\/span>({<\/span>\n \"card_number\"<\/span>:<\/span>\"4111111111111111\"<\/span>,<\/span>\n \"holder_name\"<\/span>:<\/span>\"Juan Perez Ramirez\"<\/span>,<\/span>\n \"expiration_year\"<\/span>:<\/span>\"20\"<\/span>,<\/span>\n \"expiration_month\"<\/span><\/span>:<\/span>\"12\"<\/span>,<\/span>\n \"cvv2\"<\/span>:<\/span>\"110\"<\/span>,<\/span>\n \"address\"<\/span>:<\/span>{<\/span>\n \"city\"<\/span>:<\/span>\"Quer\u00e9taro\"<\/span>,<\/span>\n \"line3\"<\/span>:<\/span>\"Queretaro\"<\/span>,<\/span>\n \"postal_code\"<\/span>:<\/span>\"76900\"<\/span>,<\/span>\n \"line1\"<\/span>:<\/span>\"Av 5 de Febrero\"<\/span>,<\/span>\n \"line2\"<\/span>:<\/span>\"Roble 207\"<\/span>,<\/span>\n \"state\"<\/span>:<\/span>\"Queretaro\"<\/span>,<\/span>\n \"country_code\"<\/span>:<\/span>\"MX\"<\/span>\n }<\/span>\n},<\/span> onSuccess<\/span>,<\/span> onError<\/span>);<\/span><\/code><\/pre><\/figure>
Creating tokens with a form<\/h2>
OpenPay.token.extractFormAndCreate()<\/span><\/code><\/p>
OpenPay<\/span>.<\/span>token<\/span>.<\/span>extractFormAndCreate<\/span>(<\/span>CREATE_FORM_OBJECT<\/span>,<\/span> SUCCESS_CALLBACK<\/span>,<\/span> ERROR_CALLBACK<\/span>);<\/span><\/code><\/pre><\/figure>
CREATE_FORM_OBJECT<\/span><\/code> is a Javascript object containing the card information.<\/li>
SUCCESS_CALLBACK<\/span><\/code> defines the function to be called if the operation was successful.<\/li>
ERROR_CALLBACK<\/span><\/code> defines the function to be called if the operation failed.<\/li><\/ul>
<<\/span>form <\/span>id<\/span>=<\/span>\"processCard\"<\/span> name<\/span>=<\/span>\"processCard\"<\/span>><\/span>\n <<\/span>p<\/span>><\/span>Holder Name:<\/<\/span><\/span>p<\/span>><<\/span>input <\/span>data-openpay-card<\/span>=<\/span>\"holder_name\"<\/span> size<\/span>=<\/span>\"50\"<\/span> type<\/span>=<\/span>\"text\"<\/span>><\/span>\n <<\/span>p<\/span>><\/span>Card number:<\/<\/span><\/span>p<\/span>><<\/span>input <\/span>data-openpay-card<\/span>