forked from GamesDoneQuick/donation-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paypalutil.py
100 lines (91 loc) · 3.65 KB
/
paypalutil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from paypal.standard.ipn.forms import PayPalIPNForm;
from paypal.standard.ipn.models import PayPalIPN;
from tracker.models import *;
from datetime import *;
import random;
from decimal import *;
import pytz;
def initialize_ipn_object(request):
flag = None
ipn_obj = None
form = PayPalIPNForm(request.POST)
if form.is_valid():
try:
ipn_obj = form.save(commit=False)
except Exception, e:
flag = "Exception while processing. (%s)" % e
else:
flag = "Invalid form. (%s)" % form.errors
if ipn_obj is None:
ipn_obj = PayPalIPN()
ipn_obj.initialize(request)
if flag is not None:
ipn_obj.set_flag(flag)
return ipn_obj;
def initialize_paypal_donation(donation, ipnObj):
created = False;
try:
donor = Donor.objects.get(paypalemail=ipnObj.payer_email.lower());
except Donor.DoesNotExist:
donor = Donor.objects.create(email=ipnObj.payer_email.lower(), paypalemail=ipnObj.payer_email.lower())
created = True;
donor.save();
if created:
donor.firstname = ipnObj.first_name;
donor.lastname = ipnObj.last_name;
donor.address_street = ipnObj.address_street;
donor.address_city = ipnObj.address_city;
donor.address_country = ipnObj.address_country;
donor.address_state = ipnObj.address_state;
donor.address_zip = ipnObj.address_zip;
donor.visibility = 'ANON';
if donation:
if donation.requestedvisibility != 'CURR':
donor.visibility = donation.requestedvisibility;
if donation.requestedalias and (not donor.alias or donation.requestedalias.lower() != donor.alias.lower()):
foundAResult = False;
currentAlias = donation.requestedalias;
while not foundAResult:
results = Donor.objects.filter(alias__iexact=currentAlias);
if results.exists():
currentAlias = donation.requestedalias + str(random.getrandbits(4));
else:
foundAResult = True;
donor.alias = currentAlias;
if donation.requestedemail and donation.requestedemail != donor.email and not Donor.objects.filter(email=donation.requestedemail).exists():
donor.email = donation.requestedemail;
donor.save();
if not donation:
donation = Donation.objects.create();
donation.domain='PAYPAL';
donation.domainId=ipnObj.txn_id;
donation.donor=donor;
donation.amount=Decimal(ipnObj.mc_gross);
donation.currency=ipnObj.mc_currency;
if not donation.timereceived:
donation.timereceived = datetime.utcnow();
donation.testdonation=ipnObj.test_ipn;
donation.fee=Decimal(ipnObj.mc_fee);
donation.event = Event.objects.all().order_by('-date')[0];
# if the user attempted to tamper with the donation amount, remove all bids
if donation.amount != ipnObj.mc_gross:
donation.modcomment += u"\n*Tampered donation amount from " + str(donation.amount) + u" to " + str(ipnObj.mc_gross) + u", removed all bids*";
donation.amount = ipnObj.mc_gross;
donation.choicebid_set.clear();
donation.challengebid_set.clear();
if not ipnObj.flag and ipnObj.payment_status.lower() in ['completed', 'refunded']:
if ipnObj.payment_status.lower() == 'completed':
donation.transactionstate = 'COMPLETED';
elif ipnObj.payment_status.lower() == 'refunded':
donation.transactionstate = 'CANCELLED';
donation.save();
# I think we only care if the _donation_ was freshly created
return donation;
def get_paypal_donation(paypalemail, amount, transactionid):
donations = Donation.objects.filter(amount=amount, domain='PAYPAL', domainId=transactionid);
if donations.exists():
donation = donations[0];
donors = Donor.objects.filter(paypalemail=paypalemail);
if donors.exists() and donation.donor.id == donors[0].id:
return donation;
return None;