Giter Site home page Giter Site logo

stripe-node's Introduction

Stripe Node.js Library

Version Build Status Downloads Try on RunKit

The Stripe Node library provides convenient access to the Stripe API from applications written in server-side JavaScript.

Please keep in mind that this package is for use with server-side Node that uses Stripe secret keys. To maintain PCI compliance, tokenization of credit card information should always be done with Stripe.js on the client side. This package should not be used for that purpose.

Documentation

See the Node API docs.

Installation

Install the package with:

npm install stripe --save

Usage

The package needs to be configured with your account's secret key which is available in your Stripe Dashboard. Require it with the key's value:

var stripe = require('stripe')('sk_test_...');

stripe.customers.create(
  { email: '[email protected]' },
  function(err, customer) {
    err; // null if no error occurred
    customer; // the created customer object
  }
);

On ES6, this looks more like:

import stripePackage from 'stripe';
const stripe = stripePackage('sk_test_...');

Using Promises

Every method returns a chainable promise which can be used instead of a regular callback:

// Create a new customer and then a new charge for that customer:
stripe.customers.create({
  email: '[email protected]'
}).then(function(customer){
  return stripe.customers.createSource(customer.id, {
    source: {
       object: 'card',
       exp_month: 10,
       exp_year: 2018,
       number: '4242 4242 4242 4242',
       cvc: 100
    }
  });
}).then(function(source) {
  return stripe.charges.create({
    amount: 1600,
    currency: 'usd',
    customer: source.customer
  });
}).then(function(charge) {
  // New charge created on a new customer
}).catch(function(err) {
  // Deal with an error
});

Configuring Timeout

Request timeout is configurable (the default is Node's default of 120 seconds):

stripe.setTimeout(20000); // in ms (this is 20 seconds)

Configuring For Connect

A per-request Stripe-Account header for use with Stripe Connect can be added to any method:

// Retrieve the balance for a connected account:
stripe.balance.retrieve({
  stripe_account: "acct_foo"
}).then(function(balance) {
  // The balance object for the connected account
}).catch(function(err) {
  // Error
});

Configuring a Proxy

An https-proxy-agent can be configured with setHttpAgent.

To use stripe behind a proxy you can pass to sdk:

if (process.env.http_proxy) {
  const ProxyAgent = require('https-proxy-agent');
  stripe.setHttpAgent(new ProxyAgent(process.env.http_proxy));
}

Examining Responses

Some information about the response which generated a resource is available with the lastResponse property:

charge.lastResponse.requestId // see: https://stripe.com/docs/api/node#request_ids
charge.lastResponse.statusCode

More Information

Development

Run all tests:

$ npm install
$ npm test

Run a single test suite:

$ npm run mocha -- test/Error.spec.js

Run a single test (case sensitive):

$ npm run mocha -- test/Error.spec.js --grep 'Populates with type'

If you wish, you may run tests using your Stripe Test API key by setting the environment variable STRIPE_TEST_API_KEY before running the tests:

$ export STRIPE_TEST_API_KEY='sk_test....'
$ npm test

stripe-node's People

Contributors

brandur avatar abh avatar michelle-stripe avatar kyleconroy avatar kjc-stripe avatar slexaxton avatar brandur-stripe avatar stephen avatar ob-stripe avatar rasmus-stripe avatar mlahey-stripe avatar padolsey avatar 2sidedfigure avatar michelle avatar kc-dot-io avatar wangjohn avatar pchw avatar copleykj avatar cupcait avatar tejasmanohar avatar stan-stripe avatar matthewarkin avatar jim-stripe avatar amber-stripe avatar bkrausz avatar thatemilio avatar fred-stripe avatar tanguylebarzic avatar xavi- avatar shale-stripe avatar

Watchers

Lin Wang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.