#uhoh-react-paymentelement
1 messages · Page 1 of 1 (latest)
Hello! You can do loadStripe(platformAPIKey, { stripeAccount: connectedAccountID }) to initialize for direct charges on a connected account.
Hey, my use case is a bit particular, because I have two products. One with connetedAccountId and one without connectedACcountId
On the first render, before the user selects anything, there is no connectedAccountId. I would like it so that when the user selects something, it will then rerender with a connectedAccountId
function StripeWrapper({ stripeAccount, children }) {
const [stripeObject, setStripeObject] = useState(null);
useEffect(() => {
const fetchStripeObject = async () => {
const stripeAccountObj = stripeAccount ? { stripeAccount } : {};
const stripePromise = await loadStripe(`${platformAPIKey}`, stripeAccountObj);
setStripeObject(stripePromise);
};
fetchStripeObject();
}, [stripeAccount]);
return (
<Elements stripe={stripeObject}>
{children}
</Elements>
);
}```
If I put a console log in useEffect, it shows that it is re-rendering when the user selects something. But for some reason, the connectedAccountId stays null.
I'm not a React developer, but does this relate? https://github.com/stripe/react-stripe-js/issues/107
I just tried the solution suggested but it does not seem to work
yes!
it's a pro
prop*
I'm basically using the StripeWrapper around my whole App
so what's happening is
- The app renders with this StripeWrapper, where stripeAccount is null.
- The user selects a product, and it changes the value of stripeAccount, which rerenders StripeElements. But for some reason the value of the connectedAccountID is null when using Stripe's createPaymentMethod.
- This causes an error because my backend is expecting a Payment Method that is createdw ith a connectedAccountID. This throws the error No such PaymentMethod: [...]; OAuth key or Stripe-Account header was used but API request was provided with a platform-owned payment method ID. Please ensure that the provided payment method matches the specified account.
I think the issue here is that you need to define stripeAccount inside of StripeWrapper otherwise it won't mutate.
Like https://stackoverflow.com/questions/60493358/can-i-declare-a-variable-outside-of-useeffect-react-hooks is a basic example discussing this
So right now, on each render, your stripeAccount is basically being reset to null
I am using stripeAccount in an outer component, which is controlled by a state
Would that not work?
That outer component contains the logic for the user selecting a product, which ten changes the stripeAccount state (that state is passed down into StripeWrapper)
Taking over but sadly I have next to no knowledge about React myself
I think the first thing I'd recommend is to add clear logs to the code that initializes Stripe to make sure you hit it and set the right account id right there
so inside fetchStripeObject add a clear log to print both the API key and the account id
uhoh-react-paymentelement
Yep, I checked the logs and it is the right account. But I believe that the Elements is initializing without a stripe account id...
I'm not sure what you called "I checked the logs". Like which logs?
there's no console.log or equivalent in the code you shared above
I added console.logs! i Just removed them so it was easier to read the code for you