def updateStatus(history_id, status): pdb.set_trace() try: con = sqlite3.connect(SQLITE_DB_PATH) con.execute(QUERY_UPDATE_PAYPAL_STATUS, {'history_id': history_id, 'status': status}) con.commit() con.close() except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo)
def isDuplicateTransaction(history_id, txn_id): try: con = sqlite3.connect(SQLITE_DB_PATH) cur = con.execute(QUERY_GET_PREVIOUS_TRANSACTIONID_COUNT, {'history_id': history_id, 'txn_id': txn_id}) count = cur.fetchone()[0] if count > 0: return True except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo) return False
def paypalVerifyPost(post_body): try: url = PAYPAL_VERIFY_URL if 'test_ipn=1' in post_body: url = PAYPAL_TEST_VERIFY_URL response = urlopen(PAYPAL_VERIFY_URL, 'cmd=_notify-validate&' + post_body) if response.code == 200 and response.read() == 'VERIFIED': return True except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo) return False
def handle(environ, start_response): """ boiler plate for wsgi stuff """ try: start_response('200 OK', [('Content-Type', 'text/plain')]) formbody = environ['wsgi.input'].read() fields = parse_qs(formbody) fields = mutate_fields(fields, original_qs=formbody) handle_fields(fields) except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo) else: return []
def insertPasswordReset(payer_id, username): try: con = sqlite3.connect(SQLITE_DB_PATH) con.execute(QUERY_INSERT_RESET_HISTORY, {'source': 'paypal', 'source_id': payer_id, 'username': username}) history_id = con.execute('select last_insert_rowid()').fetchone()[0] con.execute(QUERY_INSERT_RESET_INCOMING, {'source': 'paypal', 'source_id': payer_id, 'username': username, 'history_id': history_id}) except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo)
def insertSignup(payer_id, username, email): try: con = sqlite3.connect(SQLITE_DB_PATH) con.execute(QUERY_INSERT_SIGNUP_HISTORY, {'source': 'paypal', 'source_id': payer_id, 'username': username, 'email': email}) history_id = con.execute('select last_insert_rowid()').fetchone()[0] con.execute(QUERY_INSERT_SIGNUP_INCOMING, {'source': 'paypal', 'source_id': payer_id, 'username': username, 'email': email, 'history_id': history_id}) con.commit() con.close() except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo)
def insertPaypalTransaction(fieldDict): """ docstring """ try: _fieldDict = fieldDict.copy() con = sqlite3.connect(SQLITE_DB_PATH) historyParams = dict((field, _fieldDict[field] if field in _fieldDict else None) for field in PAYPAL_HISTORY_FIELDS) con.execute(QUERY_INSERT_PAYPAL_HISTORY, historyParams) id = con.execute('select last_insert_rowid()').fetchone()[0] _fieldDict['history_id'] = id incomingParams = dict((field, _fieldDict[field] if field in _fieldDict else None) for field in PAYPAL_INCOMING_FIELDS) con.execute(QUERY_INSERT_PAYPAL_INCOMING, incomingParams) con.commit() con.close() except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo)
def getIncomingRow(): """docstring""" incomingRow = None try: con = sqlite3.connect(SQLITE_DB_PATH) con.row_factory = sqlite3.Row cur = con.cursor() cur.execute('begin exclusive transaction') cur.execute(QUERY_GET_PAYPAL_INCOMING) incomingRow = cur.fetchone() cur.execute(QUERY_INSERT_PAYPAL_STATUS, {'history_id': incomingRow['history_id'], 'status': 'initial'}) cur.execute(QUERY_DELETE_PAYPAL_INCOMING, (incomingRow['id'],)) con.commit() con.close() except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo) else: return incomingRow
def main(): """docstring""" try: incomingRow = getIncomingRow() post_body = incomingRow['post_body'] history_id = incomingRow['history_id'] receiver_email = incomingRow['receiver_email'] payment_status = incomingRow['payment_status'] item_name = incomingRow['item_name'] txn_type = incomingRow['txn_type'] txn_id = incomingRow['txn_id'] test_ipn = incomingRow['test_ipn'] option_name1 = incomingRow['option_name1'] if not paypalVerifyPost(post_body): updateStatus(history_id, 'verification failed') elif receiver_email != SD_RECEIVER_EMAIL: updateStatus(history_id, 'incorrect reciever email') elif payment_status != 'Completed': updateStatus(history_id, 'incomplete payment') elif item_name not in ('Shell Account', 'Donation'): updateStatus(history_id, 'unknown item_name') elif txn_type not in ('subscr_signup','web_accept'): updateStatus(history_id, 'inactionable txn_type') elif isDuplicateTransaction(history_id, txn_id): updateStatus(history_id, 'duplicate transaction id') elif test_ipn == 1: updateStatus(history_id, 'test IPN') else: if item_name == 'Shell Account': if option_name1 == 'username': username = incomingRow['option_selection1'] email = incomingRow['payer_email'] payer_id = incomingRow['payer_id'] insertSignup(username, email, payer_id) updateStatus(history_id, 'signup inserted') elif option_name1 == 'reset': username = incomingRow['option_selection1'] email = incomingRow['payer_email'] insertPasswordReset(username, email, payer_id) updateStatus(history_id, 'password reset inserted') else: updateStatus(history_id, 'unknown option_name1') else: # item_name == 'Donation' insertDonation() updateStatus(history_id, 'donation') except Exception as ex: exInfo = traceback.format_exc() logException(ex, exInfo)