Comments (10)
Hey @michelle ,
I just spent a few hours trying to figure out why my createToken wasn't working, and this issue is exactly that. I feel this should perhaps be reopened or opened as a new bug "State cannot be updated prior to creating token" - as this is a 'bug' of this software component.
My use case is as follows and (and I'd imagine this is a fairly common user journey):
- User types in card details in
<CardElement />
- User submits form with payment details
- Loading animation appears on screen
- Call
stripe.createToken()
to retrieve token - Submit token and payment to stripe through my lambda function
- Loading finishes & user is redirected to review
The below code is executed upon form submission
handleSubmit = async (ev) => {
ev.preventDefault();
try {
this.setState({ loading: true }); // This is the line that causes the issue
console.log('creating token');
const { error, token } = await this.props.stripe.createToken();
console.log('token created');
... my payment submission code ...
} catch(err) {
console.log('error', err);
}
};
Happy to help fix it (if I can) - but I've a feeling it might require a reasonably sizable change in how the values are retrieved from the <*Element>
s. If the decision is to not fix this then I'm more than happy to help document the problem :) Let me know!
from react-stripe-elements.
Hi there! Are you seeing any errors in your console? Where is handleSubmit
hooked up? You'll need to call event.preventDefault()
in your handleSubmit
if it's hooked up to a form (https://facebook.github.io/react/docs/forms.html).
from react-stripe-elements.
Hi @michelle, my form is set up with multiple stages. After each stage the user clicks a "Next" button, and at the final stage, the next button turns into a "Submit" button.
createToken
is called from this next button; in the second stage, the user inputs billing information, and after clicking next, the call to stripe is made. It looks like this:
handleNextClick = (e) => {
e.preventDefault()
const { stage } = this.state
if (stage < 2) {
this.setState({stage: stage + 1})
}
// We have to submit to stripe when next is clicked on the billing section
if (stage === 1) {
this.getStripeToken()
}
}
...
// the stripe code from above
getStripeToken = () => {
console.log("trying to get stripe token");
const { form, stripe, } = this.props
console.log(stripe);
stripe.createToken({type: 'card', name: form.card_name.value})
.then(({token}) => {
console.log('Received Stripe token:', token);
})
.catch((e) => {
console.log('got error', e);
});
}
The code in getStripeToken()
is the code in handleSubmit
above; since this function gets called (along with e.preventDefault()
) I thought adding it above would introduce needless complexity.
from react-stripe-elements.
Ah, I see the issue -- the code you have that moves to the next step may be running before we're able to submit the tokenization request -- this means that the Stripe Element (and the corresponding iframe that contains the customer card data) disappears from the DOM before we're able to extract and tokenize the data. I would change your code so that you don't setState({stage: 2})
if stage === 1
until the createToken
promise resolves.
Let me know if that works!
from react-stripe-elements.
@michelle, it worked! To test I just commented out the stage change, but now I have to figure out how to trigger that when the promise resolves. Thanks!
from react-stripe-elements.
@sonarforte Glad it worked! :)
You can call this.setState({step: 2})
in the then(...)
call on your promise.
from react-stripe-elements.
Going to close this out, but please feel free to reopen if you're still seeing issues.
from react-stripe-elements.
Had the same issue and finally came across this thread.
from react-stripe-elements.
We've tried troubleshooting this issue and can't get it to work yet.
Details:
We have a multiple step checkout flow on our checkout page with multiple forms:
- Step 1: Choose Plan. user choose plan, clicks
next
button - Step 2: Enter Company info. user enters company info (name, address, etc.), clicks 'next' button which changes the state flag
this.setState({step: 3})
to go to next step (Step 3) - Step 3: Add Payment Method.
name on card
field and elements component for entering card details, clickscomplete purchase
button
Issue:
On the submission of form on step 3, we are calling stripe.createToken(cardObj, addressObj)
to handle the promise but stripe.createToken()
NEVER resolves the promise, resulting in the never making it to Stripe.
our relevant code:
https://www.dropbox.com/s/qn4wm69a6huemim/code.js?dl=0
npm deps are "node": "6.11.1"
and "stripe": "^4.13.0"
from react-stripe-elements.
Hi, just noting that @plainspace contacted Stripe support and we're helping him there. Seems to be a somewhat different problem from the one discussed in this issue.
from react-stripe-elements.
Related Issues (20)
- CardNumberElement Placeholder Not Working HOT 1
- CardCvcElement onComplete fires after three digits for AMEX card HOT 2
- docs: confirmCardPayment with split card elements needs a docs update HOT 1
- Payment Fields are not clickable in Samsung device HOT 2
- My Stripe promise with React never resolves even after trying a timeout HOT 1
- 3D Secure Test Payment Screen iFrame - Any way to preview or control width/height? HOT 1
- Not able to customize CSS of CardElement HOT 1
- Split Card Number Not Inserted with 1Password Autofill HOT 1
- Elements locale option not localizing HOT 5
- When click on stripe element its getting blur HOT 1
- About scan card feature in mobile device HOT 1
- PaymentRequestButton Does Not Load On Safari PWA HOT 1
- Stripe console message : parameter: betas is not a recognized parameter. HOT 2
- Connection Failure text in CardNumberElement HOT 3
- loadStripe does not seem to work
- styling CardElement HOT 1
- Add support for extra types of buttons for Apple Pay HOT 1
- Elements tag doesn't accept locale HOT 2
- Add a full stripe elements integration example HOT 1
- PaymentRequestButtonElement Integration Error with saved browser card HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-stripe-elements.