tco-node-api
v0.0.1
Published
A node.js wrapper for the 2Checkout API V2.
Downloads
5
Readme
2Checkout APIv2 Node Library
Requirements
- Node v0.8+
- request v2.0+
Setup
$ npm install
Using the bindings
Require the dependency.
var tco = require('twocheckout');
Setup your credentials:
tco.config(SELLER_ID, PRIVATE_KEY, BOOL(SANDBOX)); // pass true as third argument to use the sandbox
Sales
Create
var params = {
amount: 1.00,
merchant_order_id: '123',
auth_only: true,
customer: {
phone: '555-555-5556',
currency: 'USD',
lang: 'en',
email: '[email protected]',
payment_method: {
credit_card: {
number: '4111111111111111',
exp_month: 12,
exp_year: 2019,
cvv: '123'
},
address: {
name: 'Testing Tester',
address_1: '123 Test St',
address_2: 'the attic',
city: 'Columbus',
state: 'OH',
country_code: 'US',
postal_code: '43123'
}
}
}
};
tco.sales.create(params, function (error, sale) {
console.log(sale.id); //sale_id
console.log(sale.invoices[0].id); //invoice_id
});
Find
tco.sales.find(SALE_ID, function (error, sale) {
console.log(sale.id); //sale_id
console.log(sale.invoices[0].id); //invoice_id
});
List
params = {page: 1, page_size: 5}
tco.sales.list(params, function (error, data) {
console.log(data.sales[0].id); // first sale_id
console.log(data.pagenation.next); // next page
});
Invoices
Find
tco.invoices.find(INVOICE_ID, function (error, invoice) {
console.log(invoice.id); // invoice_id
});
List
params = {page: 1, page_size: 5}
tco.invoices.list(null, function (error, data) {
console.log(data.invoices[0]);
});
Capture
tco.sales.capture(INVOICE_ID, function (error, invoice) {
console.log(invoice.id); // invoice_id
console.log(invoice.needs_captured); // false
});
Refund
invoice = twocheckout.Invoice.find(INVOICE_ID) # twocheckout.invoice.Invoice
params = {
amount: 1.00,
currency_type: "vendor",
comment: "Refund Issued"
}
tco.invoices.refund(sale.invoices[0].id, params,function (error, invoice) {
console.log(invoice.id); // invoice_id
console.log(invoice.refunds[0].id); // refund_id
});
Customers
Create
params = {
phone: '555-555-5556',
currency: 'USD',
lang: 'en',
email: '[email protected]',
payment_method: {
credit_card: {
number: '4111111111111111',
exp_month: 12,
exp_year: 2019,
cvv: '123'
},
address: {
name: 'Testing Tester',
address_1: '123 Test St',
address_2: 'the attic',
city: 'Columbus',
state: 'OH',
country_code: 'US',
postal_code: '43123'
}
}
};
tco.customers.create(params, function (error, customer) {
console.log(customer.id); // customer_id
console.log(customer.payment_methods[0].id); // payment_method_id
});
Find
tco.customers.find(CUSTOMER_ID, function (error, customer) {
console.log(customer.id); // customer_id
console.log(customer.payment_methods[0].id); // payment_method_id
});
List
params = {page: 1, page_size: 5}
tco.customers.list(params, function (error, data) {
console.log(data.customers[0].id); // first customer_id
});
Update
tco.customers.update(CUSTOMER_ID, {email: "[email protected]"},function (error, customer) {
console.log(customer.id); // customer_id
});
Delete
tco.customers.delete(CUSTOMER_ID,function (error, result) {
console.log(result.code); // OK
});
Payment Methods
Create
params = {
credit_card: {
number: '4111111111111111',
exp_month: 12,
exp_year: 2019,
cvv: '123'
},
address: {
name: 'Testing Tester',
address_1: '123 Test St',
address_2: 'the attic',
city: 'Columbus',
state: 'OH',
country_code: 'US',
postal_code: '43123'
}
}
tco.payment_methods.create(CUSTOMER_ID, params, function (error, payment_method) {
console.log(payment_method.id); // payment_method_id
console.log(payment_method.brand); // VS
});
Find
tco.payment_methods.find(CUSTOMER_ID, PAYMENT_METHOD_ID, function (error, payment_method) {
console.log(payment_method.id); // payment_method_id
console.log(payment_method.brand); // VS
});
List
tco.payment_methods.list(CUSTOMER_ID, null, function (error, data) {
console.log(data.payment_methods[0].id); //first payment_method_id
});
Set Default
tco.payment_methods.default(CUSTOMER_ID, PAYMENT_METHOD_ID, function (error, payment_method) {
console.log(payment_method.default); //true
});
Delete
tco.payment_methods.delete(CUSTOMER_ID, PAYMENT_METHOD_ID, function (error, result) {
console.log(result.code); // OK
});
Subscriptions
Subscriptions (recurring items) can be created when creating a sale by passing a recurrence and optionally a duration.
Stop
tco.subscriptions.stop(SUBSCRIPTION_ID, function (error, subscription) {
console.log(subscription.id); // subscription_id
console.log(subscription.active) // false
});
Handling Exceptions
Errors will be thrown if the request is not successful. You should always check the error object in your callback so that you can cleanly handle exceptions.
tco.sales.create(params, function (error, sale) {
if(data === undefined) {
console.log(error.message)
} else {
console.log(data)
}
});
Running the tests
$ mocha --timeout 10000