NewsBlur/vendor/paypal/pro/forms.py

50 lines
2 KiB
Python
Raw Normal View History

2010-10-16 18:59:02 -04:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import forms
2014-11-07 16:22:19 -08:00
from paypal.pro.fields import CreditCardField, CreditCardExpiryField, CreditCardCVV2Field, CountryField
from paypal.pro.exceptions import PayPalFailure
2010-10-16 18:59:02 -04:00
class PaymentForm(forms.Form):
"""Form used to process direct payments."""
firstname = forms.CharField(255, label="First Name")
lastname = forms.CharField(255, label="Last Name")
street = forms.CharField(255, label="Street Address")
city = forms.CharField(255, label="City")
state = forms.CharField(255, label="State")
countrycode = CountryField(label="Country", initial="US")
zip = forms.CharField(32, label="Postal / Zip Code")
acct = CreditCardField(label="Credit Card Number")
expdate = CreditCardExpiryField(label="Expiration Date")
cvv2 = CreditCardCVV2Field(label="Card Security Code")
2014-11-07 16:22:19 -08:00
currencycode = forms.CharField(widget=forms.HiddenInput(), initial="USD")
2010-10-16 18:59:02 -04:00
def process(self, request, item):
"""Process a PayPal direct payment."""
2014-11-07 16:22:19 -08:00
from paypal.pro.helpers import PayPalWPP
wpp = PayPalWPP(request)
2010-10-16 18:59:02 -04:00
params = self.cleaned_data
params['creditcardtype'] = self.fields['acct'].card_type
params['expdate'] = self.cleaned_data['expdate'].strftime("%m%Y")
2014-11-07 16:22:19 -08:00
params['ipaddress'] = request.META.get("REMOTE_ADDR", "")
2010-10-16 18:59:02 -04:00
params.update(item)
2014-11-07 16:22:19 -08:00
try:
# Create single payment:
if 'billingperiod' not in params:
nvp_obj = wpp.doDirectPayment(params)
# Create recurring payment:
else:
nvp_obj = wpp.createRecurringPaymentsProfile(params, direct=True)
except PayPalFailure:
return False
return True
2010-10-16 18:59:02 -04:00
class ConfirmForm(forms.Form):
"""Hidden form used by ExpressPay flow to keep track of payer information."""
token = forms.CharField(max_length=255, widget=forms.HiddenInput())
2014-11-07 16:22:19 -08:00
PayerID = forms.CharField(max_length=255, widget=forms.HiddenInput())