Example #1
0
def curso(req, curso_slug):
    if req.method == 'POST':
        curso = get_object_or_404(Curso, slug=curso_slug)
        vs = {'curso': curso}  # variables para el rendereo de la plantilla
        action = req.POST.get('action')

        if action and curso.activado:
            if action == 'buy':
                nombre = req.POST.get('nombre')
                email = req.POST.get('email')
                tel = req.POST.get('telefono')
                quantity = req.POST.get('quantity')
                token = req.POST.get('stripeToken')

                if email:
                    try:
                        validate_email(email)
                    except ValidationError:
                        email = False

                # si ya ha pagado hoy
                if CursoPago.objects.filter(email=email, curso=curso, charged=True, fecha__gt=datetime.now() - timedelta(days=1)).exists():
                    return HttpResponse('ERR ALREADY COMPLETED')

                if CursoPago.objects.filter(email=email, curso=curso, charged=False, fecha__gt=datetime.now() - timedelta(days=1)).count() > 5:
                    return HttpResponse('ERR TOO MANY TRIES')

                if nombre and email and tel and quantity and token:
                    # realizar el cargo con la api de stripe
                    p = CursoPago(nombre=nombre, email=email, telefono=tel, pais=get_pais(req.META), ip=get_ip(req.META), ua=req.META['HTTP_USER_AGENT'], quantity=quantity, curso=curso, method='card')
                    p.save()

                    concept = calculate(int(quantity), curso.precio)

                    try:
                        charge = stripe.Charge.create(
                            amount=concept['amount'] * 100,
                            currency='usd',
                            card=token,
                            description=email
                        )
                    except Exception, e:
                        p.error = str(e)
                        p.save()

                        send_mail(u'¿Podemos ayudarte de alguna forma?', u'Vimos que tuviste problemas pagando el %s de Mejorando.la\n¿Podemos ayudarte de alguna forma?\nNo olvides que puedes contactarnos vía skype en mejorandola' % curso.nombre, 'Ventas Mejorando.la <*****@*****.**>', [p.email], fail_silently=True)

                        return HttpResponse('ERR')

                    # si no se realiza el cargo regresar error
                    if not charge.paid:
                        return HttpResponse('ERR')

                    p.charged = True
                    p.save()

                    req.session['p32'] = p.id

                    return HttpResponse('OK')

                else:
                    return HttpResponse('ERR')

            elif action == 'deposit' or action == 'paypal':

                nombre = req.POST.get('nombre')
                email = req.POST.get('email')
                tel = req.POST.get('telefono')
                quantity = req.POST.get('quantity')

                if email:
                    try:
                        validate_email(email)
                    except ValidationError:
                        email = False

                if CursoPago.objects.filter(email=email, curso=curso, charged=False, fecha__gt=datetime.now() - timedelta(days=1)).count() > 5:
                    return HttpResponse('ERR TOO MANY TRIES')

                if nombre and email and tel and quantity:
                    p = CursoPago(nombre=nombre, email=email, telefono=tel, pais=get_pais(req.META), ip=get_ip(req.META), ua=req.META['HTTP_USER_AGENT'], quantity=quantity, curso=curso, method=action)
                    p.save()

                    return HttpResponse('OK')

                else:
                    return HttpResponse('ERR')

            elif action == 'register':
                email = req.POST.getlist('email')
                pago = req.session.get('p32')

                if email and pago:
                    p = get_object_or_404(CursoPago, id=pago)

                    # no permitir registro sin haber pagado
                    if not p.charged:
                        return HttpResponse('ERR')

                    for e in email:
                        r = CursoRegistro(email=e, pago=p)
                        r.save()

                    return HttpResponse('OK')

                else:
                    return HttpResponse('ERR')

            else:
                return HttpResponse('ERR')
        else:
            return HttpResponse('ERR')
Example #2
0
def curso(req, curso_slug):
	if req.method == 'POST':
		curso  = get_object_or_404(Curso, slug=curso_slug)
		vs 	   = { 'curso': curso } # variables para el rendereo de la plantilla
		action = req.POST.get('action')

		if action:
			if action == 'buy':
				nombre 	 = req.POST.get('nombre')
				email  	 = req.POST.get('email')
				tel    	 = req.POST.get('telefono')
				quantity = req.POST.get('quantity')
				token 	 = req.POST.get('stripeToken')

				if nombre and email and tel and quantity and token:
					# realizar el cargo con la api de stripe
					p = CursoPago(nombre=nombre, email=email, telefono=tel, pais=get_pais(req.META), quantity=quantity, curso=curso, method='card')
					p.save()

					concept = calculate(int(quantity), curso.precio)

					try:
						charge = stripe.Charge.create(
							amount		= concept['amount']*100,
							currency	= 'usd',
							card	    = token,
							description = email
						)
					except Exception, e:  
						p.error = str(e)
						p.save()

						send_mail(u'¿Podemos ayudarte de alguna forma?', u'Vimos que tuviste problemas pagando el %s de Mejorando.la\n¿Podemos ayudarte de alguna forma?\nNo olvides que puedes contactarnos vía skype en mejorandola' % curso.nombre, 'Ventas Mejorando.la <*****@*****.**>', [p.email], fail_silently=True)

						return HttpResponse('ERR')

					# si no se realiza el cargo regresar error
					if not charge.paid: return HttpResponse('ERR')

					p.charged = True
					p.save()

					req.session['p32'] = p.id

					return HttpResponse('OK')

				else: return HttpResponse('ERR')

			elif action == 'deposit' or action == 'paypal':
				nombre 	 = req.POST.get('nombre')
				email  	 = req.POST.get('email')
				tel    	 = req.POST.get('telefono')
				quantity = req.POST.get('quantity')

				if nombre and email and tel and quantity:
					p = CursoPago(nombre=nombre, email=email, telefono=tel, pais=get_pais(req.META), quantity=quantity, curso=curso, method=action)	
					p.save()

					return HttpResponse('OK')

				else: return HttpResponse('ERR')

			elif action == 'register': 
				email = req.POST.getlist('email')
				pago  = req.session.get('p32')

				if email and pago:
					p = get_object_or_404(CursoPago, id=pago)

					# no permitir registro sin haber pagado
					if not p.charged: return HttpResponse('ERR')

					for e in email:
						r = CursoRegistro(email=e, pago=p)
						r.save()

					return HttpResponse('OK')
					
				else: return HttpResponse('ERR')

			else: return HttpResponse('ERR')
		else: return HttpResponse('ERR')