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:
customer.created
, with the customer data (which you already have if you're saving what the API returns)charge.succeeded
(orcharge.failed
), which contains the initial charge data you're looking forinvoice.created
, which is the associated invoiceinvoice.payment_succeeded
(orinvoice.payment_failed
), also telling you the status of the chargecustomer.card.created
, with the details of the new cardcustomer.subscription.created
, with the details of the customer's subscription.
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
Is 'Respond_To_Missing''s Second Argument Useful for Anything
Get Route for Base Class of Sti Class in Rails
When Is The Enumerator::Yielder#Yield Method Useful
Error Installing Gems: Cannot Load Such File - Zlib
How to "Break" Out of a Case...While in Ruby
Actionmailer Pass Local Variables to The Erb Template
How to Test Helpers Blocks in Sinatra, Using Rspec
Monitor Multiple Rails Applications
Escaping Strings for Ruby Sqlite Insert
Including Methods to a Controller from a Plugin
How to Interact with a Caldav Server from Ruby
Cucumber Re-Run Failed Scenarios Automatically with a Tag
Best Way to Create a Blog with Static Pages in Ruby