def login(request, success_url=None): if request.user.is_authenticated(): return redirect(reverse('profile')) if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): try: user = auth.authenticate(email=request.POST.get('email'), password=request.POST.get('password')) if user is not None: auth.login(request, user) messages.error(request, "You have successfully logged in") return redirect(reverse('profile')) else: subscription_not_ended = arrow.now() < arrow.get(user.subscription_end) if not subscription_not_ended: form.add_error(None,"Your subscription has now ended") form.add_error(None,"Your email or password was not recognised") except SubscriptionEnded: form.add_error(None,"Your subscription has now ended") else: form = UserLoginForm() args = {'form':form} args.update(csrf(request)) return render(request, 'login.html', args)
def register(request): #if we have a post to the url then populate the registration form with the request.POST information if request.method == 'POST': form = UserRegistrationForm(request.POST) if form.is_valid(): try: customer = stripe.Customer.create( email = form.cleaned_data['email'], card = form.cleaned_data['stripe_id'], plan = 'REG_MONTHLY', ) except stripe.error.CardError, e: messages.error(request, "Your (valid) card was declined!") if customer: user = form.save() user.stripe_id = customer.id user.subscription_end = arrow.now().replace(weeks=+4).datetime user.save() user = auth.authenticate(email=request.POST.get('email'), password = request.POST.get('password1')) if user: auth.login(request,user) messages.success(request, "You have successfully registered. Your customer id number is") return redirect(reverse('profile')) else: messages.error(request,'Unable to log you in at this time!') else: messages.error(request, "We were unable to take a payment with that card!")
def subscriptions_webhook(request): #turn our request.body in to a dictionary so we can easily pull out the values from the file event_json = json.loads(request.body) #Verify the event by fetching it from Stripe try: #firstly verify this is a real event generated by Stripe.com, if it is then assign it to the variable 'event' event = stripe.Event.retrieve(event_json["id"]) #get our user by the customer string (this pulls the User class defined in our models which looks for the inputted stripe_id from the form that was input. user = User.objects.get(stripe_id=event_json["data"]["object"]["customer"]) if user and event_json ['type']=="invoice.payment_succeeded": user.subscription_end = arrow.now().replace(weeks=+4).datetime user.save() except stripe.InvalidRequestError, e: return HttpResponse(status=404)