#skoshkarli - Paymen Element
1 messages · Page 1 of 1 (latest)
stripe={this.props.paymentProvider}
options={{ clientSecret: this.props.clientSecret }}
>
<form onSubmit={() => {}}>
<PaymentElement />
<button disabled={!this.props.paymentProvider}>Submit</button>
</form>
</Elements>
paymentProvider is stripePromise
and clientSecret is returned from the subscription PaymentIntent which i create before
Okay I think how the form is rendered might be an issue here. Take a look at the code snippets in this doc and see if they help make more sense:
https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=elements&html-or-react=react#web-collect-payment-details
what do you mean by how the form is rendered?
I am not familiar with React so I was interested in the differences between how you handle the creation of the from and the onSubmit handler vs what we have documented
yeah i just removed the onSubmit handler all together and did what they did in the checkout component as per docs and same thing
it just doesn't render anything but my submit button
when i use the CardElement though, that shows up fine
but i need the PaymentElement
Can you try logging both the Payment Provider and Client secret just prior to rendering the form?
yes they are both there, without them i was getting another issue
I actively use paymentIntents in REact - I could show my (two) files, if you want...
export const PaymentEmbed = (props) => {
const { fan } = props;
const [options, setOptions] = useState(null);
const [secret, setSecret] = useState(null);
useEffect(() => {
if (!secret) {
(async () => {
//this process INITIATES generation of a payment_secret
const result = await capturePaymentMethod({ personId: fan.Id });
result?.data && setSecret(result.data.payment_secret);
})();
} else {
setOptions({
// passing the client secret obtained in step 2
clientSecret: secret,
// Fully customizable with appearance API.
appearance: {
theme: PAYMENT_THEME
/*...*/
}
});
}
}, [fan, secret]);
const doConfirm = async (...args) => {
return stripe.confirmSetup(...args);
};
return options ? (
<Elements stripe={stripe} options={options}>
<PaymentInput confirming={doConfirm} />
</Elements>
) : (
<SpinnerDiamond />
);
};
export default function PaymentInput(props) {
const { confirming } = props;
const stripe = useStripe();
const elements = useElements();
const [error_message, set_error_message] = useState(null);
const [showSpinner, setShowSpinner] = useState(true);
const handleSubmit = async (event) => {
// We don't want to let default form submission happen here,
// which would refresh the page.
event && event.preventDefault();
if (!stripe || !elements) {
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
setShowSpinner(true);
const result = await confirming({
//`Elements` instance that was used to create the Payment Element
elements,
confirmParams: {
return_url: window.location.href
},
redirect: `if_required`
});
if (result?.error) {
switch (result.error.type) {
case ERROR_TYPES.CARD_ERROR:
case ERROR_TYPES.VALIDATION_ERROR:
set_error_message(
`${
result.error.message_code
? "Code: " + result.error.message_code + " : "
: ""
}${result.error.message} Try re-submitting`
);
break;
default:
break;
}
// Show error to your customer (e.g., payment details incomplete)
logger(result.error.message);
setShowSpinner(false);
}
/**
* if there is no error, the user record ("People") changes state,
* and this entire component is removed.
*/
};
const handleReady = async (event) => {
// We don't want to let default form submission happen here,
// which would refresh the page.
if (!stripe || !elements) {
// Stripe.js has not yet loaded.
// Make sure to disable form submission until Stripe.js has loaded.
return;
}
setShowSpinner(false);
};
...
return (
<form onSubmit={handleSubmit}>
{showSpinner && <SpinnerDiamond />}
<PaymentElement
onReady={handleReady}
options={{ business: { name: COMPANY_NAME } }}
/>
<div>{error_message ? error_message : null}</div>
<button
className="button"
type="submit"
disabled={!stripe || showSpinner}
>
Submit
</button>
</form>
);
}