Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 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 request.method == "POST":
  9. try:
  10.  
  11. stripe.PaymentIntent.create(
  12. amount=25 * 100,
  13. currency='usd',
  14. payment_method_types=['card'],
  15. customer='cus_EszB7tXQtjsQhP',
  16. setup_future_usage='off_session'
  17. )
  18.  
  19. stripe.PaymentMethod.attach(
  20. 'pm_card_visa',
  21. customer='cus_EszB7tXQtjsQhP'
  22. )
  23.  
  24. payment_methods = stripe.PaymentMethod.list(
  25. customer="cus_EszB7tXQtjsQhP",
  26. type="card",
  27. )
  28.  
  29. payment_method_id_extract = [next(x['id'] for x in payment_methods.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  30. payment_method_id = ", ".join(payment_method_id_extract)
  31.  
  32. #create subscription
  33. subscription = stripe.Subscription.create(
  34. customer=user_membership.stripe_customer_id,
  35. default_payment_method=payment_method_id,
  36. items=[
  37. { "plan": selected_membership.stripe_plan_id },
  38. ]
  39. )
  40.  
  41. payment_intent_extract = [next(x['id'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  42. payment_intent_id = ", ".join(payment_intent_extract)
  43. intent = stripe.PaymentIntent.retrieve(payment_intent_id)
  44. charges = intent.charges.data
  45.  
  46. stripe.PaymentIntent.confirm(
  47. payment_intent_id,
  48. payment_method='pm_card_visa'
  49. )
  50.  
  51.  
  52. """
  53. #save the token to the customer in stripe API
  54. customer = stripe.Customer.retrieve(user_membership.stripe_customer_id)
  55. customer.source = token # 4242424242424242
  56. customer.save()
  57. """
  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.  
  66. '''
  67. Now we can create the subscription using only the customer as we don't need to pass their
  68. credit card source anymore
  69. '''
  70.  
  71.  
  72.  
  73. """
  74. return redirect(reverse('memberships:update-transactions',
  75. kwargs={
  76. 'subscription_id': subscription.id
  77. }))
  78. """
  79. except:
  80. messages.info(request, "An error has occurred, investigate it in the console")
  81.  
  82. payment_intents = stripe.PaymentIntent.list()
  83. client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
  84. client_secret = ", ".join(client_secret_extract)
  85. context = {
  86. 'publishKey': publishKey,
  87. 'selected_membership': selected_membership,
  88. 'client_secret': client_secret
  89. }
  90.  
  91. return render(request, "memberships/membership_payment.html", context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement