stripe-as-promised

Wrap Stripe.js async methods to return promises

stripe-as-promised

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 pollReceivermethod as-is. cancelReceiverPolldoes not notify the callback passed to pollReceiverof 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

HomePage

https://github.com/bendrucker/stripe-as-promised

Repository

https://github.com/bendrucker/stripe-as-promised.git


上一篇:stripe-errback
下一篇:rollup-plugin-remap

相关推荐

  • vigour-fs-promised

    vigourfs, but methods return promises vigourfspromised vigourfs, but methods return promises Usa...

    1 年前
  • supertest-promised

    Make supertest promisecompatible What? This is a simple module that converts supertest's API to r...

    2 年前
  • supertest-as-promised

    Supercharge supertest with a promise interface supertestaspromised SuperTest as Promi...

    1 年前
  • stripe-errback

    Wrap Stripe.js async methods to use nodestyle callbacks stripeerrback Build Status(https://travisc...

    14 天前
  • stripe-debug

    npm install for Stripe.js — for testing only stripedebug for Stripe.js — for testing only. Downlo...

    14 天前
  • stripe-dat

    track stripe charges via dat stripedat track stripe charges via dat npmnpmimagenpmurl travistrav...

    8 个月前
  • stripe-cohort

    Query Stripe customers by cohort stripecohort Create Stripe(https://stripe.com) customer cohor...

    4 个月前
  • stripe-charges

    Query Stripe charges stripecharges A Stripe(https://stripe.com) charges API for node get a t...

    4 个月前
  • stripe

    Stripe API wrapper Stripe Node.js Library Version(https://img.shields.io/npm/v/stripe.svg)(https:...

    1 年前
  • sinon-as-promised

    Sugar methods for using sinon.js stubs with promises sinonaspromised Build Status(https://travisci....

    1 年前

官方社区

扫码加入 JavaScript 社区