#adamerrinwright-retrieve-paymentintent
1 messages · Page 1 of 1 (latest)
@livid ridge What are you really trying to do? Why do you need to retrieve a PI client-side from a charge, i don't really grasp the question
@livid ridge any more details?
Thank you @young mica , grabbed coffee real quick. so I'm trying to capture ChargeID, it looks like after confirmCardPayment, I should be able to stripe.retrievePaymentIntent for the ChargeID,
yeah that's not how it works really
This is my flow so far ```js
apiInstance.post("/payments/create", {
amount: total * 100,
shipping: {
name: recipientName,
address: {
...shippingAddress,
},
},
vendors: sellers
})
.then(({ data: clientSecret }) => {
stripe.createPaymentMethod({
type: "card",
card: cardElement,
billing_details: {
name: nameOnCard,
address: {
...billingAddress,
},
},
})
.then(({ paymentMethod }) => {
stripe.confirmCardPayment(clientSecret, {
payment_method: paymentMethod.id,
receipt_email: email, // Check if email is going thru.
})
.then(({ data: clientSecret}) => {
// console.log(paymentIntent)
stripe
.retrievePaymentIntent({clientSecret})
.then((result) => {
if (result){
return result;
} else {
throw new Error('BAD HTTP REQ')
}
stripe.retrievePaymentIntent is client-side, this is definitely not where you would do any of this
you need to go back to your server. And then you need to call the Capture PaymentIntent API https://stripe.com/docs/api/payment_intents/capture you don't need to look at the Charge
Sorry I meant Capture const paymentIntent = await stripe.paymentIntents.capture(
then you don't need a Charge id either, you just need to call this server-side and that takes the PaymentIntent id as the first parameter, the pi_123
Okay, so I cant capture a PaymentIntent.chargeID with StripeJS's retrievePaymentIntent or confirmCardPayment?
Hello! I'm taking over for @young mica, let me catch up... 🙂
To clarify, you're trying to capture a Charge client side that's already been authorized? As in you're placing a hold on funds and capturing them later?
Yes according to the code block above, I should be able to confirmCardPayment and then wait for the payment_intent.succeeded
So that I can do stripe.retrievePaymentIntent(paymentIntentSuccessID) something like that
To yield the Object that holds the paymentintent.Charges.id
So the Payment Intent has capture_method set to manual? https://stripe.com/docs/api/payment_intents/create#create_payment_intent-capture_method
Ive never played with this setting let me check... No its on Automatic
Okay, so you're not placing a hold on funds. I'm not sure I understand what you're trying to do. Can you provide more details?
Oh sorry, Im trying to get the ChargeID from a Successful payment intent, so I can use it for Transfers
But you're trying to get the Charge ID client-side?
Well since this StripeJS stripe.confirmCardPayment(clientSecret, {... performs a POST req
I thought that I should also be able to take this StripeJS code stripe.retrievePaymentIntent(clientSecret) to perform another Req for the payment_intent.succeeded object
Because with this Return it looks like I could retrieve the ChargeID through that
Because it would hopefully save me the extra 1 hour of adding more backend code haha
Sorry, I mean, what are you doing to do with the Charge ID client-side once you have it?
Im going to test this soon, but if this cannot be done Client side, then i will try the Capture a PaymentIntent backend code
You mentioned something about Transfers, but that doesn't make sense to me as you can't create a Transfer or do anything with them client-side.
You should not capture Payment Intents server-side unless the customer is not present and you're doing an off-session transaction.
Im going to maybe create a var chargeID = result.charge.id, and then do ```js
*CLIENT SIDE
fetch("/v1/transfers),
*SERVER SIDE
app.post("/v1/transfers") async (req, res) => {
try{
const transfer = await stripe.transfers.create({
amount: 400,
currency: 'usd',
destination: 'acct_1HGE34HBsyfQ9rBx',
source_transaction: 'chargeID',
});
Why not send the Payment Intent ID to your server-side code instead and then get the Charge ID server-side before creating the Transfer?
I know i cant do Transfers clientside, but maybe I can pass ChargeID from clientside to Serverside, then do the Transfers Serverside
Okay lemme read this
I do not recommend that flow. You should only deal with the Payment Intent client-side and do the rest server-side.
We would need to study more of what this means
That sounds good to me if thats more secure, let me think of the code for this
It's basically the code you shared above, but instead of passing the Charge ID to your /v1/transfers endpoint you transfer the Payment Intent ID. Then, server-side, you fetch the Payment Intent using that ID, grab the Charge ID from the result, and create a Transfer with it.
Trying this now
So Im trying to wait for the confirmCardPayment to return the paymentIntentID so I can pass it to the fetch(/pintents/pi_qh29cnr8qfqv8nv/capturel
I feel like I already did this yesterday, hold on let me work on this
Hello, its getting cold out there, yes currently trying things
When you retrieve the PaymentIntent object from the confirmCardPayment is it possible to wait for it to succeed so that the Charge.id will be in the JSON?
no
you don't get the Charges array back in the client-side retrueve
as we explained, you ahve to fetch it server-side
you don't event need the charge id at all to capture anything
@livid ridge are you unblocked
So now I believe Im getting a CORS error for sending Frontend from my Localhost
Current Frontend code```js
.then(({ paymentMethod }) => {
stripe.confirmCardPayment(clientSecret, {
payment_method: paymentMethod.id,
receipt_email: email, // Check if email is going thru. It is in the field.
})
.then(({ paymentIntent }) => {
fetch(https://us-central1-ourcompany-5fa49.cloudfunctions.net/api/payment_intents/${paymentIntent.id}/capture, {
}) .then(res => res.text()) // convert to plain text
.then(text => console.log(text))
current Backend code ```js
app.get("/payment_intents/:id/capture", async (req, res) => {
const id = req.params.id; // <-- as simple as that
console.log(res)
res.send(paymentIntent);
});
Im gonna try on Live site to see if i can get past CORS error, then I guess Ill try something else if that doesnt work
yeah you seem to post to a different domain than you're on
this is more a normal/basic JS error, it's not Stripe related
Thanks for the help rubeus, koopajah, and hmunoz, I'll keep checking this out!