forked from marcy-terui/spec2019-theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet_use.py
57 lines (52 loc) · 1.6 KB
/
wallet_use.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
import os
from datetime import datetime
import boto3
import requests
def wallet_use(event, context):
wallet_table = boto3.resource('dynamodb').Table(os.environ['WALLET_TABLE'])
history_table = boto3.resource('dynamodb').Table(os.environ['PAYMENT_HISTORY_TABLE'])
body = json.loads(event['body'])
result = wallet_table.get_item(
ConsistentRead=True,
Key={
'id': body['userId']
}
)
user_wallet = result['Item']
total_amount = user_wallet['amount'] - body['useAmount']
if total_amount < 0:
return {
'statusCode': 400,
'body': json.dumps({'errorMessage': 'There was not enough money.'})
}
wallet_table.update_item(
Key={
'id': user_wallet['id']
},
AttributeUpdates={
'amount': {
'Value': total_amount,
'Action': 'PUT'
}
}
)
history_table.put_item(
Item={
'walletId': user_wallet['id'],
'transactionId': body['transactionId'],
'useAmount': body['useAmount'],
'locationId': body['locationId'],
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
)
requests.post(os.environ['NOTIFICATION_ENDPOINT'], json={
'transactionId': body['transactionId'],
'userId': body['userId'],
'useAmount': body['useAmount'],
'totalAmount': int(total_amount)
})
return {
'statusCode': 202,
'body': json.dumps({'result': 'Assepted. Please wait for the notification.'})
}