Exemplo n.º 1
0
def paypal_ipn(req):

    logging.error('PAYPAL IPN STARTED: payment_status %s, item_number %s, payer_email %s' % (req.POST.get('payment_status'), req.POST.get('item_number'), req.POST.get('payer_email')))

    # si estamos hablando de un pago
    if req.POST.get('payment_status') == 'Completed':

        # validar los datos que se reciben con paypal
        r = urllib.urlopen('https://www.paypal.com/cgi-bin/webscr', req.body + '&cmd=_notify-validate').read()

        logging.error('PAYPAL IPN VALIDATION: %s' % r)

        # respuesta de paypal
        if r == 'VERIFIED':
            curso = get_object_or_404(Curso, id=req.POST.get('item_number'))

            p = CursoPago.objects.filter(email=req.POST.get('payer_email'), curso=curso)

            if p.exists():
                p = p[0]

                p.charged = True
                p.save()
            else:
                p = CursoPago(nombre='%s %s' % (req.POST.get('first_name'), req.POST.get('last_name')), email=req.POST.get('payer_email'), telefono=req.POST.get('contact_phone'), pais=req.POST.get('address_country'), quantity=1, curso=curso, method='paypal', charged=True)
                p.save()

                logging.error('PAYPAL IPN: El pago no esta registrado en la bd')

            r = CursoRegistro(email=p.email, pago=p)
            r.save()

    return HttpResponse('OK')
Exemplo n.º 2
0
def paypal_ipn(req):

	logging.error('PAYPAL IPN STARTED: payment_status %s, item_number %s, payer_email %s' % (req.POST.get('payment_status'), req.POST.get('item_number'), req.POST.get('payer_email')) )

	# si estamos hablando de un pago
	if req.POST.get('payment_status') == 'Completed':

		# validar los datos que se reciben con paypal
		r = urllib.urlopen('https://www.paypal.com/cgi-bin/webscr', req.body+'&cmd=_notify-validate').read()

		logging.error('PAYPAL IPN VALIDATION: %s' % r)

		# respuesta de paypal
		if r == 'VERIFIED':
			curso = get_object_or_404(Curso, id=req.POST.get('item_number'))

			p = CursoPago.objects.filter(email=req.POST.get('payer_email'), curso=curso)

			if p.exists():
				p = p[0]

				p.charged = True
				p.save()
			else:
				p = CursoPago(nombre='%s %s' % (req.POST.get('first_name'), req.POST.get('last_name')), email=req.POST.get('payer_email'), telefono=req.POST.get('contact_phone'), pais=req.POST.get('address_country'), quantity=1, curso=curso, method='paypal', charged=True)	
				p.save()

				logging.error('PAYPAL IPN: El pago no esta registrado en la bd')

			r = CursoRegistro(email=p.email, pago=p)
			r.save()

	return HttpResponse('OK')
Exemplo n.º 3
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')
Exemplo n.º 4
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')
Exemplo n.º 5
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:
					quantity = int(quantity)

					rate  = math.floor( quantity / 5 )
					total = curso.precio * quantity

					# descuento de plazas gratis
					discount = rate * curso.precio

					#aplicar el descuento solo cuando no sea cada 5 (que es cuando hay una plaza gratis mas)
					if (quantity % 5) != 0:
						discount += (curso.precio * 0.1) * (quantity-1 if quantity < 5 else quantity - (5 * rate) )

					amount = total - discount

					# 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()

					try:
						charge = stripe.Charge.create(
							amount		= int(amount)*100,
							currency	= 'usd',
							card	    = token,
							description = email
						)
					except Exception: return HttpResponse('ERR')

					# si se realiza el cargo con exito enviar mail de confirmacion de pago
					if not charge.paid: return HttpResponse('ERR')

					p.charged = True
					p.save()

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

					vs['amount']   = amount
					vs['discount'] = discount
					vs['total']    = total
					vs['pago']     = p

					send_mail('curso_pago', vs, 'Gracias por tu pago al %s de Mejorando.la INC' % curso.nombre, email)

					return HttpResponse('OK')

				else: return HttpResponse('ERR')

			elif action == 'deposit':
				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='deposit')
					p.save()

					send_mail('curso_info', vs, 'Informacion para realizar pago al %s de Mejorando.la INC' % curso.nombre, email)

					return HttpResponse('OK')

				else: return HttpResponse('ERR')
			elif 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='paypal')
					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)

					if not p.charged: return HttpResponse('ERR')

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

						# integracion con la plataforma
						try:
							r = requests.post(u'%spreregistro' % settings.PLATAFORMA_API_URL, { 'slug': curso.slug, 'email': e, 'passwd': settings.PLATAFORMA_API_KEY })
						except: return HttpResponse('ERR')

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

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

	try:
		curso = Curso.objects.get(slug=curso_slug)
	except Curso.DoesNotExist: curso = None

	vs = RequestContext(req, { 'curso': curso, 'publishable_key': settings.STRIPE_PUBLISHABLE_KEY })
	try:
		return render_to_response('%s.html' % curso_slug, vs)
	except TemplateDoesNotExist:
		if not curso: raise Http404

		return render_to_response('cursos/curso.html', vs)
Exemplo n.º 6
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:  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')

	try:
		curso = Curso.objects.get(slug=curso_slug)
	except Curso.DoesNotExist: curso = None

	vs = RequestContext(req, { 'curso': curso, 'publishable_key': settings.STRIPE_PUBLISHABLE_KEY })
	try:
		return render_to_response('%s.html' % curso_slug, vs)
	except TemplateDoesNotExist:
		if not curso: raise Http404

		return render_to_response('cursos/curso.html', vs)