Advertisement
Brianojee

Untitled

Feb 29th, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. if form.is_valid():
  2.             try:
  3.                 customer = stripe.Customer.create(
  4.                         email=form.cleaned_data['email'],
  5.                         card=form.cleaned_data['stripe_id'],  # this is currently the card token/id
  6.                         plan='REG_MONTHLY_',
  7.                 )
  8.  
  9.                 if customer:
  10.                     user = form.save()  # save here to create the user and get its instance
  11.  
  12.                     # now we replace the card id with the actual user id for later
  13.                     user.stripe_id = customer.id
  14.                     user.subscription_end = arrow.now().replace(weeks=+4).datetime  # add 4 weeks from now
  15.                     user.save()
  16.  
  17.                 # check we saved correctly and can login
  18.                 user = auth.authenticate(email=request.POST.get('email'),
  19.                                          password=request.POST.get('password1'))
  20.  
  21.                 if user:
  22.                     auth.login(request, user)
  23.                     messages.success(request, "You have successfully registered")
  24.                     return redirect(reverse('profile'))
  25.  
  26.                 else:
  27.                     messages.error(request, "unable to log you in at this time!")
  28.  
  29.             except stripe.error.CardError, e:
  30.                 form.add_error(request, "Your card was declined!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement