Creating Charges

Use Stripe's API and your server-side code to process charges. If you need help after reading this, check out our answers to common questions or chat live with other developers in #stripe on freenode.

Once you've securely collected and tokenized your customer's credit card using Checkout or Stripe.js, you can charge the card. Unlike collection, which occurs in the browser, charge attempts are made from your server, normally using one of our client libraries. If you haven't already, install the library for your favorite language now. This tutorial shows code for Ruby, PHP, Python, and Node, but we also have libraries for Java and Go.

On your server, grab the Stripe token in the POST parameters submitted by your form. From there, it's one simple API call to charge the card:

# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a charge: this will charge the user's card
begin
  charge = Stripe::Charge.create(
    :amount => 1000, # Amount in cents
    :currency => "usd",
    :source => token,
    :description => "Example charge"
  )
rescue Stripe::CardError => e
  # The card has been declined
end
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = request.POST['stripeToken']

# Create a charge: this will charge the user's card
try:
  charge = stripe.Charge.create(
      amount=1000, # Amount in cents
      currency="usd",
      source=token,
      description="Example charge"
  )
except stripe.error.CardError as e:
  # The card has been declined
  pass
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create a charge: this will charge the user's card
try {
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, // Amount in cents
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
    ));
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_BQokikJOvBiI2HlWgH4olfQ2";

// Get the credit card details submitted by the form
String token = request.getParameter("stripeToken");

// Create a charge: this will charge the user's card
try {
  Map<String, Object> chargeParams = new HashMap<String, Object>();
  chargeParams.put("amount", 1000); // Amount in cents
  chargeParams.put("currency", "usd");
  chargeParams.put("source", token);
  chargeParams.put("description", "Example charge");

  Charge charge = Charge.create(chargeParams);
} catch (CardException e) {
  // The card has been declined
}
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
var token = request.body.stripeToken; // Using Express

// Create a charge: this will charge the user's card
var charge = stripe.charges.create({
  amount: 1000, // Amount in cents
  currency: "usd",
  source: token,
  description: "Example charge"
}, function(err, charge) {
  if (err && err.type === 'StripeCardError') {
    // The card has been declined
  }
});

As a convenience, if you're logged in while reading this page, we've pre-filled the example with your test secret API key. Only you can see this value. This will authenticate you to Stripe, so keep it secret and keep it safe. Remember to replace the test key with your live key in production. You can get all your keys from your account page.

That's it! If the charge creation request succeeds, the card has been successfully charged. You will automatically receive your money in two days. If the charge attempt fails, we'll return an error instead.

Saving credit card details for later

Stripe tokens can only be used once, but that doesn't mean you have to request your customer's card details for every payment. Stripe provides a Customer object type that makes it easy to save this—and other—information for later use.

Instead of charging the card immediately, create a new Customer, saving the token on the Customer in the process. This will let you charge the customer at any point in the future:

# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a Customer
customer = Stripe::Customer.create(
  :source => token,
  :description => "Example customer"
)

# Charge the Customer instead of the card
Stripe::Charge.create(
    :amount => 1000, # in cents
    :currency => "usd",
    :customer => customer.id
)

# YOUR CODE: Save the customer ID and other info in a database for later!

# YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

Stripe::Charge.create(
  :amount   => 1500, # $15.00 this time
  :currency => "usd",
  :customer => customer_id # Previously stored, then retrieved
)
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = request.POST['stripeToken']

# Create a Customer
customer = stripe.Customer.create(
  source=token,
  description="Example customer"
)

# Charge the Customer instead of the card
stripe.Charge.create(
  amount=1000, # in cents
  currency="usd",
  customer=customer.id
)

# YOUR CODE: Save the customer ID and other info in a database for later!

# YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

stripe.Charge.create(
  amount=1500, # $15.00 this time
  currency="usd",
  customer=customer_id # Previously stored, then retrieved
)
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "Example customer")
);

// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => 1000, // Amount in cents
  "currency" => "usd",
  "customer" => $customer->id)
);

// YOUR CODE: Save the customer ID and other info in a database for later!

// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

\Stripe\Charge::create(array(
  "amount"   => 1500, // $15.00 this time
  "currency" => "usd",
  "customer" => $customerId // Previously stored, then retrieved
  ));
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_BQokikJOvBiI2HlWgH4olfQ2";

// Get the credit card details submitted by the form
String token = request.getParameter("stripeToken");

