Esempio n. 1
0
def ipn(request, order_class):
    """Instant Payment Notification callback.
	See https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro
	for details."""
    # TODO: add some logging here, as all the errors will occur silently
    try:
        payment_id = int(request.POST['invoice'].split("-")[1])
        if order_class == "advertising":
            payment = get_object_or_404(Payment,
                                        id=payment_id,
                                        status__in=('in_progress',
                                                    'partially_paid', 'paid',
                                                    'failed'),
                                        backend='paypal')
        else:
            payment = get_object_or_404(FeaturedEventPayment,
                                        id=payment_id,
                                        status__in=('in_progress',
                                                    'partially_paid', 'paid',
                                                    'failed'),
                                        backend='paypal')

    except (KeyError, ValueError):
        return HttpResponseBadRequest()
    charset = request.POST.get('charset', 'UTF-8')
    request.encoding = charset
    data = request.POST.dict()
    data['cmd'] = '_notify-validate'

    # Encode data as PayPal wants it.
    for k, v in data.items():
        data[k] = v.encode(charset)

    udata = urlencode(data)
    url = get_backend_settings('paypal')['url']
    r = urllib2.Request(url)
    r.add_header("Content-type", "application/x-www-form-urlencoded")
    h = urllib2.urlopen(r, udata)
    result = h.read()
    h.close()

    if result == "VERIFIED":
        # TODO: save foreign-id from data['txn_id']
        if payment.status == 'in_progress':
            amount = Decimal(request.POST['mc_gross'])
            # TODO: handle different IPN calls, e.g. refunds
            payment.on_payment(amount)
        return HttpResponse('OKTHXBAI')
    else:
        # XXX: marking the payment as failed would create a security hole
        return HttpResponseNotFound()
Esempio n. 2
0
def ipn(request, order_class):
	"""Instant Payment Notification callback.
	See https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro
	for details."""
	# TODO: add some logging here, as all the errors will occur silently
	try:
		payment_id = int(request.POST['invoice'].split("-")[1])
		if order_class=="advertising":
			payment = get_object_or_404(Payment, id=payment_id, status__in=('in_progress', 'partially_paid', 'paid', 'failed'), backend='paypal')
		else:
			payment = get_object_or_404(FeaturedEventPayment, id=payment_id, status__in=('in_progress', 'partially_paid', 'paid', 'failed'), backend='paypal')
		
	except (KeyError, ValueError):
		return HttpResponseBadRequest()
	charset = request.POST.get('charset', 'UTF-8')
	request.encoding = charset
	data = request.POST.dict()
	data['cmd'] = '_notify-validate'

	# Encode data as PayPal wants it.
	for k, v in data.items():
		data[k] = v.encode(charset)

	udata = urlencode(data)
	url = get_backend_settings('paypal')['url']
	r = urllib2.Request(url)
	r.add_header("Content-type", "application/x-www-form-urlencoded")
	h = urllib2.urlopen(r, udata)
	result = h.read()
	h.close()

	if result == "VERIFIED":
		# TODO: save foreign-id from data['txn_id']
		if payment.status == 'in_progress':
			amount = Decimal(request.POST['mc_gross'])
			# TODO: handle different IPN calls, e.g. refunds
			payment.on_payment(amount)
		return HttpResponse('OKTHXBAI')
	else:
		# XXX: marking the payment as failed would create a security hole
		return HttpResponseNotFound()
Esempio n. 3
0
 def __init__(self, *args, **kwargs):
     super(PaypalConfirmationForm, self).__init__(*args, **kwargs)
     # a keyword, haha :)
     self.fields['return'] = forms.CharField(widget=forms.HiddenInput())
     paypal = get_backend_settings('paypal')
     customer = self.payment.get_customer_data()
     self.fields['invoice'].initial = self.payment.pk
     self.fields['first_name'].initial = customer.get('first_name', '')
     self.fields['last_name'].initial = customer.get('last_name', '')
     self.fields['email'].initial = customer.get('email', '')
     self.fields['city'].initial = customer.get('city', '')
     self.fields['country'].initial = customer.get('country_iso', '')
     self.fields['zip'].initial = customer.get('postal_code', '')
     self.fields['amount'].initial = self.payment.amount
     self.fields['currency_code'].initial = self.payment.currency
     self.fields['return'].initial = paypal['url']
     self.fields['business'].initial = paypal['email']
     i = 1
     for item in self.payment.get_items():
         self.fields['item_name_%d' %
                     i] = forms.CharField(widget=forms.HiddenInput())
         self.fields['item_name_%d' % i].initial = item['name']
         self.fields['amount_%d' %
                     i] = forms.DecimalField(widget=forms.HiddenInput())
         self.fields['amount_%d' % i].initial = item['unit_price']
         self.fields['quantity_%d' %
                     i] = forms.DecimalField(widget=forms.HiddenInput())
         self.fields['quantity_%d' % i].initial = item['quantity']
         i += 1
     try:
         self.fields['return'].initial = paypal['return_url']
     except KeyError:
         # TODO: use https when needed
         self.fields['return'].initial = 'http://%s%s' % (
             Site.objects.get_current().domain,
             reverse('mamona-paypal-return',
                     kwargs={'payment_id': self.payment.id}))
     self.fields['notify_url'].initial = 'http://%s%s' % (
         Site.objects.get_current().domain, reverse('mamona-paypal-ipn'))
