Reputation: 1319
So I'm letting my user create a subscription with a free trial.
Node
const subscriptionWithTrial = async (req, res) => {
const {email, payment_method, user_id} = req.body;
const customer = await stripe.customers.create({
payment_method: payment_method,
email: email,
invoice_settings: {
default_payment_method: payment_method,
},
});
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{ plan: process.env.STRIPE_PRODUCT }],
trial_period_days: 14,
expand: ['latest_invoice.payment_intent', 'pending_setup_intent'],
});
res.json({subscription});
}
app.post('/subWithTrial', payment.subscriptionWithTrial);
React
const handleSubmitSubTrial = async (event) => {
if (!stripe || !elements) {
console.log("stripe.js hasn't loaded")
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
const result = await stripe.createPaymentMethod({
type: 'card',
card: elements.getElement(CardElement),
billing_details: {
email: email,
},
});
stripe.createToken(elements.getElement(CardElement)).then(function(result) {
});
if (result.error) {
console.log(result.error.message);
} else {
try {
const res = await axios.post('/subWithTrial', {'payment_method': result.paymentMethod.id, 'email': email, 'user_id': auth0Context.user.sub});
const {pending_setup_intent, latest_invoice} = res.data.subscription;
if (pending_setup_intent) {
const {client_secret, status} = res.data.subscription.pending_setup_intent;
if (status === "requires_action") {
stripe.confirmCardSetup(client_secret).then(function(result) {
if (result.error) {
console.log('There was an issue!');
console.log(result.error);
} else {
// The setup has succeeded. Display a success message.
console.log('Success');
}
I'm using card number 4000008260003178
so I'm pretty sure it's supposed to require authentication (which works) then result into result.error
for insufficient funds on line stripe.confirmCardSetup(client_secret).then(function(result) { if (result.error) {
. But this doesn't happen. Instead it gives me console.log('Success');
Upvotes: 0
Views: 86
Reputation: 5847
This is expected. stripe.confirmCardSetup
doesn't actually attempt to move money, it just sets the card up to do future off-session payments without needing 3DS.
Since you created your subscription with a 14 day trial, the actual first invoice payment won't happen till that's up. Meaning that in this case the test card is able to be set up correctly and won't see the payment failure till the trial period is up 14 days later.
Upvotes: 2