0

When I submit card details from frontend it fails to successfully POST in Django Rest server.

I think the error with Django code. If you know how to configure strip with Django, please help

DJANGO VIEWS.PY

@csrf_exempt
def chargeview(request,*args,**kwargs): # new
    if request.method == 'POST':

        charge = stripe.Charge.create(
            amount=500,
            currency='usd',
            description='A Django charge',
            source=request.POST['token']
        )
        return render(request, 'payments/charge.html')

ERROR

raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'stripeToken'

REACT JS CODE

import React, { useState } from "react";
import StripeCheckout from "react-stripe-checkout";
import axios from 'axios';

const StripeBtn = () => {
  const publishableKey = "pk_test_some key";
   const price = 1000
   const onToken = token => {
    const body = {
      amount: 1000,
      token: token
  };
  console.log(body.token)
  axios
      .post("http://localhost:8000/payments/", {token:body.token})
      .then(response => {
        console.log(response);

      })
      .catch(error => {
        console.log("Payment Error: ", error);
        alert("Payment Error");
      });
  };
  return (
    <StripeCheckout
      label="Go Premium" //Component button text
      name="Business LLC" //Modal Header
      description="Upgrade to a premium account today."
      panelLabel="Go Premium" //Submit button in modal
      amount={price} //Amount in cents $9.99
      token={onToken}
      stripeKey={publishableKey}
      image="" //Pop-in header image
      billingAddress={false}
    />
  );
};
export default StripeBtn;

RESULTANT TOKEN FROM REACT FRONTEND THAT PASS TO DJANGO USING AXIOS


{id: "tok_1G***WLSNV4Son7o8TBJY", object: "token", card: {…}, client_ip: "***", created: ***, …}
card: {id: "card_1Gmn7*****oPKqrwrXa", object: "card", address_city: null, address_country: null, address_line1: null, …}
client_ip: "1***77"
created: 15***39325
email: "anoop***il.com"
id: "tok_1Gm***4Son7o8TBJY"
livemode: false
object: "token"
type: "card"
used: false
Anoop K George
  • 1,605
  • 12
  • 40
  • Did you take a look at https://stackoverflow.com/questions/61370921/multivaluedictkeyerror-stripetoken-django-error – Aurélien May 25 '20 at 19:21
  • The error message doesn't exactly jive with your code. Are you sure you are running the most up-to-date copy of your backend/frontend? The error message implies that you are trying to access a "stripeToken" property on a dict which doesn't exist. But you don't appear to be doing that anywhere. – ttmarek May 25 '20 at 20:42
  • When user submit card it generate a token, I have no Idea what to do with that – Anoop K George May 25 '20 at 20:44

0 Answers0