stripe.confirmCardSetup not attaching card to customer - stripe-payments

I'm using expressjs with sapper/svelte on the front.
I'm attempting to use stripe SetupIntent api and stripe.confirmCardSetup to save card details and attach it to a customer for future charges. I believe after reading the documentation I understand the flow of the process and my code is based on their example from the custom payment flow section
After submitting the credit card form, I get nothing. No card attached to the customer, no console log output and no errors in my application. The setupIntent is successfulLY created and I'm able to view it in my stripe dashboard but no card (paymentMethod) is created and attached to the customer. Here is my steps:
1- When a user create an account, I create a customer in stripe and
save the stripe customer's id in my db
2- I use that stripe customerid to create a setupIntent on the server
const intent = stripe.setupIntents.create({
customer: customer
}).then(intent => {
return res.end(JSON.stringify({ info: intent.client_secret }));
});
At this stage, I need the client_secret to use it on the front with my card-element to submit the card details securely to stripe.
I confirm the setupIntent is indeed in my stripe dashboard. So this part is fine.
Now to the front part of my code :
I get the client_secret on the front of my application and use stripe.js to collect and submit the card details to be attached as a paymentMethod to the customer (which the client_secret has the customer.id), so the code is :
let clientsecret
let stripe = null
let cardelement = null
stripe = await loadStripe('pk_test_mykeys');
var elements = await stripe.elements();
cardelement = await elements.create('card');
cardelement.on('ready', ()=>{});
cardelement.on('change', ({error}) => {
const displayError = document.getElementById('card-errors');
if (error) {
displayError.textContent = error.message;
} else {
displayError.textContent = '';
}
}); //end of card.on fn
cardelement.mount("#card-element");
return{
stripe : stripe,
cardelement : cardelement
}
function clickhandle(){
stripe = stripe
stripe.confirmCardSetup( csec , {
payment_method: {
card: cardelement,
billing_details: { user : "testing setupIntent"
}
},
setup_future_usage : 'on_session'
})
.then(function(result) {
if (result.error) {
console.log("there is an error")
}
if(result.setupIntent){
console.log("here is the setupintent success result")
}
});
}
Stripe.js works because I tested a paymentIntent and charging credit card on a different part of the application and the executed charges are showing in the stripe dashboard, so it is not the stripe.js script.
Is my process flow correct? Use stripe customer id to create setupIntent on my server and on the front the above code which collects card details and submit it using stripe.confirmCardSetup which will attach the paymentMethod/card details to the customer from the setupIntent client_secret?
Any help would be appreciated as I read the documentation and it is clear and straightforward. I followed it step by step. What am I missing?

The correct process is described here: https://stripe.com/docs/payments/save-and-reuse#web-create-setup-intent and you're doing things a bit different that may be causing some issues.
I suspect that your inclusion of future_usage - which isn't actually a parameter to that function call - is likely the problem.

After reading #floatingLomas comments, I went back and read carefully the documentation again. Here are the issues that need to be clear:
1- the form where you will mount the #card-elements needs to include the client_secret, whether in a hidden field or using data- attribute.
read here if you don't know about it
So --note the data-secret attribute --my form code now is :
<form data-secret={customersecret} on:submit={stripehandle} id="payment-form">
<label for="card-element">Enter Your Credit Card Details:</label>
<div id="card-element"></div>
<div id="card-errors" role="alert"></div>
<button id="submit">Pay -svelte2 component</button>
</form>
2- remove future_usage as #floatingLomas suggested. The correct parameter is "Usage" : "on_session" because the future_usage is for the paymentMethod api not the setupIntent api
3- The parameter billing_details: { "user" : "testing setupIntent" } was wrong.
The api has parameter for name, phone, address but not user ;)
So the confirmCardSetup() code is the following:
stripe.confirmCardSetup( csec , {
payment_method: {
card: cardelement,
billing_details: { "name" : "testing setupIntent" }
}
})
.then(function(result) {
console.log("result :", result)
})

Related

How to update credit card payment in stripe

