Adding redirect to pledge cancel and complete URLs when using amazon payment API

pull/1/head
icellama21 2012-04-23 15:24:16 -04:00
parent b4e424b9f5
commit 337a692995
2 changed files with 33 additions and 28 deletions

View File

@ -16,6 +16,7 @@ import datetime
import logging
import urlparse
import time
import urllib
logger = logging.getLogger(__name__)
@ -186,14 +187,13 @@ def ProcessIPN(request):
def amazonPaymentReturn(request):
'''
This is the complete view called after the co-branded API completes. It is called whenever the user
approves a preapproval or a pledge.
approves a preapproval or a pledge. This URL is set via the PAY api.
'''
try:
# pick up all get and post parameters and display
output = "payment complete"
output += request.method + "\n" + str(request.REQUEST.items())
print output
signature = request.GET['signature']
reference = request.GET['callerReference']
@ -207,6 +207,9 @@ def amazonPaymentReturn(request):
#
transaction = Transaction.objects.get(secret=reference)
logging.info("Amazon Co-branded Return URL called for transaction id: %d" % transaction.id)
logging.info(request.GET)
#
# BUGBUG, for now lets map amazon status code to paypal, just to keep things uninform
#
@ -284,11 +287,22 @@ def amazonPaymentReturn(request):
transaction=transaction)
transaction.save()
return HttpResponse("Success")
# Redirect to our pledge success URL
return_path = "{0}?{1}".format(reverse('pledge_complete'),
urllib.urlencode({'tid':transaction.id}))
return_url = urlparse.urljoin(settings.BASE_URL, return_path)
return HttpResponseRedirect(return_url)
except:
logging.error("Amazon co-branded return-url FAILED with exception:")
traceback.print_exc()
return HttpResponseBadRequest("Error")
cancel_path = "{0}?{1}".format(reverse('pledge_cancel'),
urllib.urlencode({'tid':transaction.id}))
cancel_url = urlparse.urljoin(settings.BASE_URL, cancel_path)
return HttpResponseRedirect(cancel_url)
class AmazonRequest:
@ -361,6 +375,10 @@ class Pay( AmazonRequest ):
try:
logging.debug("Amazon PAY operation for transaction ID %d" % transaction.id)
# Replace our return URL with a redirect through our internal URL
self.original_return_url = return_url
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
if not options:
options = {}
@ -399,6 +417,7 @@ class Pay( AmazonRequest ):
self.errorMessage = body
except:
logging.error("Amazon PAY FAILED with exception:")
traceback.print_exc()
self.errorMessage = "Error: Server Error"
@ -495,6 +514,7 @@ class Execute(AmazonRequest):
self.errorMessage = body
except:
logging.error("Amazon EXECUTE FAILED with exception:")
traceback.print_exc()
self.errorMessage = "Error: Server Error"
@ -581,6 +601,7 @@ class PaymentDetails(AmazonRequest):
self.errorMessage = body
except:
logging.error("Amazon PAYMENTDETAILS FAILED with exception:")
self.errorMessage = "Error: ServerError"
traceback.print_exc()
@ -639,6 +660,7 @@ class CancelPreapproval(AmazonRequest):
self.errorMessage = body
except:
logging.error("Amazon CANCELPREAPPROVAL FAILED with exception:")
traceback.print_exc()
self.errorMessage = "Error: Server Error"
@ -681,6 +703,7 @@ class RefundPayment(AmazonRequest):
self.errorMessage = body
except:
logging.error("Amazon REFUNDPAYMENT FAILED with exception:")
traceback.print_exc()
self.errorMessage = "Error: Server Error"
@ -747,6 +770,7 @@ class PreapprovalDetails(AmazonRequest):
except:
# If the boto API fails, it also throws an exception and we end up here
logging.error("Amazon PREAPPROVALDETAILS FAILED with exception:")
self.errorMessage = "Error: ServerError"
traceback.print_exc()

View File

@ -114,18 +114,12 @@ def testAuthorize(request):
receiver_list = [{'email': TEST_RECEIVERS[0], 'amount':20.00},
{'email': TEST_RECEIVERS[1], 'amount':10.00}]
# Set the return url for the processor
if settings.PAYMENT_PROCESSOR == 'amazon':
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
else:
return_url = None
if campaign_id:
campaign = Campaign.objects.get(id=int(campaign_id))
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, amount, campaign=campaign, return_url=return_url, list=None, user=None)
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, amount, campaign=campaign, return_url=None, list=None, user=None)
else:
t, url = p.authorize('USD', TARGET_TYPE_NONE, amount, campaign=None, return_url=return_url, list=None, user=None)
t, url = p.authorize('USD', TARGET_TYPE_NONE, amount, campaign=None, return_url=None, list=None, user=None)
if url:
logger.info("testAuthorize: " + url)
@ -194,14 +188,7 @@ def testModify(request):
t = Transaction.objects.get(id=int(request.GET['transaction']))
p = PaymentManager()
# Set the return url for the processor
if settings.PAYMENT_PROCESSOR == 'amazon':
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
else:
return_url = None
status, url = p.modify_transaction(t, amount, return_url=return_url)
status, url = p.modify_transaction(t, amount, return_url=None)
if url:
logger.info("testModify: " + url)
@ -263,18 +250,12 @@ def testPledge(request):
else:
receiver_list = [{'email':TEST_RECEIVERS[0], 'amount':78.90}, {'email':TEST_RECEIVERS[1], 'amount':34.56}]
# Set the return url for the processor
if settings.PAYMENT_PROCESSOR == 'amazon':
return_url = settings.BASE_URL + reverse('AmazonPaymentReturn')
else:
return_url = None
if campaign_id:
campaign = Campaign.objects.get(id=int(campaign_id))
t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user, return_url=return_url)
t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user, return_url=None)
else:
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=user, return_url=return_url)
t, url = p.pledge('USD', TARGET_TYPE_NONE, receiver_list, campaign=None, list=None, user=user, return_url=None)
if url:
logger.info("testPledge: " + url)