├── .gitignore ├── .jshintrc ├── .project ├── .travis.yml ├── README.md ├── lib ├── openpay.js └── url-utils.js ├── package-lock.json ├── package.json └── test ├── bank-accounts.test.js ├── cards.test.js ├── charges_test.js ├── colombia ├── cards.test.colombia.js ├── charges_test.colombia.js ├── clients_test.colombia.js ├── plans.test.colombia.js ├── pse_test.colombia.js ├── stores_test.colombia.js ├── subscriptions.test.colombia.js ├── tokens_test.colombia.js └── webhooks_test.colombia.js ├── customers_test.js ├── fees.test.js ├── group_test.js ├── payouts_test.js ├── peru ├── cards.pe.test.js ├── charges.pe.test.js ├── checkouts.pe.test.js ├── customers.pe.test.js ├── tokens.pe.test.js └── webhooks.pe.test.js ├── plans.test.js ├── subscriptions.test.js ├── test.js └── transfers.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /npm-debug.log 3 | .settings/org.eclipse.wst.jsdt.ui.superType.container 4 | .settings/org.eclipse.wst.jsdt.ui.superType.name 5 | package-lock.json 6 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | // -------------------------------------------------------------------- 3 | // JSHint Nodeclipse Configuration v0.11 4 | // Strict Edition with some relaxations and switch to Node.js, no `use strict` 5 | // by Ory Band, Michael Haschke, Paul Verest 6 | // https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.ui/templates/common-templates/.jshintrc 7 | // JSHint Documentation is at http://www.jshint.com/docs/options/ 8 | // JSHint Integration v0.9.9 comes with JSHInt 2.1.10 , see https://github.com/eclipsesource/jshint-eclipse 9 | // -------------------------------------------------------------------- 10 | // from https://gist.github.com/haschek/2595796 11 | // 12 | // This is a options template for [JSHint][1], using [JSHint example][2] 13 | // and [Ory Band's example][3] as basis and setting config values to 14 | // be most strict: 15 | // 16 | // * set all enforcing options to true 17 | // * set all relaxing options to false 18 | // * set all environment options to false, except the node value 19 | // * set all JSLint legacy options to false 20 | // 21 | // [1]: http://www.jshint.com/ 22 | // [2]: https://github.com/jshint/node-jshint/blob/master/example/config.json //404 23 | // [3]: https://github.com/oryband/dotfiles/blob/master/jshintrc 24 | // 25 | // @author http://michael.haschke.biz/ 26 | // @license http://unlicense.org/ 27 | 28 | // == Enforcing Options =============================================== 29 | // 30 | // These options tell JSHint to be more strict towards your code. Use 31 | // them if you want to allow only a safe subset of JavaScript, very 32 | // useful when your codebase is shared with a big number of developers 33 | // with different skill levels. Was all true. 34 | 35 | "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). 36 | "curly" : true, // Require {} for every new block or scope. 37 | "eqeqeq" : true, // Require triple equals i.e. `===`. 38 | "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`. 39 | "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` 40 | "latedef" : true, // Prohibit variable use before definition. 41 | "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. 42 | "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. 43 | "noempty" : true, // Prohibit use of empty blocks. 44 | "nonew" : true, // Prohibit use of constructors for side-effects. 45 | "plusplus" : false, // Prohibit use of `++` & `--`. //coding style related only 46 | "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions. 47 | "undef" : true, // Require all non-global variables be declared before they are used. 48 | "strict" : false, // Require `use strict` pragma in every file. 49 | "trailing" : true, // Prohibit trailing whitespaces. 50 | 51 | // == Relaxing Options ================================================ 52 | // 53 | // These options allow you to suppress certain types of warnings. Use 54 | // them only if you are absolutely positive that you know what you are 55 | // doing. Was all false. 56 | "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). 57 | "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. 58 | "debug" : false, // Allow debugger statements e.g. browser breakpoints. 59 | "eqnull" : false, // Tolerate use of `== null`. 60 | "es5" : true, // Allow EcmaScript 5 syntax. // es5 is default https://github.com/jshint/jshint/issues/1411 61 | "esnext" : false, // Allow ES.next (ECMAScript 6) specific features such as `const` and `let`. 62 | "evil" : false, // Tolerate use of `eval`. 63 | "expr" : false, // Tolerate `ExpressionStatement` as Programs. 64 | "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside. 65 | "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). 66 | "iterator" : false, // Allow usage of __iterator__ property. 67 | "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block. 68 | "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. 69 | "laxcomma" : true, // Suppress warnings about comma-first coding style. 70 | "loopfunc" : false, // Allow functions to be defined within loops. 71 | "multistr" : false, // Tolerate multi-line strings. 72 | "onecase" : false, // Tolerate switches with just one case. 73 | "proto" : false, // Tolerate __proto__ property. This property is deprecated. 74 | "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. 75 | "scripturl" : false, // Tolerate script-targeted URLs. 76 | "smarttabs" : false, // Tolerate mixed tabs and spaces when the latter are used for alignmnent only. 77 | "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. 78 | "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. 79 | "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. 80 | "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function. 81 | 82 | // == Environments ==================================================== 83 | // 84 | // These options pre-define global variables that are exposed by 85 | // popular JavaScript libraries and runtime environments—such as 86 | // browser or node.js. 87 | "browser" : false, // Standard browser globals e.g. `window`, `document`. 88 | "couch" : false, // Enable globals exposed by CouchDB. 89 | "devel" : false, // Allow development statements e.g. `console.log();`. 90 | "dojo" : false, // Enable globals exposed by Dojo Toolkit. 91 | "jquery" : false, // Enable globals exposed by jQuery JavaScript library. 92 | "mootools" : false, // Enable globals exposed by MooTools JavaScript framework. 93 | "node" : true, // Enable globals available when code is running inside of the NodeJS runtime environment. 94 | "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape. 95 | "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework. 96 | "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment. 97 | "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host. 98 | 99 | // == JSLint Legacy =================================================== 100 | // 101 | // These options are legacy from JSLint. Aside from bug fixes they will 102 | // not be improved in any way and might be removed at any point. 103 | "nomen" : false, // Prohibit use of initial or trailing underbars in names. 104 | "onevar" : false, // Allow only one `var` statement per function. 105 | "passfail" : false, // Stop on first error. 106 | "white" : false, // Check against strict whitespace and indentation rules. 107 | 108 | // == Undocumented Options ============================================ 109 | // 110 | // While I've found these options in [example1][2] and [example2][3] 111 | // they are not described in the [JSHint Options documentation][4]. 112 | // 113 | // [4]: http://www.jshint.com/options/ 114 | 115 | "maxerr" : 100, // Maximum errors before stopping. 116 | "predef" : [ // Extra globals. 117 | //"exampleVar", 118 | //"anotherCoolGlobal", 119 | //"iLoveDouglas" 120 | ] 121 | //, "indent" : 2 // Specify indentation spacing 122 | } 123 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | openpay-node 4 | 5 | 6 | 7 | 8 | 9 | com.eclipsesource.jshint.ui.builder 10 | 11 | 12 | 13 | 14 | 15 | org.nodeclipse.ui.NodeNature 16 | org.eclipse.wst.jsdt.core.jsNature 17 | 18 | 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | before_install: 5 | - "npm config set strict-ssl false" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Openpay nodejs](https://www.openpay.mx/img/github/nodejs.jpg) 2 | 3 | ## Installation 4 | 5 | `npm install openpay` 6 | 7 | ## Documentation 8 | 9 | Full API documentation available at http://docs.openpay.mx/. 10 | 11 | ## Overview 12 | 13 | ```js 14 | //class 15 | var Openpay = require('openpay'); 16 | //instantiation 17 | var openpay = new Openpay(' your merchant id ', ' your private key ', [ isProduction ]); 18 | //use the api 19 | openpay.< resource_name >.< method_name >( ... ) 20 | ``` 21 | 22 | All methods accept an optional callback as last argument. 23 | 24 | The callback function should follow the format: function(error, body, response) {...}. 25 | * error: null if the response status code is 200, 201, 204 26 | * body: null if the response status code is different from 200, 201, 204 27 | 28 | ## Examples 29 | 30 | ### Creating a customer 31 | ```js 32 | var newCustomer = { 33 | "name":"John", 34 | "email":"johndoe@example.com", 35 | "last_name":"Doe", 36 | "address":{ 37 | "city":"Queretaro", 38 | "state":"Queretaro", 39 | "line1":"Calle Morelos no 10", 40 | "line2":"col. san pablo", 41 | "postal_code":"76000", 42 | "country_code":"MX" 43 | }, 44 | "phone_number":"44209087654" 45 | }; 46 | 47 | openpay.customers.create(newCustomer, function(error, body, response) { 48 | error; // null if no error occurred (status code != 200||201||204) 49 | body; // contains the object returned if no error occurred (status code == 200||201||204) 50 | response; // contains the complete response including the status, statusCode, statusMessage, and more information 51 | }); 52 | ``` 53 | 54 | ### Charging 55 | ```js 56 | var newCharge = { 57 | "method": "card", 58 | "card": { 59 | "card_number": "4111111111111111", 60 | "holder_name": "John Doe", 61 | "expiration_year": "20", 62 | "expiration_month": "12", 63 | "cvv2": "110", 64 | }, 65 | "amount" : 200.00, 66 | "description" : "Service Charge", 67 | "order_id" : "oid-00721" 68 | }; 69 | openpay.charges.create(testCreateCharge, function (error, body, response){ 70 | // ... 71 | }); 72 | ``` 73 | 74 | ### Payout 75 | ```js 76 | var payout = { 77 | "method": "bank_account", 78 | "bank_account":{ 79 | "clabe":"012298026516924616", 80 | "holder_name": "John Doe" 81 | }, 82 | "amount" : 10.50, 83 | "description" : "Monthly payment" 84 | }; 85 | openpay.payouts.create(payout, function (error, body, response){ 86 | // ... 87 | }); 88 | ``` 89 | 90 | ## Configuration 91 | Before use the library will be necessary to set up your Merchant ID and Private key. 92 | 93 | ```js 94 | var Openpay = require('openpay'); 95 | var openpay = new Openpay('your merchant id', 'your private key', 'mx', false); 96 | openpay.setTimeout(30000); 97 | ``` 98 | 99 | In addition, you can set the merchant id, private key, and the mode like this 100 | 101 | ```js 102 | openpay.setMerchantId(' your merchant id '); 103 | openpay.setPrivateKey(' your private key '); 104 | openpay.setProductionReady(true) 105 | ``` 106 | 107 | Once configured the library, you can use it to interact with Openpay API services. 108 | 109 | ## Development 110 | 111 | To run the tests you'll need your sandbox credentials: merchant id and private key from your [Dashboard](https://sandbox-dashboard.openpay.mx/): 112 | 113 | ```bash 114 | $ npm install -g mocha 115 | $ npm test 116 | ``` 117 | 118 | # Implementation 119 | 120 | ## Usage for Mexico 121 | 122 | ### Bank accounts 123 | 124 | #### Creating a bank account 125 | ```js 126 | var customerId = 'customer ID'; 127 | var newBankAccount = { 128 | "clabe": "111111111111111111", 129 | "holder_name": "Juan H", 130 | "alias": "Alias" 131 | }; 132 | openpay.customers.bankaccounts.create( 133 | customerId, 134 | newBankAccount, 135 | function (error, body, response) { 136 | // ... 137 | }); 138 | ``` 139 | 140 | #### Get bank account: 141 | 142 | ```js 143 | var customerId = 'Customer ID'; 144 | var bankAccountId = 'Bank Account Id'; 145 | openpay.customers.bankaccounts.get( 146 | customerId, 147 | bankAccountId, 148 | function (error, body, response) { 149 | // ... 150 | }); 151 | ``` 152 | 153 | #### Delete bank account: 154 | ```js 155 | var customerId = 'Customer ID'; 156 | var bankAccountId = 'Bank Account Id'; 157 | openpay.customers.bankaccounts.delete( 158 | customerId, 159 | bankAccountId, 160 | function (error, body, response) { 161 | // ... 162 | }); 163 | ``` 164 | 165 | #### List bank accounts: 166 | ```js 167 | var customerId = 'Customer ID'; 168 | var data = { 169 | "creation": "2021-10-22", // Format yyyy-mm-dd 170 | "offset": "", 171 | "limit": "" 172 | } 173 | openpay.customers.bankaccounts.list( 174 | customerId, 175 | data, 176 | function (error, body, response) { 177 | // ... 178 | }); 179 | ``` 180 | 181 | Instead of the 'creation' field in the 'data' object, you can use: 182 | * "creation[gte]" : "2021-10-22" to find bank accounts created after the given date 183 | * "creation[lte]" : "2021-10-22" to find bank accounts created before the given date 184 | 185 | ### Cards 186 | 187 | #### Create a card 188 | 189 | ```js 190 | var newCard = { 191 | "card_number": "4111111111111111", 192 | "holder_name": "Juan Perez", 193 | "expiration_year": 2021, 194 | "expiration_month": "12", 195 | "cvv2": "111" 196 | }; 197 | openpay.cards.create( 198 | newCard, 199 | function (error, body, response) { 200 | // ... 201 | }); 202 | ``` 203 | Other way to create a card is through a customer 204 | 205 | ```js 206 | var customerId = 'Customer ID'; 207 | var newCard = { 208 | "card_number": "4111111111111111", 209 | "holder_name": "Juan Perez", 210 | "expiration_year": 2021, 211 | "expiration_month": "12", 212 | "cvv2": "111" 213 | }; 214 | openpay.customers.cards.create( 215 | customerId, newCard, 216 | function (error, body, response) { 217 | // ... 218 | }); 219 | ``` 220 | 221 | #### Get a card 222 | 223 | ```js 224 | var cardId = 'Card ID'; 225 | openpay.cards.get( 226 | cardId, 227 | function (error, body, response) { 228 | // ... 229 | }); 230 | ``` 231 | 232 | Just as the method to create a new card, you can get a card through a customer, as is shown below: 233 | 234 | ```js 235 | var cardId = 'Card ID'; 236 | var customerId = 'Customer ID'; 237 | openpay.customers.cards.get( 238 | cardId, 239 | customerId, 240 | function (error, body, response) { 241 | // ... 242 | }); 243 | ``` 244 | 245 | ### Charges 246 | 247 | ### Customers 248 | 249 | ### Fees 250 | 251 | ### Groups 252 | 253 | ### Payouts 254 | 255 | ### Plans 256 | 257 | ### Subscriptions 258 | 259 | ### Tests 260 | 261 | ### Transfers 262 | 263 | ## Usage for Colombia 264 | 265 | ### Cards 266 | #### Create a card 267 | There are three ways to create a card, the first one is with a token. 268 | ```js 269 | var data = { 270 | "token_id": "Token ID", 271 | "device_session_id": "Device session ID" 272 | }; 273 | openpay.cards.create( 274 | data, 275 | function (error, body, response) { 276 | // ... 277 | }); 278 | ``` 279 | The second one is with the card information only. 280 | ```js 281 | var newCard = { 282 | "card_number": "4111111111111111", 283 | "holder_name": "Juan Perez", 284 | "expiration_year": '28', 285 | "expiration_month": "12", 286 | "cvv2": "111" 287 | }; 288 | openpay.cards.create(newCard, 289 | function (error, body, response) { 290 | // ... 291 | }); 292 | ``` 293 | And the last one is through a customer. 294 | ```js 295 | var customerId = 'Customer ID'; 296 | var newCard = { 297 | "card_number": "4111111111111111", 298 | "holder_name": "Juan Perez", 299 | "expiration_year": '28', 300 | "expiration_month": "12", 301 | "cvv2": "111" 302 | }; 303 | openpay.customers.cards.create( 304 | customerId, 305 | newCard, 306 | function (error, body, response) { 307 | // ... 308 | }); 309 | ``` 310 | #### Get a card 311 | ###### Without customer 312 | ```js 313 | var cardId = 'Card ID'; 314 | openpay.cards.get( 315 | cardId, 316 | function (error, body, response) { 317 | // ... 318 | }); 319 | ``` 320 | ###### With customer 321 | ```js 322 | var customerId = 'Customer ID'; 323 | var cardId = 'Card ID'; 324 | openpay.customers.cards.get( 325 | customerId, 326 | cardId, 327 | function (error, body, response) { 328 | // ... 329 | }); 330 | ``` 331 | #### Delete a card 332 | ###### Without customer 333 | ```js 334 | var cardId = 'Card ID'; 335 | openpay.cards.delete( 336 | cardId, 337 | function (error, body, response) { 338 | // ... 339 | }); 340 | ``` 341 | ###### With customer 342 | ```js 343 | var customerId = 'Customer ID'; 344 | var cardId = 'Card ID'; 345 | openpay.customers.cards.delete( 346 | customerId, 347 | cardId, 348 | function (error, body, response) { 349 | // ... 350 | }); 351 | ``` 352 | 353 | #### List cards 354 | ###### Without customer 355 | ```js 356 | var searchParams = { 357 | "creation": "2021-10-22", // Format yyyy-mm-dd 358 | "offset": "", 359 | "limit": "" 360 | } 361 | openpay.cards.list( 362 | searchParams, 363 | function (error, body, response) { 364 | // ... 365 | }); 366 | ``` 367 | ###### With customer 368 | ```js 369 | var customerId = 'Customer ID'; 370 | var searchParams = { 371 | "creation": "2021-10-22", // Format yyyy-mm-dd 372 | "offset": "", 373 | "limit": "" 374 | } 375 | openpay.customers.cards.list( 376 | customerId, 377 | searchParams, 378 | function (error, body, response) { 379 | // ... 380 | }); 381 | ``` 382 | Instead of the 'creation' field in the 'searchParams' object, you can use: 383 | * "creation[gte]" : "2021-10-22" to find bank accounts created after the given date 384 | * "creation[lte]" : "2021-10-22" to find bank accounts created before the given date 385 | 386 | ### Charges 387 | #### Create a charge 388 | ###### With customer 389 | ```js 390 | var customerId = 'Customer ID'; 391 | var newCharge = { 392 | "method": "card", 393 | "source_id": "", 394 | "amount": 20, 395 | "description": "Test Charge", 396 | "currency": "COP", 397 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 398 | } 399 | openpay.customers.charges.create( 400 | customerId, 401 | newCharge, 402 | function (error, body, response) { 403 | // ... 404 | }); 405 | ``` 406 | 407 | ###### Without customer 408 | ```js 409 | var newCharge = { 410 | "source_id": "kdx205scoizh93upqbte", 411 | "method": "card", 412 | "amount": 200, 413 | "currency": "COP", 414 | "iva": "10", 415 | "description": "Cargo inicial a mi cuenta", 416 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f", 417 | "customer": { 418 | "name": "Cliente Colombia", 419 | "last_name": "Vazquez Juarez", 420 | "phone_number": "4448936475", 421 | "email": "juan.vazquez@empresa.co" 422 | } 423 | } 424 | 425 | penpay.charges.create( 426 | newCharge, 427 | function (error, body, response) { 428 | // ... 429 | }); 430 | ``` 431 | #### Create a store charge 432 | ###### With customer 433 | ```js 434 | var customerId = 'Customer ID'; 435 | var newCharge = { 436 | "method": "store", 437 | "source_id": "", 438 | "amount": 20, 439 | "description": "Test Charge", 440 | "currency": "COP", 441 | "device_session_id": null 442 | } 443 | openpay.customers.charges.create( 444 | customerId, 445 | newCharge, 446 | function (error, body, response) { 447 | // ... 448 | }); 449 | ``` 450 | ###### Without customer 451 | ```js 452 | var newCharge = { 453 | "source_id": null, 454 | "method": "store", 455 | "amount": 200, 456 | "currency": "COP", 457 | "iva": "10", 458 | "description": "Cargo inicial a mi cuenta", 459 | "device_session_id": null, 460 | "customer": { 461 | "name": "Cliente Colombia", 462 | "last_name": "Vazquez Juarez", 463 | "phone_number": "4448936475", 464 | "email": "juan.vazquez@empresa.co" 465 | } 466 | } 467 | 468 | penpay.charges.create( 469 | newCharge, 470 | function (error, body, response) { 471 | // ... 472 | }); 473 | ``` 474 | 475 | #### Refund a charge 476 | Only card charges can be refunded. 477 | 478 | ```js 479 | var chargeId = 'Charge ID'; 480 | var data = { 481 | "description": "testing refound", 482 | "amount": 200 483 | } 484 | openpay.charges.refund( 485 | chargeId, 486 | data, 487 | function (error, body, response) { 488 | // ... 489 | }); 490 | ``` 491 | #### Get a charge 492 | 493 | ###### Without customer 494 | ```js 495 | var chargeId = 'Charge ID'; 496 | openpay.charges.get( 497 | chargeId, 498 | function (error, body, response) { 499 | // ... 500 | }); 501 | ``` 502 | ###### With customer 503 | ```js 504 | var customerId = 'Customer ID'; 505 | var chargeId = 'Charge ID'; 506 | openpay.customers.charges.get( 507 | customerId, 508 | chargeId, 509 | function (error, body, response) { 510 | // ... 511 | }); 512 | ``` 513 | #### List charges 514 | ###### Without customer 515 | ```js 516 | var searchParams = { 517 | 'order_id': 'Order ID', 518 | 'creation': 'yyyy-mm-dd', 519 | 'offset': 1, 520 | 'limit': 1, 521 | 'amount': 100, 522 | 'status': 'IN_PROGRESS' 523 | } 524 | openpay.charges.list(searchParams, 525 | function (error, body, response) { 526 | // ... 527 | }); 528 | ``` 529 | ###### With customer 530 | ```js 531 | var customerId = 'Customer ID'; 532 | var searchParams = { 533 | 'order_id': 'Order ID', 534 | 'creation': 'yyyy-mm-dd', 535 | 'offset': 1, 536 | 'limit': 1, 537 | 'amount': 100, 538 | 'status': 'IN_PROGRESS' 539 | } 540 | openpay.customers.charges.list( 541 | customerId, 542 | searchParams, 543 | function (error, body, response) { 544 | // ... 545 | }); 546 | ``` 547 | 548 | Instead of the 'creation' field in the 'searchParams' object, you can use: 549 | * "creation[gte]" : "2021-10-22" to find charges created after the given date 550 | * "creation[lte]" : "2021-10-22" to find charges created before the given date 551 | 552 | Instead of the 'amount' field in the 'searchParams' object, you can use: 553 | * "amount[gte]" : 100 to find charges with amount bigger than the amount given 554 | * "amount[lte]" : 100 to find charges with amount smaller than the amount given 555 | 556 | Allowed statuses: 557 | * IN_PROGRESS 558 | * COMPLETED 559 | * REFUNDED 560 | * CHARGEBACK_PENDING 561 | * CHARGEBACK_ACCEPTED 562 | * CHARGEBACK_ADJUSTMENT 563 | * CHARGE_PENDING 564 | * CANCELLED 565 | * FAILED 566 | ### Customers 567 | 568 | ####Create a customer 569 | ```js 570 | var newCustomer = { 571 | 'external_id' : '', 572 | "name": "Pedro Diego", 573 | "last_name": "Alatorre Martínez", 574 | "email": "pedro.alatorre@comercio.com", 575 | "phone_number" : "5744484951", 576 | "customer_address": { 577 | "department":"Medellín", 578 | "city":"Antioquía", 579 | "additional":"Avenida 7f bis # 138-58 Apartamento 942" 580 | } 581 | }; 582 | openpay.customers.create( 583 | newCustomer, 584 | function (error, body, response) { 585 | // ... 586 | }); 587 | ``` 588 | #### Update a customer 589 | ```js 590 | var customerId = 'Customer ID'; 591 | var newData = { 592 | "name": "Pedro Diego", 593 | "last_name": "Alatorre Martínez", 594 | "email": "pedro.alatorre@comercio.com", 595 | "phone_number": "5744484951", 596 | "customer_address": { 597 | "department": "Medellín", 598 | "city": "Antioquía", 599 | "additional": "Avenida 7f bis # 138-58 Apartamento 942" 600 | } 601 | }; 602 | openpay.customers.update( 603 | customerId, 604 | newData, 605 | function (error, body, response) { 606 | // ... 607 | }); 608 | ``` 609 | #### Get a customer 610 | ```js 611 | var customerId = 'Customer ID'; 612 | openpay.customers.get( 613 | customerId, 614 | function (error, body, response) { 615 | // ... 616 | }); 617 | ``` 618 | #### Delete a customer 619 | ```js 620 | var customerId = 'Customer ID'; 621 | openpay.customers.delete( 622 | customerId, 623 | function (error, body, response) { 624 | // ... 625 | }); 626 | ``` 627 | 628 | #### List clients 629 | ```js 630 | const searchParams = { 631 | 'external_id': 'External ID', 632 | 'creation': 'yyyy-mm-dd', 633 | 'offset': 1, 634 | 'limit': 1 635 | } 636 | openpay.customers.list( 637 | searchParams, 638 | function (error, body, response) { 639 | // ... 640 | }); 641 | ``` 642 | Instead of the 'creation' field in the 'searchParams' object, you can use: 643 | * "creation[gte]" : "2021-10-22" to find customers created after the given date 644 | * "creation[lte]" : "2021-10-22" to find customers created before the given date 645 | 646 | ### Plans 647 | #### Create a plan 648 | ```js 649 | var newPlan = { 650 | "amount": 150, 651 | "status_after_retry": "CANCELLED", 652 | "retry_times": 2, 653 | "name": "Curso de ingles", 654 | "repeat_unit": "month", 655 | "trial_days": "30", 656 | "repeat_every": "1" 657 | }; 658 | openpay.plans.create( 659 | newPlan, 660 | function (error, body, response) { 661 | // ... 662 | }); 663 | ``` 664 | Allowed values for 'status_after_retry': 665 | * UNPAID 666 | * CANCELLED 667 | 668 | 669 | #### Update a plan 670 | ```js 671 | var planId = 'Plan ID'; 672 | var newData = { 673 | "name": "New name", 674 | "trial_days": 1 675 | } 676 | openpay.plans.update( 677 | planId, 678 | newData, 679 | function (error, body, response) { 680 | // ... 681 | }); 682 | ``` 683 | #### Get a plan 684 | ```js 685 | var planId = 'Plan ID'; 686 | openpay.plans.get( 687 | planId, 688 | function (error, body, response) { 689 | // ... 690 | }); 691 | ``` 692 | #### Delete a plan 693 | ```js 694 | var planId = 'Plan ID'; 695 | openpay.plans.delete( 696 | planId, 697 | function (error, body, response) { 698 | // ... 699 | }); 700 | ``` 701 | 702 | #### List plans 703 | ```js 704 | var searchParams = { 705 | 'creation': 'yyyy-mm-dd', 706 | 'limit': 10, 707 | 'offset': 1 708 | }; 709 | openpay.plans.list(searchParams, function (error, body, response) { 710 | // ... 711 | }); 712 | ``` 713 | Instead of the 'creation' field in the 'searchParams' object, you can use: 714 | * "creation[gte]" : "2021-10-22" to find plans created after the given date 715 | * "creation[lte]" : "2021-10-22" to find plans created before the given date 716 | 717 | ### PSE 718 | #### Create PSE 719 | ###### Without a client 720 | ```js 721 | var newPse = { 722 | "method": "bank_account", 723 | "amount": 10000, 724 | "currency": "COP", 725 | "description": "Cargo inicial a mi cuenta", 726 | "iva": "1900", 727 | "redirect_url": "/", 728 | "customer": { 729 | "name": "Cliente Colombia", 730 | "last_name": "Vazquez Juarez", 731 | "email": "juan.vazquez@empresa.co", 732 | "phone_number": "4448936475", 733 | "requires_account": false, 734 | "customer_address": { 735 | "department": "Medellín", 736 | "city": "Antioquía", 737 | "additional": "Avenida 7m bis #174-25 Apartamento 637" 738 | } 739 | } 740 | } 741 | openpay.pse.create( 742 | newPse, 743 | function (error, body, response) { 744 | // ... 745 | }); 746 | ``` 747 | ###### With existing client 748 | ```js 749 | var customerId = 'Customer ID'; 750 | var newPse = { 751 | "method": "bank_account", 752 | "amount": 10000, 753 | "currency": "COP", 754 | "description": "Cargo inicial a mi cuenta", 755 | "iva": "1900", 756 | "redirect_url": "/" 757 | } 758 | openpay.customers.pse.create( 759 | customerId, 760 | newPse, 761 | function (error, body, response) { 762 | // ... 763 | }); 764 | ``` 765 | ### Subscriptions 766 | #### Create a subscription 767 | ```js 768 | var customerId = 'Customer ID'; 769 | var newSubscription = { 770 | "card": { 771 | "card_number": "4111111111111111", 772 | "holder_name": "Juan Perez Ramirez", 773 | "expiration_year": "20", 774 | "expiration_month": "12", 775 | "cvv2": "110", 776 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 777 | }, 778 | "plan_id": "pbi4kb8hpb64x0uud2eb", 779 | "trial_end_date": "yyyy-mm-dd", 780 | "source_id": "pbiskbfhps64f0uudgeb" 781 | } 782 | openpay.customers.subscriptions.create( 783 | customerId, 784 | newSubscription, 785 | function (error, body, response) { 786 | // ... 787 | }); 788 | ``` 789 | #### Update subscription 790 | ```js 791 | var subscriptionId = 'Subscription ID'; 792 | const newData = { 793 | "trial_end_date": "2021-12-12", 794 | "cancel_at_period_end": true, 795 | "source_id": "pbiskbfhps64f0uudgeb", 796 | "card": { 797 | "card_number": "4111111111111111", 798 | "holder_name": "Juan Perez Ramirez", 799 | "expiration_year": "20", 800 | "expiration_month": "12", 801 | "cvv2": "110", 802 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 803 | } 804 | } 805 | openpay.customers.subscriptions.update( 806 | subscriptionId, 807 | newData, 808 | function (error, body, response) { 809 | // ... 810 | }); 811 | ``` 812 | #### Get a subscription 813 | ```js 814 | var customerId = 'Customer ID'; 815 | var subscriptionId = 'Subscription ID'; 816 | openpay.customers.subscriptions.get( 817 | customerId, 818 | subscriptionId, 819 | function (error, body, response) { 820 | // ... 821 | }); 822 | ``` 823 | #### Cancel a subscription 824 | ```js 825 | var subscriptionId = 'Subscription ID'; 826 | openpay.customers.subscriptions.delete( 827 | subscriptionId, 828 | function (error, body, response) { 829 | // ... 830 | }); 831 | ``` 832 | #### List subscriptions 833 | ```js 834 | var searchParams = { 835 | 'creation': 'yyyy-mm-dd', 836 | 'limit': 10, 837 | 'offset': 1 838 | }; 839 | openpay.customers.subscriptions.list( 840 | searchParams, 841 | function (error, body, response) { 842 | // ... 843 | }); 844 | ``` 845 | Instead of the 'creation' field in the 'searchParams' object, you can use: 846 | * "creation[gte]" : "2021-10-22" to find subscriptions created after the given date 847 | * "creation[lte]" : "2021-10-22" to find subscriptions created before the given date 848 | 849 | ### Webhooks 850 | #### Create a webhook 851 | ```js 852 | var newWebhook = { 853 | "url": "https://my-site.com/my-webhook", 854 | "user": "juanito", 855 | "password": "passjuanito", 856 | "event_types": [ 857 | "charge.refunded", 858 | "charge.failed", 859 | "charge.cancelled", 860 | "charge.created", 861 | "chargeback.accepted" 862 | ] 863 | } 864 | openpay.webhooks.create( 865 | newWebhook, 866 | function (error, body, response) { 867 | // ... 868 | }); 869 | ``` 870 | #### Get a webhook 871 | ```js 872 | var webhookId = 'Webhook ID'; 873 | openpay.webhooks.get( 874 | webhookId, 875 | function (error, body, response) { 876 | // ... 877 | }); 878 | ``` 879 | 880 | #### Delete a webhook 881 | ```js 882 | var webhookId = 'Webhook ID'; 883 | openpay.webhooks.delete( 884 | webhookId, 885 | function (error, body, response) { 886 | // ... 887 | }); 888 | ``` 889 | #### List webhooks 890 | ```js 891 | openpay.webhooks.list( 892 | function (error, body, response) { 893 | // ... 894 | }); 895 | ``` 896 | 897 | ### Tokens 898 | #### Create a token 899 | ```js 900 | var newToken = { 901 | "card_number": "4111111111111111", 902 | "holder_name": "Juan Perez Ramirez", 903 | "expiration_year": "29", 904 | "expiration_month": "12", 905 | "cvv2": "110", 906 | "address": { 907 | "city": "Bogotá", 908 | "country_code": "CO", 909 | "postal_code": "110511", 910 | "line1": "Av 5 de Febrero", 911 | "line2": "Roble 207", 912 | "line3": "col carrillo", 913 | "state": "Bogota" 914 | } 915 | }; 916 | openpay.tokens.create( 917 | tokenNew, 918 | function (error, body, response) { 919 | // ... 920 | }); 921 | ``` 922 | #### Get a token 923 | ```js 924 | var tokenId = 'Token ID'; 925 | openpay.tokens.get( 926 | tokenId, 927 | function (error, body, response) { 928 | // ... 929 | }); 930 | ``` 931 | ### Stores 932 | #### List stores 933 | ```js 934 | var location = { 935 | "latitud": 4.65589142889691, 936 | "longitud": -74.11335673251888, 937 | "kilometers": 10, 938 | "amount": 1 939 | } 940 | openpay.stores.list( 941 | location, 942 | function (error, body, response) { 943 | // ... 944 | }); 945 | ``` 946 | 947 | ## Usage for Peru 948 | ### Charges 949 | ####Create a charge 950 | ###### With customer 951 | ```js 952 | var customerId = 'Customer ID'; 953 | const newCharge = { 954 | "source_id": "kdx205scoizh93upqbte", 955 | "method": "card", 956 | "amount": 716, 957 | "currency": "PEN", 958 | "description": "Cargo inicial a mi cuenta", 959 | "order_id": "oid-65584", 960 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 961 | } 962 | openpay.customers.charges.create( 963 | customerId, 964 | newCharge, 965 | function (error, body, response) { 966 | // ... 967 | }); 968 | ``` 969 | ###### Without customer 970 | ```js 971 | var newCharge = { 972 | "source_id": "kdx205scoizh93upqbte", 973 | "method": "card", 974 | "amount": 100, 975 | "currency": "PEN", 976 | "description": "Cargo inicial a mi cuenta", 977 | "order_id": "oid-65584", 978 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f", 979 | "customer": { 980 | "name": "Cliente Perú", 981 | "last_name": "Vazquez Juarez", 982 | "phone_number": "4448936475", 983 | "email": "juan.vazquez@empresa.pe" 984 | } 985 | }; 986 | openpay.charges.create( 987 | newCharge, 988 | function (error, body, response) { 989 | // ... 990 | }); 991 | ``` 992 | ###### Store charge 993 | ```js 994 | var newCharge = { 995 | "source_id": null, 996 | "method": "store", 997 | "amount": 100, 998 | "currency": "PEN", 999 | "description": "Cargo inicial a mi cuenta", 1000 | "order_id": "oid-65584", 1001 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f", 1002 | "customer": { 1003 | "name": "Cliente Perú", 1004 | "last_name": "Vazquez Juarez", 1005 | "phone_number": "4448936475", 1006 | "email": "juan.vazquez@empresa.pe" 1007 | } 1008 | }; 1009 | openpay.charges.create( 1010 | newCharge, 1011 | function (error, body, response) { 1012 | // ... 1013 | }); 1014 | ``` 1015 | #### Get a charge 1016 | ###### Without customer 1017 | ```js 1018 | var chargeId = 'Charge ID'; 1019 | openpay.charges.get( 1020 | chargeId, 1021 | function (error, body, response) { 1022 | // ... 1023 | }); 1024 | ``` 1025 | ###### With customer 1026 | ```js 1027 | var customerId = 'Customer ID'; 1028 | var chargeId = 'Charge ID'; 1029 | openpay.customers.charges.get( 1030 | customerId, 1031 | chargeId, 1032 | function (error, body, response) { 1033 | // ... 1034 | }); 1035 | ``` 1036 | #### List charges 1037 | ###### Without customer 1038 | ```js 1039 | var searchParams = { 1040 | 'order_id': 'Order ID', 1041 | 'creation': 'yyyy-mm-dd', 1042 | 'offset': 1, 1043 | 'limit': 1, 1044 | 'amount': 100, 1045 | 'status': 'IN_PROGRESS' 1046 | } 1047 | openpay.charges.list( 1048 | searchParams, 1049 | function (error, body, response) { 1050 | // ... 1051 | }); 1052 | ``` 1053 | ######With customer 1054 | ```js 1055 | var customerId = 'Customer ID'; 1056 | var searchParams = { 1057 | 'order_id': 'Order ID', 1058 | 'creation': 'yyyy-mm-dd', 1059 | 'offset': 1, 1060 | 'limit': 1, 1061 | 'amount': 100, 1062 | 'status': 'IN_PROGRESS' 1063 | }; 1064 | 1065 | openpay.customers.charges.list( 1066 | customerId, 1067 | searchParams, 1068 | function (error, body, response) { 1069 | // ... 1070 | }); 1071 | ``` 1072 | Instead of the 'creation' field in the 'searchParams' object, you can use: 1073 | 1074 | * "creation[gte]" : "2021-10-22" to find charges created after the given date 1075 | * "creation[lte]" : "2021-10-22" to find charges created before the given date 1076 | 1077 | Instead of the 'amount' field in the 'searchParams' object, you can use: 1078 | 1079 | * "amount[gte]" : 100 to find charges with amount bigger than the amount given 1080 | * "amount[lte]" : 100 to find charges with amount smaller than the amount given 1081 | 1082 | Allowed statuses: 1083 | * IN_PROGRESS 1084 | * COMPLETED 1085 | * REFUNDED 1086 | * CHARGEBACK_PENDING 1087 | * CHARGEBACK_ACCEPTED 1088 | * CHARGEBACK_ADJUSTMENT 1089 | * CHARGE_PENDING 1090 | * CANCELLED 1091 | * FAILED 1092 | 1093 | 1094 | ### Checkouts 1095 | #### Create a checkout 1096 | ###### Without customer 1097 | ```js 1098 | var newCheckout = { 1099 | "amount": 250, 1100 | "currency": "PEN", 1101 | "description": "Cargo cobro con link", 1102 | "redirect_url": "https://misitioempresa.pe", 1103 | "order_id": "oid-66393", 1104 | "expiration_date": "2021-08-31 12:50", 1105 | "send_email": "true", 1106 | "customer": { 1107 | "name": "Cliente Perú", 1108 | "last_name": "Vazquez Juarez", 1109 | "phone_number": "4448936475", 1110 | "email": "juan.vazquez@empresa.pe" 1111 | } 1112 | } 1113 | openpay.checkouts.create( 1114 | newCheckout, 1115 | function (error, body, response) { 1116 | // ... 1117 | }); 1118 | ``` 1119 | ###### With customer 1120 | ```js 1121 | var customerId = 'Customer ID'; 1122 | var newCheckout = { 1123 | "amount": 250, 1124 | "currency": "PEN", 1125 | "description": "Cargo cobro con link cliente", 1126 | "redirect_url": "https://misitioempresa.pe", 1127 | "order_id": "oid-87491", 1128 | "send_email": "true" 1129 | } 1130 | openpay.customers.checkouts.create( 1131 | customerId, 1132 | newCheckout, 1133 | function (error, body, response) { 1134 | // ... 1135 | }); 1136 | ``` 1137 | #### List checkouts 1138 | ```js 1139 | var searchParams = { 1140 | "limit": 2, 1141 | "startDate": "20211001", // Format: yyyymmdd 1142 | "endDate": "20211011" // Format: yyyymmdd 1143 | }; 1144 | openpay.checkouts.list( 1145 | searchParams, 1146 | function (error, body, response) { 1147 | // ... 1148 | }); 1149 | ``` 1150 | 1151 | #### Update checkout 1152 | ```js 1153 | var checkoutId = 'Checkout ID'; 1154 | var newStatus = "available"; 1155 | var newData = { 1156 | "expiration_date": "2021-10-26 13:43" //Format: yyyy-mm-dd HH:mm 1157 | } 1158 | 1159 | openpay.checkouts.update( 1160 | checkoutId, 1161 | newStatus, 1162 | newData, 1163 | function (error, body, response) { 1164 | // ... 1165 | }); 1166 | ``` 1167 | Allowed statuses: 1168 | * available 1169 | * other 1170 | * other 1171 | 1172 | #### Get checkout 1173 | ```js 1174 | var checkoutId = 'Checkout ID'; 1175 | openpay.checkouts.get( 1176 | checkoutId, 1177 | function (error, body, response) { 1178 | // ... 1179 | }); 1180 | ``` 1181 | ### Customers 1182 | #### Create a customer 1183 | ```js 1184 | var newCustomer = { 1185 | "name": "Juan", 1186 | "last_name": "Perez", 1187 | "email": "juan.perez.@email.com", 1188 | "phone_number": "1234567890", 1189 | "address": { 1190 | "country_code": "PE", 1191 | "postal_code": "12345", 1192 | "city": "Lima", 1193 | "state": "Lima", 1194 | "line1": "Perú", 1195 | "line2": "Perú", 1196 | "line3": "Perú" 1197 | } 1198 | }; 1199 | openpay.customers.create( 1200 | newCustomer, 1201 | function (error, body, response) { 1202 | // ... 1203 | }); 1204 | ``` 1205 | #### Update a customer 1206 | ```js 1207 | var customerId = 'Customer ID'; 1208 | var newData = { 1209 | "name": "Juan", 1210 | "last_name": "Perez", 1211 | "email": "juan.perez.@email.com", 1212 | "phone_number": "1234567890", 1213 | "address": { 1214 | "country_code": "PE", 1215 | "postal_code": "12345", 1216 | "city": "Lima", 1217 | "state": "Lima", 1218 | "line1": "Perú", 1219 | "line2": "Perú", 1220 | "line3": "Perú" 1221 | } 1222 | } 1223 | openpay.customers.update( 1224 | customerId, 1225 | newData, 1226 | function (error, body, response) { 1227 | // ... 1228 | }); 1229 | ``` 1230 | #### Get a customer 1231 | ```js 1232 | var customerId = 'Customer ID'; 1233 | openpay.customers.get( 1234 | customerId, 1235 | function (error, body, respose) { 1236 | // ... 1237 | }); 1238 | ``` 1239 | #### Delete a customer 1240 | ```js 1241 | var customerId = 'Customer ID'; 1242 | openpay.customers.delete( 1243 | customerId, 1244 | function (error, body, respose) { 1245 | // ... 1246 | }); 1247 | ``` 1248 | #### List clients 1249 | ```js 1250 | var searchParams = { 1251 | 'external_id': 'External ID', 1252 | 'creation': 'yyyy-mm-dd', 1253 | 'offset': 1, 1254 | 'limit': 1 1255 | }; 1256 | openpay.customers.list( 1257 | searchParams, 1258 | function (error, body, response) { 1259 | // ... 1260 | }); 1261 | ``` 1262 | Instead of the 'creation' field in the 'searchParams' object, you can use: 1263 | * "creation[gte]" : "2021-10-22" to find customers created after the given date 1264 | * "creation[lte]" : "2021-10-22" to find customers created before the given date 1265 | ### Cards 1266 | #### Create a card 1267 | ###### With customer 1268 | 1269 | ```js 1270 | var customerId = 'Customer ID'; 1271 | var newCard = { 1272 | "holder_name": "Juan Perez", 1273 | "card_number": "4111111111111111", 1274 | "cvv2": "651", 1275 | "expiration_month": "09", 1276 | "expiration_year": "25" 1277 | }; 1278 | openpay.customers.cards.create( 1279 | customerId, 1280 | newCard, 1281 | function (error, body, response) { 1282 | // ... 1283 | }); 1284 | ``` 1285 | ###### Without customer 1286 | ```js 1287 | var newCard = { 1288 | "holder_name": "Juan Perez", 1289 | "card_number": "4111111111111111", 1290 | "cvv2": "651", 1291 | "expiration_month": "09", 1292 | "expiration_year": "25" 1293 | }; 1294 | openpay.cards.create( 1295 | newCard, 1296 | function (error, body, response) { 1297 | // ... 1298 | }); 1299 | ``` 1300 | ###### With Token 1301 | ```js 1302 | var data = { 1303 | "token_id": "Token ID", 1304 | "device_session_id": "Device session ID" 1305 | }; 1306 | openpay.customers.cards.create( 1307 | data, 1308 | function (error, body, response) { 1309 | // ... 1310 | }); 1311 | ``` 1312 | ###### With Token and customer 1313 | ```js 1314 | var data = { 1315 | "token_id": "Token ID", 1316 | "device_session_id": "Device session ID" 1317 | }; 1318 | var customerId = 'Customer ID'; 1319 | openpay.customers.cards.create( 1320 | customerId, 1321 | data, 1322 | function (error, body, response) { 1323 | // ... 1324 | }); 1325 | ``` 1326 | #### Get a card 1327 | ###### Without customer 1328 | ```js 1329 | var cardId = 'Card ID'; 1330 | openpay.cards.get( 1331 | cardId, 1332 | function (error, body, response) { 1333 | // ... 1334 | }); 1335 | ``` 1336 | ###### With customer 1337 | ```js 1338 | var customerId = 'Customer ID'; 1339 | var cardId = 'Card ID'; 1340 | openpay.customers.cards.get( 1341 | customerId, 1342 | cardId, 1343 | function (error, body, response) { 1344 | // ... 1345 | }); 1346 | ``` 1347 | #### Delete a card 1348 | ###### With customer 1349 | ```js 1350 | var customerId = 'Customer ID'; 1351 | var cardId = 'Card ID'; 1352 | openpay.customers.cards.delete( 1353 | customerId, 1354 | cardId, 1355 | function (error, body, response) { 1356 | // ... 1357 | }); 1358 | ``` 1359 | ###### Without customer 1360 | ```js 1361 | var cardId = 'Card ID'; 1362 | openpay.cards.delete( 1363 | cardId, 1364 | function (error, body, response) { 1365 | // ... 1366 | }); 1367 | ``` 1368 | #### List cards 1369 | ###### With customer 1370 | ```js 1371 | var customerId = 'Customer ID'; 1372 | var searchParams = { 1373 | 'creation': '2021-01-01', // Format yyyy-mm-dd 1374 | 'offset': 10, 1375 | 'limit': 1 1376 | }; 1377 | openpay.customers.cards.list( 1378 | customerId, 1379 | searchParams, 1380 | function (error, body, response) { 1381 | // ... 1382 | }); 1383 | ``` 1384 | ###### Without customer 1385 | ```js 1386 | var searchParams = { 1387 | 'creation': '2021-01-01', // Format yyyy-mm-dd 1388 | 'offset': 10, 1389 | 'limit': 1 1390 | }; 1391 | openpay.cards.list( 1392 | searchParams, 1393 | function (error, body, response) { 1394 | // ... 1395 | }); 1396 | ``` 1397 | Instead of the 'creation' field in the 'searchParams' object, you can use: 1398 | * "creation[gte]" : "2021-10-22" to find cards created after the given date 1399 | * "creation[lte]" : "2021-10-22" to find cards created before the given date 1400 | ### Webhooks 1401 | #### Create a webhook 1402 | ```js 1403 | var webhook = { 1404 | "url": "https://mysite.com/myWebhook", 1405 | "user": "juanito", 1406 | "password": "passjuanito", 1407 | "event_types": [ 1408 | "charge.failed", 1409 | "charge.cancelled", 1410 | "charge.created", 1411 | "chargeback.accepted" 1412 | ] 1413 | } 1414 | openpay.webhooks.create(webhook, function (error, body, response) { 1415 | printLog(response.statusCode, body, error); 1416 | assert.equal(response.statusCode, 201, ''); 1417 | done(); 1418 | }); 1419 | ``` 1420 | The allowed values fot the field *event_types* are: 1421 | * charge.refunded 1422 | * charge.failed 1423 | * charge.cancelled 1424 | * charge.created 1425 | * charge.succeeded 1426 | * charge.rescored.to.decline 1427 | * subscription.charge.failed 1428 | * payout.created 1429 | * payout.succeeded 1430 | * payout.failed 1431 | * transfer.succeeded 1432 | * fee.succeeded 1433 | * fee.refund.succeeded 1434 | * spei.received 1435 | * chargeback.created 1436 | * chargeback.rejected 1437 | * chargeback.accepted 1438 | * order.created 1439 | * order.activated 1440 | * order.payment.received 1441 | * order.completed 1442 | * order.expired 1443 | * order.cancelled 1444 | * order.payment.cancelled 1445 | #### Get a webhook 1446 | ```js 1447 | var webhookId = 'Webhook ID'; 1448 | openpay.webhooks.get(webhookId, 1449 | function (error, body, response) { 1450 | // ... 1451 | }); 1452 | ``` 1453 | #### Delete a webhook 1454 | ```js 1455 | var webhookId = 'Webhook ID'; 1456 | openpay.webhooks.delete( 1457 | webhookId, 1458 | function (error, body, response) { 1459 | // ... 1460 | }); 1461 | ``` 1462 | #### List webhooks 1463 | ```js 1464 | openpay.webhooks.list( 1465 | function (error, body, response) { 1466 | // ... 1467 | }); 1468 | ``` 1469 | ### Tokens 1470 | #### Create a token 1471 | ```js 1472 | const newToken = { 1473 | "card_number": "4111111111111111", 1474 | "holder_name": "Juan Perez Ramirez", 1475 | "expiration_year": "21", 1476 | "expiration_month": "12", 1477 | "cvv2": "110", 1478 | "address": { 1479 | "city": "Lima", 1480 | "country_code": "PE", 1481 | "postal_code": "110511", 1482 | "line1": "Av 5 de Febrero", 1483 | "line2": "Roble 207", 1484 | "line3": "col carrillo", 1485 | "state": "Lima" 1486 | } 1487 | } 1488 | openpay.tokens.create( 1489 | newToken, 1490 | function (error, body, response) { 1491 | // ... 1492 | }); 1493 | ``` 1494 | #### Get a token 1495 | ```js 1496 | var tokenId = 'Token ID'; 1497 | openpay.tokens.get( 1498 | tokenId, 1499 | function (error, body, response) { 1500 | // ... 1501 | }); 1502 | ``` 1503 | -------------------------------------------------------------------------------- /lib/openpay.js: -------------------------------------------------------------------------------- 1 | const urllib = require('urllib'); 2 | const _ = require('underscore'); 3 | var urlUtils = require('./url-utils') 4 | 5 | Openpay.BASE_URL = 'https://api.openpay.mx'; 6 | Openpay.API_VERSION = '/v1/'; 7 | Openpay.SANDBOX_URL = 'https://sandbox-api.openpay.mx'; 8 | Openpay.SANDBOX_API_VERSION = '/v1/'; 9 | 10 | 11 | function Openpay(merchantId, privateKey, countryCode = 'mx', isProductionReady) { 12 | this.merchantId = merchantId; 13 | this.privateKey = privateKey; 14 | this.isSandbox = isProductionReady ? false : true; 15 | this.timeout = 90000; 16 | this.define({ 17 | isSandbox: this.isSandbox, 18 | privateKey: this.privateKey, 19 | merchantId: this.merchantId, 20 | timeout: this.timeout 21 | }); 22 | this.setBaseUrl(countryCode); 23 | 24 | this.setMerchantId = function (merchantId) { 25 | this.merchantId = merchantId; 26 | this._reDefine(); 27 | }; 28 | 29 | this.setPrivateKey = function (privateKey) { 30 | this.privateKey = privateKey; 31 | this._reDefine(); 32 | }; 33 | 34 | this.setProductionReady = function (isProductionReady) { 35 | this.isSandbox = !isProductionReady; 36 | this._reDefine(); 37 | }; 38 | 39 | this.setTimeout = function (timeout) { 40 | this.timeout = timeout; 41 | this._reDefine(); 42 | }; 43 | 44 | this._reDefine = function () { 45 | this.define({ 46 | isSandbox: this.isSandbox, 47 | privateKey: this.privateKey, 48 | merchantId: this.merchantId, 49 | timeout: this.timeout 50 | }); 51 | }; 52 | } 53 | 54 | Openpay.prototype.define = function (baseData) { 55 | this.groups = new Groups(baseData); 56 | this.merchant = new Merchant(baseData); 57 | this.charges = new Charges(baseData); 58 | this.payouts = new Payouts(baseData); 59 | this.fees = new Fees(baseData); 60 | this.plans = new Plans(baseData); 61 | this.cards = new Cards(baseData); 62 | this.customers = new Customers(baseData); 63 | this.webhooks = new Webhooks(baseData); 64 | this.tokens = new Tokens(baseData); 65 | this.checkouts = new Checkouts(baseData); 66 | this.pse = new Pse(baseData); 67 | this.stores = new Stores(baseData); 68 | }; 69 | 70 | Openpay.prototype.setBaseUrl = function (countryCode) { 71 | console.log('setting base url from country') 72 | switch (countryCode) { 73 | case 'pe': 74 | console.log('Country Peru'); 75 | Openpay.BASE_URL = "https://api.openpay.pe"; 76 | Openpay.SANDBOX_URL = "https://sandbox-api.openpay.pe"; 77 | break; 78 | case 'co': 79 | console.log('Country Colombia') 80 | Openpay.BASE_URL = "https://api.openpay.co"; 81 | Openpay.SANDBOX_URL = "https://sandbox-api.openpay.co"; 82 | break 83 | case 'mx': 84 | console.log('Country Mexico'); 85 | console.log('Default value'); 86 | break 87 | default: 88 | console.error('Error country code, setting mx default.'); 89 | } 90 | } 91 | 92 | function Merchant(baseData) { 93 | var baseUrl = baseData.merchantId; 94 | 95 | this.get = function (callback) { 96 | sendRequest(_.extend(baseData, { 97 | apiUrl: baseUrl, 98 | requestData: {method: 'GET'}, 99 | callback: callback 100 | })); 101 | }; 102 | } 103 | 104 | function Charges(baseData) { 105 | var baseUrl = baseData.merchantId + '/charges'; 106 | 107 | this.create = function (data, callback) { 108 | sendRequest(_.extend(baseData, { 109 | apiUrl: baseUrl, 110 | requestData: {method: 'POST', json: data}, 111 | callback: callback 112 | })); 113 | }; 114 | 115 | this.list = function (data, callback) { 116 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 117 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 118 | sendRequest(_.extend(baseData, { 119 | apiUrl: baseUrl + query, 120 | requestData: {method: 'GET'}, 121 | callback: callback 122 | })); 123 | }; 124 | 125 | this.get = function (transactionId, callback) { 126 | sendRequest(_.extend(baseData, { 127 | apiUrl: baseUrl + '/' + transactionId, 128 | requestData: {method: 'GET'}, 129 | callback: callback 130 | })); 131 | }; 132 | 133 | this.capture = function (transactionId, data, callback) { 134 | sendRequest(_.extend(baseData, { 135 | apiUrl: baseUrl + '/' + transactionId + '/capture', 136 | requestData: {method: 'POST', json: data}, 137 | callback: callback 138 | })); 139 | }; 140 | 141 | this.refund = function (transactionId, data, callback) { 142 | sendRequest(_.extend(baseData, { 143 | apiUrl: baseUrl + '/' + transactionId + '/refund', 144 | requestData: {method: 'POST', json: data}, 145 | callback: callback 146 | })); 147 | }; 148 | } 149 | 150 | 151 | function Payouts(baseData) { 152 | var baseUrl = baseData.merchantId + '/payouts'; 153 | 154 | this.create = function (data, callback) { 155 | sendRequest(_.extend(baseData, { 156 | apiUrl: baseUrl, 157 | requestData: {method: 'POST', json: data}, 158 | callback: callback 159 | })); 160 | }; 161 | 162 | this.list = function (data, callback) { 163 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 164 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 165 | sendRequest(_.extend(baseData, { 166 | apiUrl: baseUrl + query, 167 | requestData: {method: 'GET'}, 168 | callback: callback 169 | })); 170 | }; 171 | 172 | this.get = function (transactionId, callback) { 173 | sendRequest(_.extend(baseData, { 174 | apiUrl: baseUrl + '/' + transactionId, 175 | requestData: {method: 'GET'}, 176 | callback: callback 177 | })); 178 | }; 179 | } 180 | 181 | 182 | function Fees(baseData) { 183 | var baseUrl = baseData.merchantId + '/fees'; 184 | 185 | this.create = function (data, callback) { 186 | sendRequest(_.extend(baseData, { 187 | apiUrl: baseUrl, 188 | requestData: {method: 'POST', json: data}, 189 | callback: callback 190 | })); 191 | }; 192 | 193 | this.list = function (data, callback) { 194 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 195 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 196 | sendRequest(_.extend(baseData, { 197 | apiUrl: baseUrl + query, 198 | requestData: {method: 'GET'}, 199 | callback: callback 200 | })); 201 | }; 202 | } 203 | 204 | 205 | function Customers(baseData) { 206 | var baseUrl = baseData.merchantId + '/customers'; 207 | 208 | this.create = function (data, callback) { 209 | sendRequest(_.extend(baseData, { 210 | apiUrl: baseUrl, 211 | requestData: {method: 'POST', json: data}, 212 | callback: callback 213 | })); 214 | }; 215 | 216 | this.list = function (data, callback) { 217 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 218 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 219 | sendRequest(_.extend(baseData, { 220 | apiUrl: baseUrl + query, 221 | requestData: {method: 'GET'}, 222 | callback: callback 223 | })); 224 | }; 225 | 226 | this.get = function (customerId, callback) { 227 | sendRequest(_.extend(baseData, { 228 | apiUrl: baseUrl + '/' + customerId, 229 | requestData: {method: 'GET'}, 230 | callback: callback 231 | })); 232 | }; 233 | 234 | this.update = function (customerId, data, callback) { 235 | sendRequest(_.extend(baseData, { 236 | apiUrl: baseUrl + '/' + customerId, 237 | requestData: {method: 'PUT', json: data}, 238 | callback: callback 239 | })); 240 | }; 241 | 242 | this.delete = function (customerId, callback) { 243 | sendRequest(_.extend(baseData, { 244 | apiUrl: baseUrl + '/' + customerId, 245 | requestData: {method: 'DELETE'}, 246 | callback: callback 247 | })); 248 | }; 249 | 250 | this.charges = { 251 | baseUrl: baseData.merchantId + '/customers/', 252 | 253 | create: function (customerId, data, callback) { 254 | sendRequest(_.extend(baseData, { 255 | apiUrl: this.baseUrl + customerId + '/charges', 256 | requestData: {method: 'POST', json: data}, 257 | callback: callback 258 | })); 259 | }, 260 | 261 | list: function (customerId, data, callback) { 262 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 263 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 264 | sendRequest(_.extend(baseData, { 265 | apiUrl: this.baseUrl + customerId + '/charges' + query, 266 | requestData: {method: 'GET'}, 267 | callback: callback 268 | })); 269 | }, 270 | 271 | get: function (customerId, transactionId, callback) { 272 | sendRequest(_.extend(baseData, { 273 | apiUrl: this.baseUrl + customerId + '/charges/' + transactionId, 274 | requestData: {method: 'GET'}, 275 | callback: callback 276 | })); 277 | }, 278 | 279 | capture: function (customerId, transactionId, data, callback) { 280 | sendRequest(_.extend(baseData, { 281 | apiUrl: this.baseUrl + customerId + '/charges/' + transactionId + '/capture', 282 | requestData: {method: 'POST', json: data}, 283 | callback: callback 284 | })); 285 | }, 286 | 287 | refund: function (customerId, transactionId, data, callback) { 288 | sendRequest(_.extend(baseData, { 289 | apiUrl: this.baseUrl + customerId + '/charges/' + transactionId + '/refund', 290 | requestData: {method: 'POST', json: data}, 291 | callback: callback 292 | })); 293 | } 294 | }; 295 | 296 | this.transfers = { 297 | baseUrl: baseData.merchantId + '/customers/', 298 | 299 | create: function (customerId, data, callback) { 300 | sendRequest(_.extend(baseData, { 301 | apiUrl: this.baseUrl + customerId + '/transfers', 302 | requestData: {method: 'POST', json: data}, 303 | callback: callback 304 | })); 305 | }, 306 | 307 | list: function (customerId, data, callback) { 308 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 309 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 310 | sendRequest(_.extend(baseData, { 311 | apiUrl: this.baseUrl + customerId + '/transfers' + query, 312 | requestData: {method: 'GET'}, 313 | callback: callback 314 | })); 315 | }, 316 | 317 | get: function (customerId, transactionId, callback) { 318 | sendRequest(_.extend(baseData, { 319 | apiUrl: this.baseUrl + customerId + '/transfers/' + transactionId, 320 | requestData: {method: 'GET'}, 321 | callback: callback 322 | })); 323 | } 324 | }; 325 | 326 | this.payouts = { 327 | baseUrl: baseData.merchantId + '/customers/', 328 | 329 | create: function (customerId, data, callback) { 330 | sendRequest(_.extend(baseData, { 331 | apiUrl: this.baseUrl + customerId + '/payouts', 332 | requestData: {method: 'POST', json: data}, 333 | callback: callback 334 | })); 335 | }, 336 | 337 | list: function (customerId, data, callback) { 338 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 339 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 340 | sendRequest(_.extend(baseData, { 341 | apiUrl: this.baseUrl + customerId + '/payouts' + query, 342 | requestData: {method: 'GET'}, 343 | callback: callback 344 | })); 345 | }, 346 | 347 | get: function (customerId, transactionId, callback) { 348 | sendRequest(_.extend(baseData, { 349 | apiUrl: this.baseUrl + customerId + '/payouts/' + transactionId, 350 | requestData: {method: 'GET'}, 351 | callback: callback 352 | })); 353 | } 354 | }; 355 | 356 | this.subscriptions = { 357 | baseUrl: baseData.merchantId + '/customers/', 358 | 359 | create: function (customerId, data, callback) { 360 | sendRequest(_.extend(baseData, { 361 | apiUrl: this.baseUrl + customerId + '/subscriptions', 362 | requestData: {method: 'POST', json: data}, 363 | callback: callback 364 | })); 365 | }, 366 | 367 | list: function (customerId, data, callback) { 368 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 369 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 370 | sendRequest(_.extend(baseData, { 371 | apiUrl: this.baseUrl + customerId + '/subscriptions' + query, 372 | requestData: {method: 'GET'}, 373 | callback: callback 374 | })); 375 | }, 376 | 377 | get: function (customerId, subscriptionId, callback) { 378 | sendRequest(_.extend(baseData, { 379 | apiUrl: this.baseUrl + customerId + '/subscriptions/' + subscriptionId, 380 | requestData: {method: 'GET'}, 381 | callback: callback 382 | })); 383 | }, 384 | 385 | update: function (customerId, subscriptionId, data, callback) { 386 | sendRequest(_.extend(baseData, { 387 | apiUrl: this.baseUrl + customerId + '/subscriptions/' + subscriptionId, 388 | requestData: {method: 'PUT', json: data}, 389 | callback: callback 390 | })); 391 | }, 392 | 393 | delete: function (customerId, subscriptionId, callback) { 394 | sendRequest(_.extend(baseData, { 395 | apiUrl: this.baseUrl + customerId + '/subscriptions/' + subscriptionId, 396 | requestData: {method: 'DELETE'}, 397 | callback: callback 398 | })); 399 | } 400 | }; 401 | 402 | this.cards = { 403 | baseUrl: baseData.merchantId + '/customers/', 404 | 405 | create: function (customerId, data, callback) { 406 | sendRequest(_.extend(baseData, { 407 | apiUrl: this.baseUrl + customerId + '/cards', 408 | requestData: {method: 'POST', json: data}, 409 | callback: callback 410 | })); 411 | }, 412 | 413 | list: function (customerId, data, callback) { 414 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 415 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 416 | sendRequest(_.extend(baseData, { 417 | apiUrl: this.baseUrl + customerId + '/cards' + query, 418 | requestData: {method: 'GET'}, 419 | callback: callback 420 | })); 421 | }, 422 | 423 | get: function (customerId, cardId, callback) { 424 | sendRequest(_.extend(baseData, { 425 | apiUrl: this.baseUrl + customerId + '/cards/' + cardId, 426 | requestData: {method: 'GET'}, 427 | callback: callback 428 | })); 429 | }, 430 | 431 | delete: function (customerId, cardId, callback) { 432 | sendRequest(_.extend(baseData, { 433 | apiUrl: this.baseUrl + customerId + '/cards/' + cardId, 434 | requestData: {method: 'DELETE'}, 435 | callback: callback 436 | })); 437 | }, 438 | update: function (customerId, cardId, data, callback) { 439 | sendRequest(_.extend(baseData, { 440 | apiUrl: this.baseUrl + customerId + '/cards/' + cardId, 441 | requestData: {method: 'PUT', json: data}, 442 | callback: callback 443 | })); 444 | } 445 | }; 446 | 447 | this.bankaccounts = { 448 | baseUrl: baseData.merchantId + '/customers/', 449 | 450 | create: function (customerId, data, callback) { 451 | sendRequest(_.extend(baseData, { 452 | apiUrl: this.baseUrl + customerId + '/bankaccounts', 453 | requestData: {method: 'POST', json: data}, 454 | callback: callback 455 | })); 456 | }, 457 | 458 | list: function (customerId, data, callback) { 459 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 460 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 461 | sendRequest(_.extend(baseData, { 462 | apiUrl: this.baseUrl + customerId + '/bankaccounts' + query, 463 | requestData: {method: 'GET'}, 464 | callback: callback 465 | })); 466 | }, 467 | 468 | get: function (customerId, bankId, callback) { 469 | sendRequest(_.extend(baseData, { 470 | apiUrl: this.baseUrl + customerId + '/bankaccounts/' + bankId, 471 | requestData: {method: 'GET'}, 472 | callback: callback 473 | })); 474 | }, 475 | 476 | delete: function (customerId, bankId, callback) { 477 | sendRequest(_.extend(baseData, { 478 | apiUrl: this.baseUrl + customerId + '/bankaccounts/' + bankId, 479 | requestData: {method: 'DELETE'}, 480 | callback: callback 481 | })); 482 | } 483 | }; 484 | 485 | this.checkouts = { 486 | baseUrl: baseData.merchantId + '/customers/', 487 | 488 | create: function (customerId, data, callback) { 489 | sendRequest(_.extend(baseData, { 490 | apiUrl: this.baseUrl + customerId + '/checkouts', 491 | requestData: {method: 'POST', json: data}, 492 | callback: callback 493 | })); 494 | } 495 | }; 496 | 497 | this.pse = { 498 | baseUrl: baseData.merchantId + '/customers/', 499 | create: function (customerId, data, callback) { 500 | sendRequest(_.extend(baseData, { 501 | apiUrl: this.baseUrl + customerId + '/charges', 502 | requestData: {method: 'POST', json: data}, 503 | callback: callback 504 | })); 505 | } 506 | } 507 | } 508 | 509 | 510 | function Cards(baseData) { 511 | var baseUrl = baseData.merchantId + '/cards'; 512 | 513 | this.create = function (data, callback) { 514 | sendRequest(_.extend(baseData, { 515 | apiUrl: baseUrl, 516 | requestData: {method: 'POST', json: data}, 517 | callback: callback 518 | })); 519 | }; 520 | 521 | this.list = function (data, callback) { 522 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 523 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 524 | sendRequest(_.extend(baseData, { 525 | apiUrl: baseUrl + query, 526 | requestData: {method: 'GET'}, 527 | callback: callback 528 | })); 529 | }; 530 | 531 | this.get = function (cardId, callback) { 532 | sendRequest(_.extend(baseData, { 533 | apiUrl: baseUrl + '/' + cardId, 534 | requestData: {method: 'GET'}, 535 | callback: callback 536 | })); 537 | }; 538 | 539 | this.delete = function (cardId, callback) { 540 | sendRequest(_.extend(baseData, { 541 | apiUrl: baseUrl + '/' + cardId, 542 | requestData: {method: 'DELETE'}, 543 | callback: callback 544 | })); 545 | }; 546 | 547 | this.update = function (cardId, data, callback) { 548 | sendRequest(_.extend(baseData, { 549 | apiUrl: baseUrl + '/' + cardId, 550 | requestData: {method: 'PUT', json: data}, 551 | callback: callback 552 | })); 553 | }; 554 | } 555 | 556 | function Plans(baseData) { 557 | var baseUrl = baseData.merchantId + '/plans'; 558 | 559 | this.create = function (data, callback) { 560 | sendRequest(_.extend(baseData, { 561 | apiUrl: baseUrl, 562 | requestData: {method: 'POST', json: data}, 563 | callback: callback 564 | })); 565 | }; 566 | 567 | this.list = function (data, callback) { 568 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 569 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 570 | sendRequest(_.extend(baseData, { 571 | apiUrl: baseUrl + query, 572 | requestData: {method: 'GET'}, 573 | callback: callback 574 | })); 575 | }; 576 | 577 | this.get = function (planId, callback) { 578 | sendRequest(_.extend(baseData, { 579 | apiUrl: baseUrl + '/' + planId, 580 | requestData: {method: 'GET'}, 581 | callback: callback 582 | })); 583 | }; 584 | 585 | this.update = function (planId, data, callback) { 586 | sendRequest(_.extend(baseData, { 587 | apiUrl: baseUrl + '/' + planId, 588 | requestData: {method: 'PUT', json: data}, 589 | callback: callback 590 | })); 591 | }; 592 | 593 | this.delete = function (planId, callback) { 594 | sendRequest(_.extend(baseData, { 595 | apiUrl: baseUrl + '/' + planId, 596 | requestData: {method: 'DELETE'}, 597 | callback: callback 598 | })); 599 | }; 600 | 601 | this.listSubscriptions = function (planId, data, callback) { 602 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 603 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 604 | sendRequest(_.extend(baseData, { 605 | apiUrl: baseUrl + '/' + planId + '/subscriptions' + query, 606 | requestData: {method: 'GET'}, 607 | callback: callback 608 | })); 609 | }; 610 | } 611 | 612 | 613 | function Webhooks(baseData) { 614 | var baseUrl = baseData.merchantId + '/webhooks'; 615 | 616 | this.create = function (data, callback) { 617 | sendRequest(_.extend(baseData, { 618 | apiUrl: baseUrl, 619 | requestData: {method: 'POST', json: data}, 620 | callback: callback 621 | })); 622 | }; 623 | 624 | this.verify = function (webhook_id, verification_code, callback) { 625 | sendRequest(_.extend(baseData, { 626 | apiUrl: baseUrl + '/' + webhook_id + '/verify' + '/' + verification_code, 627 | requestData: {method: 'POST', json: '{}'}, 628 | callback: callback 629 | })); 630 | }; 631 | 632 | this.get = function (webhook_id, callback) { 633 | sendRequest(_.extend(baseData, { 634 | apiUrl: baseUrl + '/' + webhook_id, 635 | requestData: {method: 'GET'}, 636 | callback: callback 637 | })); 638 | }; 639 | 640 | 641 | this.delete = function (webhook_id, callback) { 642 | sendRequest(_.extend(baseData, { 643 | apiUrl: baseUrl + '/' + webhook_id, 644 | requestData: {method: 'DELETE'}, 645 | callback: callback 646 | })); 647 | }; 648 | 649 | this.list = function (callback) { 650 | sendRequest(_.extend(baseData, { 651 | apiUrl: baseUrl, 652 | requestData: {method: 'GET'}, 653 | callback: callback 654 | })); 655 | }; 656 | 657 | } 658 | 659 | function Tokens(baseData) { 660 | var baseUrl = baseData.merchantId + '/tokens'; 661 | 662 | this.create = function (data, callback) { 663 | sendRequest(_.extend(baseData, { 664 | apiUrl: baseUrl, 665 | requestData: {method: 'POST', json: data}, 666 | callback: callback 667 | })); 668 | }; 669 | 670 | this.get = function (token_id, callback) { 671 | sendRequest(_.extend(baseData, { 672 | apiUrl: baseUrl + '/' + token_id, 673 | requestData: {method: 'GET'}, 674 | callback: callback 675 | })); 676 | }; 677 | 678 | this.list = function (callback) { 679 | sendRequest(_.extend(baseData, { 680 | apiUrl: baseUrl, 681 | requestData: {method: 'GET'}, 682 | callback: callback 683 | })); 684 | }; 685 | 686 | } 687 | 688 | function Checkouts(baseData) { 689 | var baseUrl = baseData.merchantId + '/checkouts'; 690 | 691 | this.create = function (data, callback) { 692 | sendRequest(_.extend(baseData, { 693 | apiUrl: baseUrl, 694 | requestData: {method: 'POST', json: data}, 695 | callback: callback 696 | })); 697 | }; 698 | 699 | this.get = function (checkout_id, callback) { 700 | sendRequest(_.extend(baseData, { 701 | apiUrl: baseUrl + '/' + checkout_id, 702 | requestData: {method: 'GET'}, 703 | callback: callback 704 | })); 705 | }; 706 | 707 | this.list = function (data, callback) { 708 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 709 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 710 | sendRequest(_.extend(baseData, { 711 | apiUrl: baseUrl + query, 712 | requestData: {method: 'GET'}, 713 | callback: callback 714 | })); 715 | }; 716 | 717 | this.update = function (checkoutId, status, data, callback) { 718 | sendRequest(_.extend(baseData, { 719 | apiUrl: baseUrl + '/' + checkoutId + "?status=" + status, 720 | requestData: {method: 'PUT', json: data}, 721 | callback: callback 722 | })); 723 | }; 724 | } 725 | 726 | function Stores(baseData) { 727 | var baseUrl = 'stores'; 728 | 729 | this.list = function (data, callback) { 730 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 731 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 732 | sendStoreRequest(_.extend(baseData, { 733 | apiUrl: baseUrl + query, 734 | requestData: {method: 'GET'}, 735 | callback: callback 736 | })); 737 | }; 738 | } 739 | 740 | function Pse(baseData) { 741 | var baseUrl = baseData.merchantId + '/charges'; 742 | 743 | this.create = function (data, callback) { 744 | sendRequest(_.extend(baseData, { 745 | apiUrl: baseUrl, 746 | requestData: {method: 'POST', json: data}, 747 | callback: callback 748 | })); 749 | }; 750 | } 751 | 752 | function Groups(baseData) { 753 | baseData.groupId = baseData.merchantId; 754 | var baseUrl = 'groups' 755 | this.customers = { 756 | baseUrl: 'groups/' + baseData.merchantId + '/customers', 757 | 758 | create: function (data, callback) { 759 | sendRequest(_.extend(baseData, { 760 | apiUrl: this.baseUrl, 761 | requestData: {method: 'POST', json: data}, 762 | callback: callback 763 | })); 764 | }, 765 | 766 | list: function (data, callback) { 767 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 768 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 769 | sendRequest(_.extend(baseData, { 770 | apiUrl: this.baseUrl + query, 771 | requestData: {method: 'GET'}, 772 | callback: callback 773 | })); 774 | }, 775 | 776 | get: function (customerId, callback) { 777 | sendRequest(_.extend(baseData, { 778 | apiUrl: this.baseUrl + '/' + customerId, 779 | requestData: {method: 'GET'}, 780 | callback: callback 781 | })); 782 | }, 783 | 784 | update: function (customerId, data, callback) { 785 | sendRequest(_.extend(baseData, { 786 | apiUrl: this.baseUrl + '/' + customerId, 787 | requestData: {method: 'PUT', json: data}, 788 | callback: callback 789 | })); 790 | }, 791 | 792 | delete: function (customerId, callback) { 793 | sendRequest(_.extend(baseData, { 794 | apiUrl: this.baseUrl + '/' + customerId, 795 | requestData: {method: 'DELETE'}, 796 | callback: callback 797 | })); 798 | }, 799 | 800 | cards: { 801 | baseUrl: 'groups/' + baseData.merchantId + '/customers/', 802 | 803 | create: function (customerId, data, callback) { 804 | sendRequest(_.extend(baseData, { 805 | apiUrl: this.baseUrl + customerId + '/cards', 806 | requestData: {method: 'POST', json: data}, 807 | callback: callback 808 | })); 809 | }, 810 | 811 | list: function (customerId, data, callback) { 812 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 813 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 814 | sendRequest(_.extend(baseData, { 815 | apiUrl: this.baseUrl + customerId + '/cards' + query, 816 | requestData: {method: 'GET'}, 817 | callback: callback 818 | })); 819 | }, 820 | 821 | get: function (customerId, cardId, callback) { 822 | sendRequest(_.extend(baseData, { 823 | apiUrl: this.baseUrl + customerId + '/cards/' + cardId, 824 | requestData: {method: 'GET'}, 825 | callback: callback 826 | })); 827 | }, 828 | 829 | delete: function (customerId, cardId, callback) { 830 | sendRequest(_.extend(baseData, { 831 | apiUrl: this.baseUrl + customerId + '/cards/' + cardId, 832 | requestData: {method: 'DELETE'}, 833 | callback: callback 834 | })); 835 | } 836 | }, 837 | 838 | charges: { 839 | baseUrl: 'groups/' + baseData.merchantId + '/merchants/', 840 | 841 | create: function (merchantId, customerId, data, callback) { 842 | sendRequest(_.extend(baseData, { 843 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/charges', 844 | requestData: {method: 'POST', json: data}, 845 | callback: callback 846 | })); 847 | }, 848 | 849 | capture: function (merchantId, customerId, transactionId, data, callback) { 850 | sendRequest(_.extend(baseData, { 851 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/charges/' + transactionId + '/capture', 852 | requestData: {method: 'POST', json: data}, 853 | callback: callback 854 | })); 855 | }, 856 | 857 | refund: function (merchantId, customerId, transactionId, data, callback) { 858 | sendRequest(_.extend(baseData, { 859 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/charges/' + transactionId + '/refund', 860 | requestData: {method: 'POST', json: data}, 861 | callback: callback 862 | })); 863 | } 864 | }, 865 | 866 | subscriptions: { 867 | baseUrl: 'groups/' + baseData.merchantId + '/merchants/', 868 | 869 | create: function (merchantId, customerId, data, callback) { 870 | sendRequest(_.extend(baseData, { 871 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/subscriptions', 872 | requestData: {method: 'POST', json: data}, 873 | callback: callback 874 | })); 875 | }, 876 | 877 | list: function (merchantId, customerId, data, callback) { 878 | var query = (data && _.isObject(data) && !_.isArray(data) && !_.isFunction(data) && !_.isEmpty(data)) ? stringifyParams(data) : ''; 879 | var callback = _.isFunction(callback) ? callback : _.isFunction(data) ? data : null; 880 | sendRequest(_.extend(baseData, { 881 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/subscriptions' + query, 882 | requestData: {method: 'GET'}, 883 | callback: callback 884 | })); 885 | }, 886 | 887 | get: function (merchantId, customerId, subscriptionId, callback) { 888 | sendRequest(_.extend(baseData, { 889 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/subscriptions/' + subscriptionId, 890 | requestData: {method: 'GET'}, 891 | callback: callback 892 | })); 893 | }, 894 | 895 | update: function (merchantId, customerId, subscriptionId, data, callback) { 896 | sendRequest(_.extend(baseData, { 897 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/subscriptions/' + subscriptionId, 898 | requestData: {method: 'PUT', json: data}, 899 | callback: callback 900 | })); 901 | }, 902 | 903 | delete: function (merchantId, customerId, subscriptionId, callback) { 904 | sendRequest(_.extend(baseData, { 905 | apiUrl: this.baseUrl + merchantId + '/customers/' + customerId + '/subscriptions/' + subscriptionId, 906 | requestData: {method: 'DELETE'}, 907 | callback: callback 908 | })); 909 | } 910 | } 911 | }; 912 | 913 | this.charges = { 914 | baseUrl: 'groups/' + baseData.merchantId + '/merchants/', 915 | 916 | create: function (merchantId, data, callback) { 917 | sendRequest(_.extend(baseData, { 918 | apiUrl: this.baseUrl + merchantId + '/charges', 919 | requestData: {method: 'POST', json: data}, 920 | callback: callback 921 | })); 922 | }, 923 | 924 | capture: function (merchantId, transactionId, data, callback) { 925 | sendRequest(_.extend(baseData, { 926 | apiUrl: this.baseUrl + merchantId + '/charges/' + transactionId + '/capture', 927 | requestData: {method: 'POST', json: data}, 928 | callback: callback 929 | })); 930 | }, 931 | 932 | refund: function (merchantId, transactionId, data, callback) { 933 | sendRequest(_.extend(baseData, { 934 | apiUrl: this.baseUrl + merchantId + '/charges/' + transactionId + '/refund', 935 | requestData: {method: 'POST', json: data}, 936 | callback: callback 937 | })); 938 | } 939 | }; 940 | 941 | } 942 | 943 | 944 | var stringifyParams = function (params) { 945 | return '?' + _.map(_.pairs(params), function (arr) { 946 | return arr.join('='); 947 | }).join('&'); 948 | } 949 | 950 | var sendRequest = function (data) { 951 | var baseUrl = data.isSandbox ? Openpay.SANDBOX_URL + Openpay.SANDBOX_API_VERSION : Openpay.BASE_URL + Openpay.API_VERSION; 952 | const url = baseUrl + urlUtils.escapeBrackets(data.apiUrl); 953 | const options = { 954 | auth: data.privateKey + ':', 955 | method: data.requestData.method || 'GET', 956 | contentType: 'json', 957 | timeout: data.timeout, 958 | data: data.requestData.json, 959 | dataType: 'json' 960 | }; 961 | 962 | urllib.request(url, options, function (err, body, res) { 963 | var resCode = res ? res.statusCode : null; 964 | var error = resCode && (resCode != 200 && resCode != 201 && resCode != 204) ? body : null; 965 | data.callback(err ? err : error, err || error ? null : body, res); 966 | }); 967 | } 968 | 969 | var sendStoreRequest = function (data) { 970 | var baseUrl = data.isSandbox ? Openpay.SANDBOX_URL + "/" : Openpay.BASE_URL + "/"; 971 | const url = baseUrl + urlUtils.escapeBrackets(data.apiUrl); 972 | const options = { 973 | auth: data.privateKey + ':', 974 | method: data.requestData.method || 'GET', 975 | contentType: 'json', 976 | timeout: data.timeout, 977 | data: data.requestData.json, 978 | dataType: 'json' 979 | }; 980 | 981 | urllib.request(url, options, function (err, body, res) { 982 | var resCode = res ? res.statusCode : null; 983 | var error = resCode && (resCode != 200 && resCode != 201 && resCode != 204) ? body : null; 984 | data.callback(err ? err : error, err || error ? null : body, res); 985 | }); 986 | } 987 | 988 | module.exports = Openpay; 989 | -------------------------------------------------------------------------------- /lib/url-utils.js: -------------------------------------------------------------------------------- 1 | function escapeBrackets(url) { 2 | url = url.replace(new RegExp('\\[', 'g'), encodeURI('[')) 3 | url = url.replace(new RegExp('\]', 'g'), encodeURI(']')) 4 | console.log('3:', url) 5 | return url; 6 | } 7 | 8 | exports.escapeBrackets = escapeBrackets; 9 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "openpay", 3 | "description": "Openpay library", 4 | "version": "2.0.1", 5 | "dependencies": { 6 | "underscore": "1.13.6", 7 | "urllib": "^2.36.1" 8 | }, 9 | "main": "lib/openpay.js", 10 | "devDependencies": { 11 | "mocha": "10.2.0" 12 | }, 13 | "scripts": { 14 | "test": "mocha" 15 | }, 16 | "engines": { 17 | "node": ">=0.6.x" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/bank-accounts.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | var Openpay = require('../lib/openpay'); 4 | /*Sandbox*/ 5 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 6 | openpay.setTimeout(20000); 7 | var enableLogging = true; 8 | var customerId = ''; 9 | var testCreateCustomer = { 10 | "name": "Juan", 11 | "email": "juan@nonexistantdomain.com" 12 | }; 13 | 14 | describe('Get all bank_account with creation filter', function () { 15 | this.timeout(0); 16 | it('should return statusCode 200', function (done) { 17 | var searchParams = { 18 | 'creation[gte]': '2021-01-01', 19 | 'limit':1 20 | }; 21 | openpay.customers.create(testCreateCustomer, function (error, body) { 22 | openpay.customers.bankaccounts.list(body.id, searchParams, function (error, body, response) { 23 | printLog(response.statusCode, body, error); 24 | assert.equal(response.statusCode, 200, 'Status code != 400'); 25 | done(); 26 | }); 27 | }); 28 | }); 29 | }); 30 | 31 | function printLog(code, body, error) { 32 | if (enableLogging) { 33 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 34 | } 35 | if (code >= 300) { 36 | console.log(' '); 37 | console.log(error); 38 | console.log(' '); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/cards.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | describe('Get cards list with creation[lte] filter', function () { 11 | this.timeout(0); 12 | it('should return cards list and 200 status code', function (done) { 13 | var searchParams = { 14 | 'creation[gte]': '2021-01-01', 15 | 'limit': 1 16 | }; 17 | openpay.cards.list(searchParams, function (error, body, response) { 18 | printLog(response.statusCode, body, error); 19 | assert.equal(response.statusCode, 200, ''); 20 | done(); 21 | }); 22 | }) 23 | }) 24 | 25 | function printLog(code, body, error) { 26 | if (enableLogging) { 27 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 28 | } 29 | if (code >= 300) { 30 | console.log(' '); 31 | console.log(error); 32 | console.log(' '); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/charges_test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | describe('Get charges list with amount[lte] filter', function () { 11 | this.timeout(0); 12 | it('should return charges list and 200 status code', function(done) { 13 | var searchParams = { 14 | 'amount[lte]': 10000, 15 | 'creation[gte]': '2021-01-01', 16 | 'limit':1 17 | }; 18 | openpay.charges.list(searchParams, function (error, body, response) { 19 | printLog(response.statusCode, body, error); 20 | assert.equal(response.statusCode, 200, ''); 21 | done(); 22 | }); 23 | }) 24 | }) 25 | 26 | function printLog(code, body, error){ 27 | if(enableLogging){ 28 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 29 | } 30 | if(code>=300){ 31 | console.log(' '); 32 | console.log(error); 33 | console.log(' '); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/colombia/cards.test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | var testCard = { 11 | "card_number": "4111111111111111", 12 | "holder_name": "Juan Perez", 13 | "expiration_year": '28', 14 | "expiration_month": "12", 15 | "cvv2": "111" 16 | }; 17 | 18 | var newToken = { 19 | "card_number": "4111111111111111", 20 | "holder_name": "Juan Perez Ramirez", 21 | "expiration_year": "28", 22 | "expiration_month": "12", 23 | "cvv2": "110", 24 | "address": { 25 | "city": "Bogotá", 26 | "country_code": "CO", 27 | "postal_code": "110511", 28 | "line1": "Av 5 de Febrero", 29 | "line2": "Roble 207", 30 | "line3": "col carrillo", 31 | "state": "Bogota" 32 | } 33 | } 34 | var testCreateCustomer = { 35 | "name": "Juan", 36 | "email": "juan@nonexistantdomain.com", 37 | "requires_account": false 38 | }; 39 | 40 | var cardCreated; 41 | var tokenCreated; 42 | var customerCreated; 43 | 44 | describe('Testing cards', function () { 45 | this.timeout(0); 46 | describe('Testing Merchant cards', function () { 47 | describe('create card', function () { 48 | it('should return statusCode 200||201', function (done) { 49 | openpay.cards.create(testCard, function (error, body, response) { 50 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 51 | done(); 52 | }) 53 | }); 54 | }); 55 | describe('create card with token', function () { 56 | it('should return statusCode 200||201', function (done) { 57 | openpay.tokens.create(newToken, function (error, body, response) { 58 | tokenCreated = body; 59 | openpay.cards.create({ 60 | token_id: tokenCreated.id, 61 | device_session_id: tokenCreated.id 62 | }, function (error, body, response) { 63 | cardCreated = body; 64 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 65 | done(); 66 | }); 67 | }); 68 | }); 69 | }); 70 | 71 | describe('get card', function () { 72 | it('should return statusCode 200||201', function (done) { 73 | openpay.cards.get(cardCreated.id, function (error, body, response) { 74 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 75 | done(); 76 | }) 77 | }); 78 | }); 79 | 80 | describe('List cards', function () { 81 | var searchParams = { 82 | 'limit': 1 83 | }; 84 | it('should return statusCode 200||201', function (done) { 85 | openpay.cards.list(searchParams, function (error, body, response) { 86 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 87 | done(); 88 | }) 89 | }); 90 | }); 91 | 92 | describe('update card', function () { 93 | it('should return statusCode 200||201', function (done) { 94 | openpay.cards.update(cardCreated.id, {holder_name: 'new holder_name'}, function (error, body, response) { 95 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 96 | done(); 97 | }) 98 | }); 99 | }); 100 | 101 | describe('delete card', function () { 102 | it('should return statusCode 200||201', function (done) { 103 | openpay.cards.delete(cardCreated.id, function (error, body, response) { 104 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 105 | done(); 106 | }) 107 | }); 108 | }); 109 | 110 | 111 | }); 112 | 113 | describe("Testing Customer cards", function () { 114 | describe('create customer', function () { 115 | it('', function (done) { 116 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 117 | customerCreated = body; 118 | done(); 119 | }); 120 | }); 121 | 122 | }); 123 | 124 | describe('create card', function () { 125 | it('should return statusCode 200||201', function (done) { 126 | openpay.customers.cards.create(customerCreated.id, testCard, function (error, body, response) { 127 | cardCreated = body; 128 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 129 | done(); 130 | }) 131 | }); 132 | }); 133 | 134 | describe('create card with token', function () { 135 | it('should return statusCode 200||201', function (done) { 136 | openpay.tokens.create(newToken, function (error, body, response) { 137 | tokenCreated = body; 138 | openpay.customers.cards.create(customerCreated.id, { 139 | token_id: tokenCreated.id, 140 | device_session_id: tokenCreated.id 141 | }, function (error, body, response) { 142 | cardCreated = body; 143 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 144 | done(); 145 | }); 146 | }); 147 | }); 148 | }); 149 | 150 | describe('get card', function () { 151 | it('should return statusCode 200||201', function (done) { 152 | openpay.customers.cards.get(customerCreated.id, cardCreated.id, function (error, body, response) { 153 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 154 | done(); 155 | }) 156 | }); 157 | }); 158 | 159 | describe('List cards', function () { 160 | var searchParams = { 161 | 'limit': 1 162 | }; 163 | it('should return statusCode 200||201', function (done) { 164 | openpay.customers.cards.list(customerCreated.id, searchParams, function (error, body, response) { 165 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 166 | done(); 167 | }) 168 | }); 169 | }); 170 | 171 | describe('update card', function () { 172 | it('should return statusCode 200||201', function (done) { 173 | openpay.customers.cards.update(customerCreated.id, cardCreated.id, {holder_name: 'new holder_name'}, function (error, body, response) { 174 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 175 | done(); 176 | }) 177 | }); 178 | }); 179 | 180 | describe('delete card', function () { 181 | it('should return statusCode 200||201', function (done) { 182 | openpay.customers.cards.delete(customerCreated.id, cardCreated.id, function (error, body, response) { 183 | assert.equal(response.statusCode === 200 || response.statusCode === 204, true, ''); 184 | done(); 185 | }) 186 | }); 187 | }); 188 | 189 | describe('delete customer', function () { 190 | it('delete customer', function (done) { 191 | openpay.customers.delete(customerCreated.id, function (error, body, response) { 192 | assert.equal(response.statusCode === 200 || response.statusCode === 204, true, ''); 193 | done(); 194 | }); 195 | }); 196 | 197 | }); 198 | }) 199 | 200 | }) 201 | 202 | function printLog(code, body, error) { 203 | if (enableLogging) { 204 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 205 | } 206 | if (code >= 300) { 207 | console.log(' '); 208 | console.log(error); 209 | console.log(' '); 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /test/colombia/charges_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | 11 | var testCreateCustomer = { 12 | "name": "Juan", 13 | "email": "juan@nonexistantdomain.com", 14 | "requires_account": false 15 | }; 16 | 17 | var newCard = { 18 | "holder_name": "DinnersClub", 19 | "card_number": 4111111111111111, 20 | "cvv2": "651", 21 | "expiration_month": 11, 22 | "expiration_year": 28 23 | } 24 | 25 | var chargeCreated; 26 | var cardCreated; 27 | var testCard = { 28 | "card_number": "4111111111111111", 29 | "holder_name": "Juan Perez", 30 | "expiration_year": 28, 31 | "expiration_month": "12", 32 | "cvv2": "111" 33 | }; 34 | 35 | var newCharge = { 36 | "source_id": "kdx205scoizh93upqbte", 37 | "method": "card", 38 | "amount": 200, 39 | "currency": "COP", 40 | "iva": "10", 41 | "description": "Cargo inicial a mi cuenta", 42 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f", 43 | "customer": { 44 | "name": "Cliente Colombia", 45 | "last_name": "Vazquez Juarez", 46 | "phone_number": "4448936475", 47 | "email": "juan.vazquez@empresa.co" 48 | } 49 | } 50 | 51 | var testCreateCharge = { 52 | "method": "card", 53 | "source_id": "", 54 | "amount": 20, 55 | "description": "Test Charge", 56 | "currency": "COP", 57 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 58 | 59 | } 60 | 61 | describe('Testing charges', function () { 62 | this.timeout(0); 63 | describe('Merchant charges', function () { 64 | describe('Create charge with existing card', function () { 65 | it('should return statusCode 200||201', function (done) { 66 | openpay.cards.create(newCard, function (error, body, response) { 67 | cardCreated = body; 68 | newCharge.source_id = body.id; 69 | openpay.charges.create(newCharge, function (error, body, response) { 70 | chargeCreated = body; 71 | printLog(response.statusCode, body, error); 72 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, 'Status code == 200'); 73 | done(); 74 | }); 75 | }); 76 | 77 | 78 | }); 79 | }); 80 | describe('Refund merchant charge', function () { 81 | it('should return statusCode 200', function (done) { 82 | openpay.charges.create(newCharge, function (error, body, response) { 83 | openpay.charges.refund(body.id, { 84 | description: "testing refound", 85 | amount: 200 86 | }, function (error, body, response) { 87 | printLog(response.statusCode, body, error); 88 | assert.equal(response.statusCode, 200, ''); 89 | done(); 90 | }); 91 | }); 92 | 93 | }); 94 | }); 95 | describe('Create store charge', function () { 96 | it('should return statusCode 200||201', function (done) { 97 | newCharge.method = "store"; 98 | newCharge.source_id = null; 99 | openpay.charges.create(newCharge, function (error, body, response) { 100 | chargeCreated = newCharge; 101 | assert.equal(response.statusCode, 200, ''); 102 | done(); 103 | }); 104 | }) 105 | }); 106 | 107 | describe('Get charge', function () { 108 | it('should return statusCode 200||201', function (done) { 109 | openpay.charges.get(chargeCreated.id, function (error, body, response) { 110 | assert.equal(response.statusCode, 200, ''); 111 | done(); 112 | }) 113 | }) 114 | }); 115 | 116 | describe('Get All charges', function () { 117 | it('should return statusCode 200||201', function (done) { 118 | openpay.charges.list({}, function (error, body, response) { 119 | assert.equal(response.statusCode, 200, ''); 120 | done(); 121 | }) 122 | }) 123 | }); 124 | }); 125 | 126 | describe('customer charges', function () { 127 | var customer; 128 | var cardCreated; 129 | var chargeCreated; 130 | describe('Create customer charge with existing card', function () { 131 | it('should return statusCode 200||201', function (done) { 132 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 133 | customer = body; 134 | openpay.customers.cards.create(customer.id, testCard, function (error, body, response) { 135 | testCreateCharge.source_id = body.id; 136 | cardCreated = body; 137 | openpay.customers.charges.create(customer.id, testCreateCharge, function (error, body, response) { 138 | chargeCreated = body; 139 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 140 | done(); 141 | }) 142 | }); 143 | 144 | }); 145 | }); 146 | }); 147 | describe('Refund customer charge', function () { 148 | it('should return statusCode 200||201', function (done) { 149 | openpay.customers.charges.refund(customer.id, chargeCreated.id, { 150 | description: "testing refound", 151 | amount: 20 152 | }, function (error, body, response) { 153 | printLog(response.statusCode, body, error); 154 | assert.equal(response.statusCode, 200, ''); 155 | done(); 156 | }); 157 | }) 158 | }); 159 | 160 | describe('Create store charge', function () { 161 | it('should return statusCode 200||201', function (done) { 162 | newCharge.method = "store"; 163 | newCharge.source_id = null; 164 | newCharge.customer = null; 165 | openpay.customers.charges.create(customer.id, newCharge, function (error, body, response) { 166 | chargeCreated = body; 167 | assert.equal(response.statusCode, 200, ''); 168 | done(); 169 | }); 170 | }) 171 | }); 172 | 173 | describe('Get charge', function () { 174 | it('should return statusCode 200||201', function (done) { 175 | openpay.customers.charges.get(customer.id, chargeCreated.id, function (error, body, response) { 176 | assert.equal(response.statusCode, 200, ''); 177 | done(); 178 | }) 179 | }) 180 | }); 181 | describe('Get All charges', function () { 182 | it('should return statusCode 200||201', function (done) { 183 | openpay.customers.charges.list(customer.id, {}, function (error, body, response) { 184 | assert.equal(response.statusCode, 200, ''); 185 | done(); 186 | }) 187 | }) 188 | }); 189 | }) 190 | 191 | 192 | }); 193 | 194 | function printLog(code, body, error) { 195 | if (enableLogging) { 196 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 197 | } 198 | if (code >= 300) { 199 | console.log(' '); 200 | console.log(error); 201 | console.log(' '); 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /test/colombia/clients_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(10000); 8 | var enableLogging = true; 9 | 10 | var testCreateCustomer = { 11 | "name": "Juan", 12 | "email": "juan@nonexistantdomain.com", 13 | "requires_account": false 14 | }; 15 | 16 | describe('Customer testing', function () { 17 | var customer; 18 | describe('Create customer', function () { 19 | it('should return statusCode 200||201', function (done) { 20 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 21 | customer = body; 22 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 23 | done(); 24 | }); 25 | }); 26 | }); 27 | 28 | describe('Update customer', function () { 29 | it('should return statusCode 200||201', function (done) { 30 | testCreateCustomer.name = "Client update"; 31 | openpay.customers.update(customer.id, testCreateCustomer, function (error, body, response) { 32 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 33 | done(); 34 | }) 35 | }); 36 | }); 37 | 38 | describe('Get customer', function () { 39 | it('should return statusCode 200||201', function (done) { 40 | openpay.customers.get(customer.id, function (error, body, response) { 41 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 42 | done(); 43 | }); 44 | }); 45 | }); 46 | 47 | describe('Customer List', function () { 48 | it('should return statusCode 200||201', function (done) { 49 | var searchParams = { 50 | 'limit': 10 51 | }; 52 | openpay.customers.list(searchParams, function (error, body, response) { 53 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 54 | done(); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('Delete customer', function () { 60 | it('should return statusCode 200||201', function (done) { 61 | openpay.customers.delete(customer.id, function (error, body, response) { 62 | assert.equal(response.statusCode === 200 || response.statusCode === 201 || response.statusCode === 204, true, ''); 63 | done(); 64 | }); 65 | }); 66 | }); 67 | 68 | }) 69 | 70 | function printLog(code, body, error) { 71 | if (enableLogging) { 72 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 73 | } 74 | if (code >= 300) { 75 | console.log(' '); 76 | console.log(error); 77 | console.log(' '); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /test/colombia/plans.test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(20000); 8 | var enableLogging = true; 9 | 10 | var newPlan = { 11 | "amount": 150, 12 | "status_after_retry": "cancelled", 13 | "retry_times": 2, 14 | "name": "Curso de ingles", 15 | "repeat_unit": "month", 16 | "trial_days": "30", 17 | "repeat_every": "1" 18 | } 19 | describe('Plans testing', function () { 20 | var plan; 21 | describe('Create plan', function () { 22 | it('should return statusCode 200||201', function (done) { 23 | openpay.plans.create(newPlan, function (error, body, response) { 24 | plan = body; 25 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 26 | done(); 27 | }); 28 | }); 29 | }); 30 | 31 | describe('Update plan', function () { 32 | it('should return statusCode 200||201', function (done) { 33 | const updatePlan = {name:"plan updated"} 34 | openpay.plans.update(plan.id, updatePlan, function (error, body, response) { 35 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 36 | done(); 37 | }) 38 | }); 39 | }); 40 | 41 | describe('Get plan', function () { 42 | it('should return statusCode 200||201', function (done) { 43 | openpay.plans.get(plan.id, function (error, body, response) { 44 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 45 | done(); 46 | }); 47 | }); 48 | }); 49 | 50 | describe('Plan List', function () { 51 | it('should return statusCode 200||201', function (done) { 52 | var searchParams = { 53 | 'limit': 10 54 | }; 55 | openpay.plans.list(searchParams, function (error, body, response) { 56 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 57 | done(); 58 | }); 59 | }); 60 | }); 61 | 62 | describe('Delete plan', function () { 63 | it('should return statusCode 200||201', function (done) { 64 | openpay.plans.delete(plan.id, function (error, body, response) { 65 | assert.equal(response.statusCode === 200 || response.statusCode === 201 || response.statusCode === 204, true, ''); 66 | done(); 67 | }); 68 | }); 69 | }); 70 | 71 | }) 72 | 73 | function printLog(code, body, error) { 74 | if (enableLogging) { 75 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 76 | } 77 | if (code >= 300) { 78 | console.log(' '); 79 | console.log(error); 80 | console.log(' '); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /test/colombia/pse_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | var newPse = { 11 | "method": "bank_account", 12 | "amount": 10000, 13 | "currency": "COP", 14 | "description": "Cargo inicial a mi cuenta", 15 | "iva": "1900", 16 | "redirect_url": "/", 17 | "customer": { 18 | "name": "Cliente Colombia", 19 | "last_name": "Vazquez Juarez", 20 | "email": "juan.vazquez@empresa.co", 21 | "phone_number": "4448936475", 22 | "requires_account": false, 23 | "customer_address": { 24 | "department": "Medellín", 25 | "city": "Antioquía", 26 | "additional": "Avenida 7m bis #174-25 Apartamento 637" 27 | } 28 | } 29 | } 30 | 31 | var newPseWithCustomer = { 32 | "method": "bank_account", 33 | "amount": 10000, 34 | "currency": "COP", 35 | "description": "Cargo inicial a mi cuenta", 36 | "iva": "1900", 37 | "redirect_url": "/" 38 | } 39 | 40 | var testCreateCustomer = { 41 | "name": "Juan", 42 | "email": "juan@nonexistantdomain.com", 43 | "requires_account": false 44 | }; 45 | 46 | 47 | describe('Testing pse', function () { 48 | this.timeout(0); 49 | 50 | 51 | describe('create pse with existing client', function () { 52 | it('should return statusCode 200||201', function (done) { 53 | openpay.pse.create(newPse, function (error, body, response) { 54 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 55 | done(); 56 | }); 57 | }); 58 | }); 59 | 60 | describe('create pse with new client', function () { 61 | var customerCreated; 62 | it('create customer', function (done) { 63 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 64 | customerCreated = body; 65 | done(); 66 | }); 67 | }); 68 | 69 | it('should return statusCode 200||201', function (done) { 70 | openpay.customers.pse.create(customerCreated.id, newPseWithCustomer, function (error, body, response) { 71 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 72 | done(); 73 | }) 74 | }); 75 | 76 | }); 77 | }) 78 | 79 | function printLog(code, body, error) { 80 | if (enableLogging) { 81 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 82 | } 83 | if (code >= 300) { 84 | console.log(' '); 85 | console.log(error); 86 | console.log(' '); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /test/colombia/stores_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('', '', 'co', false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | const location = { 11 | "latitud": 4.65589142889691, 12 | "longitud": -74.11335673251888, 13 | "kilometers": 10, 14 | "amount": 1 15 | } 16 | 17 | describe('List stores', function () { 18 | it('List stores by location', function (done) { 19 | openpay.stores.list(location, function (error, body, response) { 20 | printLog(response.statusCode, body, error); 21 | 22 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 23 | done(); 24 | }); 25 | }); 26 | }); 27 | 28 | function printLog(code, body, error) { 29 | if (enableLogging) { 30 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 31 | } 32 | if (code >= 300) { 33 | console.log(' '); 34 | console.log(error); 35 | console.log(' '); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /test/colombia/subscriptions.test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | var Openpay = require('../../lib/openpay'); 4 | /*Sandbox*/ 5 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 6 | openpay.setTimeout(30000); 7 | var enableLogging = true; 8 | 9 | 10 | var newPlan = { 11 | "amount": 150, 12 | "status_after_retry": "cancelled", 13 | "retry_times": 2, 14 | "name": "Curso de ingles", 15 | "repeat_unit": "month", 16 | "trial_days": "30", 17 | "repeat_every": "1" 18 | } 19 | 20 | var subscription = { 21 | "card": { 22 | "card_number": "4111111111111111", 23 | "holder_name": "Juan Perez Ramirez", 24 | "expiration_year": "20", 25 | "expiration_month": "12", 26 | "cvv2": "110", 27 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 28 | }, 29 | "plan_id": "pbi4kb8hpb64x0uud2eb" 30 | } 31 | 32 | var testCreateCustomer = { 33 | "name": "Juan", 34 | "email": "juan@nonexistantdomain.com", 35 | "requires_account": false 36 | }; 37 | 38 | describe('Plans testing', function () { 39 | var plan; 40 | var customer; 41 | var subscription; 42 | 43 | describe('Create plan', function () { 44 | it('should return statusCode 200||201', function (done) { 45 | openpay.plans.create(newPlan, function (error, body, response) { 46 | plan = body; 47 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 48 | done(); 49 | }); 50 | }); 51 | }); 52 | 53 | describe('Create customer', function () { 54 | it('create customer for test', function (done) { 55 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 56 | customer = body; 57 | done(); 58 | }); 59 | }); 60 | }); 61 | 62 | 63 | describe('Create subscription', function () { 64 | subscription.plan_id = plan?.id; 65 | it('should return statusCode 200||201', function (done) { 66 | openpay.customers.subscriptions.create(customer.id, subscription, function (error, body, response) { 67 | subscription = body; 68 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 69 | done(); 70 | }); 71 | }); 72 | }); 73 | 74 | describe('Update subscription', function () { 75 | it('should return statusCode 200||201', function (done) { 76 | const updateSubscription = {trial_end_date: "2021-12-12"} 77 | openpay.customers.subscriptions.update(subscription.id, updateSubscription, function (error, body, response) { 78 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 79 | done(); 80 | }) 81 | }); 82 | }); 83 | 84 | describe('Get subscription', function () { 85 | it('should return statusCode 200||201', function (done) { 86 | openpay.customers.subscriptions.get(customer.id, subscription.id, function (error, body, response) { 87 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 88 | done(); 89 | }); 90 | }); 91 | }); 92 | 93 | 94 | describe('subscription List', function () { 95 | it('should return statusCode 200||201', function (done) { 96 | var searchParams = { 97 | 'limit': 10 98 | }; 99 | openpay.customers.subscriptions.list(searchParams, function (error, body, response) { 100 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 101 | done(); 102 | }); 103 | }); 104 | }); 105 | 106 | describe('Cancell subscription', function () { 107 | it('should return statusCode 200||201', function (done) { 108 | openpay.customers.subscriptions.delete(subscription.id, function (error, body, response) { 109 | assert.equal(response.statusCode === 200 || response.statusCode === 201 || response.statusCode === 204, true, ''); 110 | done(); 111 | }); 112 | }); 113 | 114 | }); 115 | 116 | }); 117 | 118 | function printLog(code, body, error) { 119 | if (enableLogging) { 120 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 121 | } 122 | if (code >= 300) { 123 | console.log(' '); 124 | console.log(error); 125 | console.log(' '); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /test/colombia/tokens_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(20000); 8 | var enableLogging = true; 9 | 10 | const tokenNew = { 11 | "card_number": "4111111111111111", 12 | "holder_name": "Juan Perez Ramirez", 13 | "expiration_year": "29", 14 | "expiration_month": "12", 15 | "cvv2": "110", 16 | "address": { 17 | "city": "Bogotá", 18 | "country_code": "CO", 19 | "postal_code": "110511", 20 | "line1": "Av 5 de Febrero", 21 | "line2": "Roble 207", 22 | "line3": "col carrillo", 23 | "state": "Bogota" 24 | } 25 | } 26 | describe('Token testing', function () { 27 | var token; 28 | describe('Create Token', function () { 29 | it('should return statusCode 200||201', function (done) { 30 | openpay.tokens.create(tokenNew, function (error, body, response) { 31 | token = body; 32 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 33 | done(); 34 | }); 35 | }); 36 | }); 37 | describe('Get Token', function () { 38 | it('should return statusCode 200||201', function (done) { 39 | openpay.tokens.get(token.id, function (error, body, response) { 40 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 41 | done(); 42 | }); 43 | }); 44 | }); 45 | }) 46 | 47 | function printLog(code, body, error) { 48 | if (enableLogging) { 49 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 50 | } 51 | if (code >= 300) { 52 | console.log(' '); 53 | console.log(error); 54 | console.log(' '); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test/colombia/webhooks_test.colombia.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('mwf7x79goz7afkdbuyqd', 'sk_94a89308b4d7469cbda762c4b392152a', 'co', false); 7 | openpay.setTimeout(20000); 8 | var enableLogging = true; 9 | 10 | const newWebhook = { 11 | "url": "https://webhook.site/ab854ec2-2ac1-423b-9c0b-8502fd056cc9", 12 | "user": "juanito", 13 | "password": "passjuanito", 14 | "event_types": [ 15 | "charge.refunded", 16 | "charge.failed", 17 | "charge.cancelled", 18 | "charge.created", 19 | "chargeback.accepted" 20 | ] 21 | } 22 | describe('Webhook testing', function () { 23 | var webhook; 24 | describe('Create webhook', function () { 25 | it('should return statusCode 200||201', function (done) { 26 | openpay.webhooks.create(newWebhook, function (error, body, response) { 27 | webhook = body; 28 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 29 | done(); 30 | }); 31 | }); 32 | }); 33 | 34 | 35 | describe('Get webhook', function () { 36 | it('should return statusCode 200||201', function (done) { 37 | openpay.webhooks.get(webhook.id, function (error, body, response) { 38 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 39 | done(); 40 | }); 41 | }); 42 | }); 43 | 44 | describe('Webhook List', function () { 45 | it('should return statusCode 200||201', function (done) { 46 | openpay.webhooks.list(function (error, body, response) { 47 | assert.equal(response.statusCode === 200 || response.statusCode === 201, true, ''); 48 | done(); 49 | }); 50 | }); 51 | }); 52 | 53 | describe('Delete webhook', function () { 54 | it('should return statusCode 200||201', function (done) { 55 | openpay.webhooks.delete(webhook.id, function (error, body, response) { 56 | assert.equal(response.statusCode === 200 || response.statusCode === 201 || response.statusCode === 204, true, ''); 57 | done(); 58 | }); 59 | }); 60 | }); 61 | 62 | }) 63 | 64 | function printLog(code, body, error) { 65 | if (enableLogging) { 66 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 67 | } 68 | if (code >= 300) { 69 | console.log(' '); 70 | console.log(error); 71 | console.log(' '); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /test/customers_test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('gdntnaxvkcdviesgaxem', 'sk_b97e606487d34b44ab66e03d5bd14747'); 7 | openpay.setTimeout(10000); 8 | var enableLogging = true; 9 | 10 | describe('Get customers list with creation[gte] filter', function() { 11 | this.timeout(0); 12 | it('should return customer list and 200 status code', function(done) { 13 | var searchParams = { 14 | 'creation[gte]': '2021-01-01', 15 | 'limit':1 16 | }; 17 | openpay.groups.customers.list(searchParams, function (error, body, response) { 18 | printLog(response.statusCode, body, error); 19 | assert.equal(response.statusCode, 200, ''); 20 | done(); 21 | }); 22 | }) 23 | }) 24 | 25 | function printLog(code, body, error){ 26 | if(enableLogging){ 27 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 28 | } 29 | if(code>=300){ 30 | console.log(' '); 31 | console.log(error); 32 | console.log(' '); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/fees.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 7 | openpay.setTimeout(2000); 8 | var enableLogging = true; 9 | 10 | describe('Get fees list with creation[lte] filter', function() { 11 | this.timeout(0); 12 | it('should return fees list and 200 status code', function(done) { 13 | var searchParams = { 14 | 'creation[gte]': '2021-01-01', 15 | 'limit':1 16 | }; 17 | openpay.fees.list(searchParams, function (error, body, response) { 18 | printLog(response.statusCode, body, error); 19 | assert.equal(response.statusCode, 200, ''); 20 | done(); 21 | }); 22 | }) 23 | }) 24 | 25 | function printLog(code, body, error){ 26 | if(enableLogging){ 27 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 28 | } 29 | if(code>=300){ 30 | console.log(' '); 31 | console.log(error); 32 | console.log(' '); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/group_test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | // var request = require('request'); 4 | var urllib = require('urllib'); 5 | 6 | var Openpay = require('../lib/openpay'); 7 | /*Sandbox*/ 8 | var openpay = new Openpay('gdntnaxvkcdviesgaxem', 'sk_b97e606487d34b44ab66e03d5bd14747'); 9 | openpay.setTimeout(10000); 10 | 11 | var enableLogging = true; 12 | var testCreateCharges = true; 13 | 14 | // Defining a valid expiration year for cards, adding 5 years to the current one 15 | var validExpirationYear = (new Date().getFullYear() + 5).toString().substr(2, 2); 16 | 17 | describe('Testing group API', function(){ 18 | this.timeout(0); 19 | 20 | var testCreateCustomer = { 21 | "name":"Juan", 22 | "email":"juan@nonexistantdomain.com", 23 | "requires_account":false 24 | }; 25 | var testUpdateCustomer = { 26 | "name":"Juan", 27 | "email":"juan@nonexistantdomain.com", 28 | "phone_number":"123456789" 29 | }; 30 | 31 | var newlyCreatedCustomerId = ''; 32 | describe('Testing customers', function(){ 33 | describe('Create customer', function(){ 34 | it('should return statusCode 200||201', function (done){ 35 | openpay.groups.customers.create(testCreateCustomer, function (error, body, response){ 36 | printLog(response.statusCode, body, error); 37 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 38 | newlyCreatedCustomerId = body.id; 39 | done(); 40 | }); 41 | }); 42 | }); 43 | describe('Get all customers without constraints', function(){ 44 | it('should return statusCode 200', function (done){ 45 | openpay.groups.customers.list({}, function (error, body, response){ 46 | printLog(response.statusCode, body, error); 47 | assert.equal(response.statusCode, 200, ''); 48 | done(); 49 | }); 50 | }); 51 | }); 52 | describe('Get customer', function(){ 53 | it('should return statusCode 200', function (done){ 54 | openpay.groups.customers.get(newlyCreatedCustomerId, function (error, body, response){ 55 | printLog(response.statusCode, body, error); 56 | assert.equal(response.statusCode, 200, ''); 57 | done(); 58 | }); 59 | }); 60 | }); 61 | describe('Update customer', function(){ 62 | it('should return statusCode 200', function (done){ 63 | openpay.groups.customers.update(newlyCreatedCustomerId, testUpdateCustomer, function (error, body, response){ 64 | printLog(response.statusCode, body, error); 65 | assert.equal(response.statusCode, 200, ''); 66 | done(); 67 | }); 68 | }); 69 | }); 70 | describe('Get all customers with constraints', function(){ 71 | it('should return statusCode 200', function (done){ 72 | openpay.groups.customers.list({'creation':'2013-12-10'}, function (error, body, response){ 73 | printLog(response.statusCode, body, error); 74 | assert.equal(response.statusCode, 200, ''); 75 | done(); 76 | }); 77 | }); 78 | }); 79 | }); 80 | 81 | var testCard ={ 82 | "card_number":"4111111111111111", 83 | "holder_name":"Juan Perez", 84 | "expiration_year": validExpirationYear, 85 | "expiration_month":"12", 86 | "cvv2":"111" 87 | }; 88 | var newlyCreatedCardId = ''; 89 | var newlyCreatedCustomerCardId = ''; 90 | 91 | describe('Testing cards API', function(){ 92 | 93 | describe('Add customer card', function(){ 94 | it('should return statusCode 200||201', function (done){ 95 | openpay.groups.customers.cards.create(newlyCreatedCustomerId, testCard, function (error, body, response){ 96 | printLog(response.statusCode, body, error); 97 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 98 | newlyCreatedCustomerCardId = body.id; 99 | newlyCreatedCardId = body.id; 100 | done(); 101 | }); 102 | }); 103 | }); 104 | describe('Get all customer cards without constraints', function(){ 105 | it('should return statusCode 200', function (done){ 106 | openpay.groups.customers.cards.list(newlyCreatedCustomerId, {}, function (error, body, response){ 107 | printLog(response.statusCode, body, error); 108 | assert.equal(response.statusCode, 200, ''); 109 | done(); 110 | }); 111 | }); 112 | }); 113 | describe('Get customer card', function(){ 114 | it('should return statusCode 200', function (done){ 115 | openpay.groups.customers.cards.get(newlyCreatedCustomerId, newlyCreatedCustomerCardId, function (error, body, response){ 116 | printLog(response.statusCode, body, error); 117 | assert.equal(response.statusCode, 200, ''); 118 | done(); 119 | }); 120 | }); 121 | }); 122 | }); 123 | 124 | //var testGetCharge = 'tlogyahn68d2qurjqhqt'; 125 | var testExistingCardCharge = { 126 | "source_id" : '', 127 | "method" : "card", 128 | "amount" : 50, 129 | "description" : "Test existing card charge" 130 | }; 131 | var testCreateCharge = { 132 | "method": "card", 133 | "card": { 134 | "card_number": "4111111111111111", 135 | "holder_name": "Aa Bb", 136 | "expiration_year": validExpirationYear, 137 | "expiration_month": "12", 138 | "cvv2": "110", 139 | }, 140 | "amount" : 20, 141 | "description" : "Test Charge" 142 | }; 143 | var testCreateChargeWithoutCapture = { 144 | "method": "card", 145 | "card": { 146 | "card_number": "4111111111111111", 147 | "holder_name": "Aa Bb", 148 | "expiration_year": validExpirationYear, 149 | "expiration_month": "12", 150 | "cvv2": "110", 151 | }, 152 | "amount" : 20, 153 | "description" : "Test Charge", 154 | "capture" : false 155 | }; 156 | var testCreateBankAccountCharge = { 157 | "method" : "bank_account", 158 | "amount" : 50, 159 | "description" : "Test bank account charge" 160 | }; 161 | var testCreateStoreCharge = { 162 | "method" : "store", 163 | "amount" : 60.01, 164 | "description" : "Test store charge" 165 | }; 166 | var testRefundData = {"description":"Testing refund"}; 167 | 168 | var merchantId = 'm1qp3av1ymcfufkuuoah'; 169 | 170 | describe('Testing charges', function(){ 171 | if(testCreateCharges){ 172 | describe('Create charge with existing card', function(){ 173 | it('should return statusCode 200||201', function (done){ 174 | testExistingCardCharge.source_id = newlyCreatedCardId; 175 | //console.log(testExistingCardCharge); 176 | openpay.groups.charges.create(merchantId, testExistingCardCharge, function (error, body, response){ 177 | printLog(response.statusCode, body, error); 178 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 179 | done(); 180 | }); 181 | }); 182 | }); 183 | var newlyCreatedTransactionId = ''; 184 | describe('Create charge with new card', function(){ 185 | it('should return statusCode 200||201', function (done){ 186 | openpay.groups.charges.create(merchantId, testCreateCharge, function (error, body, response){ 187 | printLog(response.statusCode, body, error); 188 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 189 | newlyCreatedTransactionId = body.id; 190 | done(); 191 | }); 192 | }); 193 | }); 194 | describe('Refund merchant charge', function(){ 195 | it('should return statusCode 200', function (done){ 196 | openpay.groups.charges.refund(merchantId, newlyCreatedTransactionId, testRefundData, function (error, body, response){ 197 | printLog(response.statusCode, body, error); 198 | assert.equal(response.statusCode, 200, ''); 199 | done(); 200 | }); 201 | }); 202 | }); 203 | describe('Create charge without capture', function(){ 204 | it('should return statusCode 200||201', function (done){ 205 | openpay.groups.charges.create(merchantId, testCreateChargeWithoutCapture, function (error, body, response){ 206 | printLog(response.statusCode, body, error); 207 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 208 | newlyCreatedTransactionId = body.id; 209 | done(); 210 | }); 211 | }); 212 | }); 213 | describe('Capture charge', function(){ 214 | it('should return statusCode 200||201', function (done){ 215 | openpay.groups.charges.capture(merchantId, newlyCreatedTransactionId, null, function (error, body, response){ 216 | printLog(response.statusCode, body, error); 217 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 218 | done(); 219 | }); 220 | }); 221 | }); 222 | } 223 | 224 | describe('Create customer charge with existing card', function(){ 225 | it('should return statusCode 200||201', function (done){ 226 | testExistingCardCharge.source_id = newlyCreatedCustomerCardId; //fails if use merchant card 227 | //console.log(testExistingCardCharge); 228 | openpay.groups.customers.charges.create(merchantId, newlyCreatedCustomerId, testExistingCardCharge, function (error, body, response){ 229 | printLog(response.statusCode, body, error); 230 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 231 | done(); 232 | }); 233 | }); 234 | }); 235 | var newlyCreatedCustomerTransactionId = ''; 236 | describe('Create customer charge with new card', function(){ 237 | it('should return statusCode 200||201', function (done){ 238 | openpay.groups.customers.charges.create(merchantId, newlyCreatedCustomerId, testCreateCharge, function (error, body, response){ 239 | printLog(response.statusCode, body, error); 240 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 241 | newlyCreatedCustomerTransactionId = body.id; 242 | done(); 243 | }); 244 | }); 245 | }); 246 | }); 247 | describe('Testing subscriptions', function(){ 248 | newlyCreatedPlanId = 'pdusfyuqsdze2sejn7qy'; 249 | var newlyCreatedSubscriptionId = ''; 250 | describe('Create subscription', function(){ 251 | it('should return statusCode 200||201', function (done){ 252 | var testSubscription = {"plan_id": newlyCreatedPlanId, "card_id": newlyCreatedCustomerCardId, "trial_days": "30"}; 253 | openpay.groups.customers.subscriptions.create(merchantId, newlyCreatedCustomerId, testSubscription, function (error, body, response){ 254 | printLog(response.statusCode, body, error); 255 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 256 | newlyCreatedSubscriptionId = body.id; 257 | done(); 258 | }); 259 | }); 260 | }); 261 | describe('Get all subscriptions without constraints', function(){ 262 | it('should return statusCode 200', function (done){ 263 | openpay.groups.customers.subscriptions.list(merchantId, newlyCreatedCustomerId, function (error, body, response){ 264 | printLog(response.statusCode, body, error); 265 | assert.equal(response.statusCode, 200, 'Status code == 200'); 266 | done(); 267 | }); 268 | }); 269 | }); 270 | describe('Get subscription', function(){ 271 | it('should return statusCode 200', function (done){ 272 | openpay.groups.customers.subscriptions.get(merchantId, newlyCreatedCustomerId, newlyCreatedSubscriptionId, function (error, body, response){ 273 | printLog(response.statusCode, body, error); 274 | assert.equal(response.statusCode, 200, ''); 275 | done(); 276 | }); 277 | }); 278 | }); 279 | describe('Update subscription', function(){ 280 | it('should return statusCode 200', function (done){ 281 | openpay.groups.customers.subscriptions.update(merchantId, newlyCreatedCustomerId, newlyCreatedSubscriptionId, {"trial_end_date": "2022-02-11"}, function (error, body, response){ 282 | printLog(response.statusCode, body, error); 283 | assert.equal(response.statusCode, 200, ''); 284 | done(); 285 | }); 286 | }); 287 | }); 288 | describe('Delete subscription', function(){ 289 | it('should return statusCode 204', function (done){ 290 | openpay.groups.customers.subscriptions.delete(merchantId, newlyCreatedCustomerId, newlyCreatedSubscriptionId, function (error, body, response){ 291 | printLog(response.statusCode, body, error); 292 | assert.equal(response.statusCode, 204, ''); 293 | done(); 294 | }); 295 | }); 296 | }); 297 | }); 298 | describe('Create and delete customer', function(){ 299 | it('should return statusCode 200||201', function (done){ 300 | openpay.groups.customers.create(testCreateCustomer, function (error, body, response){ 301 | printLog(response.statusCode, body, error); 302 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 303 | newlyCreatedCustomerId = body.id; 304 | done(); 305 | }); 306 | }); 307 | 308 | it('should return statusCode 200||201', function (done){ 309 | openpay.groups.customers.cards.create(newlyCreatedCustomerId, testCard, function (error, body, response){ 310 | printLog(response.statusCode, body, error); 311 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 312 | newlyCreatedCustomerCardId = body.id; 313 | done(); 314 | }); 315 | }); 316 | 317 | it('should return statusCode 204', function (done){ 318 | openpay.groups.customers.cards.delete(newlyCreatedCustomerId, newlyCreatedCustomerCardId, function (error, body, response){ 319 | printLog(response.statusCode, body, error); 320 | assert.equal(response.statusCode, 204, ''); 321 | done(); 322 | }); 323 | }); 324 | }); 325 | describe('Delete customer', function(){ 326 | it('should return statusCode 204', function (done){ 327 | openpay.groups.customers.delete(newlyCreatedCustomerId, function (error, body, response){ 328 | printLog(response.statusCode, body, error); 329 | assert.equal(response.statusCode, 204, ''); 330 | done(); 331 | }); 332 | }); 333 | }); 334 | 335 | }); 336 | 337 | 338 | function printLog(code, body, error){ 339 | if(enableLogging){ 340 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 341 | } 342 | if(code>=300){ 343 | console.log(' '); 344 | console.log(error); 345 | console.log(' '); 346 | } 347 | } 348 | 349 | function getVerificationCode(url, callback) { 350 | urllib.request(url, function(err, body, res){ 351 | var resCode = res.statusCode; 352 | var error = (resCode!=200 && resCode!=201 && resCode!=204) ? body : null; 353 | var verification_code = null; 354 | console.info('error: ' + error); 355 | if (!error) { 356 | verification_code = body.toString().substring(body.indexOf('verification_code') + 28 , body.indexOf('verification_code') + 28 + 8); 357 | console.info('verification_code: ' + verification_code); 358 | } 359 | callback(error, verification_code); 360 | }); 361 | } 362 | -------------------------------------------------------------------------------- /test/payouts_test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 7 | openpay.setTimeout(20000); 8 | var enableLogging = true; 9 | 10 | describe('Get all payouts with filters creation and amount', function(){ 11 | this.timeout(0); 12 | it('should return statusCode 200', function (done){ 13 | var searchParams = { 14 | 'amount[lte]': 1000, 15 | 'creation[gte]': '2020-01-01', 16 | 'limit':1 17 | }; 18 | openpay.payouts.list(searchParams, function (error, body, response){ 19 | printLog(response.statusCode, body, error); 20 | assert.equal(response.statusCode, 200, 'Status code == 200'); 21 | done(); 22 | }); 23 | }); 24 | }); 25 | 26 | function printLog(code, body, error){ 27 | if(enableLogging){ 28 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 29 | } 30 | if(code>=300){ 31 | console.log(' '); 32 | console.log(error); 33 | console.log(' '); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/peru/cards.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | const card = { 10 | "holder_name": "DinnersClub", 11 | "card_number": "4111111111111111", 12 | "cvv2": "651", 13 | "expiration_month": "09", 14 | "expiration_year": "25" 15 | }; 16 | const customer = { 17 | "name": "Marco", 18 | "last_name": "Morales Perez", 19 | "email": "marco.mp@qrsof.com", 20 | "phone_number": "5744484951", 21 | "address": { 22 | "country_code": "PE", 23 | "postal_code": "12345", 24 | "city": "Lima", 25 | "state": "Lima", 26 | "line1": "Perú", 27 | "line2": "Perú", 28 | "line3": "Perú" 29 | } 30 | }; 31 | 32 | var cardId = ''; 33 | 34 | describe('Get cards list with creation[lte] filter', function () { 35 | this.timeout(0); 36 | it('should return cards list and 200 status code', function (done) { 37 | var searchParams = { 38 | 'creation[lte]': '2021-01-01', 39 | 'limit': 1 40 | }; 41 | openpay.cards.list(searchParams, function (error, body, response) { 42 | printLog(response.statusCode, body, error); 43 | assert.equal(response.statusCode, 200, ''); 44 | done(); 45 | }); 46 | }) 47 | }) 48 | 49 | 50 | describe('Create cards', function () { 51 | this.timeout(0); 52 | describe('create customer card', function () { 53 | it('should return card 201 status code', function (done) { 54 | openpay.customers.create(customer, function (error, body, response) { 55 | const customerId = response.data.id; 56 | openpay.customers.cards.create(customerId, card, function (error, body, response) { 57 | printLog(response.statusCode, body, error); 58 | assert.equal(response.statusCode, 201, ''); 59 | done(); 60 | }); 61 | }); 62 | }) 63 | 64 | }); 65 | 66 | describe('create merchant card', function () { 67 | it('should return card 201 status code', function (done) { 68 | openpay.cards.create(card, function (error, body, response) { 69 | printLog(response.statusCode, body, error); 70 | assert.equal(response.statusCode, 201, ''); 71 | done(); 72 | }); 73 | }); 74 | 75 | }); 76 | 77 | }); 78 | 79 | 80 | describe('get cards', function () { 81 | 82 | describe('get card by id', function () { 83 | it("should return card and 200 status code", function () { 84 | openpay.cards.create(card, function (error, body, response) { 85 | const cardId = response.data.id; 86 | openpay.cards.get(cardId, function (error, body, response) { 87 | assert.equal(response.statusCode, 200, ''); 88 | assert.equal(response.data.id, cardId, ''); 89 | done(); 90 | }); 91 | }); 92 | }); 93 | }); 94 | 95 | describe('get customer cards', function () { 96 | it('should customer card list', function () { 97 | var searchParams = { 98 | 'creation[gte]': '2021-01-01', 99 | 'limit': 1 100 | }; 101 | openpay.customers.list(searchParams, function (error, body, response) { 102 | const customer = response.data[0]; 103 | console.log('response', response.data[0]); 104 | openpay.customers.cards.list(customer.id, {}, function (error, body, response) { 105 | printLog(response.statusCode, body, error); 106 | assert.equal(response.statusCode, 200, ''); 107 | assert(response.data.size >= 1); 108 | done(); 109 | }); 110 | }); 111 | }); 112 | }); 113 | 114 | describe('get customer card id', function () { 115 | it("should card response and 200 status code", function () { 116 | var searchParams = { 117 | 'creation[gte]': '2021-01-01', 118 | 'limit': 1 119 | }; 120 | openpay.customers.list(searchParams, function (error, body, response) { 121 | const customer = response.data[0]; 122 | openpay.customers.cards.create(customer.id, card, function (error, body, response) { 123 | const cardCreated = response.data; 124 | openpay.customers.cards.get(customer.id, cardCreated.id, function (error, body, response) { 125 | assert.equal(response.statusCode, 200, ''); 126 | assert.equal(cardCreated.id, response.data.id, ''); 127 | done(); 128 | }); 129 | }); 130 | }); 131 | }); 132 | }); 133 | 134 | }); 135 | 136 | 137 | describe('delete card', function () { 138 | it('create customer', function (done) { 139 | var searchParams = { 140 | 'creation[gte]': '2021-01-01', 141 | 'limit': 1 142 | }; 143 | openpay.customers.list(searchParams, function (error, body, response) { 144 | const customerGet = response.data[0]; 145 | openpay.customers.cards.create(customerGet.id, card, function (error, body, response) { 146 | const cardCreated = response.data; 147 | openpay.customers.cards.delete(customerGet.id, cardCreated.id, function (error, body, response) { 148 | assert.equal(response.statusCode, 204, ''); 149 | done(); 150 | }); 151 | }); 152 | }); 153 | }); 154 | }); 155 | 156 | 157 | function printLog(code, body, error) { 158 | if (enableLogging) { 159 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 160 | } 161 | if (code >= 300) { 162 | console.log(' '); 163 | console.log(error); 164 | console.log(' '); 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /test/peru/charges.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | const merchantCharge = { 11 | "source_id": "kdx205scoizh93upqbte", 12 | "method": "card", 13 | "amount": 716, 14 | "currency": "PEN", 15 | "description": "Cargo inicial a mi cuenta", 16 | "order_id": "oid-65584", 17 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f", 18 | "customer": { 19 | "name": "Cliente Perú", 20 | "last_name": "Vazquez Juarez", 21 | "phone_number": "4448936475", 22 | "email": "juan.vazquez@empresa.pe" 23 | } 24 | } 25 | 26 | const customerCharge = { 27 | "source_id": "kdx205scoizh93upqbte", 28 | "method": "card", 29 | "amount": 716, 30 | "currency": "PEN", 31 | "description": "Cargo inicial a mi cuenta", 32 | "order_id": "oid-65584", 33 | "device_session_id": "kR1MiQhz2otdIuUlQkbEyitIqVMiI16f" 34 | } 35 | 36 | const token = { 37 | "card_number": "4111111111111111", 38 | "holder_name": "Juan Perez Ramirez", 39 | "expiration_year": "21", 40 | "expiration_month": "12", 41 | "cvv2": "110", 42 | "address": { 43 | "city": "Lima", 44 | "country_code": "PE", 45 | "postal_code": "110511", 46 | "line1": "Av 5 de Febrero", 47 | "line2": "Roble 207", 48 | "line3": "col carrillo", 49 | "state": "Lima" 50 | } 51 | } 52 | 53 | describe('Get charges list', function () { 54 | this.timeout(0); 55 | describe('Get charges list with creation[gte] filter', function () { 56 | it('should return charges list and 200 status code', function (done) { 57 | var searchParams = { 58 | 'creation[gte]': '2021-01-01', 59 | 'limit': 1 60 | }; 61 | openpay.charges.list(searchParams, function (error, body, response) { 62 | printLog(response.statusCode, body, error); 63 | assert.equal(response.statusCode, 200, ''); 64 | done(); 65 | }); 66 | }); 67 | }); 68 | describe('Get charges list with no filter', function () { 69 | it('should return charges list and 200 status code', function (done) { 70 | openpay.charges.list({}, function (error, body, response) { 71 | printLog(response.statusCode, body, error); 72 | assert.equal(response.statusCode, 200, ''); 73 | done(); 74 | }); 75 | }); 76 | }); 77 | describe('Get customer charges list with creation[gte] filter', function () { 78 | it('should return charges list and 200 status code', function (done) { 79 | openpay.customers.list({}, function (error, body, response) { 80 | const customer = response.data[0]; 81 | var searchParams = { 82 | 'creation[gte]': '2021-01-01', 83 | 'limit': 1 84 | }; 85 | openpay.customers.charges.list(customer.id, searchParams, function (error, body, response) { 86 | printLog(response.statusCode, body, error); 87 | assert.equal(response.statusCode, 200, ''); 88 | done(); 89 | }); 90 | }) 91 | }); 92 | }); 93 | describe('Get customer charges list with no filter', function () { 94 | it('should return charges list and 200 status code', function (done) { 95 | openpay.customers.list({}, function (error, body, response) { 96 | const customer = response.data[0]; 97 | openpay.customers.charges.list(customer.id, {}, function (error, body, response) { 98 | printLog(response.statusCode, body, error); 99 | assert.equal(response.statusCode, 200, ''); 100 | done(); 101 | }); 102 | }) 103 | }); 104 | }); 105 | }); 106 | 107 | describe('Create charges', function () { 108 | this.timeout(0); 109 | describe('create customer charge', function () { 110 | it('should return card 200 status code', function (done) { 111 | openpay.tokens.create(token, function (error, body, response) { 112 | const token = response.data; 113 | customerCharge.source_id = token.id; 114 | customerCharge.order_id = 'oid-65784'; 115 | openpay.customers.list({}, function(error, body, response) { 116 | const customer = response.data[0]; 117 | openpay.customers.charges.create(customer.id, customerCharge, function (error, body, response) { 118 | printLog(response.statusCode, body, error); 119 | assert.equal(response.statusCode, 200, ''); 120 | done(); 121 | }); 122 | }); 123 | }); 124 | }); 125 | }); 126 | 127 | describe('create merchant charge', function () { 128 | it('should return charge 200 status code', function (done) { 129 | openpay.tokens.create(token, function (error, body, response) { 130 | const token = response.data; 131 | merchantCharge.source_id = token.id; 132 | openpay.charges.create(merchantCharge, function (error, body, response) { 133 | printLog(response.statusCode, body, error); 134 | assert.equal(response.statusCode, 200, ''); 135 | done(); 136 | }); 137 | }); 138 | }); 139 | }); 140 | 141 | describe('create merchant store charge', function () { 142 | it('should return charge 200 status code', function (done) { 143 | merchantCharge.source_id = ''; 144 | merchantCharge.order_id = 'oid-63384'; 145 | merchantCharge.method = 'store'; 146 | openpay.charges.create(merchantCharge, function (error, body, response) { 147 | printLog(response.statusCode, body, error); 148 | assert.equal(response.statusCode, 200, ''); 149 | done(); 150 | }); 151 | }); 152 | }); 153 | }); 154 | 155 | describe('Get charges', function () { 156 | 157 | describe('Get charge by id', function () { 158 | it("should return card and 200 status code", function () { 159 | openpay.tokens.create(token, function (error, body, response) { 160 | const token = response.data; 161 | merchantCharge.source_id = token.id; 162 | merchantCharge.order_id = 'oid-65539'; 163 | openpay.charges.create(merchantCharge, function (error, body, response) { 164 | const charge = response.data; 165 | openpay.charges.get(charge.id, function (error, body, response) { 166 | printLog(response.statusCode, body, error); 167 | assert.equal(response.statusCode, 200, ''); 168 | assert.equal(response.data.id, charge.id, ''); 169 | done(); 170 | }); 171 | }); 172 | }); 173 | }); 174 | }); 175 | 176 | describe('Get customer charge by id', function () { 177 | it('should customer card list', function () { 178 | openpay.tokens.create(token, function (error, body, response) { 179 | const token = response.data; 180 | customerCharge.source_id = token.id; 181 | customerCharge.order_id = 'oid-25784' 182 | openpay.customers.list({}, function(error, body, response) { 183 | const customer = response.data[0]; 184 | openpay.customers.charges.create(customer.id, customerCharge, function (error, body, response) { 185 | const charge = response.data; 186 | openpay.customers.charges.get(customer.id, charge.id, function(error, body, response) { 187 | printLog(response.statusCode, body, error); 188 | assert.equal(response.statusCode, 200, ''); 189 | assert.equal(response.data.id, charge.id, ''); 190 | done(); 191 | }); 192 | }); 193 | }); 194 | }); 195 | }); 196 | }); 197 | }); 198 | 199 | function printLog(code, body, error) { 200 | if (enableLogging) { 201 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 202 | } 203 | if (code >= 300) { 204 | console.log(' '); 205 | console.log(error); 206 | console.log(' '); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /test/peru/checkouts.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | const customer = { 11 | "name": "Marco", 12 | "last_name": "Morales Perez", 13 | "email": "marco.mp@qrsof.com", 14 | "phone_number": "5744484951", 15 | "address": { 16 | "country_code": "PE", 17 | "postal_code": "12345", 18 | "city": "Lima", 19 | "state": "Lima", 20 | "line1": "Perú", 21 | "line2": "Perú", 22 | "line3": "Perú" 23 | } 24 | }; 25 | 26 | const customerCheckout = { 27 | "amount": 250, 28 | "currency": "PEN", 29 | "description": "Cargo cobro con link cliente", 30 | "redirect_url": "https://misitioempresa.pe", 31 | "order_id": "oid-87491", // Cambiar por un OID nuevo 32 | "send_email": "true" 33 | } 34 | 35 | const merchantCheckout = { 36 | "amount": 250, 37 | "currency": "PEN", 38 | "description": "Cargo cobro con link", 39 | "redirect_url": "https://misitioempresa.pe", 40 | "order_id": "oid-66393", // Cambiar por un OID nuevo 41 | "expiration_date": "2021-08-31 12:50", 42 | "send_email": "true", 43 | "customer": { 44 | "name": "Cliente Perú", 45 | "last_name": "Vazquez Juarez", 46 | "phone_number": "4448936475", 47 | "email": "juan.vazquez@empresa.pe" 48 | } 49 | } 50 | 51 | describe('List checkouts', function () { 52 | this.timeout(0); 53 | describe('List merchant checkouts', function () { 54 | it('should return checkout list and 200 status code', function (done) { 55 | const searchParams = { 56 | "limit": 2, 57 | "startDate": "20211001", 58 | "endDate": "20211011" 59 | }; 60 | openpay.checkouts.list(searchParams, function (error, body, response) { 61 | printLog(response.statusCode, body, error); 62 | assert.equal(response.statusCode, 200, ''); 63 | done(); 64 | }); 65 | }); 66 | }); 67 | }) 68 | 69 | 70 | describe('Create checkouts', function () { 71 | this.timeout(0); 72 | describe('create customer checkout', function () { 73 | it('should return checkout 200 status code', function (done) { 74 | openpay.customers.create(customer, function (error, body, response) { 75 | const customerId = response.data.id; 76 | openpay.customers.checkouts.create(customerId, customerCheckout, function (error, body, response) { 77 | printLog(response.statusCode, body, error); 78 | assert.equal(response.statusCode, 200, ''); 79 | done(); 80 | }); 81 | }); 82 | }) 83 | }); 84 | 85 | describe('create merchant checkout', function () { 86 | it('should return checkout 200 status code', function (done) { 87 | openpay.checkouts.create(merchantCheckout, function (error, body, response) { 88 | printLog(response.statusCode, body, error); 89 | assert.equal(response.statusCode, 200, ''); 90 | done(); 91 | }); 92 | }); 93 | }); 94 | }); 95 | 96 | 97 | describe('Get checkouts', function () { 98 | this.timeout(0); 99 | describe('get checkout by id', function () { 100 | it("should return checkout and 200 status code", function () { 101 | openpay.checkouts.create(merchantCheckout, function (error, body, response) { 102 | const checkoutId = response.data.id; 103 | openpay.checkouts.get(checkoutId, function (error, body, response) { 104 | assert.equal(response.statusCode, 200, ''); 105 | assert.equal(response.data.id, checkoutId, ''); 106 | done(); 107 | }); 108 | }); 109 | }); 110 | }); 111 | }); 112 | 113 | describe('Update checkouts', function () { 114 | this.timeout(0); 115 | it('should return checkouts status 200', function () { 116 | openpay.checkouts.create(merchantCheckout, function (error, body, reponse) { 117 | const checkout = reponse.data; 118 | const expirationDate = "2021-10-26 13:43" 119 | const status = "available"; 120 | const data = {"expiration_date": expirationDate} 121 | 122 | openpay.checkouts.update(checkout.id, status, data, function (error, body, response) { 123 | printLog(response.statusCode, body, error); 124 | assert.equal(response.statusCode, 200, ''); 125 | assert.equal(response.data.id, checkout.id, ''); 126 | assert.equal(response.data.status, status, ''); 127 | assert.equal(response.data.expiration_date, expirationDate.replace(' ','T') + ':00.000-050', ''); 128 | }); 129 | }); 130 | }); 131 | }); 132 | 133 | function printLog(code, body, error) { 134 | if (enableLogging) { 135 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 136 | } 137 | if (code >= 300) { 138 | console.log(' '); 139 | console.log(error); 140 | console.log(' '); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /test/peru/customers.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(10000); 8 | var enableLogging = true; 9 | const customer = { 10 | "name": "Marco", 11 | "last_name": "Morales Perez", 12 | "email": "marco.mp@qrsof.com", 13 | "phone_number": "5744484951", 14 | "address": { 15 | "country_code": "PE", 16 | "postal_code": "12345", 17 | "city": "Lima", 18 | "state": "Lima", 19 | "line1": "Perú", 20 | "line2": "Perú", 21 | "line3": "Perú" 22 | } 23 | }; 24 | describe('Get customers list with creation[gte] filter', function () { 25 | this.timeout(0); 26 | it('should return customer list and 200 status code', function (done) { 27 | var searchParams = { 28 | 'creation[gte]': '2021-01-01', 29 | 'limit': 1 30 | }; 31 | openpay.customers.list(searchParams, function (error, body, response) { 32 | printLog(response.statusCode, body, error); 33 | assert.equal(response.statusCode, 200, ''); 34 | done(); 35 | }); 36 | }) 37 | }) 38 | 39 | describe('Create customer ', function () { 40 | this.timeout(0); 41 | it('should return customer 201 status code', function (done) { 42 | openpay.customers.create(customer, function (error, body, response) { 43 | printLog(response.statusCode, body, error); 44 | assert.equal(response.statusCode, 201, ''); 45 | done(); 46 | }); 47 | }) 48 | }); 49 | 50 | 51 | describe('Actualizar customer ', function () { 52 | this.timeout(0); 53 | const customerUpdateRequest = { 54 | "name": "Marco Update", 55 | "last_name": "Morales Perez", 56 | "email": "marco.mp.update@qrsof.com", 57 | "phone_number": "5744484951", 58 | "address": { 59 | "country_code": "PE", 60 | "postal_code": "12345", 61 | "city": "Lima", 62 | "state": "Lima", 63 | "line1": "Perú", 64 | "line2": "Perú", 65 | "line3": "Perú" 66 | } 67 | } 68 | it('should return customer 200 status code', function (done) { 69 | openpay.customers.create(customer, function (error, body, response) { 70 | console.log('response: ', response); 71 | openpay.customers.update(response.data.id, customerUpdateRequest, function (error, body, response) { 72 | printLog(response.statusCode, body, error); 73 | assert.equal(response.statusCode, 200, ''); 74 | done(); 75 | }); 76 | }); 77 | }) 78 | }); 79 | 80 | 81 | describe("get client", function () { 82 | it("should return a customer", function (done) { 83 | openpay.customers.create(customer, function (error, body, response) { 84 | const customerId = response.data.id; 85 | openpay.customers.get(customerId, function (error, body, respose) { 86 | assert.equal(respose.data.id, customerId, ''); 87 | assert.equal(respose.data.name, 'Marco', ''); 88 | done() 89 | }); 90 | }); 91 | }); 92 | 93 | }); 94 | 95 | 96 | describe("delete customer", function () { 97 | it("should return a 200", function (done) { 98 | openpay.customers.create(customer, function (error, body, response) { 99 | const customerId = response.data.id; 100 | openpay.customers.delete(customerId, function (error, body, respose) { 101 | assert.equal(respose.statusCode, 204, ''); 102 | done() 103 | }); 104 | }); 105 | }); 106 | }); 107 | 108 | function printLog(code, body, error) { 109 | if (enableLogging) { 110 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 111 | } 112 | if (code >= 300) { 113 | console.log(' '); 114 | console.log(error); 115 | console.log(' '); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /test/peru/tokens.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | 10 | const token = { 11 | "card_number": "4111111111111111", 12 | "holder_name": "Juan Perez Ramirez", 13 | "expiration_year": "21", 14 | "expiration_month": "12", 15 | "cvv2": "110", 16 | "address": { 17 | "city": "Lima", 18 | "country_code": "PE", 19 | "postal_code": "110511", 20 | "line1": "Av 5 de Febrero", 21 | "line2": "Roble 207", 22 | "line3": "col carrillo", 23 | "state": "Lima" 24 | } 25 | } 26 | 27 | describe('Create Token', function () { 28 | this.timeout(0); 29 | it('should return token 201 status code', function (done) { 30 | openpay.tokens.create(token, function (error, body, response) { 31 | printLog(response.statusCode, body, error); 32 | assert.equal(response.statusCode, 201, ''); 33 | done(); 34 | }); 35 | }); 36 | }); 37 | 38 | describe('Get Token', function () { 39 | 40 | describe('Get Token by ID', function () { 41 | it("should return token and 200 status code", function () { 42 | openpay.tokens.create(token, function (error, body, response) { 43 | const tokenId = response.data.id; 44 | openpay.tokens.get(tokenId, function (error, body, response) { 45 | assert.equal(response.statusCode, 200, ''); 46 | assert.equal(response.data.id, tokenId, ''); 47 | done(); 48 | }); 49 | }); 50 | }); 51 | }); 52 | }); 53 | 54 | function printLog(code, body, error) { 55 | if (enableLogging) { 56 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 57 | console.log(body) 58 | } 59 | if (code >= 300) { 60 | console.log(' '); 61 | console.log(error); 62 | console.log(' '); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /test/peru/webhooks.pe.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m3cji4ughukthjcsglv0', 'sk_f934dfe51645483e82106301d985a4f6', 'pe',false); 7 | openpay.setTimeout(30000); 8 | var enableLogging = true; 9 | const webhook = { 10 | "url": "https://webhook.site/dffe8335-b0bb-493f-a38d-3e61b711bd6a", // Cambiar por URL válida 11 | "user": "juanito", 12 | "password": "passjuanito", 13 | "event_types": [ 14 | "charge.failed", 15 | "charge.cancelled", 16 | "charge.created", 17 | "chargeback.accepted" 18 | ] 19 | } 20 | 21 | describe('Create Webhooks', function () { 22 | this.timeout(0); 23 | it('should return webhook 201 status code', function (done) { 24 | openpay.webhooks.create(webhook, function (error, body, response) { 25 | printLog(response.statusCode, body, error); 26 | assert.equal(response.statusCode, 201, ''); 27 | done(); 28 | }); 29 | }); 30 | }); 31 | 32 | describe('Get webhooks list', function () { 33 | this.timeout(0); 34 | it('should return webhooks list and 200 status code', function (done) { 35 | openpay.webhooks.list(function (error, body, response) { 36 | printLog(response.statusCode, body, error); 37 | assert.equal(response.statusCode, 200, ''); 38 | done(); 39 | }); 40 | }) 41 | }) 42 | 43 | 44 | describe('Get wehbook by ID', function () { 45 | it("should return card and 200 status code", function () { 46 | openpay.webhooks.create(webhook, function (error, body, response) { 47 | const webhookId = response.data.id; 48 | openpay.webhooks.get(webhookId, function (error, body, response) { 49 | assert.equal(response.statusCode, 200, ''); 50 | assert.equal(response.data.id, webhookId, ''); 51 | done(); 52 | }); 53 | }); 54 | }); 55 | }); 56 | 57 | describe('Eliminar Webhook', function () { 58 | it("should return card and 204 status code", function () { 59 | openpay.webhooks.list(function (error, body, response) { 60 | const webhookGet = response.data[0]; 61 | openpay.webhooks.delete(webhookGet.id, function (error, body, response) { 62 | assert.equal(response.statusCode, 204, ''); 63 | done(); 64 | }); 65 | }); 66 | }); 67 | }); 68 | 69 | 70 | function printLog(code, body, error) { 71 | if (enableLogging) { 72 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 73 | } 74 | if (code >= 300) { 75 | console.log(' '); 76 | console.log(error); 77 | console.log(' '); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /test/plans.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | 4 | var Openpay = require('../lib/openpay'); 5 | /*Sandbox*/ 6 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 7 | openpay.setTimeout(20000); 8 | var enableLogging = true; 9 | 10 | describe('Get plans list with creation[lte] filter', function() { 11 | this.timeout(0); 12 | it('should return plans list and 200 status code', function(done){ 13 | var searchParams = { 14 | 'creation[gte]': '2021-01-01', 15 | 'limit':1 16 | }; 17 | openpay.plans.list(searchParams, function (error, body, response) { 18 | printLog(response.statusCode, body, error); 19 | assert.equal(response.statusCode, 200, ''); 20 | done(); 21 | }); 22 | }) 23 | }) 24 | 25 | function printLog(code, body, error){ 26 | if(enableLogging){ 27 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 28 | } 29 | if(code>=300){ 30 | console.log(' '); 31 | console.log(error); 32 | console.log(' '); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/subscriptions.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | var Openpay = require('../lib/openpay'); 4 | /*Sandbox*/ 5 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 6 | openpay.setTimeout(30000); 7 | var enableLogging = true; 8 | var testCreateCustomer = { 9 | "name": "Juan", 10 | "email": "juan@nonexistantdomain.com" 11 | }; 12 | 13 | describe('Get all subscriptions with creation filter', function () { 14 | this.timeout(0); 15 | it('should return statusCode 200', function (done) { 16 | var searchParams = { 17 | 'creation[gte]': '2021-01-01', 18 | 'limit':1 19 | }; 20 | openpay.customers.create(testCreateCustomer, function (error, body) { 21 | openpay.customers.subscriptions.list(body.id, searchParams, function (error, body, response) { 22 | printLog(response.statusCode, body, error); 23 | assert.equal(response.statusCode, 200, 'Status code != 400'); 24 | done(); 25 | }); 26 | }); 27 | }); 28 | }); 29 | 30 | function printLog(code, body, error) { 31 | if (enableLogging) { 32 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 33 | } 34 | if (code >= 300) { 35 | console.log(' '); 36 | console.log(error); 37 | console.log(' '); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | var urllib = require('urllib'); 4 | 5 | var Openpay = require('../lib/openpay'); 6 | /*Sandbox*/ 7 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 8 | openpay.setTimeout(10000); 9 | 10 | var enableLogging = true; 11 | var testCreateCharges = true; 12 | var testCreatePayouts = false; 13 | var testBankAccountId = 'bmopptj5st1hx8ddouha'; 14 | 15 | // IMPORTANT NOTE ABOUT WEBHOOKS 16 | // !! Future contributors, please read: !! 17 | // 18 | // The webhook url requires opening a new requestbin in https://opey-requestbin.herokuapp.com 19 | // and entering the id as a parameter further down; otherwise the webhook check will fail. 20 | // The current one will remain open for 60 days, according to the docs (starting Feb 3, 2021) 21 | // If you test again after 60 days have passed, please update the url and the date 22 | // in both comments, here and down below. 23 | // (There should be a better way to do this) 24 | 25 | // Defining a valid expiration year for cards, adding 5 years to the current one 26 | var validExpirationYear = (new Date().getFullYear() + 5).toString().substr(2, 2); 27 | 28 | describe('Testing whole API', function(){ 29 | this.timeout(0); 30 | 31 | var testCreateCustomer = { 32 | "name":"Juan", 33 | "email":"juan@nonexistantdomain.com", 34 | // The customer requires an account to charge fees and receive transfers 35 | "requires_account": true 36 | }; 37 | var testUpdateCustomer = { 38 | "name":"Juan", 39 | "email":"juan@nonexistantdomain.com", 40 | "phone_number":"123456789" 41 | }; 42 | 43 | describe('Testing Webhook', function() { 44 | var webhook; 45 | var webhook_params = { 46 | // Update the requestbin url here. Last change: Feb 3, 2021 47 | 'url' : 'https://opey-requestbin.herokuapp.com/12i2q011', 48 | 'event_types' : [ 49 | 'charge.refunded', 50 | 'charge.failed', 51 | 'charge.cancelled', 52 | 'charge.created', 53 | 'chargeback.accepted' 54 | ] 55 | }; 56 | 57 | describe('Create Webhook', function() { 58 | it('Should return statusCode 201', function(done) { 59 | openpay.webhooks.create(webhook_params, function (error, body, response){ 60 | printLog(response.statusCode, body, error); 61 | assert.equal(response.statusCode, 201, ''); 62 | webhook = body; 63 | done(); 64 | }); 65 | }); 66 | }); 67 | 68 | describe('Get webhook by id and status verified', function () { 69 | it('Should return status code 200', function (done) { 70 | openpay.webhooks.get(webhook.id, function (error, body, response) { 71 | printLog(response.statusCode, body, error); 72 | assert.equal(response.statusCode, 200, ''); 73 | assert.equal(body.status, 'verified', ''); 74 | done(); 75 | }); 76 | }); 77 | }); 78 | /* 79 | describe('Verify webhook code', function() { 80 | it('Should return statusCode 204', function(done) { 81 | console.info(webhook.url + '?inspect'); 82 | getVerificationCode(webhook.url + '?inspect', function(error, verification_code) { 83 | console.info('webhook.id = ' + webhook.id); 84 | console.info('verification_code = ' + verification_code); 85 | openpay.webhooks.verify(webhook.id, verification_code, function(error, body, response) { 86 | printLog(response.statusCode, body, error); 87 | assert.equal(response.statusCode, 204, ''); 88 | done(); 89 | }); 90 | }) 91 | }); 92 | }); 93 | */ 94 | 95 | 96 | describe('Get webhook by id and status verified', function () { 97 | it('Should return status code 200', function (done) { 98 | openpay.webhooks.get(webhook.id, function (error, body, response) { 99 | printLog(response.statusCode, body, error); 100 | assert.equal(response.statusCode, 200, ''); 101 | assert.equal(body.status, 'verified', ''); 102 | done(); 103 | }); 104 | }); 105 | }); 106 | 107 | describe('List webhooks by id merchant', function () { 108 | it('Should return status code 200', function (done) { 109 | openpay.webhooks.list(function (error, body, response) { 110 | printLog(response.statusCode, body, error); 111 | assert.equal(response.statusCode, 200, ''); 112 | assert.equal(body.length, 1, ''); 113 | done(); 114 | }); 115 | }); 116 | }); 117 | 118 | describe('Delete webhook by id', function () { 119 | it('Should return statusCode 204', function (done) { 120 | openpay.webhooks.delete(webhook.id, function (error, body, response) { 121 | printLog(response.statusCode, body, error); 122 | assert.equal(response.statusCode, 204, ''); 123 | done(); 124 | }); 125 | }); 126 | }); 127 | 128 | }); 129 | 130 | 131 | describe('Testing merchant', function () { 132 | describe('Get merchant', function () { 133 | it('should return statusCode 200', function (done) { 134 | openpay.merchant.get(function (error, body, response) { 135 | printLog(response.statusCode, body, error); 136 | assert.equal(response.statusCode, 200, ''); 137 | done(); 138 | }); 139 | }); 140 | }); 141 | }); 142 | 143 | var newlyCreatedCustomerId = ''; 144 | describe('Testing customers', function () { 145 | describe('Create customer', function () { 146 | it('should return statusCode 200||201', function (done) { 147 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 148 | printLog(response.statusCode, body, error); 149 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 150 | newlyCreatedCustomerId = body.id; 151 | done(); 152 | }); 153 | }); 154 | }); 155 | describe('Get all customers without constraints', function () { 156 | it('should return statusCode 200', function (done) { 157 | openpay.customers.list({}, function (error, body, response) { 158 | printLog(response.statusCode, body, error); 159 | assert.equal(response.statusCode, 200, ''); 160 | done(); 161 | }); 162 | }); 163 | }); 164 | describe('Get customer', function () { 165 | it('should return statusCode 200', function (done) { 166 | openpay.customers.get(newlyCreatedCustomerId, function (error, body, response) { 167 | printLog(response.statusCode, body, error); 168 | assert.equal(response.statusCode, 200, ''); 169 | done(); 170 | }); 171 | }); 172 | }); 173 | describe('Update customer', function () { 174 | it('should return statusCode 200', function (done) { 175 | openpay.customers.update(newlyCreatedCustomerId, testUpdateCustomer, function (error, body, response) { 176 | printLog(response.statusCode, body, error); 177 | assert.equal(response.statusCode, 200, ''); 178 | done(); 179 | }); 180 | }); 181 | }); 182 | describe('Get all customers with constraints', function () { 183 | it('should return statusCode 200', function (done) { 184 | openpay.customers.list({'creation': '2013-12-10'}, function (error, body, response) { 185 | printLog(response.statusCode, body, error); 186 | assert.equal(response.statusCode, 200, ''); 187 | done(); 188 | }); 189 | }); 190 | }); 191 | }); 192 | 193 | 194 | var testCard ={ 195 | "card_number":"4111111111111111", 196 | "holder_name":"Juan Perez", 197 | "expiration_year": validExpirationYear, 198 | "expiration_month":"12", 199 | "cvv2":"111" 200 | }; 201 | var newlyCreatedCardId = ''; 202 | var newlyCreatedCustomerCardId = ''; 203 | 204 | describe('Testing cards API', function () { 205 | describe('Add card', function () { 206 | it('should return statusCode 200||201', function (done) { 207 | openpay.cards.create(testCard, function (error, body, response) { 208 | printLog(response.statusCode, body, error); 209 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 210 | newlyCreatedCardId = body.id; 211 | done(); 212 | }); 213 | }); 214 | }); 215 | describe('Get all cards without constraints', function () { 216 | it('should return statusCode 200', function (done) { 217 | openpay.cards.list({}, function (error, body, response) { 218 | printLog(response.statusCode, body, error); 219 | assert.equal(response.statusCode, 200, ''); 220 | done(); 221 | }); 222 | }); 223 | }); 224 | describe('Get card', function () { 225 | it('should return statusCode 200', function (done) { 226 | openpay.cards.get(newlyCreatedCardId, function (error, body, response) { 227 | printLog(response.statusCode, body, error); 228 | assert.equal(response.statusCode, 200, ''); 229 | done(); 230 | }); 231 | }); 232 | }); 233 | 234 | describe('Add customer card', function () { 235 | it('should return statusCode 200||201', function (done) { 236 | openpay.customers.cards.create(newlyCreatedCustomerId, testCard, function (error, body, response) { 237 | printLog(response.statusCode, body, error); 238 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 239 | newlyCreatedCustomerCardId = body.id; 240 | done(); 241 | }); 242 | }); 243 | }); 244 | describe('Get all customer cards without constraints', function () { 245 | it('should return statusCode 200', function (done) { 246 | openpay.customers.cards.list(newlyCreatedCustomerId, {}, function (error, body, response) { 247 | printLog(response.statusCode, body, error); 248 | assert.equal(response.statusCode, 200, ''); 249 | done(); 250 | }); 251 | }); 252 | }); 253 | describe('Get customer card', function () { 254 | it('should return statusCode 200', function (done) { 255 | openpay.customers.cards.get(newlyCreatedCustomerId, newlyCreatedCustomerCardId, function (error, body, response) { 256 | printLog(response.statusCode, body, error); 257 | assert.equal(response.statusCode, 200, ''); 258 | done(); 259 | }); 260 | }); 261 | }); 262 | }); 263 | 264 | 265 | var testBankAccount = { 266 | "clabe": "021180000118359717", 267 | "holder_name": "Juan H" 268 | }; 269 | var newlyCreatedBankAccountId = ''; 270 | 271 | describe('Testing bankaccounts', function () { 272 | describe('Create bankaccount', function () { 273 | it('should return statusCode 200||201', function (done) { 274 | openpay.customers.bankaccounts.create(newlyCreatedCustomerId, testBankAccount, function (error, body, response) { 275 | printLog(response.statusCode, body, error); 276 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, ''); 277 | newlyCreatedBankAccountId = body.id; 278 | done(); 279 | }); 280 | }); 281 | }); 282 | describe('Get all bank accounts without constraints', function () { 283 | it('should return statusCode 200', function (done) { 284 | openpay.customers.bankaccounts.list(newlyCreatedCustomerId, {}, function (error, body, response) { 285 | printLog(response.statusCode, body, error); 286 | assert.equal(response.statusCode, 200, ''); 287 | done(); 288 | }); 289 | }); 290 | }); 291 | describe('Get bankaccount', function () { 292 | it('should return statusCode 200', function (done) { 293 | openpay.customers.bankaccounts.get(newlyCreatedCustomerId, newlyCreatedBankAccountId, function (error, body, response) { 294 | printLog(response.statusCode, body, error); 295 | assert.equal(response.statusCode, 200, ''); 296 | done(); 297 | }); 298 | }); 299 | }); 300 | }); 301 | 302 | 303 | //var testGetCharge = 'tlogyahn68d2qurjqhqt'; 304 | var testExistingCardCharge = { 305 | "source_id" : '', 306 | "method" : "card", 307 | "amount" : 50, 308 | "description" : "Test existing card charge" 309 | }; 310 | var testCreateCharge = { 311 | "method": "card", 312 | "card": { 313 | "card_number": "4111111111111111", 314 | "holder_name": "Aa Bb", 315 | "expiration_year": validExpirationYear, 316 | "expiration_month": "12", 317 | "cvv2": "110", 318 | }, 319 | "amount" : 20, 320 | "description" : "Test Charge" 321 | }; 322 | var testCreateChargeWithoutCapture = { 323 | "method": "card", 324 | "card": { 325 | "card_number": "4111111111111111", 326 | "holder_name": "Aa Bb", 327 | "expiration_year": validExpirationYear, 328 | "expiration_month": "12", 329 | "cvv2": "110", 330 | }, 331 | "amount" : 20, 332 | "description" : "Test Charge", 333 | "capture" : false 334 | }; 335 | var testCreateBankAccountCharge = { 336 | "method" : "bank_account", 337 | "amount" : 50, 338 | "description" : "Test bank account charge" 339 | }; 340 | var testCreateStoreCharge = { 341 | "method" : "store", 342 | "amount" : 60.01, 343 | "description" : "Test store charge" 344 | }; 345 | var testRefundData = {"description":"Testing refund"}; 346 | 347 | describe('Testing charges', function () { 348 | describe('Get all charges without constraints', function () { 349 | it('should return statusCode 200', function (done) { 350 | openpay.charges.list({}, function (error, body, response) { 351 | printLog(response.statusCode, body, error); 352 | assert.equal(response.statusCode, 200, 'Status code == 200'); 353 | done(); 354 | }); 355 | }); 356 | }); 357 | if (testCreateCharges) { 358 | describe('Create charge with existing card', function () { 359 | it('should return statusCode 200||201', function (done) { 360 | testExistingCardCharge.source_id = newlyCreatedCardId; 361 | //console.log(testExistingCardCharge); 362 | openpay.charges.create(testExistingCardCharge, function (error, body, response) { 363 | printLog(response.statusCode, body, error); 364 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 365 | done(); 366 | }); 367 | }); 368 | }); 369 | var newlyCreatedTransactionId = ''; 370 | describe('Create charge with new card', function () { 371 | it('should return statusCode 200||201', function (done) { 372 | openpay.charges.create(testCreateCharge, function (error, body, response) { 373 | printLog(response.statusCode, body, error); 374 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 375 | newlyCreatedTransactionId = body.id; 376 | done(); 377 | }); 378 | }); 379 | }); 380 | describe('Get charge', function () { 381 | it('should return statusCode 200', function (done) { 382 | openpay.charges.get(newlyCreatedTransactionId, function (error, body, response) { 383 | printLog(response.statusCode, body, error); 384 | assert.equal(response.statusCode, 200, ''); 385 | done(); 386 | }); 387 | }); 388 | }); 389 | describe('Refund merchant charge', function () { 390 | it('should return statusCode 200', function (done) { 391 | openpay.charges.refund(newlyCreatedTransactionId, testRefundData, function (error, body, response) { 392 | printLog(response.statusCode, body, error); 393 | assert.equal(response.statusCode, 200, ''); 394 | done(); 395 | }); 396 | }); 397 | }); 398 | describe('Create charge without capture', function () { 399 | it('should return statusCode 200||201', function (done) { 400 | openpay.charges.create(testCreateChargeWithoutCapture, function (error, body, response) { 401 | printLog(response.statusCode, body, error); 402 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 403 | newlyCreatedTransactionId = body.id; 404 | done(); 405 | }); 406 | }); 407 | }); 408 | describe('Capture charge', function () { 409 | it('should return statusCode 200||201', function (done) { 410 | openpay.charges.capture(newlyCreatedTransactionId, null, function (error, body, response) { 411 | printLog(response.statusCode, body, error); 412 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 413 | done(); 414 | }); 415 | }); 416 | }); 417 | describe('Create charge with new bank account', function () { 418 | it('should return statusCode 200||201', function (done) { 419 | openpay.charges.create(testCreateBankAccountCharge, function (error, body, response) { 420 | printLog(response.statusCode, body, error); 421 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 422 | newlyCreatedTransactionId = body.id; 423 | done(); 424 | }); 425 | }); 426 | }); 427 | describe('Create charge on Store', function () { 428 | it('should return statusCode 200', function (done) { 429 | openpay.charges.create(testCreateStoreCharge, function (error, body, response) { 430 | assert.equal(response.statusCode, 200, 'Status code != 200'); 431 | assert.notEqual(body.id, null); 432 | assert.equal(body.method, 'store'); 433 | assert.equal(body.payment_method.type, 'store'); 434 | assert.notEqual(body.payment_method.reference, null); 435 | assert.notEqual(body.payment_method.barcode_url, null); 436 | done(); 437 | }); 438 | }); 439 | }); 440 | } 441 | 442 | describe('Create customer charge with existing card', function () { 443 | it('should return statusCode 200||201', function (done) { 444 | testExistingCardCharge.source_id = newlyCreatedCustomerCardId; //fails if use merchant card 445 | //console.log(testExistingCardCharge); 446 | openpay.customers.charges.create(newlyCreatedCustomerId, testExistingCardCharge, function (error, body, response) { 447 | printLog(response.statusCode, body, error); 448 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 449 | done(); 450 | }); 451 | }); 452 | }); 453 | var newlyCreatedCustomerTransactionId = ''; 454 | describe('Create customer charge with new card', function () { 455 | it('should return statusCode 200||201', function (done) { 456 | openpay.customers.charges.create(newlyCreatedCustomerId, testCreateCharge, function (error, body, response) { 457 | printLog(response.statusCode, body, error); 458 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 459 | newlyCreatedCustomerTransactionId = body.id; 460 | done(); 461 | }); 462 | }); 463 | }); 464 | describe('Get all customer charges without constraints', function () { 465 | it('should return statusCode 200', function (done) { 466 | openpay.customers.charges.list(newlyCreatedCustomerId, {}, function (error, body, response) { 467 | printLog(response.statusCode, body, error); 468 | assert.equal(response.statusCode, 200, 'Status code == 200'); 469 | done(); 470 | }); 471 | }); 472 | }); 473 | describe('Get customer charge', function () { 474 | it('should return statusCode 200', function (done) { 475 | openpay.customers.charges.get(newlyCreatedCustomerId, newlyCreatedCustomerTransactionId, function (error, body, response) { 476 | printLog(response.statusCode, body, error); 477 | assert.equal(response.statusCode, 200, ''); 478 | done(); 479 | }); 480 | }); 481 | }); 482 | /*describe('Refund customer charge', function(){ 483 | it('should return statusCode 200', function (done){ 484 | openpay.customers.charges.refund(newlyCreatedCustomerId, newlyCreatedCustomerTransactionId, testRefundData, function (error, body, response){ 485 | printLog(response.statusCode, body, error); 486 | assert.equal(response.statusCode, 200, ''); 487 | done(); 488 | }); 489 | }); 490 | });*/ 491 | describe('Create customer charge with new bank account', function () { 492 | it('should return statusCode 200||201', function (done) { 493 | openpay.customers.charges.create(newlyCreatedCustomerId, testCreateBankAccountCharge, function (error, body, response) { 494 | printLog(response.statusCode, body, error); 495 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 496 | newlyCreatedCustomerTransactionId = body.id; 497 | done(); 498 | }); 499 | }); 500 | }); 501 | }); 502 | 503 | 504 | describe('Testing transfers', function () { 505 | var newlyCreatedTransactionId = ''; 506 | describe('Create transfer', function () { 507 | it('should return statusCode 200||201', function (done) { 508 | var temporalCustomerId = ''; 509 | openpay.customers.create(testCreateCustomer, function (error, body, response) { 510 | temporalCustomerId = body.id; 511 | var testTransfer = { 512 | "customer_id": temporalCustomerId, 513 | "amount": 1.50, 514 | "description": "Test transfer" 515 | }; 516 | openpay.customers.transfers.create(newlyCreatedCustomerId, testTransfer, function (error, body, response) { 517 | printLog(response.statusCode, body, error); 518 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 519 | newlyCreatedTransactionId = body.id; 520 | openpay.customers.delete(temporalCustomerId, function (error, response, body) { 521 | done(); 522 | }); 523 | }); 524 | }); 525 | }); 526 | }); 527 | describe('Get all transfers without constraints', function () { 528 | it('should return statusCode 200', function (done) { 529 | console.log('newlyCreatedCustomerId', newlyCreatedCustomerId) 530 | openpay.customers.transfers.list(newlyCreatedCustomerId, {}, function (error, body, response) { 531 | printLog(response.statusCode, body, error); 532 | assert.equal(response.statusCode, 200, 'Status code == 200'); 533 | done(); 534 | }); 535 | }); 536 | }); 537 | describe('Get transfer', function () { 538 | it('should return statusCode 200', function (done) { 539 | openpay.customers.transfers.get(newlyCreatedCustomerId, newlyCreatedTransactionId, function (error, body, response) { 540 | printLog(response.statusCode, body, error); 541 | assert.equal(response.statusCode, 200, ''); 542 | done(); 543 | }); 544 | }); 545 | }); 546 | }); 547 | 548 | 549 | var testCardPayout = { 550 | "method": "card", 551 | "card": { 552 | "card_number": "4111111111111111", 553 | "holder_name": "Juan P", 554 | "bank_code": "012" 555 | }, 556 | "amount": 1.50, 557 | "description": "Test card payout" 558 | }; 559 | var testBankAccountPayout = { 560 | "method": "bank_account", 561 | "bank_account": { 562 | "clabe": "012298026516924616", 563 | "holder_name": "Juan P" 564 | }, 565 | "amount": 1.50, 566 | "description": "Test bank account payout" 567 | }; 568 | 569 | describe('Testing payouts', function () { 570 | describe('Get all payouts without constraints', function () { 571 | it('should return statusCode 200', function (done) { 572 | openpay.payouts.list({}, function (error, body, response) { 573 | printLog(response.statusCode, body, error); 574 | assert.equal(response.statusCode, 200, 'Status code == 200'); 575 | done(); 576 | }); 577 | }); 578 | }); 579 | if (testCreatePayouts) { 580 | var newlyCreatedTransactionId = ''; 581 | describe('Create payout with new card', function () { 582 | it('should return statusCode 200||201', function (done) { 583 | openpay.payouts.create(testCardPayout, function (error, body, response) { 584 | printLog(response.statusCode, body, error); 585 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 586 | newlyCreatedTransactionId = body.id; 587 | done(); 588 | }); 589 | }); 590 | }); 591 | describe('Get payout', function () { 592 | it('should return statusCode 200', function (done) { 593 | openpay.payouts.get(newlyCreatedTransactionId, function (error, body, response) { 594 | printLog(response.statusCode, body, error); 595 | assert.equal(response.statusCode, 200, ''); 596 | done(); 597 | }); 598 | }); 599 | }); 600 | describe('Create payout with new bank account', function () { 601 | it('should return statusCode 200||201', function (done) { 602 | openpay.payouts.create(testBankAccountPayout, function (error, body, response) { 603 | printLog(response.statusCode, body, error); 604 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 605 | newlyCreatedTransactionId = body.id; 606 | done(); 607 | }); 608 | }); 609 | }); 610 | describe('Create payout with existing card', function () { 611 | it('should return statusCode 200||201', function (done) { 612 | var testExistingCardPayout = { 613 | "method": "card", 614 | "destination_id": newlyCreatedCardId, 615 | "amount": 1.50, 616 | "description": "Test payout with existing card" 617 | }; 618 | openpay.payouts.create(testExistingCardPayout, function (error, body, response) { 619 | printLog(response.statusCode, body, error); 620 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 621 | newlyCreatedTransactionId = body.id; 622 | done(); 623 | }); 624 | }); 625 | }); 626 | describe('Create payout with existing bank account', function () { 627 | it('should return statusCode 200||201', function (done) { 628 | var testExistingBankAccountPayout = { 629 | "method": "bank_account", 630 | "destination_id": testBankAccountId, 631 | "amount": 1.50, 632 | "description": "Test payout with existing bank account" 633 | }; 634 | openpay.payouts.create(testExistingBankAccountPayout, function (error, body, response) { 635 | printLog(response.statusCode, body, error); 636 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 637 | newlyCreatedTransactionId = body.id; 638 | done(); 639 | }); 640 | }); 641 | }); 642 | 643 | var newlyCreatedCustomerTransactionId = ''; 644 | describe('Create customer payout with new card', function () { 645 | it('should return statusCode 200||201', function (done) { 646 | openpay.customers.payouts.create(newlyCreatedCustomerId, testCardPayout, function (error, body, response) { 647 | printLog(response.statusCode, body, error); 648 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 649 | newlyCreatedCustomerTransactionId = body.id; 650 | done(); 651 | }); 652 | }); 653 | }); 654 | describe('Get all customer payouts without constraints', function () { 655 | it('should return statusCode 200', function (done) { 656 | openpay.customers.payouts.list(newlyCreatedCustomerId, {}, function (error, body, response) { 657 | printLog(response.statusCode, body, error); 658 | assert.equal(response.statusCode, 200, 'Status code == 200'); 659 | done(); 660 | }); 661 | }); 662 | }); 663 | describe('Get customer payout', function () { 664 | it('should return statusCode 200', function (done) { 665 | openpay.customers.payouts.get(newlyCreatedCustomerId, newlyCreatedCustomerTransactionId, function (error, body, response) { 666 | printLog(response.statusCode, body, error); 667 | assert.equal(response.statusCode, 200, ''); 668 | done(); 669 | }); 670 | }); 671 | }); 672 | describe('Create customer payout with new bank account', function () { 673 | it('should return statusCode 200||201', function (done) { 674 | openpay.customers.payouts.create(newlyCreatedCustomerId, testBankAccountPayout, function (error, body, response) { 675 | printLog(response.statusCode, body, error); 676 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 677 | newlyCreatedCustomerTransactionId = body.id; 678 | done(); 679 | }); 680 | }); 681 | }); 682 | var testExistingItemPayout = { 683 | "method": "card", 684 | "destination_id": "", 685 | "amount": 1.50, 686 | "description": "Test existing item payout" 687 | }; 688 | describe('Create customer payout with existing card', function () { 689 | it('should return statusCode 200||201', function (done) { 690 | testExistingItemPayout.destination_id = newlyCreatedCustomerCardId; 691 | openpay.customers.payouts.create(newlyCreatedCustomerId, testExistingItemPayout, function (error, body, response) { 692 | printLog(response.statusCode, body, error); 693 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 694 | done(); 695 | }); 696 | }); 697 | }); 698 | describe('Create customer payout with existing bank account', function () { 699 | it('should return statusCode 200||201', function (done) { 700 | testExistingItemPayout.destination_id = newlyCreatedBankAccountId; 701 | openpay.customers.payouts.create(newlyCreatedCustomerId, testExistingItemPayout, function (error, body, response) { 702 | printLog(response.statusCode, body, error); 703 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 704 | done(); 705 | }); 706 | }); 707 | }); 708 | } 709 | }); 710 | 711 | 712 | describe('Testing fees', function () { 713 | describe('Charge fee', function () { 714 | it('should return statusCode 200||201', function (done) { 715 | var testFee = { 716 | "customer_id": newlyCreatedCustomerId, 717 | "amount": 1.50, 718 | "description": "Test fee" 719 | }; 720 | openpay.fees.create(testFee, function (error, body, response) { 721 | printLog(response.statusCode, body, error); 722 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 723 | done(); 724 | }); 725 | }); 726 | }); 727 | describe('Get all fees without constraints', function () { 728 | it('should return statusCode 200', function (done) { 729 | openpay.fees.list({}, function (error, body, response) { 730 | printLog(response.statusCode, body, error); 731 | assert.equal(response.statusCode, 200, 'Status code == 200'); 732 | done(); 733 | }); 734 | }); 735 | }); 736 | }); 737 | 738 | var testPlan = { 739 | "name": "Test plan", 740 | "amount": 15.00, 741 | "repeat_every": "1", 742 | "repeat_unit": "month", 743 | "retry_times": 2, 744 | "status_after_retry": "cancelled", 745 | "trial_days": "30" 746 | }; 747 | var newlyCreatedPlanId = ''; 748 | describe('Testing plans', function () { 749 | describe('Create plan', function () { 750 | it('should return statusCode 200||201', function (done) { 751 | openpay.plans.create(testPlan, function (error, body, response) { 752 | printLog(response.statusCode, body, error); 753 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 754 | newlyCreatedPlanId = body.id; 755 | done(); 756 | }); 757 | }); 758 | }); 759 | describe('Get all plans without constraints', function () { 760 | it('should return statusCode 200', function (done) { 761 | openpay.plans.list(function (error, body, response) { 762 | printLog(response.statusCode, body, error); 763 | assert.equal(response.statusCode, 200, 'Status code == 200'); 764 | done(); 765 | }); 766 | }); 767 | }); 768 | describe('Get plan', function () { 769 | it('should return statusCode 200', function (done) { 770 | openpay.plans.get(newlyCreatedPlanId, function (error, body, response) { 771 | printLog(response.statusCode, body, error); 772 | assert.equal(response.statusCode, 200, ''); 773 | done(); 774 | }); 775 | }); 776 | }); 777 | describe('Update plan', function () { 778 | it('should return statusCode 200', function (done) { 779 | openpay.plans.update(newlyCreatedPlanId, {"name": "Test plan"}, function (error, body, response) { 780 | printLog(response.statusCode, body, error); 781 | assert.equal(response.statusCode, 200, ''); 782 | done(); 783 | }); 784 | }); 785 | }); 786 | describe('Get plan subscriptions', function () { 787 | it('should return statusCode 200', function (done) { 788 | openpay.plans.listSubscriptions(newlyCreatedPlanId, function (error, body, response) { 789 | printLog(response.statusCode, body, error); 790 | assert.equal(response.statusCode, 200, ''); 791 | done(); 792 | }); 793 | }); 794 | }); 795 | }); 796 | 797 | describe('Testing subscriptions', function () { 798 | var newlyCreatedSubscriptionId = ''; 799 | describe('Create subscription', function () { 800 | it('should return statusCode 200||201', function (done) { 801 | var testSubscription = { 802 | "plan_id": newlyCreatedPlanId, 803 | "card_id": newlyCreatedCustomerCardId, 804 | "trial_days": "30" 805 | }; 806 | openpay.customers.subscriptions.create(newlyCreatedCustomerId, testSubscription, function (error, body, response) { 807 | printLog(response.statusCode, body, error); 808 | assert.equal(response.statusCode == 200 || response.statusCode == 201, true, 'Status code == 200'); 809 | newlyCreatedSubscriptionId = body.id; 810 | done(); 811 | }); 812 | }); 813 | }); 814 | describe('Get all subscriptions without constraints', function () { 815 | it('should return statusCode 200', function (done) { 816 | openpay.customers.subscriptions.list(newlyCreatedCustomerId, function (error, body, response) { 817 | printLog(response.statusCode, body, error); 818 | assert.equal(response.statusCode, 200, 'Status code == 200'); 819 | done(); 820 | }); 821 | }); 822 | }); 823 | describe('Get subscription', function () { 824 | it('should return statusCode 200', function (done) { 825 | openpay.customers.subscriptions.get(newlyCreatedCustomerId, newlyCreatedSubscriptionId, function (error, body, response) { 826 | printLog(response.statusCode, body, error); 827 | assert.equal(response.statusCode, 200, ''); 828 | done(); 829 | }); 830 | }); 831 | }); 832 | describe('Update subscription', function () { 833 | it('should return statusCode 200', function (done) { 834 | openpay.customers.subscriptions.update(newlyCreatedCustomerId, newlyCreatedSubscriptionId, {"trial_end_date": "2022-02-11"}, function (error, body, response) { 835 | printLog(response.statusCode, body, error); 836 | assert.equal(response.statusCode, 200, ''); 837 | done(); 838 | }); 839 | }); 840 | }); 841 | describe('Delete subscription', function () { 842 | it('should return statusCode 204', function (done) { 843 | openpay.customers.subscriptions.delete(newlyCreatedCustomerId, newlyCreatedSubscriptionId, function (error, body, response) { 844 | printLog(response.statusCode, body, error); 845 | assert.equal(response.statusCode, 204, ''); 846 | done(); 847 | }); 848 | }); 849 | }); 850 | }); 851 | 852 | 853 | describe('Delete plan', function () { 854 | it('should return statusCode 204', function (done) { 855 | openpay.plans.delete(newlyCreatedPlanId, function (error, body, response) { 856 | printLog(response.statusCode, body, error); 857 | assert.equal(response.statusCode, 204, ''); 858 | done(); 859 | }); 860 | }); 861 | }); 862 | describe('Delete card', function () { 863 | it('should return statusCode 204', function (done) { 864 | openpay.cards.delete(newlyCreatedCardId, function (error, body, response) { 865 | printLog(response.statusCode, body, error); 866 | assert.equal(response.statusCode, 204, ''); 867 | done(); 868 | }); 869 | }); 870 | }); 871 | describe('Delete customer card', function () { 872 | it('should return statusCode 204', function (done) { 873 | openpay.customers.cards.delete(newlyCreatedCustomerId, newlyCreatedCustomerCardId, function (error, body, response) { 874 | printLog(response.statusCode, body, error); 875 | assert.equal(response.statusCode, 204, ''); 876 | done(); 877 | }); 878 | }); 879 | }); 880 | 881 | describe('Delete bankaccount', function () { 882 | it('should return statusCode 204', function (done) { 883 | openpay.customers.bankaccounts.delete(newlyCreatedCustomerId, newlyCreatedBankAccountId, function (error, body, response) { 884 | printLog(response.statusCode, body, error); 885 | assert.equal(response.statusCode, 204, ''); 886 | done(); 887 | }); 888 | }); 889 | }); 890 | 891 | describe('Delete customer', function () { 892 | it('should return statusCode 204', function (done) { 893 | openpay.customers.delete(newlyCreatedCustomerId, function (error, body, response) { 894 | printLog(response.statusCode, body, error); 895 | assert.equal(response.statusCode, 204, ''); 896 | done(); 897 | }); 898 | }); 899 | }); 900 | 901 | }); 902 | 903 | 904 | function printLog(code, body, error) { 905 | if (enableLogging) { 906 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 907 | } 908 | if (code >= 300) { 909 | console.log(' '); 910 | console.log(error); 911 | console.log(' '); 912 | } 913 | } 914 | 915 | function getVerificationCode(url, callback) { 916 | urllib.request(url, function(err, body, res){ 917 | var resCode = res.statusCode; 918 | var error = (resCode!=200 && resCode!=201 && resCode!=204) ? body : null; 919 | var verification_code = null; 920 | console.info('error: ' + error); 921 | if (!error) { 922 | verification_code = body.toString().substring(body.indexOf('verification_code') + 28 , body.indexOf('verification_code') + 28 + 8); 923 | console.info('verification_code: ' + verification_code); 924 | } 925 | callback(error, verification_code); 926 | }); 927 | } 928 | -------------------------------------------------------------------------------- /test/transfers.test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var _ = require('underscore'); 3 | var Openpay = require('../lib/openpay'); 4 | /*Sandbox*/ 5 | var openpay = new Openpay('m1qp3av1ymcfufkuuoah', 'sk_ed05f1de65fa4a67a3d3056a4efa2905'); 6 | openpay.setTimeout(3000) 7 | 8 | var enableLogging = true; 9 | var customerId = ''; 10 | var testCreateCustomer = { 11 | "name": "Juan", 12 | "email": "juan@nonexistantdomain.com" 13 | }; 14 | 15 | describe('Get all transfers with creation filter', function () { 16 | this.timeout(0); 17 | it('should return statusCode 200', function (done) { 18 | var searchParams = { 19 | 'creation[lte]': '2021-01-01', 20 | 'limit': 10000 21 | }; 22 | console.log('timeout:', openpay.timeout) 23 | openpay.customers.create(testCreateCustomer, function (error, body) { 24 | openpay.customers.transfers.list(body.id, searchParams, function (error, body, response) { 25 | console.log('timeout', response.timeout) 26 | printLog(response.statusCode, body, error); 27 | assert.equal(response.statusCode, 200, 'Status code != 400'); 28 | done(); 29 | }); 30 | }); 31 | }) 32 | }); 33 | 34 | function printLog(code, body, error) { 35 | if (enableLogging) { 36 | console.log(code, _.isUndefined(body) || _.isNull(body) ? '' : _.isArray(body) ? _.pluck(body, 'id') : body.id); 37 | } 38 | if (code >= 300) { 39 | console.log(' '); 40 | console.log(error); 41 | console.log(' '); 42 | } 43 | } 44 | --------------------------------------------------------------------------------