I have a flow using nodejs and reactjs to let users subscribe to my site.
So when user logs in he can see a few packages.
When the user selects a package and enters card details, I do the following:
1 - Create a new customer, based on user details (name, email etc, fetched since user logged in)
2 - Create a subscription for the newly create customer according to price_id selected
3 - Collect in the frontend the card number/cvc/expire date with:
const cardElement = elements.getElement(CardElement);
4 - Make a call to stripe from frontend with:
const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: cardElement,
billing_details: {
name: name,
}
}
});
Im not sure if this was the best flow. However, it is working.
I also managed updating subscription and canceling it.
However, Im having an hard time in changing credit card details.
What I understood from docs I should use the same call as I did when I create the card payment.
So I collect again the credit card number and call again the same function:
const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: cardElement,
billing_details: {
name: name,
}
}
});
However, the call is done to:
https://api.stripe.com/v1/payment_intents/pi_XXXX/confirm
and returns a 400 with this info:
type: "invalid_request_error", code: "payment_intent_unexpected_state", doc_url: "https://stripe.com/docs/error-codes/payment-intent-unexpected-state"
Should I use something else to update credit card info? Or am I calling it in the wrong way?
Your initial flow of calling confirmCardPayment() is correct, that is what is recommended in Stripe's docs too: https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=elements.
hard time in changing credit card details. What I understood from docs I should use the same call as I did when I create the card payment.
To just collect card details and create a PaymentMethod, you should call createPaymentMethod() [0] from Stripe.js. That will convert a customer's card into a PaymentMethod like pm_123.
You will then send that PaymentMethod to your backend server, where (using your server-side Stripe API library like stripe-node) you'll attach it to a Stripe Customer [1] and also update as the Customer's default PaymentMethod for recurring payments [2].
[0] https://stripe.com/docs/js/payment_methods/create_payment_method
[1] https://stripe.com/docs/api/payment_methods/attach
[2] https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method

Saving credit card of a Custom Account's customer in Stripe

We are building a Platform.
In our Platform we create Custom Connect Accounts in Stripe.
For these Custom Connect Account we create customer accounts. Essentially the customer accounts are end-customers of our Custom Connect (Company)accounts in the Platform.
Now we would like to store credit card information of the customer accounts (for a particular custom connect account).
We followed the instructions here to create a setupIntent. The code is as below, here the stripe_account is the account_id of the custom connect (Company) account and customer['id'] is the id of the customer account -
intent = stripe.SetupIntent.create(
customer=customer['id'],
stripe_account = stripe_account
)
We pass this intent.client_secret to our front end. In the Javascript we are calling this -
setupForm.addEventListener('submit', function(ev) {
ev.preventDefault();
stripe.confirmCardSetup(
clientSecret,
{stripe_account : stripe_account},
{
payment_method: {
card: cardElement,
billing_details: {
name: cardholderName.value,
},
},
}
).then(function(result) {
if (result.error) {
// Display error.message in your UI.
} else {
// The setup has succeeded. Display a success message.
}
});
});
But we are getting the error, No such setupintent: 'seti_1IBkyZ4ZQzThevDR3MR433aI'. Clearly the setupintent that was generated from Stripe is not being accepted here. What are we doing wrong?
The likely cause of this error is that you're not initializing Stripe.js with the stripeAccount the Setup Intent exists on. Your Stripe.js initialization code should look something like this:
var stripe = Stripe('pk_test_YOUR_PUBLISHABLE_KEY', {
stripeAccount: 'acct_CONNECTED_ACCOUNT_ID'
});
That will allow Stripe.js to make requests on behalf of the connected account (which is where the Setup Intent exists).

How to retrieve the Stripe fee for a payment from a connected account (Node.js)

I've been reading the documentation for how to retrieve the Stripe fee from a given payment here:
// Set your secret key. Remember to switch to your live secret key in production!
// See your keys here: https://dashboard.stripe.com/account/apikeys
const stripe = require('stripe')('sk_test_xyz');
const paymentIntent = await stripe.paymentIntents.retrieve(
'pi_1Gpl8kLHughnNhxyIb1RvRTu',
{
expand: ['charges.data.balance_transaction'],
}
);
const feeDetails = paymentIntent.charges.data[0].balance_transaction.fee_details;
However I want to retrieve the Stripe fee for a payment made to a connected account. If I try the code above with a payment intent from a linked account I get the error:
Error: No such payment_intent: 'pi_1Gpl8kLHughnNhxyIb1RvRTu'
However, I can actually see the payment intent listed when I receive the posted data from the webhook:
{ id: 'evt_1HFJfyLNyLwMDlAN7ItaNezN',
object: 'event',
account: 'acct_1FxPu7LTTTTMDlAN',
api_version: '2019-02-11',
created: 1597237650,
data:
{ object:
{ id: 'pi_1Gpl8kLHughnNhxyIb1RvRTu',
object: 'payment_intent',
Any tips?
I want to retrieve the Stripe fee for a payment made to a connected
account. If I try the code above with a payment intent from a linked
account I get the error:
In order to retrieve the Stripe fee for a payment made on behalf of a connected account (using a direct Charge) you need to make the retrieve request as the connected account by specifying the special Stripe-Account header in the request. When using stripe-node we'll add that header for you automatically if you pass in the account ID as part of the request options. For example:
const paymentIntent = await stripe.paymentIntents.retrieve(
"pi_1HCSheKNuiVAYpc7siO5HkJC",
{
expand: ["charges.data.balance_transaction"],
},
{
stripeAccount: "acct_1GDvMqKNuiVAYpc7",
}
);
You can read more about making requests on behalf of connected accounts in stripe-node and our other libraries here: https://stripe.com/docs/api/connected_accounts

Collect an email address with Stripe

I'm following https://stripe.com/docs/payments/accept-a-payment and it works.
I need to collect a customer email address. I am using the client-server integration, as I believe this is necessary to support a dynamic price, set with the following code:
router.post("/create-payment-intent", async (req, res) => {
const stripe = require("stripe")("redacted");
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd"
});
res.send({
clientSecret: paymentIntent.client_secret
});
});
I've been very confused by the documentation. (I've previously used PayPal for payments, which has its own issues.)
How can I collect an email address, as part of the Stripe checkout process?
Could someone point me at the correct page?
You'd collect the email address yourself, using a HTML element on your checkout page. You then have the choice to create a Stripe customer with this information and pass that into your PaymentIntent creation if you wish to reuse the customer later. Or you can just pass the email address in the receipt_email field when creating the PaymentIntent.

Stripe Connect PaymentIntent error: No such payment_intent

I'm using stripe connect in my API, and I would like to update and process an existing paymentIntent. The paymentIntent creation is successful using the NodeJS stripe package
const paymentIntent = await stripe.paymentIntents.create(
{
payment_method_types: ["card"],
amount: 1499, // in cents
currency: "usd"
},
{
stripe_account: "acct_xxx"
}
)
This successfully returns a paymentIntent object with id ('pi_yyy'), client_secret ('pi_yyy_secret_zzz'), status ('requires_payment_method') and more fields.
However, when using the returned payment intent id to further update the payment intent or calling stripe.createPaymentMethod on the frontend with the client_secret, an error is returned:
Error: No such payment_intent: pi_yyy
In my case I saw Error: No such payment_intent: pi_yyy in the BROWSER when confirming a PaymentIntent without passing stripeAccount to Stripe. Make sure you're passing a stripeAccount:
//pass stripeAccount to Stripe when using Stripe Connect in the browser
let stripe = Stripe(stripePublishableKey, {
stripeAccount: stripeAccountId,
})
let result = await stripe.confirmCardPayment(clientSecret,{
...
})
https://stripe.com/docs/connect/enable-payment-acceptance-guide
For those who are still wondering with this issue,
These errors are usually caused by either a mismatch in API keys or by trying to access objects that exist on a different account. Double check your publishableKey and secret key from your console both from the same account.
In my case, I got to explicitly specify the payment intent account:
const refund = await stripe.refunds.create({
payment_intent: stripe_payment_intent_id,
reason: 'requested_by_customer',
}, {
stripeAccount: account_id,
});
I had the same error and it was because I'd discounted the payment amount to below what stripe will accept (I made it free with a coupon in WooCommerce).
This may help someone out there

Resources