// Create a Customer
Map<String, Object> customerParams = new HashMap<String, Object>();
customerParams.put("source", token);
customerParams.put("description", "Example customer");

Customer customer = Customer.create(customerParams);

// Charge the Customer instead of the card
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 1000); // Amount in cents
chargeParams.put("currency", "usd");
chargeParams.put("customer", customer.getId());

Charge.create(chargeParams);

// YOUR CODE: Save the customer ID and other info in a database for later!

// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

Map<String, Object> otherChargeParams = new HashMap<String, Object>();
otherChargeParams.put("amount", 1500); // $15.00 this time
otherChargeParams.put("currency", "usd");
otherChargeParams.put("customer", customerId); // Previously stored, then retrieved

Charge.create(otherChargeParams);
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
var token = request.body.stripeToken; // Using Express

stripe.customers.create({
  source: token,
  description: 'payinguser@example.com'
}).then(function(customer) {
  return stripe.charges.create({
    amount: 1000, // Amount in cents
    currency: "usd",
    customer: customer.id
  });
}).then(function(charge) {
  // YOUR CODE: Save the customer ID and other info in a database for later!
});

// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!

stripe.charges.create({
  amount: 1500, // Amount in cents
  currency: "usd",
  customer: customerId // Previously stored, then retrieved
});

After creating a customer in Stripe with a stored payment method, you can charge that customer at any point in time by passing the customer ID—instead of a card representation—in the charge request. Be certain to store the customer ID on your side for later use.

Storing information in metadata

Stripe supports adding metadata to the most common requests you'll make, such as processing charges. Metadata is for your use only: it isn't shown to customers or factored into whether or not a charge is declined or blocked by our fraud prevention system.

Through metadata, you can associate other information—meaningful to you—with Stripe activity. Any metadata you include will be viewable in the Dashboard (e.g., when looking at the page for an individual charge), and will also be available in common reports and exports. As an example, your store's order ID can be attached to the charge used to pay for that order. Doing so allows you, your accountant, or your finance team to easily reconcile charges in Stripe to orders in your system.

If you using Radar to flag payments for review, consider passing any additional order information as metadata. By doing so, you'll have more information about the payment available within the Dashboard which can that can expedite your review process.

# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a charge: this will charge the user's card
begin
  charge = Stripe::Charge.create(
    :amount => 1000, # Amount in cents
    :currency => "usd",
    :source => token,
    :description => "Example charge",
    :metadata => {"order_id" => "6735"}
  )
rescue Stripe::CardError => e
  # The card has been declined
end
# Set your secret key: remember to change this to your live secret key in production
# See your keys here: https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"

# Get the credit card details submitted by the form
token = request.POST['stripeToken']

# Create a charge: this will charge the user's card
try:
  charge = stripe.Charge.create(
    amount=1000, # Amount in cents
    currency="usd",
    source=token,
    description="Example charge",
    metadata={"order_id": "6735"}
  )
except stripe.error.CardError:
  # The card has been declined
  pass
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create a charge: this will charge the user's card
try {
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, // Amount in cents
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge",
    "metadata" => array("order_id" => "6735")
    ));
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.apiKey = "sk_test_BQokikJOvBiI2HlWgH4olfQ2";

// Get the credit card details submitted by the form
String token = request.getParameter("stripeToken");

// Create a charge: this will charge the user's card
try {
  Map<String, Object> chargeParams = new HashMap<String, Object>();
  chargeParams.put("amount", 1000); // Amount in cents
  chargeParams.put("currency", "usd");
  chargeParams.put("source", token);
  chargeParams.put("description", "Example charge");
  Map<String, String> initialMetadata = new HashMap<String, String>();
  initialMetadata.put("order_id", "6735");
  chargeParams.put("metadata", initialMetadata);

  Charge charge = Charge.create(chargeParams);
} catch (CardException e) {
  // The card has been declined
}
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

// Get the credit card details submitted by the form
var token = request.body.stripeToken; // Using Express

// Create a charge: this will charge the user's card
var charge = stripe.charges.create({
  amount: 1000, // Amount in cents
  currency: "usd",
  source: token,
  description: "Example charge",
  metadata: {'order_id': '6735'}
}, function(err, charge) {
  if (err && err.type === 'StripeCardError') {
    // The card has been declined
  }
});

Next steps

Congrats! You've processed your first charge using Stripe. Some things you might want to see next: