Chris Hansen
Chris Hansen

Reputation: 8687

Handling stripe with reactJs

I'm trying to handle credit card payments using stripe and reactjs. I'm following the tutorial here https://github.com/stripe-archive/react-stripe-elements. See the get started section. The only problem I have is that I need to submit the credit card form on the component page where the code is (the first page that is created) and not the CheckoutForm page. How do I do that?

I tried doing the following

 var elements = useElements();
 const card = elements.getElement(CardElement);
 const result = await stripePromise.createToken(card);
 if (result.error) {
    console.dir(result.error.message);
 } else {
    console.dir(result.token);
 }

but I'm not able to use a hook function useElements in a class compomnent.

Upvotes: 0

Views: 284

Answers (1)

Nolan H
Nolan H

Reputation: 7459

First, the library you've linked to is deprecated, the new version is this one. Since you're using the hooks you must be using the new library already, though.

If you're using Class components, the documentation provides an example of an alternate integration pattern using the <Elements> provider and <ElementsConsumer> explicitly.

Upvotes: 1

Related Questions