From a757f6f99bb1085484d9d4ae62e7f1e30f40899d Mon Sep 17 00:00:00 2001 From: Raymond Yee Date: Thu, 22 Dec 2011 20:34:24 -0500 Subject: [PATCH] Versions that include movement to utcnow() and changing what comes out PaymentManager.checkStatus() -- but payment to primary recipient is now broken. Need to investigate --- frontend/templates/campaign_admin.html | 7 +- frontend/views.py | 23 ++++-- payment/manager.py | 100 ++++++++++++++++--------- 3 files changed, 85 insertions(+), 45 deletions(-) diff --git a/frontend/templates/campaign_admin.html b/frontend/templates/campaign_admin.html index a373a10c..92972c47 100644 --- a/frontend/templates/campaign_admin.html +++ b/frontend/templates/campaign_admin.html @@ -23,13 +23,10 @@ {% if check_status_results %} -

Campaign checkstatus output:

- - {% endif %} diff --git a/frontend/views.py b/frontend/views.py index d4731681..3d2e4362 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -7,6 +7,7 @@ import datetime from re import sub from itertools import islice from decimal import Decimal as D +from xml.etree import ElementTree as ET import requests import oauth2 as oauth @@ -422,10 +423,6 @@ def campaign_admin(request): context = {} - # first task: run PaymentManager.checkStatus() to update Campaign statuses - # does it return data to display? - - def campaigns_types(): # pull out Campaigns with Transactions that are ACTIVE -- and hence can be executed # Campaign.objects.filter(transaction__status='ACTIVE') @@ -458,8 +455,22 @@ def campaign_admin(request): if 'campaign_checkstatus' in request.POST.keys(): # campaign_checkstatus try: - check_status_results = pm.checkStatus() - command_status = _("PaymentDetails and PreapprovalDetails have been applied") + status = pm.checkStatus() + check_status_results = "" + # parse the output to display chat transaction statuses have been updated + if len(status["preapprovals"]): + for t in status["preapprovals"]: + check_status_results += "

Preapproval key: %s updated

" % (t["key"]) + else: + check_status_results += "

No preapprovals needed updating

" + if len(status["payments"]): + for t in status["payments"]: + info = ", ".join(["%s:%s" % (k,v) for (k,v) in t.items()]) + check_status_results += "

Payment updated: %s

" % (info) + + else: + check_status_results += "

No payments needed updating

