├── .gitignore ├── LICENSE ├── README.md ├── lib ├── 2checkout.js └── 2checkout │ ├── account.js │ ├── checkout.js │ ├── error.js │ ├── notification.js │ ├── payments.js │ ├── products.js │ ├── response.js │ ├── sales.js │ └── utils.js ├── package.json └── test ├── account_spec.js ├── checkout_spec.js ├── notification_spec.js ├── payments_spec.js ├── products_spec.js ├── response_spec.js ├── sales_spec.js └── spec_helper.js /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.lock 3 | *.DS_Store 4 | *.swp 5 | *.out 6 | *.idea/* 7 | node_modules/ 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2014 - 2Checkout 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DEPRECATED!!! This SDK can only be used with the legacy 2Checkout platform which is depreciated. Please use [2checkout-node-sdk](https://github.com/2Checkout/2checkout-node-sdk) with the current 2Checkout platform. 2 | ===================== 3 | 4 | Full documentation for each binding is provided in the **[wiki](https://github.com/2Checkout/2checkout-node/wiki)**. 5 | 6 | This library provides developers with a simple set of bindings to the 2Checkout Payment API, Hosted Checkout, Instant Notification Service and Admin API. 7 | 8 | To use, download or clone the repository and install with npm. 9 | 10 | ```shell 11 | git clone https://github.com/craigchristenson/2checkout-node.git 12 | npm install 2checkout-node 13 | ``` 14 | 15 | Then you can require the module and setup the 2Checkout object with a number of options shown below. 16 | 17 | ```javascript 18 | // Require the module 19 | var Twocheckout = require('2checkout-node'); 20 | 21 | // Pass in your private key and seller ID 22 | var tco = new Twocheckout({ 23 | apiUser: "APIuser1817037", // Admin API Username, required for Admin API bindings 24 | apiPass: "APIpass1817037", // Admin API Password, required for Admin API bindings 25 | sellerId: "1817037", // Seller ID, required for all non Admin API bindings 26 | privateKey: "3508079E-5383-44D4-BF69-DC619C0D9811", // Payment API private key, required for checkout.authorize binding 27 | secretWord: "tango" // Secret Word, required for response and notification checks 28 | }); 29 | ``` 30 | 31 | Example Payment API Usage 32 | ----------------- 33 | 34 | *Example Request:* 35 | 36 | ```javascript 37 | // Setup the authorization object 38 | var params = { 39 | "merchantOrderId": "123", 40 | "token": "MWQyYTI0ZmUtNjhiOS00NTIxLTgwY2MtODc3MWRlNmZjY2Jh", 41 | "currency": "USD", 42 | "total": "10.00", 43 | "billingAddr": { 44 | "name": "Testing Tester", 45 | "addrLine1": "123 Test St", 46 | "city": "Columbus", 47 | "state": "Ohio", 48 | "zipCode": "43123", 49 | "country": "USA", 50 | "email": "example@2co.com", 51 | "phoneNumber": "5555555555" 52 | } 53 | }; 54 | 55 | // Make the call using the authorization object and your callback function 56 | tco.checkout.authorize(params, function (error, data) { 57 | if (error) { 58 | console.log(error.message); 59 | } else { 60 | console.log(JSON.stringify(data)); 61 | } 62 | }); 63 | ``` 64 | 65 | *Example Response:* 66 | 67 | ```json 68 | { 69 | "validationErrors": null, 70 | "response": { 71 | "type": "AuthResponse", 72 | "currencyCode": "USD", 73 | "lineItems": [ 74 | { 75 | "description": "", 76 | "duration": "1 Year", 77 | "options": [], 78 | "price": "6.99", 79 | "quantity": "2", 80 | "recurrence": "1 Month", 81 | "startupFee": null, 82 | "productId": "123", 83 | "tangible": "N", 84 | "name": "Demo Item 1", 85 | "type": "product" 86 | }, 87 | { 88 | "description": "", 89 | "duration": null, 90 | "options": [ 91 | { 92 | "optName": "Size", 93 | "optValue": "Large", 94 | "optSurcharge": "1.00" 95 | } 96 | ], 97 | "price": "1.99", 98 | "quantity": "1", 99 | "recurrence": null, 100 | "startupFee": null, 101 | "productId": "", 102 | "tangible": "N", 103 | "name": "Demo Item 2", 104 | "type": "product" 105 | }, 106 | { 107 | "description": "", 108 | "duration": null, 109 | "options": [], 110 | "price": "3.00", 111 | "quantity": "1", 112 | "recurrence": null, 113 | "startupFee": null, 114 | "productId": "", 115 | "tangible": "Y", 116 | "name": "Shipping Fee", 117 | "type": "shipping" 118 | } 119 | ], 120 | "transactionId": "205203115673", 121 | "billingAddr": { 122 | "addrLine1": "123 Test St", 123 | "addrLine2": null, 124 | "city": "Columbus", 125 | "zipCode": "43123", 126 | "phoneNumber": "5555555555", 127 | "phoneExtension": null, 128 | "email": "example@2co.com", 129 | "name": "Testing Tester", 130 | "state": "Ohio", 131 | "country": "USA" 132 | }, 133 | "shippingAddr": { 134 | "addrLine1": "123 Test St", 135 | "addrLine2": "", 136 | "city": "Columbus", 137 | "zipCode": "43123", 138 | "phoneNumber": null, 139 | "phoneExtension": null, 140 | "email": null, 141 | "name": "Testing Tester", 142 | "state": "OH", 143 | "country": "USA" 144 | }, 145 | "merchantOrderId": "123", 146 | "orderNumber": "205203115664", 147 | "recurrentInstallmentId": null, 148 | "responseMsg": "Successfully authorized the provided credit card", 149 | "responseCode": "APPROVED", 150 | "total": "19.97", 151 | "errors": null 152 | }, 153 | "exception": null 154 | } 155 | ``` 156 | 157 | Example Admin API Usage 158 | ----------------- 159 | 160 | *Example Request:* 161 | 162 | ```javascript 163 | tco.sales.retrieve({sale_id: 205203115664}, function (error, data) { 164 | if (error) { 165 | console.log(error); 166 | } else { 167 | console.log(data); 168 | } 169 | }); 170 | ``` 171 | 172 | Example Checkout Usage: 173 | ----------------------- 174 | 175 | *Example Request:* 176 | 177 | ```javascript 178 | // Setup checkout params 179 | var params = { 180 | mode: '2CO', 181 | li_0_name: 'Test Product', 182 | li_0_price: '0.01' 183 | }; 184 | 185 | // Get a URL encoded payment link 186 | var link = tco.checkout.link(params); 187 | ``` 188 | 189 | *Example Response:* 190 | ```javascript 191 | https://www.2checkout.com/checkout/purchase?mode=2CO&li_0_name=Test%20Product&li_0_price=0.01&sid=1817037 192 | ``` 193 | 194 | Example Return Usage: 195 | --------------------- 196 | 197 | *Example Request (Using Express):* 198 | 199 | ```javascript 200 | if (tco.response.valid(request.body), 0.01) { 201 | response.send("Valid"); 202 | } else { 203 | response.send("Invalid"); 204 | } 205 | ``` 206 | 207 | Example INS Notifications Usage: 208 | ------------------ 209 | 210 | *Example Usage (Using Express):* 211 | 212 | ```javascript 213 | if (tco.notification.valid(request.body)) { 214 | response.send("Valid"); 215 | } else { 216 | response.send("Invalid"); 217 | } 218 | ``` 219 | 220 | Exceptions: 221 | ----------- 222 | Errors are returned as the first argument to your callback if they occur. It is best to always check for these before attempting to work with the response. 223 | 224 | *Example* 225 | 226 | ```javascript 227 | tco.checkout.authorize(params, function (error, data) { 228 | if (error) { 229 | console.log(error.message); 230 | } else { 231 | console.log(JSON.stringify(data)); 232 | } 233 | }); 234 | ``` 235 | 236 | Full documentation for each binding is provided in the **[wiki](https://github.com/2Checkout/2checkout-node/wiki)**. 237 | -------------------------------------------------------------------------------- /lib/2checkout.js: -------------------------------------------------------------------------------- 1 | var sales = require('./2checkout/sales'), 2 | products = require('./2checkout/products'), 3 | account = require('./2checkout/account'), 4 | payments = require('./2checkout/payments'), 5 | response = require('./2checkout/response'), 6 | notification = require('./2checkout/notification'), 7 | checkout = require('./2checkout/checkout'); 8 | TwocheckoutError = require('./2checkout/error'); 9 | 10 | var Twocheckout = module.exports = function (options) { 11 | // Setup Options 12 | if (typeof options.privateKey !== 'undefined') { 13 | this.privateKey = options.privateKey; 14 | } 15 | if (typeof options.sellerId !== 'undefined') { 16 | this.sellerId = options.sellerId; 17 | } 18 | if (typeof options.apiUser !== 'undefined') { 19 | this.apiUser = options.apiUser; 20 | } 21 | if (typeof options.apiPass !== 'undefined') { 22 | this.apiPass = options.apiPass; 23 | } 24 | if (typeof options.secretWord !== 'undefined') { 25 | this.secretWord = options.secretWord; 26 | } 27 | if (typeof options.demo !== 'undefined') { 28 | this.demo = options.demo; 29 | } 30 | this.domain = 'https://www.2checkout.com'; 31 | 32 | // Setup Methods 33 | this.sales = new sales(this); 34 | this.products = new products(this); 35 | this.account = new account(this); 36 | this.payments = new payments(this); 37 | this.response = new response(this); 38 | this.notification = new notification(this); 39 | this.checkout = new checkout(this); 40 | 41 | return this; 42 | }; 43 | -------------------------------------------------------------------------------- /lib/2checkout/account.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"); 2 | 3 | var account = module.exports = function (options) { 4 | this.options = options; 5 | }; 6 | 7 | account.prototype.company = function (callback) { 8 | this.options.type = "admin"; 9 | this.options.path = "/api/acct/detail_company_info"; 10 | this.options.method = "GET"; 11 | this.options.payload = {}; 12 | return utils.execute(this.options, callback); 13 | }; 14 | 15 | account.prototype.contact = function (callback) { 16 | this.options.type = "admin"; 17 | this.options.path = "/api/acct/detail_contact_info"; 18 | this.options.method = "GET"; 19 | this.options.payload = {}; 20 | return utils.execute(this.options, callback); 21 | }; -------------------------------------------------------------------------------- /lib/2checkout/checkout.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"); 2 | var qs = require('querystring'); 3 | 4 | var checkout = module.exports = function (options) { 5 | this.options = options; 6 | }; 7 | 8 | checkout.prototype.form = function (params, button) { 9 | html = '
'; 17 | return html; 18 | }; 19 | 20 | checkout.prototype.direct = function (params, button) { 21 | html = ''; 29 | html += ''; 30 | return html; 31 | }; 32 | 33 | checkout.prototype.link = function (params) { 34 | params.sid = this.options.sellerId; 35 | return this.options.domain+'/checkout/purchase?'+qs.stringify(params); 36 | }; 37 | 38 | checkout.prototype.authorize = function (args, callback) { 39 | this.options.type = "payment"; 40 | this.options.path = "/checkout/api/1/"+this.options.sellerId+"/rs/authService"; 41 | this.options.method = "POST"; 42 | this.options.payload = args; 43 | return utils.execute(this.options, callback); 44 | }; -------------------------------------------------------------------------------- /lib/2checkout/error.js: -------------------------------------------------------------------------------- 1 | var util = require('util'); 2 | var TwocheckoutError = module.exports = function(code, message) { 3 | this.name = 'TwocheckoutError'; 4 | this.code = code; 5 | this.message = message; 6 | } 7 | 8 | util.inherits(TwocheckoutError, Error); 9 | 10 | -------------------------------------------------------------------------------- /lib/2checkout/notification.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"), 2 | crypto = require('crypto'); 3 | 4 | var notification = module.exports = function (options) { 5 | this.options = options; 6 | }; 7 | 8 | notification.prototype.valid = function (data) { 9 | return crypto.createHash('md5').update( 10 | data.sale_id + this.options.sellerId + data.invoice_id + this.options.secretWord 11 | ).digest("hex").toUpperCase() == data.md5_hash; 12 | }; -------------------------------------------------------------------------------- /lib/2checkout/payments.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"); 2 | 3 | var payments = module.exports = function (options) { 4 | options.type = 'admin'; 5 | this.options = options; 6 | }; 7 | 8 | payments.prototype.pending = function (callback) { 9 | this.options.path = "/api/acct/detail_pending_payment"; 10 | this.options.method = "GET"; 11 | this.options.payload = {}; 12 | return utils.execute(this.options, callback); 13 | }; 14 | 15 | payments.prototype.list = function (callback) { 16 | this.options.path = "/api/acct/list_payments"; 17 | this.options.method = "GET"; 18 | this.options.payload = {}; 19 | return utils.execute(this.options, callback); 20 | }; -------------------------------------------------------------------------------- /lib/2checkout/products.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"); 2 | 3 | var products = module.exports = function (options) { 4 | this.options = options; 5 | }; 6 | 7 | products.prototype.retrieve = function (args, callback) { 8 | this.options.type = "admin"; 9 | this.options.path = "/api/products/detail_product"; 10 | this.options.method = "GET"; 11 | this.options.payload = args; 12 | return utils.execute(this.options, callback); 13 | }; 14 | 15 | products.prototype.list = function (args, callback) { 16 | this.options.type = "admin"; 17 | this.options.path = "/api/products/list_products"; 18 | this.options.method = "GET"; 19 | this.options.payload = args; 20 | return utils.execute(this.options, callback); 21 | }; 22 | 23 | products.prototype.create = function (args, callback) { 24 | this.options.type = "admin"; 25 | this.options.path = "/api/products/create_product"; 26 | this.options.method = "POST"; 27 | this.options.payload = args; 28 | return utils.execute(this.options, callback); 29 | }; 30 | 31 | products.prototype.update = function (args, callback) { 32 | this.options.type = "admin"; 33 | this.options.path = "/api/products/update_product"; 34 | this.options.method = "POST"; 35 | this.options.payload = args; 36 | return utils.execute(this.options, callback); 37 | }; 38 | 39 | products.prototype.delete = function (args, callback) { 40 | this.options.type = "admin"; 41 | this.options.path = "/api/products/delete_product"; 42 | this.options.method = "POST"; 43 | this.options.payload = args; 44 | return utils.execute(this.options, callback); 45 | }; -------------------------------------------------------------------------------- /lib/2checkout/response.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"), 2 | crypto = require('crypto'); 3 | 4 | var response = module.exports = function (options) { 5 | this.options = options; 6 | }; 7 | 8 | response.prototype.valid = function (data, total) { 9 | data.order_number = this.options.demo === true ? "1" : data.order_number; 10 | return crypto.createHash('md5').update( 11 | this.options.secretWord + this.options.sellerId + data.order_number + total 12 | ).digest("hex").toUpperCase() == data.key; 13 | }; -------------------------------------------------------------------------------- /lib/2checkout/sales.js: -------------------------------------------------------------------------------- 1 | var utils = require("./utils"); 2 | 3 | var sales = module.exports = function (options) { 4 | this.options = options; 5 | }; 6 | 7 | sales.prototype.retrieve = function (args, callback) { 8 | this.options.type = "admin"; 9 | this.options.path = "/api/sales/detail_sale"; 10 | this.options.method = "GET"; 11 | this.options.payload = args; 12 | return utils.execute(this.options, callback); 13 | }; 14 | 15 | sales.prototype.list = function (args, callback) { 16 | this.options.type = "admin"; 17 | this.options.path = "/api/sales/list_sales"; 18 | this.options.method = "GET"; 19 | this.options.payload = args; 20 | return utils.execute(this.options, callback); 21 | }; 22 | 23 | sales.prototype.refund = function (args, callback) { 24 | this.options.type = "admin"; 25 | if (typeof args.lineitem_id !== 'undefined') { 26 | this.options.path = "/api/sales/refund_lineitem"; 27 | } else { 28 | this.options.path = "/api/sales/refund_invoice"; 29 | } 30 | this.options.method = "POST"; 31 | this.options.payload = args; 32 | return utils.execute(this.options, callback); 33 | }; 34 | 35 | sales.prototype.ship = function (args, callback) { 36 | this.options.type = "admin"; 37 | this.options.path = "/api/sales/mark_shipped"; 38 | this.options.method = "POST"; 39 | this.options.payload = args; 40 | return utils.execute(this.options, callback); 41 | }; 42 | 43 | sales.prototype.comment = function (args, callback) { 44 | this.options.type = "admin"; 45 | this.options.path = "/api/sales/create_comment"; 46 | this.options.method = "POST"; 47 | this.options.payload = args; 48 | return utils.execute(this.options, callback); 49 | }; 50 | 51 | sales.prototype.stop = function (args, callback) { 52 | this.options.type = "admin"; 53 | this.options.path = "/api/sales/stop_lineitem_recurring"; 54 | this.options.method = "POST"; 55 | this.options.payload = args; 56 | return utils.execute(this.options, callback); 57 | }; 58 | -------------------------------------------------------------------------------- /lib/2checkout/utils.js: -------------------------------------------------------------------------------- 1 | var TwocheckoutError = require('./error'); 2 | const got = require('got'); 3 | exports.execute = function (args, callback) { 4 | var options = { 5 | method: args.method, 6 | responseType: 'json', 7 | prefixUrl: 'https://www.2checkout.com', 8 | }; 9 | 10 | if (args.type === "admin") { 11 | options.username = args.apiUser; 12 | options.password = args.apiPass; 13 | 14 | if(args.method === "POST") { 15 | options.form = args.payload; 16 | } else { 17 | options.searchParams = args.payload; 18 | } 19 | } else { 20 | args.payload.privateKey = args.privateKey; 21 | args.payload.sellerId = args.sellerId; 22 | options.body = JSON.stringify(args.payload); 23 | } 24 | 25 | let path = (args.path.charAt(0) === '/') ? args.path.substr(1) : args.path; 26 | (async () => { 27 | try { 28 | const response = await got(path, options); 29 | callback(null, response.body); 30 | } catch (error) { 31 | const parsedResponse = typeof error.response !== 'undefined' ? error.response.body : null; 32 | if (parsedResponse) { 33 | if (args.type === "admin") { 34 | if (parsedResponse.errors) { 35 | callback(new TwocheckoutError(parsedResponse.errors[0].code, parsedResponse.errors[0].message)); 36 | } 37 | } else { 38 | if (parsedResponse.exception) { 39 | callback(new TwocheckoutError(parsedResponse.exception.errorCode, parsedResponse.exception.errorMsg)); 40 | } 41 | } 42 | } else { 43 | callback(new TwocheckoutError('500', 'Error parsing JSON response from 2Checkout API.')); 44 | } 45 | } 46 | })(); 47 | }; 48 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "2checkout-node", 3 | "version": "2.0.0", 4 | "description": "A node.js wrapper for the 2Checkout Payment API.", 5 | "main": "lib/2checkout.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "http://github.com/2Checkout/2checkout-node.git" 9 | }, 10 | "engines": { 11 | "node": ">=10.19.0" 12 | }, 13 | "devDependencies": { 14 | "mocha": "^1.13.0" 15 | }, 16 | "dependencies": { 17 | "got": "11.*" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/account_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | 5 | describe('account', function(){ 6 | describe('company()', function(){ 7 | it('should return a successful response or error', function(done){ 8 | tco.account.company(function (error, data) { 9 | if (error) { 10 | assert.ok(error.code); 11 | assert.ok(error.message); 12 | } else { 13 | assert.ok(data.vendor_company_info); 14 | assert.ok(data.vendor_company_info.vendor_id); 15 | } 16 | done(); 17 | }); 18 | }); 19 | }); 20 | 21 | describe('contact', function(){ 22 | it('should return a successful response or error', function(done){ 23 | tco.account.contact(function (error, data) { 24 | if (error) { 25 | assert.ok(error.code); 26 | assert.ok(error.message); 27 | } else { 28 | assert.ok(data.vendor_contact_info); 29 | } 30 | done(); 31 | }); 32 | }); 33 | }); 34 | }); -------------------------------------------------------------------------------- /test/checkout_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | describe('checkout', function(){ 5 | describe('authorize()', function(){ 6 | it('should return a success or unauthorized response', function(done){ 7 | tco.checkout.authorize(authorize, function (error, data) { 8 | if (error) { 9 | return assert.equal("Unauthorized", error.message); 10 | } else { 11 | return assert.equal("APPROVED", data.response.responseCode); 12 | } 13 | done(); 14 | }); 15 | }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /test/notification_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | describe('notification', function(){ 5 | describe('valid()', function(){ 6 | it('should return true', function(){ 7 | assert.ok(tco.notification.valid(notification)); 8 | }); 9 | }); 10 | }); -------------------------------------------------------------------------------- /test/payments_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | 5 | describe('payments', function(){ 6 | describe('pending()', function(){ 7 | it('should return a successful response', function(done){ 8 | tco.payments.pending(function (error, data) { 9 | assert.ok(data.payment); 10 | done(); 11 | }); 12 | }); 13 | }); 14 | 15 | describe('list()', function(){ 16 | it('should return a successful response', function(done){ 17 | tco.payments.list(function (error, data) { 18 | assert.ok(data.payments); 19 | done(); 20 | }); 21 | }); 22 | }); 23 | }); -------------------------------------------------------------------------------- /test/products_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | 5 | describe('products', function(){ 6 | describe('list()', function(){ 7 | it('should return a successful response', function(done){ 8 | tco.products.list(list, function (error, data) { 9 | assert.equal(2, data.products.length); 10 | done(); 11 | }); 12 | }); 13 | }); 14 | 15 | describe('create()', function(){ 16 | it('should return a successful response', function(done){ 17 | tco.products.create(product_create, function (error, data) { 18 | assert.ok(data.product_id); 19 | product_data.product_id = data.product_id; 20 | product_update.product_id = data.product_id; 21 | done(); 22 | }); 23 | }); 24 | }); 25 | 26 | describe('retrieve()', function(){ 27 | it('should return a successful response', function(done){ 28 | tco.products.retrieve(product_data, function (error, data) { 29 | assert.equal(product_data.product_id, data.product.product_id); 30 | done(); 31 | }); 32 | }); 33 | }); 34 | 35 | describe('update()', function(){ 36 | it('should return a successful response', function(done){ 37 | tco.products.update(product_update, function (error, data) { 38 | assert.equal("OK", data.response_code); 39 | done(); 40 | }); 41 | }); 42 | }); 43 | 44 | describe('delete()', function(){ 45 | it('should return a successful response', function(done){ 46 | tco.products.delete(product_update, function (error, data) { 47 | assert.equal("OK", data.response_code); 48 | done(); 49 | }); 50 | }); 51 | }); 52 | 53 | }); 54 | -------------------------------------------------------------------------------- /test/response_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | describe('response', function(){ 5 | describe('valid()', function(){ 6 | it('should return true', function(){ 7 | assert.ok(tco.response.valid(response, "0.01")); 8 | }); 9 | }); 10 | }); -------------------------------------------------------------------------------- /test/sales_spec.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"), 2 | tco = require("./spec_helper"); 3 | 4 | 5 | describe('sales', function(){ 6 | describe('retrieve()', function(){ 7 | it('should return a successful response or error', function(done){ 8 | tco.sales.retrieve(sale_retrieve, function (error, data) { 9 | if (error) { 10 | assert.ok(error.code); 11 | assert.ok(error.message); 12 | } else { 13 | assert.equal("OK", data.response_code); 14 | assert.equal(sale_retrieve.sale_id, data.sale.sale_id); 15 | } 16 | done(); 17 | }); 18 | }); 19 | }); 20 | 21 | describe('list()', function(){ 22 | it('should return a successful response or error', function(done){ 23 | tco.sales.list(list, function (error, data) { 24 | if (error) { 25 | assert.ok(error.code); 26 | assert.ok(error.message); 27 | } else { 28 | assert.equal("OK", data.response_code); 29 | assert.equal(list.pagesize, data.sale_summary.length); 30 | } 31 | done(); 32 | }); 33 | }); 34 | }); 35 | 36 | describe('refund()', function(){ 37 | it('should return a successful response or error', function(done){ 38 | tco.sales.refund(sale_refund, function (error, data) { 39 | if (error) { 40 | assert.ok(error.code); 41 | assert.ok(error.message); 42 | } else { 43 | assert.equal("OK", data.response_code); 44 | } 45 | done(); 46 | }); 47 | }); 48 | }); 49 | 50 | describe('stop()', function(){ 51 | it('should return a successful response or error', function(done){ 52 | tco.sales.stop(sale_stop, function (error, data) { 53 | if (error) { 54 | assert.ok(error.code); 55 | assert.ok(error.message); 56 | } else { 57 | assert.equal("OK", data.response_code); 58 | } 59 | done(); 60 | }); 61 | }); 62 | }); 63 | 64 | describe('ship()', function(){ 65 | it('should return a successful response or error', function(done){ 66 | tco.sales.ship(sale_ship, function (error, data) { 67 | if (error) { 68 | assert.ok(error.code); 69 | assert.ok(error.message); 70 | } else { 71 | assert.equal("OK", data.response_code); 72 | } 73 | done(); 74 | }); 75 | }); 76 | }); 77 | 78 | 79 | describe('comment()', function(){ 80 | it('should return a successful response or error', function(done){ 81 | tco.sales.comment(sale_comment, function (error, data) { 82 | if (error) { 83 | assert.ok(error.code); 84 | assert.ok(error.message); 85 | } else { 86 | assert.equal("OK", data.response_code); 87 | } 88 | done(); 89 | }); 90 | }); 91 | }); 92 | }); 93 | -------------------------------------------------------------------------------- /test/spec_helper.js: -------------------------------------------------------------------------------- 1 | var Twocheckout = require("../lib/2checkout.js"); 2 | 3 | var tco = module.exports = new Twocheckout({ 4 | apiUser: "api_username", 5 | apiPass: "api_password", 6 | sellerId: "seller_id", 7 | privateKey: "private_key", 8 | secretWord: "secret_word", 9 | demo: true, 10 | }); 11 | 12 | //global data 13 | list = { 14 | pagesize: "2" 15 | }; 16 | 17 | //sale data 18 | sale_retrieve = { 19 | sale_id: "250342762742" 20 | }; 21 | sale_refund = { 22 | sale_id: "250342762742", 23 | comment: "test", 24 | category: "5" 25 | }; 26 | sale_ship = { 27 | sale_id: "250342762742", 28 | tracking_number: "123" 29 | }; 30 | sale_reauth = { 31 | sale_id: "250342762742" 32 | }; 33 | sale_stop = { 34 | lineitem_id: "4834917634" 35 | }; 36 | sale_comment = { 37 | sale_id: "250342762742", 38 | sale_comment: "nodejs test" 39 | }; 40 | 41 | //product data 42 | product_create = { 43 | name: "test product", 44 | price: "0.01" 45 | }; 46 | product_update = { 47 | price: "1.00", 48 | product_id: 123 49 | }; 50 | product_data = { 51 | product_id: 123 52 | }; 53 | 54 | //notification data 55 | notification = { 56 | invoice_id: "4632527490", 57 | md5_hash: "4FB7CD1CD57BBEFCCA462F3DE823C50A", 58 | sale_id: "4632527448" 59 | }; 60 | 61 | //response data 62 | response = { 63 | key: '7AB926D469648F3305AE361D5BD2C3CB', 64 | order_number: '4632527448' 65 | }; 66 | 67 | //authorize data 68 | authorize = { 69 | "merchantOrderId": "123", 70 | "token": "MWQyYTI0ZmUtNjhiOS00NTIxLTgwY2MtODc3MWRlNmZjY2Jh", 71 | "currency": "USD", 72 | "total": "10.00", 73 | "demo": true, 74 | "billingAddr": { 75 | "name": "John Doe", 76 | "addrLine1": "123 Test St", 77 | "city": "Columbus", 78 | "state": "Ohio", 79 | "zipCode": "43123", 80 | "country": "USA", 81 | "email": "example@2co.com", 82 | "phoneNumber": "5555555555" 83 | } 84 | }; 85 | 86 | //checkout params 87 | params = { 88 | mode: '2CO', 89 | li_0_name: 'Test Product', 90 | li_0_price: '0.01', 91 | card_holder_name: 'John Doe', 92 | email: 'tester@2co.com', 93 | street_address: '123 test st', 94 | city: 'Columbus', 95 | state: 'Ohio', 96 | zip: '43123', 97 | country: 'USA' 98 | }; 99 | --------------------------------------------------------------------------------