https://github.com/bendrucker/stripe-as-promised
Wrap Stripe.js async methods to return promises
https://github.com/bendrucker/stripe-as-promised
Last synced: about 1 year ago
JSON representation
Wrap Stripe.js async methods to return promises
- Host: GitHub
- URL: https://github.com/bendrucker/stripe-as-promised
- Owner: bendrucker
- License: mit
- Created: 2015-04-10T14:47:15.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2020-08-05T21:05:58.000Z (almost 6 years ago)
- Last Synced: 2025-04-10T08:14:06.912Z (over 1 year ago)
- Language: JavaScript
- Size: 17.6 KB
- Stars: 15
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stripe-as-promised [](https://travis-ci.org/bendrucker/stripe-as-promised)
> Wrap [Stripe.js](https://stripe.com/docs/stripe.js)'s asynchronous methods to return promises instead of calling callbacks.
## Installing
```sh
# 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:
* [`card.createToken`](https://stripe.com/docs/stripe.js#card-createToken)
* [`bankAccount.createToken`](https://stripe.com/docs/stripe.js#bank-account-createToken)
* [`piiData.createToken`](https://stripe.com/docs/stripe.js?#pii-data-createToken)
* [`bitcoinReceiver.createReceiver`](https://stripe.com/docs/stripe.js#bitcoinreceiver-createreceiver)
* [`bitcoinReceiver.pollReceiver`](https://stripe.com/docs/stripe.js#bitcoinreceiver-pollreceiver)
* `bitcoinReceiver.getReceiver` (undocumented)
## Usage
### Example
Below is an abbreviated version of Stripe's [documented example for creating a token](https://stripe.com/docs/stripe.js#collecting-card-details):
```js
// 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:
```js
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:
```js
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](http://bendrucker.me)