#Yharoo
1 messages · Page 1 of 1 (latest)
hello! I'm not super familiar with React, but your page can load Elements only when your clientSecret is not null
That's why you have
{clientSecret && (
<Elements options={options} stripe={stripePromise}>
<CheckoutForm />
</Elements>
)}
you can refer to https://stripe.com/docs/payments/quickstart for a full React sample
I dont understand what i'm donig wrong
const [clientSecret, setClientSecret] = useState('');
useEffect(() => {
(async () => {
try {
const result = await fetch(
`${process.env.REACT_APP_URL}/createPaymentIntent`,
{
method: 'post',
credentials: 'include',
}
);
if (result.status === 200) {
const { data } = await result.json();
console.log(data);
setClientSecret(data);
return;
}
} catch (err) {
console.log(err);
}
})();
}, []);
return clientSecret ? (
<Elements stripe={stripePromise} clientSecret={clientSecret}>
<PaymentElement />
</Elements>
) : null;
};
export default PaymentEl;
I do get the client secret from the request, but I don't understand why its giving me the error even with the conditional rednering
if you log clientSecret, what do you get?
can you paste it here?
am i supposed to share that key///?
it's in test mode right?
yes
thats the client secret, not public key or secret key
I'm not sure how to "wait" for the client secret. I'm already using conditional rendering
shouldn't the clientSecret be passed in via options?
const options = {
clientSecret,
appearance,
};
return (
<div className="App">
{clientSecret && (
<Elements options={options} stripe={stripePromise}>
<CheckoutForm />
</Elements>
)}
...
omg
changed to options and now im getting that error and a whole bunch of warnings
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: 100,
currency: 'usd',
automatic_payment_methods: {
enabled: true,
},
});
console.log(paymentIntent.client_secret);
res.json({
data: paymentIntent.client_secret,
});
} catch (err) {
console.log(err);
res.sendStatus(500);
}
});``` my backend
i don't think this has anything to do with your backend. how are you loading and initializing Stripe?
import { Elements, PaymentElement } from '@stripe/react-stripe-js';
const stripePromise = loadStripe(
'pktest key'
);```
i haven't seen that error before, let me check in with someone else. I'll get back to you in a bit
i think i'm going to need you to share a minimal sample project that replicates that error to take a look, i can't replicate it on my end. If you share code, remember to remove any sensitive information like api keys.
Alternatively, you can also download the sample as it is from https://stripe.com/docs/payments/quickstart and run it to compare against your own code to see if there are any differences that may be causing that error
yea lol
nothing is working :[
[0] operable program or batch file.
[1] 'yarn' is not recognized as an internal or external command,
[1] operable program or batch file.
[0] yarn start-client exited with code 1
[1] yarn start-server exited with code 1```
all I did was npm i and npm start
do you have npm installed?
yese
how about yarn?
never used yarn
install yarn too
i'll pass on the feedback. It's odd that we're using yarn without specifying it as a prerequisite
ok that seemed to solve that error
Hello
I have a question reagarind create payment intent
app.post("/create-payment-intent",
does it have to be spelled exactly like that
This is the sample, you can rename to anything you like
ok
also,
when i press the checkout button, is that when I should be creating payment intent?
I keep getting a client secret error, but I am getting a client secret back
what's the exact error message you're getting?
react-dom.development.js:12056 Uncaught IntegrationError: In order to create a payment element, you must pass a valid PaymentIntent or SetupIntent client secret when creating the Elements group.
Thats the client secret that I get from const createPaymentIntent = async () => { try { const result = await fetch( `${process.env.REACT_APP_URL}/createPaymentIntent`, { method: 'post', credentials: 'include', } ); if (result.status === 200) { const { data } = await result.json(); console.log(data); setClientSecret(data); return; } } catch (err) { console.log(err); } };
holy hell
I found my error
sorry for wasting your guys' time
No worries! Glad that you find out the issue
its because I was passing an a string to the options prop for Elements
sorry for bothering you guys, but my client secret after checking out is null.
useEffect(() => {
if (!stripe) {
return;
}
console.log(stripe);
const clientSecret = new URLSearchParams(window.location.search).get(
'payment_intent_client_secret'
);
console.log(clientSecret); <--- this is null
if (!clientSecret) {
return;
}
stripe.retrievePaymentIntent(clientSecret).then(({ paymentIntent }) => {
switch (paymentIntent.status) {
case 'succeeded':
setMessage('Payment succeeded!');
break;
case 'processing':
setMessage('Your payment is processing.');
break;
case 'requires_payment_method':
setMessage('Your payment was not successful, please try again.');
break;
default:
setMessage('Something went wrong.');
break;
}
});
}, [stripe]);```
are you using a redirect or non-redirect based payment method for your testing? this block of code should only be triggered when the customer completes the redirect based payment method and redirect back to your return_url
uhhhh
I guess redirect? I was just following the tutorial, wasn't aware that there were 2 options..
if (!stripe || !elements) {
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: 'http://localhost:3000',
},
});
if (error.type === 'card_error' || error.type === 'validation_error') {
setMessage(error.message);
} else {
setMessage('An unexpected error occurred.');
}
};```
So i guess what youre saying is, I don't necessarily need the block of code in the useEffect?
all my webhooks are passing with status 200. I think im good to go
my bad. any payment method will be redirected to return_url after completion according to your code
useEffect code block you shared will be triggered on your return_url after customer completes the payment
was it trigger before or after the payment completion?
Ok i think i fixed the redirect thing to not redirect
redirect: 'if_required'
Everything making a bit more sense now thanks
This line will only work if the payment method selected is a non redirect payment method