I am building React/Node/Stripe app, basic setup works fine but when i want to extend my app to collect email address from input form and sent it in body to node backend to use it to create customer or send email. I see in console.log that in req.body email is present but i can fetch this field.
React
email is collected
import React, { Component } from "react";
import { CardElement, injectStripe } from "react-stripe-elements";
import styled from "styled-components";
class CheckoutForm extends Component {
constructor(props) {
super(props);
this.state = {
complete: false,
name: "",
email: ""
};
}
handleChange = input => e => {
this.setState({
[input]: e.target.value
});
console.log(this.state.email);
};
submit = async () => {
let { token } = await this.props.stripe.createToken({
name: this.state.name,
email: this.state.email
});
console.log(token);
const email = this.state.email;
const data = {
token: token.id,
email
};
let response = await fetch("/charge", {
method: "POST",
headers: {
"Content-Type": "text/plain"
},
body: JSON.stringify(data)
});
console.log(response);
if (response.ok)
this.setState({
complete: true
});
};;
render() {
if (this.state.complete) return <h1> Purchase Complete </h1>;
return (
<CheckOut>
<CheckOutForm>
<CheckOutFieldSet>
<InputRow>
<Label htmlFor=""> Name </Label>
<Input
type="text"
placeholder="Jane Doe"
onChange={this.handleChange("name")}
/>
</InputRow>
<InputRow>
<Label htmlFor=""> Email </Label>
<Input
type="email"
placeholder="jane@doe.com"
onChange={this.handleChange("email")}
/>
</InputRow>
<InputRow last>
<Label htmlFor=""> Phone </Label>
<Input type="phone" placeholder="+48 999 000 999" />
</InputRow>
</CheckOutFieldSet>
<CheckOutFieldSet>
<CardElement />
</CheckOutFieldSet>
</CheckOutForm>
<ButtonCheckOut onClick={this.submit}> Send </ButtonCheckOut>
</CheckOut>
);
}
}
export default injectStripe(CheckoutForm);
Response email is present but name is in card
object
card: {id: "card_1", object: "card",
address_city: null,
address_country: null,
address_line1: null, …}
created: 1546375333
email: "emial"
id: "tok_1Dnu4wj"
livemode: false
object: "token"
type: "card"
used: false
__proto__: Objec
Name
card:{
last4: "4242"
metadata: {}
name: "Name"
}
BACKEND
app.post("/charge", (req, res) => {
console.log(req.body)
//{"token":"tok_1DnuwhFw7kwjoel1NsD2Qd1r","email":"lll"}
stripe.charges.create({
amount: 4000,
currency: "usd",
description: req.body.email,
source: req.body.token
}).then(
status => {
res.json({
status
})
// isValid = status.ok
}
).catch(err => res.send(err))
let mail = req.body.email
console.log(mail)
//undefined
I know that console.log(req.body)
will give me token id but how to send more stuff like email address?
One more? how it is possible that name i just collected on createToken
? I is included in token?
regards