[Django]-Proper way to create stripe charge in django/python

5👍

Here’s what we use in our project, hope it helps!!

try:
    charge = stripe.Charge.create(
      amount={{amount}}, 
      currency="usd",
      customer={{customer}},
      description={{description}},
      metadata={{this.id}}
  )
except stripe.error.CardError as e:
    # Problem with the card
    pass
except stripe.error.RateLimitError as e:
    # Too many requests made to the API too quickly
    pass
except stripe.error.InvalidRequestError as e:
    # Invalid parameters were supplied to Stripe API
    pass
except stripe.error.AuthenticationError as e:
    # Authentication Error: Authentication with Stripe API failed (maybe you changed API keys recently)
    pass
except stripe.error.APIConnectionError as e:
    # Network communication with Stripe failed
    pass
except stripe.error.StripeError as e:
    # Stripe Error
    pass
else:
    #success

0👍

You might consider using django stripe payments, or using their code for reference.

Leave a comment