Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. def checkout(request):
  2. payment_intents = stripe.PaymentIntent.list()
  3. payment_intent_extract = [next(x['id'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  4. payment_intent_id = ", ".join(payment_intent_extract)
  5. client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  6. client_secret = ", ".join(client_secret_extract)
  7. return render_template('membership_payment.html', client_secret)
  8.  
  9. def PaymentView(request):
  10. user_membership = get_user_membership(request)
  11. try:
  12. selected_membership = get_selected_membership(request)
  13. except:
  14. return redirect(reverse("memberships:select"))
  15. publishKey = settings.STRIPE_PUBLISHABLE_KEY
  16. #if user clicks submit payment then run this code
  17. if request.method == "POST":
  18. try:
  19.  
  20. stripe.PaymentIntent.create(
  21. amount=25 * 100,
  22. currency='usd',
  23. payment_method_types=['card'],
  24. customer='cus_EszB7tXQtjsQhP',
  25. setup_future_usage='off_session'
  26. )
  27.  
  28. stripe.PaymentMethod.attach(
  29. 'pm_card_visa',
  30. customer='cus_EszB7tXQtjsQhP'
  31. )
  32.  
  33. payment_methods = stripe.PaymentMethod.list(
  34. customer="cus_EszB7tXQtjsQhP",
  35. type="card",
  36. )
  37.  
  38. payment_method_id_extract = [next(x['id'] for x in payment_methods.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  39. payment_method_id = ", ".join(payment_method_id_extract)
  40.  
  41. #create subscription
  42. subscription = stripe.Subscription.create(
  43. customer=user_membership.stripe_customer_id,
  44. default_payment_method=payment_method_id,
  45. items=[
  46. { "plan": selected_membership.stripe_plan_id },
  47. ]
  48. )
  49.  
  50. payment_intent_extract = [next(x['id'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  51. payment_intent_id = ", ".join(payment_intent_extract)
  52. intent = stripe.PaymentIntent.retrieve(payment_intent_id)
  53. charges = intent.charges.data
  54.  
  55. stripe.PaymentIntent.confirm(
  56. payment_intent_id,
  57. payment_method='pm_card_visa'
  58. )
  59.  
  60. subscriptions = stripe.Subscription.list(status='active')
  61. subscription_list = [x['id'] for x in subscriptions.auto_paging_iter() if x['customer'] == user_membership.stripe_customer_id]
  62. for x in subscription_list:
  63. stripe.Subscription.delete(x)
  64.  
  65. return redirect(reverse('memberships:update-transactions',
  66. kwargs={
  67. 'subscription_id': subscription.id
  68. }))
  69.  
  70. except:
  71. messages.info(request, "An error has occurred, investigate it in the console")
  72.  
  73. payment_intents = stripe.PaymentIntent.list()
  74. client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  75. client_secret = ", ".join(client_secret_extract)
  76. context = {
  77. 'publishKey': publishKey,
  78. 'selected_membership': selected_membership,
  79. 'client_secret': client_secret
  80. }
  81.  
  82. return render(request, "memberships/membership_payment.html", context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement