Mercurial > public > sg101
comparison gpp/donations/views.py @ 61:8c9344e36813
Donations: tested IPN logic with the Paypal developer sandbox.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 24 Jun 2009 01:57:10 +0000 |
parents | 5dbfb7fec629 |
children | 5899e308cdb3 |
comparison
equal
deleted
inserted
replaced
60:399a9a40bbcf | 61:8c9344e36813 |
---|---|
13 from django.http import HttpResponseServerError | 13 from django.http import HttpResponseServerError |
14 from django.contrib.auth.models import User | 14 from django.contrib.auth.models import User |
15 | 15 |
16 from donations.models import Donation | 16 from donations.models import Donation |
17 | 17 |
18 PP_DATE_FMT = '%H:%M:%S %b %d, %Y PST' | 18 PP_DATE_FMT = '%H:%M:%S %b. %d, %Y' |
19 | 19 |
20 def paypal_params(): | 20 def paypal_params(): |
21 """ | 21 """ |
22 This function returns a tuple where the 1st element is the Paypal | 22 This function returns a tuple where the 1st element is the Paypal |
23 URL and the 2nd element is the Paypal business email. This information | 23 URL and the 2nd element is the Paypal business email. This information |
135 # is this a payment received? | 135 # is this a payment received? |
136 txn_type = params.get('txn_type') | 136 txn_type = params.get('txn_type') |
137 if txn_type != 'web_accept': | 137 if txn_type != 'web_accept': |
138 logging.warning('IPN: invalid txn_type: %s' % txn_type) | 138 logging.warning('IPN: invalid txn_type: %s' % txn_type) |
139 return | 139 return |
140 | |
141 logging.debug('web_accept') | |
140 | 142 |
141 # Looks like a donation, save it to the database. | 143 # Looks like a donation, save it to the database. |
142 # Determine which user this came from, if any. | 144 # Determine which user this came from, if any. |
143 # The username is stored in the custom field if the user was logged in when | 145 # The username is stored in the custom field if the user was logged in when |
144 # the donation was made. | 146 # the donation was made. |
145 user = None | 147 user = None |
146 if 'custom' in params: | 148 if 'custom' in params: |
147 try: | 149 try: |
148 user = User.objects.get(username__exact=params['custom']) | 150 user = User.objects.get(username__exact=params['custom']) |
149 except User.DoesNotExist: | 151 except User.DoesNotExist: |
152 logging.debug('user does not exist') | |
150 pass | 153 pass |
151 | 154 |
152 is_anonymous = item_number == settings.DONATIONS_ITEM_ANON_NUM | 155 is_anonymous = item_number == settings.DONATIONS_ITEM_ANON_NUM |
153 test_ipn = params.get('test_ipn') == '1' | 156 test_ipn = params.get('test_ipn') == '1' |
154 | 157 |
164 mc_fee = decimal.Decimal(params['mc_fee']) | 167 mc_fee = decimal.Decimal(params['mc_fee']) |
165 except KeyError, decimal.InvalidOperation: | 168 except KeyError, decimal.InvalidOperation: |
166 logging.error('IPN: invalid/missing mc_gross or mc_fee') | 169 logging.error('IPN: invalid/missing mc_gross or mc_fee') |
167 return | 170 return |
168 | 171 |
169 try: | 172 payment_date = params.get('payment_date') |
170 payment_date = datetime.datetime.strptime(params['payment_date'], | 173 if payment_date is None: |
171 PP_DATE_FMT) | 174 logging.error('IPN: missing payment_date') |
172 except KeyError, ValueError: | 175 return |
173 logging.error('IPN: invalid/missing payment_date') | 176 |
174 return | 177 # strip off the timezone |
175 | 178 payment_date = payment_date[:-4] |
176 donation = Donation( | 179 try: |
177 user=user, | 180 payment_date = datetime.datetime.strptime(payment_date, PP_DATE_FMT) |
178 is_anonymous=is_anonymous, | 181 except ValueError: |
179 test_ipn=test_ipn, | 182 logging.error('IPN: invalid payment_date "%s"' % params['payment_date']) |
180 txn_id=txn_id, | 183 return |
181 first_name=first_name, | 184 |
182 last_name=last_name, | 185 try: |
183 payer_email=payer_email, | 186 donation = Donation( |
184 payer_id=payer_id, | 187 user=user, |
185 memo=memo, | 188 is_anonymous=is_anonymous, |
186 payer_status=payer_status, | 189 test_ipn=test_ipn, |
187 mc_gross=mc_gross, | 190 txn_id=txn_id, |
188 mc_fee=mc_fee, | 191 txn_type=txn_type, |
189 payment_date=payment_date) | 192 first_name=first_name, |
190 | 193 last_name=last_name, |
191 donation.save() | 194 payer_email=payer_email, |
192 logging.info('IPN: donation saved') | 195 payer_id=payer_id, |
193 | 196 memo=memo, |
197 payer_status=payer_status, | |
198 mc_gross=mc_gross, | |
199 mc_fee=mc_fee, | |
200 payment_date=payment_date) | |
201 except: | |
202 logging.exception('IPN: exception during donation creation') | |
203 else: | |
204 donation.save() | |
205 logging.info('IPN: donation saved') | |
206 |