Esempio n. 4
0
	def __init__(self, *args, **kwargs):
		super(PaypalConfirmationForm, self).__init__(*args, **kwargs)
		# a keyword, haha :)
		self.fields['return'] = forms.CharField(widget=forms.HiddenInput())
		paypal = get_backend_settings('paypal')
		customer = self.payment.get_customer_data()
		self.fields['invoice'].initial = self.payment.pk
		self.fields['first_name'].initial = customer.get('first_name', '')
		self.fields['last_name'].initial = customer.get('last_name', '')
		self.fields['email'].initial = customer.get('email', '')
		self.fields['city'].initial = customer.get('city', '')
		self.fields['country'].initial = customer.get('country_iso', '')
		self.fields['zip'].initial = customer.get('postal_code', '')
		self.fields['amount'].initial = self.payment.amount
		self.fields['currency_code'].initial = self.payment.currency
		self.fields['return'].initial = paypal['url']
		self.fields['business'].initial = paypal['email']
		i = 1
		for item in self.payment.get_items():
			self.fields['item_name_%d' % i] = forms.CharField(widget=forms.HiddenInput())
			self.fields['item_name_%d' % i].initial = item['name']
			self.fields['amount_%d' % i] = forms.DecimalField(widget=forms.HiddenInput())
			self.fields['amount_%d' % i].initial = item['unit_price']
			self.fields['quantity_%d' % i] = forms.DecimalField(widget=forms.HiddenInput())
			self.fields['quantity_%d' % i].initial = item['quantity']
			i += 1
		try:
			self.fields['return'].initial = paypal['return_url']
		except KeyError:
			# TODO: use https when needed
			self.fields['return'].initial = 'http://%s%s' % (
					Site.objects.get_current().domain,
					reverse('mamona-paypal-return', kwargs={'payment_id': self.payment.id})
					)
		self.fields['notify_url'].initial = 'http://%s%s' % (
				Site.objects.get_current().domain,
				reverse('mamona-paypal-ipn')
				)
Esempio n. 5
0
def ipn(request):
	"""Instant Payment Notification callback.
	See https://cms.paypal.com/us/cgi-bin/?&cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro
	for details."""
	# TODO: add some logging here, as all the errors will occur silently
	payment = get_object_or_404(Payment, id=request.POST['invoice'], status='in_progress', backend='paypal')
	data = list(request.POST.items())
	data.insert(0, ('cmd', '_notify-validate'))
	udata = urlencode(data)
	url = get_backend_settings('paypal')['url']
	r = urllib2.Request(url)
	r.add_header("Content-type", "application/x-www-form-urlencoded")
	h = urllib2.urlopen(r, udata)
	result = h.read()
	h.close()

	if result == "VERIFIED":
		# TODO: save foreign-id from data['txn_id']
		amount = Decimal(request.POST['mc_gross'])
		payment.on_payment(amount)
		return HttpResponse('OKTHXBAI')
	else:
		# XXX: marking the payment as failed would create a security hole
		return HttpResponseNotFound()
Esempio n. 6
0
def get_confirmation_form(payment):
	paypal = get_backend_settings('paypal')
	form = forms.PaypalConfirmationForm(payment=payment)
	return {'form': form, 'method': 'post', 'action': paypal['url']}
Esempio n. 7
0
def get_confirmation_form(payment):
    paypal = get_backend_settings('paypal')
    form = forms.PaypalConfirmationForm(payment=payment)
    return {'form': form, 'method': 'post', 'action': paypal['url']}