Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. def PaymentView(request):
  2. user_membership = get_user_membership(request)
  3. try:
  4. selected_membership = get_selected_membership(request)
  5. except:
  6. return redirect(reverse("memberships:select"))
  7. publishKey = settings.STRIPE_PUBLISHABLE_KEY
  8. #if user clicks submit payment then run this code
  9. if request.method == "POST":
  10. try:
  11. """
  12. token = request.POST['stripeToken']
  13. """
  14. # UPDATE FOR STRIPE API CHANGE 2018-05-21
  15.  
  16. '''
  17. First we need to add the source for the customer
  18. '''
  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.  
  61. """
  62. #save the token to the customer in stripe API
  63. customer = stripe.Customer.retrieve(user_membership.stripe_customer_id)
  64. customer.source = token # 4242424242424242
  65. customer.save()
  66. """
  67.  
  68. """
  69. subscriptions = stripe.Subscription.list(status='active')
  70. subscription_list = [x['id'] for x in subscriptions.auto_paging_iter() if x['customer'] == user_membership.stripe_customer_id]
  71. for x in subscription_list:
  72. stripe.Subscription.delete(x)
  73. """
  74.  
  75. '''
  76. Now we can create the subscription using only the customer as we don't need to pass their
  77. credit card source anymore
  78. '''
  79.  
  80.  
  81.  
  82. """
  83. return redirect(reverse('memberships:update-transactions',
  84. kwargs={
  85. 'subscription_id': subscription.id
  86. }))
  87. """
  88. except:
  89. messages.info(request, "An error has occurred, investigate it in the console")
  90.  
  91. payment_intents = stripe.PaymentIntent.list()
  92. client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  93. client_secret = ", ".join(client_secret_extract)
  94. context = {
  95. 'publishKey': publishKey,
  96. 'selected_membership': selected_membership,
  97. 'client_secret': client_secret
  98. }
  99.  
  100. return render(request, "memberships/membership_payment.html", context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement