【发布时间】:2019-01-24 02:48:19
【问题描述】:
我一直在尝试将条带添加到我目前正在尝试获得 MVP 的应用程序中。每当我输入测试信用卡并单击发送时,我都会被发送到带有以下错误代码的屏幕。
错误代码
Unhandled Rejection (TypeError): Cannot read property 'id' of undefined
CheckoutForm._callee$
src/components/CheckoutForm.js:17
14 | let response = await fetch("/charge", {
15 | method: "POST",
16 | headers: {"Content-Type": "text/plain"},
> 17 | body: token.id
18 | });
19 |
20 | if (response.ok) this.setState({complete: true});
服务器.js
const app = require("express")();
const stripe = require("stripe")("STRIPE_SECRET_KEY");
app.use(require("body-parser").text());
app.post("/charge", async (req, res) => {
try {
let {status} = await stripe.charges.create({
amount: 2000,
currency: "usd",
description: "An example charge",
source: req.body
});
res.json({status});
} catch (err) {
res.status(500).end();
}
});
app.listen(9000, () => console.log("Listening on port 9000"));
CheckoutForm.js
import React, {Component} from 'react';
import {CardElement, injectStripe} from 'react-stripe-elements';
class CheckoutForm extends Component {
constructor(props) {
super(props);
this.state = {complete: false};
this.submit = this.submit.bind(this);
}
async submit(ev) {
let {token} = await this.props.stripe.createToken({name: "Name"});
let response = await fetch("/charge", {
method: "POST",
headers: {"Content-Type": "text/plain"},
body: token.id
});
if (response.ok) this.setState({complete: true});
}
render() {
if (this.state.complete) return <h1>Purchase Complete</h1>;
return (
<div className="checkout">
<p>Would you like to complete the purchase?</p>
<CardElement />
<button onClick={this.submit}>Send</button>
</div>
);
}
}
export default injectStripe(CheckoutForm);
App.js
import {Elements, StripeProvider} from 'react-stripe-elements';
import CheckoutForm from './components/CheckoutForm';
<StripeProvider apiKey="STRIPE_PUBLISHABLE_KEY">
<div className="example">
<h1>React Stripe Elements Example</h1>
<Elements>
<CheckoutForm />
</Elements>
</div>
</StripeProvider>
我不是最好的,到目前为止还没有找到任何可以帮助我的东西。 请和谢谢你
【问题讨论】:
标签: javascript token stripe-payments