justdjango / django-ecommerce

An e-commerce website built with Django
GNU General Public License v3.0
2.08k stars 1.61k forks source link

Invalid Parameter Error #84

Open SaurabhBagade opened 3 years ago

SaurabhBagade commented 3 years ago

I have an issue with the card payment of stripe. So when I did according to the youtube video and tried a dummy card number. It just threw me a, 'Invalid Parameter error'. Can anyone help me with that? Here is my PaymentView...

class PaymentView(View):
    def get(self,  *args, **kwargs):
        return render(self.request, "payment.html")

    def post(self, *args, **kwargs):
        order = Order.objects.get(user=self.request.user, ordered=False)
        token = self.request.POST.get('stripeToken')
        amount = order.get_total()

        try:
            charge = stripe.Charge.create(
                amount=amount,
                currency="usd",
                source=token,
            )
            payment = Payment()
            payment.stripe_charge_id = charge['id']
            payment.user = self.request.user
            payment.amount = amount
            payment.save()

            order.ordered = True
            order.payment = payment
            order.save()
            messages.success(self.request, 'Your order has been successful !')
            return redirect('/')

        except stripe.error.CardError as e:
            # Since it's a decline, stripe.error.CardError will be caught
            body = e.json_body
            err = body.get('error', {})
            print('Status is: %s' % e.http_status)
            print('Code is: %s' % e.code)
            # param is '' in this case
            print('Param is: %s' % e.param)
            print('Message is: %s' % e.user_message)
            return redirect('/')

        except stripe.error.RateLimitError as e:
            # Too many requests made to the API too quickly
            messages.error(self.request, 'Rate Limit Error')
            return redirect('/')

        except stripe.error.InvalidRequestError as e:
            # Invalid parameters were supplied to Stripe's API
            messages.error(self.request, 'Invalid parameters')
            return redirect('/')

        except stripe.error.AuthenticationError as e:
            # Authentication with Stripe's API failed
            # (maybe you changed API keys recently)
            messages.error(self.request, 'Not authenticated')
            return redirect('/')

        except stripe.error.APIConnectionError as e:
            # Network communication with Stripe failed
            messages.error(self.request, 'Network error')
            return redirect('/')

        except stripe.error.StripeError as e:
            # Display a very generic error to the user, and maybe send
            # yourself an email
            messages.error(
                self.request, 'Something went wrong! Please try again !')
            return redirect('/')

        except Exception as e:
            # Something else happened, completely unrelated to Stripe
            messages.error(
                self.request, 'A serious error occured! We have been notified!')
            return redirect('/')
Mayson90 commented 3 years ago

I'm faced with the same error, In my case from stripe side - POST body recieved without "source". It seems self.request.POST.get('stripeToken') not working. Something wrong in payment.html script part...if past some generic token id in source body (stripe.Charge.create) it will show in stripe logs.

Mayson90 commented 3 years ago

Just find a solution...you could use testing token for visa card - "source": "tok_visa"

It will work with card number 4242424242424242 just to make sure that stripe works.

https://stripe.com/docs/testing#cards