stripe-as-promised

2.1.1 • Public • Published

stripe-as-promised Build Status

Wrap Stripe.js's asynchronous methods to return promises instead of calling callbacks.

Installing

# npm 
$ npm install stripe-as-promised

API

stripeAsPromised(Stripe, Promise) -> promisifedStripe

Stripe

Required
Type: function

The Stripe.js library

Promise

Required
Type: function

A Promise constructor

The returned promisified object promisifes the following methods in addition to exposing utility methods:

Usage

Example

Below is an abbreviated version of Stripe's documented example for creating a token:

// card === {number: '42...', ...}
Stripe.card.createToken(card, stripeResponseHandler);
 
function stripeResponseHandler(status, token) {
  if (token.error) {
    console.error('Tokenization failed');
  } else {
    console.log('Created token', token.id);
  }
}

The same logic with stripe-as-promised would be written as:

var stripe = stripeAsPromised(Stripe, Promise);
 
stripe.card.createToken(card)
  .then(function (token) {
    console.log('Created token', token.id);
  })
  .catch(function (err) {
    console.error(err);
  });

Bitcoin

For handling bitcoin transactions, you'll probably want to avoid using the pollReceiver method as-is. cancelReceiverPoll does not notify the callback passed to pollReceiver of the cancellation, so the following code could result in a promise that never resolves:

stripe.bitcoinReceiver.createReceiver(payment)
  .then(function (receiver) {
    return stripe.bitcoinReceiver.pollReceiver(receiver.id);
  })
  .then(function (receiver) {
    console.log('Payment received!');
  })
  .catch(function (err) {
    console.error('Payment error', err);
  });

If the receiver is never filled, neither statement is printed. In your application, you'll probably want to implement your own polling implementation that treats cancellations as errors that can be caught and handled downstream.

License

MIT © Ben Drucker

Package Sidebar

Install

npm i stripe-as-promised

Weekly Downloads

828

Version

2.1.1

License

MIT

Last publish

Collaborators

  • bendrucker