" + command_status = _("Transactions updated based on PaymentDetails and PreapprovalDetails") except Exception, e: check_status_results = e elif 'execute_campaigns' in request.POST.keys(): diff --git a/payment/manager.py b/payment/manager.py index 8656aad6..e310ed8d 100644 --- a/payment/manager.py +++ b/payment/manager.py @@ -37,66 +37,90 @@ class PaymentManager( object ): Run through all pay transactions and verify that their current status is as we think. ''' - doc = minidom.Document() - head = doc.createElement('transactions') - doc.appendChild(head) + #doc = minidom.Document() + #head = doc.createElement('transactions') + #doc.appendChild(head) - # look at all transacitons for stated number of past days; if past_days is not int, get all Transaction + status = {'payments':[], 'preapprovals':[]} + + # look at all PAY transactions for stated number of past days; if past_days is not int, get all Transaction + # only PAY transactions have date_payment not None try: - ref_date = datetime.now() - relativedelta(days=int(past_days)) + ref_date = datetime.utcnow() - relativedelta(days=int(past_days)) transactions = Transaction.objects.filter(date_payment__gte=ref_date) except: - ref_date = datetime.now() + ref_date = datetime.utcnow() transactions = Transaction.objects.filter(date_payment__isnull=False) logger.info(transactions) for t in transactions: - tran = doc.createElement('transaction') - tran.setAttribute("id", str(t.id)) - head.appendChild(tran) + #tran = doc.createElement('transaction') + #tran.setAttribute("id", str(t.id)) + #head.appendChild(tran) + + payment_status = {'id':t.id} p = PaymentDetails(t) if p.error() or not p.success(): logger.info("Error retrieving payment details for transaction %d" % t.id) - append_element(doc, tran, "error", "An error occurred while verifying this transaction, see server logs for details") - + #append_element(doc, tran, "error", "An error occurred while verifying this transaction, see server logs for details") + payment_status['error'] = "An error occurred while verifying this transaction, see server logs for details" else: # Check the transaction status if t.status != p.status: - append_element(doc, tran, "status_ours", t.status) - append_element(doc, tran, "status_theirs", p.status) + #append_element(doc, tran, "status_ours", t.status) + #append_element(doc, tran, "status_theirs", p.status) + payment_status['status'] = {'ours': t.status, 'theirs': p.status} + t.status = p.status t.save() + + receivers_status = [] for r in p.transactions: try: receiver = Receiver.objects.get(transaction=t, email=r['email']) + + receiver_status = {'email':r['email']} + logger.info(r) logger.info(receiver) # Check for updates on each receiver's status. Note that unprocessed delayed chained payments # will not have a status code or txn id code if receiver.status != r['status']: - append_element(doc, tran, "receiver_status_ours", receiver.status) - append_element(doc, tran, "receiver_status_theirs", - r['status'] if r['status'] is not None else 'None') + #append_element(doc, tran, "receiver_status_ours", receiver.status) + #append_element(doc, tran, "receiver_status_theirs", + # r['status'] if r['status'] is not None else 'None') + receiver_status['status'] = {'ours': receiver.status, 'theirs': r['status']} receiver.status = r['status'] receiver.save() if receiver.txn_id != r['txn_id']: - append_element(doc, tran, "txn_id_ours", receiver.txn_id) - append_element(doc, tran, "txn_id_theirs", r['txn_id']) + #append_element(doc, tran, "txn_id_ours", receiver.txn_id) + #append_element(doc, tran, "txn_id_theirs", r['txn_id']) + receiver_status['txn_id'] = {'ours':receiver.txn_id, 'theirs':r['txn_id']} + receiver.txn_id = r['txn_id'] receiver.save() except: traceback.print_exc() + if not set(["status","txn_id"]).isdisjoint(receiver_status.keys()): + receivers_status.append(receiver_status) + + if len(receivers_status): + status["receivers"] = receivers_status + + if not set(["status", "receivers"]).isdisjoint(payment_status.keys()): + status["payments"].append(payment_status) + # Now look for preapprovals that have not been paid and check on their status transactions = Transaction.objects.filter(date_authorized__gte=ref_date, date_payment=None, type=PAYMENT_TYPE_AUTHORIZATION) @@ -104,39 +128,47 @@ class PaymentManager( object ): p = PreapprovalDetails(t) - tran = doc.createElement('preapproval') - tran.setAttribute("key", str(t.preapproval_key)) - head.appendChild(tran) + #tran = doc.createElement('preapproval') + #tran.setAttribute("key", str(t.preapproval_key)) + #head.appendChild(tran) + + preapproval_status = {'id':t.id, 'key':t.preapproval_key} if p.error() or not p.success(): logger.info("Error retrieving preapproval details for transaction %d" % t.id) - append_element(doc, tran, "error", "An error occurred while verifying this transaction, see server logs for details") - + #append_element(doc, tran, "error", "An error occurred while verifying this transaction, see server logs for details") + preapproval_status["error"] = "An error occurred while verifying this transaction, see server logs for details" else: # Check the transaction status if t.status != p.status: - append_element(doc, tran, "status_ours", t.status) - append_element(doc, tran, "status_theirs", p.status) + #append_element(doc, tran, "status_ours", t.status) + #append_element(doc, tran, "status_theirs", p.status) + preapproval_status["status"] = {'ours':t.status, 'theirs':p.status} t.status = p.status t.save() # check the currency code if t.currency != p.currency: - append_element(doc, tran, "currency_ours", t.currency) - append_element(doc, tran, "currency_theirs", p.currency) + #append_element(doc, tran, "currency_ours", t.currency) + #append_element(doc, tran, "currency_theirs", p.currency) + preapproval_status["currency"] = {'ours':t.currency, 'theirs':p.currency} t.currency = p.currency t.save() # Check the amount if t.amount != D(p.amount): - append_element(doc, tran, "amount_ours", str(t.amount)) - append_element(doc, tran, "amount_theirs", str(p.amount)) + #append_element(doc, tran, "amount_ours", str(t.amount)) + #append_element(doc, tran, "amount_theirs", str(p.amount)) + preapproval_status["amount"] = {'ours':t.amount, 'theirs':p.amount} t.amount = p.amount t.save() - + + # append only if there was a change in status + if not set(['status', 'currency', 'amount']).isdisjoint(set(preapproval_status.keys())): + status["preapprovals"].append(preapproval_status) - return doc.toxml() + return status def processIPN(self, request): @@ -394,7 +426,7 @@ class PaymentManager( object ): return False # mark this transaction as executed - transaction.date_executed = datetime.now() + transaction.date_executed = datetime.utcnow() transaction.save() p = Execute(transaction) @@ -448,7 +480,7 @@ class PaymentManager( object ): transaction.create_receivers(receiver_list) # Mark as payment attempted so we will poll this periodically for status changes - transaction.date_payment = datetime.now() + transaction.date_payment = datetime.utcnow() transaction.save() p = Pay(transaction) @@ -611,7 +643,7 @@ class PaymentManager( object ): campaign=campaign, list=list, user=user, - date_payment=datetime.now(), + date_payment=datetime.utcnow(), anonymous=anonymous )