Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def checkout(request):
- payment_intents = stripe.PaymentIntent.list()
- payment_intent_extract = [next(x['id'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
- payment_intent_id = ", ".join(payment_intent_extract)
- client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
- client_secret = ", ".join(client_secret_extract)
- return render_template('membership_payment.html', client_secret)
- def PaymentView(request):
- user_membership = get_user_membership(request)
- try:
- selected_membership = get_selected_membership(request)
- except:
- return redirect(reverse("memberships:select"))
- publishKey = settings.STRIPE_PUBLISHABLE_KEY
- #if user clicks submit payment then run this code
- if request.method == "POST":
- try:
- stripe.PaymentIntent.create(
- amount=25 * 100,
- currency='usd',
- payment_method_types=['card'],
- customer='cus_EszB7tXQtjsQhP',
- setup_future_usage='off_session'
- )
- stripe.PaymentMethod.attach(
- 'pm_card_visa',
- customer='cus_EszB7tXQtjsQhP'
- )
- payment_methods = stripe.PaymentMethod.list(
- customer="cus_EszB7tXQtjsQhP",
- type="card",
- )
- payment_method_id_extract = [next(x['id'] for x in payment_methods.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
- payment_method_id = ", ".join(payment_method_id_extract)
- #create subscription
- subscription = stripe.Subscription.create(
- customer=user_membership.stripe_customer_id,
- default_payment_method=payment_method_id,
- items=[
- { "plan": selected_membership.stripe_plan_id },
- ]
- )
- payment_intent_extract = [next(x['id'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
- payment_intent_id = ", ".join(payment_intent_extract)
- intent = stripe.PaymentIntent.retrieve(payment_intent_id)
- charges = intent.charges.data
- stripe.PaymentIntent.confirm(
- payment_intent_id,
- payment_method='pm_card_visa'
- )
- subscriptions = stripe.Subscription.list(status='active')
- subscription_list = [x['id'] for x in subscriptions.auto_paging_iter() if x['customer'] == user_membership.stripe_customer_id]
- for x in subscription_list:
- stripe.Subscription.delete(x)
- return redirect(reverse('memberships:update-transactions',
- kwargs={
- 'subscription_id': subscription.id
- }))
- except:
- messages.info(request, "An error has occurred, investigate it in the console")
- payment_intents = stripe.PaymentIntent.list()
- client_secret_extract = [next(x['client_secret'] for x in payment_intents.auto_paging_iter() if x['customer'] == 'cus_EszB7tXQtjsQhP')]
- client_secret = ", ".join(client_secret_extract)
- context = {
- 'publishKey': publishKey,
- 'selected_membership': selected_membership,
- 'client_secret': client_secret
- }
- return render(request, "memberships/membership_payment.html", context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement