How to retrieve receipt_url upon successful payment completion in Stripe - stripe-payments

We are using the Stripe API to make payments for invoices using a SAPUI5/Fiori UI. The payment intent create happens via a node.js project. We are successfully able to initiate the payment and from the Stripe dashboard Payments section we can see that the payment gets processed successfully.
We have the requirement, that upon successful payment completion, we need to redirect the user to the receipt URL (receipt_url) to display the payment receipt of the just processed invoice. Below is the code we are using to invoke the create payment intent on the Stripe server:
const paymentIntent = await stripe.paymentIntents.create(
{
payment_method_types: ['card', 'us_bank_account'],
metadata: {
....
....
....
},
},
{apiKey: secretKey}
);
res.send({
clientSecret: paymentIntent.client_secret,
});
According to the Stripe documentation, we can retrieve the receipt URL by retrieving the charge within the paymentIntent, but the response we receive upon successful processing of the payment by Stripe does not contain the charge object, it just has the payment id. Is it possible in any way, to retrieve the receipt URL using only the payment intent id?
Calling the payment intent create on Stripe to process the payment, but we are not getting in the response the receipt_url value where we want to redirect the user to, upon successful payment completion.

You can find the receipt_url property on the latest_charge property (make sure you use an API version greater than 2022-11-15; in older versions, it was the last item of charges property), which you can expand on your successful Payment Intent.
For that, you need to add a parameter expand when you create a Payment Intent:
const paymentIntent = await stripe.paymentIntents.create({
…
expand: [“latest_charge”]
});
const receiptUrl = paymentIntent.latest_charge.receipt_url;

Related

NodeJs Update default credit card of customer for Stripe subscriptions and payment

I am building an app in which i am using stripe to handle all payments. And in our app we have a common feature where user can update the default card for their subscriptions but it not working as expected. I Have followed the instruction to update the customer payment method as described in the stripe documentation but it's not working as expected i still see the old credit card details as default payment method in stripe dashboard.
Updating the customer:
const customer = await stripe.customers.update(body.customerId, {
invoice_settings: {default_payment_method: body.pId},
});
Attaching payment method to customer and updating subscription:
const paymentMethod = await stripe.paymentMethods.attach(body.pId, {
customer: body.customerId,
});
await stripe.subscriptions.update(subscriptionDetails?.planId as string, {
default_payment_method: paymentMethod.id,
});
But still nothing, i am not sure what i am missing here but i couldn't update the default payment method.
The screenshot you shared refers to the payment methods saved on the Customer. If you wish to set the default payment method on the Customer, it will be under invoice_settings.default_payment_method on Customer API.
Subscription's default payment method is different from Customer's default payment method. With default_payment_method on Subscription API, it will only update the default on the Subscription object, not Customer object like the page you see. You should be able to find Subscription's default payment method at the Subscription page.
Subscription page link will be: https://dashboard.stripe.com/test/subscriptions/sub_xxx where sub_xxx is the Subscription ID.

Issues cloning a Stripe platform customer to a Connected Account Customer - No such payment_intent: 'pi_abc...zyx'

I am using the React components for Stripe.js to try and process a payment for a connected account, and collect a fee off of each payment. I am not sure if the flow between my client and server is properly picking up my cloned customer and payment method. When my test customer tries to pay, I get "No such payment_intent: 'pi_abc...zyx'". I have ensured I am using the correct private test keys on both client and server. My Connected accounts are express accounts. When I go to my Stripe dashboard to look at the 'Customers' tab under 'Connect', this is what I see:
It looks like a blank entry is being created each time I make a payment attempt.
Here are the steps I am currently taking to let the platform customer pay a Connected account:
When a customer first signs up on my website, the Stripe-firebase extension I have installed automatically generates a customer ID. These are now considered my platform customers
I allow a platform customer to create an Express Connected account, this works perfectly fine. I now have their Stripe Account Id ex: 'acct_abc...xyz`.
Now here is the flow for when a platform customer tries to make a payment to a Connected Account:
React/Client Side - loadStripe with only the test key, not with a Stripe Connected account Id
const stripePromise = loadStripe('pk_test_abc...');. I provide this to <Elements stripe={stripePromise}>
React/Client Side - User fills in payment form and presses submit. Create a paymentMethodReq from client/platform:
const paymentMethodReq = await stripe.createPaymentMethod({
type: 'card',
card: cardElement,
billing_details: billingDetails
});
Node/Server Side - Client then makes a request to my server to try and clone the platform payment method to a Connected account:
const serverPaymentMethod = await stripe.paymentMethods.create({
customer: data.customerStripeId, // Customer ID of the customer paying (platform customer)
payment_method: data.paymentMethodId, // Using the payment method Id generated from 'paymentMethodReq' on the client side
}, {
stripeAccount: connectedAccountStripeAccountId, // Using the Connected Account
});
Node/Server Side - Client then makes a request to my server to create/"clone" a new customer from the platform and link it to the Connected account
const customer = await stripe.customers.create({
payment_method: data.paymentMethodId, //Payment method id generated from 'serverPaymentMethod' above
}, {
stripeAccount: connectedAccountStripeAccountId, // Using the same Connected Account
});
Node/Server Side - Client then makes a request to my server to create a payment intent with the customer id that was linked to the Connected account along with the payment method that was generated from the server
const intent = await stripe.paymentIntents.create({
amount: data.price * 100,
currency: 'usd',
payment_method_types: ['card'],
payment_method: data.paymentMethodId, // Payment method id generated from 'serverPaymentMethod' above
customer: data.customerStripeId, // Customer Id generated from 'customer' above
description: data.desc,
capture_method: 'manual',
application_fee_amount: (data.price * 100) * 0.15,
}
React/Client Side - I try to confirm the card payment which results in the "No such payment_intent: 'pi_abc...zyx'" error
const confirmedCardPayment = await stripe.confirmCardPayment(paymentIntentResult?.data?.client_secret, {
payment_method: serverPaymentMethod?.data.id //Payment method id generated from 'serverPaymentMethod' above
});
I tried replacing stripe.confirmCardPayment on the client with a call to my server that instead confirms the payment intent like so:
const confirmPaymentIntent = await stripe.paymentIntents.confirm(
data.paymentIntentId, // Payment intent id generated from 'intent' above
{
payment_method: data.paymentMethodId // Payment method id generated from 'serverPaymentMethod' above
});
and this also results in "No such payment_intent 'pi_3K...9Rx'"
If anyone could help me figure out where I am going wrong in this process, that would be greatly appreciated. Thank you
The error indicates that the PaymentIntent belongs to a different account with whose your current call is using a key of. It could be:
You created the PaymentIntent on Platform account, then confirm it
from your Connected Account
You created the PaymentIntent on Connected Account, then confirm it
from your Platform Account
While I am seeing both PaymentIntent creation and confirmation calls doesn't use the stripeAccount parameter, I suspect that you missed the code somewhere and the PaymentIntent was actually created in your Connected Account, lead to #2 possibility above.
To debug this you can check which account the PaymentIntent belongs to, by searching its ID on Dashboard's search box, or simply write to Stripe Support with the ID and they can check it for you.

Stripe Session Getting Customer Name

I would also like to retrieve the name of the person who paid. How can I get the name value from the stripe session? I have tried this:
const sessions = await stripe.checkout.sessions.list({
limit: 1,
});
console.log(sessions.data[0].customer)
const customer = await stripe.customers.retrieve(sessions.data[0].customer);
console.log(customer)
which gets me the email, but the name is null.
UPDATE: Customers created through Checkout have a null name because it is not collected/set. The "name on card" field on the Checkout page is for the cardholder name for the payment method, not the name of the customer (see https://stripe.com/docs/api/payment_methods/object#payment_method_object-billing_details-name). If you want a Customer created through Checkout to not have a null name, you'll have to update the Customer after the session is completed.
Stripe has a dashboard setting (https://dashboard.stripe.com/settings/emails) you can enable that automatically sends receipts to customers for successful payments completed in live mode. Payments completed in test mode will not automatically send an email, but you can trigger one manually through the dashboard. You can read more about sending receipts here: https://stripe.com/docs/receipts#automatically-send-receipts-when-payments-are-successful.
If you want to grab the customer’s email and send a receipt yourself it’ll be a bit more complicated.
Wait for the Checkout Session to finish by listening for checkout_session.completed through a webhook endpoint (https://stripe.com/docs/payments/checkout/fulfill-orders#handle-the-checkoutsessioncompleted-event).
Grab the customer ID from the Checkout Session object that comes in with the checkout_session.completed event.
Using the customer ID from the previous step, retrieve the Customer object (https://stripe.com/docs/api/customers/retrieve) and get the customer’s email.

Stripe TypeError: Cannot read property 'payment_intent' of undefined

I'm integrating the payment methods to the app with Stripe and Paypal and now working on Stripe integration.
The backend is Node, Sails.js, and the frontend is React.js.
I followed the guide they provided but and tested the payment processing using testing card.
But I got the error, which says TypeError: Cannot read property 'payment_intent' of undefined.
Here is my code.
- Frontend
this.props.stripe.createPaymentMethod({
type: 'card',
card: cardElement,
}).then(({paymentMethod}) => {
// API call
})
- Backend
subscribe = await stripe.subscriptions.create({
customer: customerId,
items: [
{price: plan.stripe.stripeId},
],
expand: ['latest_invoice.payment_intent']
});
Please help me to fix this issue.
In my opinion you don't have to create the PaymentIntent per subscrition.
Here is what Stripe API docs says about paymentIntent.
A PaymentIntent guides you through the process of collecting a payment from your customer. We recommend that you create exactly one PaymentIntent for each order or customer session in your system. You can reference the PaymentIntent later to see the history of payment attempts for a particular session.
So they recommend to create the paymentIntent per transaction and in subscription case, the payment is proceed in background automatically and paymentIntent is also generated in my experience.
But on the other hand in checkout option, you need to create the paymentIntent first before proceeding the payment.
I think you code was not wrong but I think you didn't follow the correct guide from Stripe or used wrong credit card.
Please refer this link:
Stripe API Payment_intents

What to do when Strong Customer Authentication fails?

I am trying to implement a Subscription signup and payment flow with Stripe.js V3 and Strong Customer Authentication (SCA) like this:
var stripe = Stripe('pk_test_9hA8gecxBFTY3O6kUm7hl16j');
var paymentIntentSecret = 'pi_91_secret_W9';
stripe.handleCardPayment(
paymentIntentSecret
).then(function (result) {
if (result.error) {
// Display error.message in your UI.
} else {
// The payment has succeeded. Display a success message.
}
})
Everything works great when the payment succeeds.
But what am I supposed to do when it fails?
Should I redirect the user to the initial payment screen, so s/he can start over?
When I do that I get this error:
You cannot confirm this PaymentIntent because it has a status of canceled. Only a PaymentIntent with one of the following statuses may be confirmed: requires_confirmation, requires_action.
Or should I delete everything, including the previously created stripe_customer and stripe_subscription, and then start over?
Thanks for any help.
If the payment fails entirely(maybe it was declined, or 3D Secure was attempted but not completed successfully) , the PaymentIntent from the first invoice should be in the requires_payment_method state and the subscription is incomplete.
You can choose to attempt to collect new payment information from the user and use that to complete the invoice payment and activate the subscription. You can re-use the same PaymentIntent throughout this and try as many times as you wish. For example, if you had a payment form with a Card Element for collecting details, you can have the user enter a new card and call this again :
stripe.handleCardPayment(cardElement,
paymentIntentSecret
).then(function(res){...})
Alternatively you can choose to cancel the subscription entirely if you wish. Otherwise if you do nothing, or the customer isn't able to provide a payment method that works, after 24 hours, Stripe effectively cancels the subscription for you.
Your error message seems to indicate the PaymentIntent was cancelled, which might mean you cancelled the subscription, or you're trying this more than 24 hours after the initial payment, I'm not sure.
This link goes into more detail:
https://stripe.com/docs/billing/lifecycle#incomplete

Resources