I am creating an e-commerce with Django. During the user registration, I would like to get the user's credit card information so that, in the future, when the user tries to buy something, he/she does not have to insert his/her credit card information again.
Currently, I am having problems dealing with Stripe (creating this form to get the credit card info and then processing payment with the stored info both in Django and Stripe).
Based on the Stripe documentation, I understood that I should save a user's customer id in my Django database, and then, in the future, I will use this customer id to retrieve this user's info from stripe. However, I am very confused about this process:
- 1) Get card information.
- 2) Save card information (customer id in Django and create customer in Stripe)
- 3) Use the saved information for future purchases.
This is my checkout view in Django: def checkout_two(request):
if request.method == "POST":
# Creating a Customer in the stripe platform.
customer = stripe.Customer.create(
# How to create such form to get the info below?
source=request.POST['stripeToken'], # How to get token?
# email="john@email.com", # How to get email?
)
# Charging the Customer instead of the card:
# charge = stripe.Charge.create(
# amount=1000,
# currency='usd',
# customer=customer.id,
# )
# YOUR CODE: Save the customer ID and other info in a database for later.
obj = Customer.objects.create(customer_id=customer.id)
# When it's time to charge the customer again, retrieve the customer ID.
# charge = stripe.Charge.create(
# amount=1500, # $15.00 this time
# currency='usd',
# customer=obj.id, # Previously stored, then retrieved
# )
return render(request, "payments/checkout_two.html", {"customer_id":obj.id})
else:
context = {"stripe_publishable_key":STRIPE_PUBLISHABLE_KEY}
return render(request, "payments/checkout_two.html", context)