【发布时间】:2022-01-16 09:40:13
【问题描述】:
我正在尝试在 react Js 和 Django 中通过条纹使用卡支付。我正在关注https://betterprogramming.pub/how-to-integrate-django-react-app-with-stripe-payments-95709b3f23e5这个教程。
前端
const handleSubmit = async (event) => {
event.preventDefault();
const card = elements.getElement(CardElement);
const {paymentMethod, error} = await stripe.createPaymentMethod({
type: 'card',
card: card
});
ApiService.saveStripeInfo({
email, payment_method_id: paymentMethod.id})
.then(response => {
console.log(response.data);
}).catch(error => {
console.log(error)
})
}
export const api = axios.create({
baseURL: API_URL,
headers: {
"Content-type": "application/json"
}
});
export default class ApiService{
static saveStripeInfo(data={}){
return api.post(`${API_URL}/payments/save-stripe-info/`, data)
}
}
服务器
@api_view(['POST'])
def test_payment(request):
test_payment_intent = stripe.PaymentIntent.create(
amount=1000, currency='pln',
payment_method_types=['card'],
receipt_email='test@example.com')
return Response(status=status.HTTP_200_OK, data=test_payment_intent)
def save_stripe_info(request):
print('this => ',request.data)
data = request.data
email = data['email']
payment_method_id = data['payment_method_id']
# creating customer
customer = stripe.Customer.create(
email=email, payment_method=payment_method_id)
return Response(status=status.HTTP_200_OK,
data={
'message': 'Success',
'data': {'customer_id': customer.id}
}
)
但是每当我点击提交按钮时,它都会给我以下错误
AttributeError: 'WSGIRequest' 对象没有属性 'data' [12/Dec/2021 21:55:57]“POST /payments/save-stripe-info/HTTP/1.1”500 71355
完整代码请访问https://betterprogramming.pub/how-to-integrate-django-react-app-with-stripe-payments-95709b3f23e5
【问题讨论】:
标签: reactjs django stripe-payments