What is stripe?
The Stripe npm package is a library that provides a powerful and easy-to-use interface to the Stripe API, allowing developers to integrate payment processing into their Node.js applications. It supports a wide range of payment operations, from charging credit cards to managing subscriptions and handling disputes.
What are stripe's main functionalities?
Charging a Credit Card
This feature allows you to create a charge on a credit card. The amount is specified in the smallest currency unit (e.g., cents for USD).
stripe.charges.create({
amount: 2000,
currency: 'usd',
source: 'tok_amex',
description: 'Charge for jenny.rosen@example.com'
}).then(function(charge) {
// asynchronously called
});
Creating a Customer
This feature enables you to create a new customer object, which can be used for recurring charges and tracking multiple charges that are associated with the same customer.
stripe.customers.create({
email: 'customer@example.com'
}).then(function(customer) {
// asynchronously called
});
Managing Subscriptions
This feature allows you to create and manage subscriptions for recurring payments. You can specify the plan and customer to associate with the subscription.
stripe.subscriptions.create({
customer: 'cus_4fdAW5ftNQow1a',
items: [{
plan: 'plan_CBXbz9i7AIOTzr'
}]
}).then(function(subscription) {
// asynchronously called
});
Handling Webhooks
This feature is for setting up a webhook endpoint to listen for events from Stripe. This is useful for receiving notifications about various events, such as successful payments or subscription cancellations.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
let event;
try {
event = JSON.parse(request.body);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
console.log(`PaymentIntent was successful!`);
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
response.status(200).end();
});
app.listen(8000, () => {
console.log('Running on port 8000');
});
Other packages similar to stripe
braintree
Braintree is a full-stack payment platform that makes it easy to accept payments in your app or website. It offers similar functionalities to Stripe, including payment processing, subscription management, and fraud protection. Braintree is known for its PayPal integration, which can be a deciding factor for some businesses.
square-connect
Square Connect is the official Square npm package. It provides access to various Square services, including payment processing. While it offers similar features to Stripe, such as handling transactions and managing customers, it is particularly tailored for businesses that use Square's point of sale system.
mollie-api-node
Mollie is a payment service provider that offers an easy-to-implement process for integrating payments into a website or app. It supports various payment methods and is known for its simplicity. However, it might not have as extensive a feature set as Stripe, particularly in terms of global reach and customization options.
Stripe node.js bindings ![Build Status](https://travis-ci.org/stripe/stripe-node.png?branch=master)
Version 2 Update Notice
Read about Version 2 here (Released October 18th, 2013)
Installation
npm install stripe
Documentation
Documentation is available at https://stripe.com/docs/api/node.
API Overview
Every resource is accessed via your stripe
instance:
var stripe = require('stripe')(' your stripe API key ');
Every resource method accepts an optional callback as the last argument:
stripe.customers.create(
{ email: 'customer@example.com' },
function(err, customer) {
err;
customer;
}
);
Additionally, every resource method returns a promise, so you don't have to use the regular callback. E.g.
stripe.customers.create({
email: 'foo-customer@example.com'
}).then(function(customer) {
return stripe.charges.create({
amount: 1600,
currency: 'usd',
customer: customer.id
});
}).then(function(charge) {
}, function(err) {
});
Available resources & methods
Where you see params
it is a plain JavaScript object, e.g. { email: 'foo@example.com' }
- account
retrieve()
- balance
retrieve()
listTransactions([params])
retrieveTransaction(transactionId)
- charges
create(params)
list([params])
retrieve(chargeId)
capture(chargeId[, params])
refund(chargeId[, params])
update(chargeId[, params])
updateDispute(chargeId[, params])
closeDispute(chargeId[, params])
setMetadata(chargeId, metadataObject)
(metadata info)setMetadata(chargeId, key, value)
getMetadata(chargeId)
markAsSafe(chargeId)
markAsFraudulent(chargeId)
- coupons
create(params)
list([params])
retrieve(chargeId)
del(chargeId)
- customers
create(params)
list([params])
update(customerId[, params])
retrieve(customerId)
del(customerId)
setMetadata(customerId, metadataObject)
(metadata info)setMetadata(customerId, key, value)
getMetadata(customerId)
createSubscription(customerId, params)
updateSubscription(customerId, subscriptionId, [, params])
cancelSubscription(customerId, subscriptionId, [, params])
listSubscriptions(params)
createCard(customerId[, params])
listCards(customerId)
retrieveCard(customerId, cardId)
updateCard(customerId, cardId[, params])
deleteCard(customerId, cardId)
deleteDiscount(customerId)
- events (types of events)
list([params])
retrieve(eventId)
- invoiceItems
create(params)
list([params])
update(invoiceItemId[, params])
retrieve(invoiceItemId)
del(invoiceItemId)
- invoices
create(params)
list([params])
update(invoiceId[, params])
retrieve(invoiceId)
retrieveLines(invoiceId)
retrieveUpcoming(customerId[, subscriptionId])
pay(invoiceId)
- plans
create(params)
list([params])
update(planId[, params])
retrieve(planId)
del(planId)
- recipients
create(params)
list([params])
update(recipientId[, params])
retrieve(recipientId)
del(recipientId)
setMetadata(recipientId, metadataObject)
(metadata info)setMetadata(recipientId, key, value)
getMetadata(recipientId)
- tokens
create(params)
retrieve(tokenId)
- transfers
create(params)
list([params])
retrieve(transferId)
update(transferId[, params])
cancel(transferId)
listTransactions(transferId[, params])
setMetadata(transferId, metadataObject)
(metadata info)setMetadata(transferId, key, value)
getMetadata(transferId)
- bitcoinReceivers
create(params)
retrieve(receiverId)
list([params])
getMetadata(receiverId)
Configuration
stripe.setApiKey(' your secret api key ');
stripe.setTimeout(20000); // in ms
(default is node's default: 120000ms
)
More information / wikis
Development
To run the tests you'll need a Stripe Test API key (from your Stripe Dashboard):
$ npm install -g mocha
$ npm test
Note: On Windows use SET
instead of export
for setting the STRIPE_TEST_API_KEY
environment variable.
If you don't have a prefixed key (in the form sk_test_...
) you can get one by rolling your "Test Secret Key". This can be done under your dashboard (Account Setting -> API Keys -> Click the roll icon next to the "test secret key"). This should give you a new prefixed key ('sk_test_..'), which will then be usable by the node mocha tests.
Author
Originally by Ask Bjørn Hansen (ask@develooper.com). Development was sponsored by YellowBot. Now officially maintained by Stripe.