If I Have a Stripe Token from a Charge, How to Get Its Charge Id

How to get charge id after creating a subscription using Stripe?

This is exactly what Stripe's webhooks are for. After creating a customer with an initial subscription, you'll get six webhook notifications:

  1. customer.created, with the customer data (which you already have if you're saving what the API returns)
  2. charge.succeeded (or charge.failed), which contains the initial charge data you're looking for
  3. invoice.created, which is the associated invoice
  4. invoice.payment_succeeded (or invoice.payment_failed), also telling you the status of the charge
  5. customer.card.created, with the details of the new card
  6. customer.subscription.created, with the details of the customer's subscription.

Stripe's API, like many APIs and many payment solutions, is built to be used with webhooks. If you're not taking advantage of webhooks, you're going to be missing functionality, and you're probably working too hard for what can be done without webhooks.

Stripe works to deliver the data to you. If you're writing code to poll Stripe, you're working way, way too hard.

Retrieve token/Create charge - Stripe

How are you serving your backend --- Express?

If you're seeing a 404 when you submit your form to /charge it sounds like you might not have a app.post route setup for /charge in Express.

You can read through the guide on routing for a little more detail
https://expressjs.com/en/guide/routing.html

If you want to see a simple working example, take a look at this (make sure to replace the pk_test and sk_test with your actual test keys):

var stripe = require("stripe")("sk_test_xxxyyyzzz");
var express = require('express'), bodyParser = require('body-parser');

var urlencodedParser = bodyParser.urlencoded({ extended: false })
var app = express();

app.get('/',function(req, res) {
// for kicks, just sending checkout
res.send('<form action="/charge" method="POST">Buy it !<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_xxxyyyyzzzz"></script></form>')
});

app.post('/charge',urlencodedParser, function(req, res) {

// grab a token
var token = req.body.stripeToken;

// creating a charge, for real use add things like error handling
stripe.charges.create({
amount: 2000,
currency: "usd",
source: token, // obtained with Stripe.js
description: "Charge"
}, function(err, charge) {
res.send("You made a charge: "+ charge.id);
});
});

app.listen(5000)

Retrieve Stripe charge id upon failed charge

I finally used the meta data to store my record id with the charge. So I am able to retrieve it using this metadata.

charge = Stripe::Charge.create(
{
amount: 2000,
currency: 'eur',
source: token,
description: 'Test',
metadata: { record_id: 23 }
}, { stripe_account: 'xxxxx' })

stripe creating token with card id

You can not create a new token for an existing card as this would not make sense. The card is already saved on the customer and you can charge it.

The easiest solution here is likely to do the reverse and save the card on a customer when you get a token tok_XXXX. This way, you always charge a card by passing the customer parameter as the customer id cus_XXXX and the source parameter as the card id card_XXXX.

Otherwise, you need to handle this dynamically so that you know if you are getting a token (tok_XXXX) or a card id (card_XXXX) and pass different parameters based on that decision. Your front-end code should know which case you ended up in.



Related Topics



Leave a reply



Submit