Guest User

Untitled

a guest
Sep 25th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. +----+--------------------+---------------------+---------------+--------------------------+---------+--------------+--------+
  2. | id | charge_id | created_at | email | description | user_id | theplan | amount |
  3. +----+--------------------+---------------------+---------------+--------------------------+---------+--------------+--------+
  4. | 16 | cus_72tu5qgHlo5qQM | 2015-09-25 04:50:42 | root@root.com | Charge for root@root.com | 1 | test1usd | 195 |
  5. | 17 | cus_72uCNERCFC6ce2 | 2015-09-25 05:09:02 | root@root.com | Charge for root@root.com | 1 | test12months | 1999 |
  6. +----+--------------------+---------------------+---------------+--------------------------+---------+--------------+--------+
  7.  
  8. class Payment(models.Model):
  9. class Meta:
  10. app_label = 'name'
  11.  
  12. charge_id = models.CharField(max_length=128, null=True, blank=True)
  13. user = models.ForeignKey(User)
  14. created_at = models.DateTimeField(null=False, auto_now_add=True)
  15. email = models.CharField(max_length=256, null=True, blank=True)
  16. theplan = models.CharField(max_length=64, null=True, blank=True)
  17. amount = models.CharField(max_length=16, null=True, blank=True)
  18. description = models.CharField(max_length=256, null=True, blank=True)
  19.  
  20. class StripeApi(View):
  21. @staticmethod
  22. def post(request):
  23. charge = stripe.Customer.create(
  24. source=request.POST['stripeToken'],
  25. email=request.POST['stripeEmail'],
  26. plan=request.POST['plan'],
  27. description='Charge for {}'.format(request.POST.get("stripeEmail", "")),
  28. )
  29. customer_description = charge["description"]
  30. customer_email = charge["email"]
  31. customer_id = charge.id
  32. plan = stripe.Plan.retrieve(
  33. request.POST['plan']
  34. )
  35. payment_plan = plan.id
  36. payment_amount = plan.amount
  37. try:
  38. site_user = User.objects.get(username=customer_email)
  39. Payment.objects.create(
  40. charge_id=customer_id,
  41. user=site_user,
  42. email=customer_email,
  43. description=customer_description,
  44. theplan=payment_plan,
  45. amount=payment_amount
  46. )
  47. except User.DoesNotExist:
  48. return HttpResponse(
  49. json.dumps({
  50. 'success': 'There is no user with this email address in DB'
  51. })
  52. )
  53. return HttpResponse(
  54. json.dumps({
  55. 'success': True
  56. }), content_type="application/json")
  57. @staticmethod
  58. def get(request):
  59. return render(request, 'index.html',
  60. {
  61. 'stripe_pub_key': settings.STRIPE_PUBLISHABLE_KEY
  62. })
Add Comment
Please, Sign In to add comment