coinbase-commerce-test

1.0.8 • Public • Published

CircleCI

Coinbase Commerse

The official Node.js library for the Coinbase Commerse API.

Table of contents

Node.js versions

Node.js v0.10.48 and above are supported and tested.

Documentation

For more details visit Coinbase API docs.

To start using library, you need to register on Commerce SignUp. And get your API_KEY from user settings.

Next init a Client for interacting with the API. Method accepts 4 params apiKey, baseUrl, apiVersion, timeout only apikey is mandatory. You can set params later as in example:

var coinbase = require('coinbase-commerse-test');
var Client = coinbase.Client;
 
var clientObj = Client.init('API_KEY');
clientObj.setTimeout(3000);

You can call list, all, create, retrieve, updateById, deleteById methods from an API resource classes as well as save, delete, insert, update methods from API resource class instances.

Each API method returns an ApiResource which representing the JSON response from the API. Also when the response data is parsed into objects, the appropriate ApiResource subclasses will be used automatically.

Client support Common API Errors and Warnings handling. All errors occuring during interaction with the API will be raised as exceptions.

Error Status Code
APIError *
InvalidRequestError 400
ParamRequiredError 400
ValidationError 400
AuthenticationError 401
ResourceNotFoundError 404
RateLimitExceededError 429
InternalServerError 500
ServiceUnavailableError 503

Installation

Install with npm:

npm install coinbase-commerce --save

Usage

var coinbase = require('coinbase-commerse-test');
var Client = coinbase.Client;
 
Client.init('API_KEY');

Checkouts

Checkouts API docs More examples how to use charges in the examples/resources/checkout.js file

Load checkout resource class

var coinbase = require('coinbase-commerce-test');
var Checkout = coinbase.Checkout;

Retrieve

Checkout.retrieve(<checkout_id>, function (error, callback) {
  console.log(error);
  console.log(callback);
});

Create

var checkoutData = {
    "name": 'The Sovereign Individual',
    "description": 'Mastering the Transition to the Information Age',
    "pricing_type": 'fixed_price',
    "local_price": {
        "amount": "100.00",
        "currency": "USD"
    },
    "requested_info": ["name", "email"]
};
Checkout.create(checkoutData, function (error, callback) {
  console.log(error);
  console.log(callback);
});
 
// or
 
var checkoutObj = new Checkout();
 
checkoutObj.name = 'The Sovereign Individual';
checkoutObj.description = 'Mastering the Transition to the Information Age';
checkoutObj.pricing_type = 'fixed_price';
checkoutObj.local_price = {
    "amount": "100.00",
    "currency": "USD"
};
checkoutObj.requested_info = ["name", "email"];
 
checkoutObj.save(function (error, callback) {
  console.log(error);
  console.log(callback);
});

Update

var checkoutObj = new Checkout();
 
checkoutObj.id = <checkout_id>;
checkoutObj.name = 'new name';
 
checkoutObj.save(function (error, callback) {
  console.log(error);
  console.log(callback);
});
// or
var newParams = {
    "name": 'New name'
};
 
Checkout.updateById(<checkout_id>, newParams, function (error, response) {
  console.log(error);
  console.log(response);
});

Delete

var checkoutObj = new Checkout();
 
checkoutObj.id = <checkout_id>;
checkoutObj.delete(function (error, response) {
 console.log(error);
 console.log(response);
});
 
// or
 
Checkout.deleteById(<checkout_id>, function (error, response) {
 console.log(error);
 console.log(response);  
});

List

var params = {
    limit: 2,
    order: 'desc'
};
 
Checkout.list(params, function (error, list, pagination) {
  console.log(error);
  console.log(list);
  console.log(pagination);
});

Get all checkouts

var params = {
  order: 'desc'  
};
 
Checkout.all(params, function (error, list) {
  console.log(error);
  console.log(list);
});
 

Charges

Charges API docs More examples how to use charges in the examples/resources/charge.js file

Load charge resource class

var coinbase = require('coinbase-commerce-test');
var Charge = coinbase.Charge;

Retrieve

Charge.retrieve(<charge_id>, function (error, response) {
  console.log(error);
  console.log(response);
});

Create

var chargeData = {
    "name": "The Sovereign Individual",
    "description": "Mastering the Transition to the Information Age",
    "local_price": {
        "amount": "100.00",
        "currency": "USD"
    },
    "pricing_type": "fixed_price"
 
}
Charge.create(chargeData, function (error, response) {
  console.log(error);
  console.log(response);
});
 
// or
var chargeObj = new Charge();
 
chargeObj.name = "The Sovereign Individual";
chargeObj.description = "Mastering the Transition to the Information Age";
chargeObj.local_price = {
    "amount": "100.00",
    "currency": "USD"
};
chargeObj.pricing_type = "fixed_price";
chargeObj.save(function (error, response) {
  console.log(error);
  console.log(response);
});

List

Charge.list({}, function (error, list, pagination) {
  console.log(error);
  console.log(list);
  console.log(pagination);
});

Get all charnges

Charge.all({}, function (error, list) {
  console.log(error);
  console.log(list);
});

Events

Events API Docs More examples how to use charges in the examples/resources/event.js file

Load event resource class

var coinbase = require('coinbase-commerce-test');
var Event = coinbase.Event;

Retrieve

Event.retrieve(<event_id>, function (error, response) {
    console.log(error);
    console.log(response);
});

List

Event.list({}, function (error, list, pagination) {
  console.log(error);
  console.log(list);
  console.log(pagination);
});

Get all events

Event.all({}, function (error, list) {
  console.log(error);
  console.log(list);
});

Using Promises

Every method returns a promise. You can use promise instead of callback

// Try create and retrieve created charge
var chargeObj = new Charge({
    "description": "Mastering the Transition to the Information Age",
    "metadata": {
        "customer_id": "id_1005",
        "customer_name": "Satoshi Nakamoto"
    },
    "name": "Test Name",
    "payments": [],
    "pricing_type": "no_price"
});
 
chargeObj.save().then(function (response) {
    console.log('Created charge(promise)');
    console.log(response);
 
    if (response && response.id) {
        return Charge.retrieve(response.id);
    }
}).then(function (response) {
    console.log('Retrieved charge(promise)');
    console.log(response);
}).catch(function (error) {
    console.log(error);
});

Webhooks

Coinbase Commerce signs the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can find a simple example of how to use this with Express in the examples/webhook folder

Verify Signature header

var  isVerified = Webhook.verifyWebhookSignature(signature, body, sharedSecret)

Testing and Contributing

Any and all contributions are welcome! The process is simple: fork this repo, make your changes, run the test suite, and submit a pull request. To run the tests, clone the repository and then run all tests:

npm install
npm run test

License

MIT

Package Sidebar

Install

npm i coinbase-commerce-test

Weekly Downloads

4

Version

1.0.8

License

MIT

Unpacked Size

35.8 kB

Total Files

35

Last publish

Collaborators

  • imeleshko