Skip to content

Commit

Permalink
Added support for pushing failed transactions to SQS. Next step is mo…
Browse files Browse the repository at this point in the history
…nitoring and taking care of transactions
  • Loading branch information
Raghav Bhasin authored and Raghav Bhasin committed Aug 19, 2018
1 parent c712242 commit cfc1d4e
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions scripts/ACH Processor/ACH Proccessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import dwollav2
import os
from twilio.rest import Client
import json


dwolla_client = dwollav2.Client(
Expand All @@ -14,32 +15,43 @@

client = boto3.resource('dynamodb')
transactions_table = client.Table('DrackerTransactions')
def lambda_handler(event, context):
rows = transactions_table.scan()['Items']
for row in rows:
transactions = row['transactions']
new_list = []
for item in transactions:
status = check_status(item['id'])
if status == 'processed':
message = 'Transaction ' + item['id'] + ' for $' + item['amount'] + ' has been processed from your bank account'
twillio_client.messages.create(to=item['phone'], from_= os.environ.get('twillio_phone'), body= message)
elif status == 'failed':
message = 'Transaction ' + item['id'] + ' for $' + item['amount'] + ' could not be cleared'
twillio_client.messages.create(to=item['phone'], from_= os.environ.get('twillio_phone'), body= message)
else:
new_list.append(item)
queue = (boto3.resource('sqs')).get_queue_by_name(QueueName='DrackerFailedTransactions')

if len(new_list) == 0:
transactions_table.delete_item(
Key={
'email': row['email']
}
)
else:
row['transactions'] = new_list
transactions_table.put_item(Item=row)
def lambda_handler(event, context):
try:
rows = transactions_table.scan()['Items']
for row in rows:
transactions = row['transactions']
new_list = []
for item in transactions:
status = check_status(item['id'])
if status == 'processed':
message = 'Transaction ' + item['id'] + ' for $' + item['amount'] + ' has been processed from your bank account'
try:
twillio_client.messages.create(to=item['phone'], from_= os.environ.get('twillio_phone'), body= message)
except Exception as err:
print(err)
elif status == 'failed':
queue.send_message(MessageBody=json.dumps(item))
message = 'Transaction ' + item['id'] + ' for $' + item['amount'] + ' could not be cleared'
try:
twillio_client.messages.create(to=item['phone'], from_= os.environ.get('twillio_phone'), body= message)
except Exception as err:
print(err)
else:
new_list.append(item)

if len(new_list) == 0:
transactions_table.delete_item(
Key={
'email': row['email']
}
)
else:
row['transactions'] = new_list
transactions_table.put_item(Item=row)
except Exception as err:
print(err)
return 200


Expand Down

0 comments on commit cfc1d4e

Please sign in to comment.