wip iugu
This commit is contained in:
@@ -12,7 +12,14 @@ from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
|
||||
from boto3clients import dynamodb_client, s3_client
|
||||
from config import BILLING_TEMPLATE_URI, BUCKET_NAME, ORDER_TABLE, PAPERFORGE_API
|
||||
from config import (
|
||||
BILLING_TEMPLATE_URI,
|
||||
BUCKET_NAME,
|
||||
HTTP_CONNECT_TIMEOUT,
|
||||
HTTP_READ_TIMEOUT,
|
||||
ORDER_TABLE,
|
||||
PAPERFORGE_API,
|
||||
)
|
||||
|
||||
logger = Logger(__name__)
|
||||
order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
@@ -50,7 +57,11 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
|
||||
try:
|
||||
# Send template URI and data to Paperforge API to generate a PDF
|
||||
r = requests.post(PAPERFORGE_API, data=json_data, timeout=6)
|
||||
r = requests.post(
|
||||
PAPERFORGE_API,
|
||||
data=json_data,
|
||||
timeout=(HTTP_CONNECT_TIMEOUT, HTTP_READ_TIMEOUT),
|
||||
)
|
||||
r.raise_for_status()
|
||||
except requests.exceptions.Timeout:
|
||||
logger.info('The request timed out')
|
||||
|
||||
@@ -9,10 +9,18 @@ from layercake.dynamodb import (
|
||||
)
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE
|
||||
from config import IUGU_ACCOUNT_ID, IUGU_API_TOKEN, IUGU_TEST_MODE, ORDER_TABLE
|
||||
from iugu import Credentials, Iugu
|
||||
|
||||
logger = Logger(__name__)
|
||||
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
iugu = Iugu(
|
||||
Credentials(
|
||||
IUGU_ACCOUNT_ID,
|
||||
IUGU_API_TOKEN,
|
||||
test_mode=IUGU_TEST_MODE,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
|
||||
@@ -4,27 +4,77 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE
|
||||
from config import (
|
||||
IUGU_ACCOUNT_ID,
|
||||
IUGU_API_TOKEN,
|
||||
IUGU_POSTBACK_URL,
|
||||
IUGU_TEST_MODE,
|
||||
ORDER_TABLE,
|
||||
)
|
||||
from iugu import Credentials, Iugu, Order
|
||||
|
||||
logger = Logger(__name__)
|
||||
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
iugu = Iugu(
|
||||
Credentials(
|
||||
IUGU_ACCOUNT_ID,
|
||||
IUGU_API_TOKEN,
|
||||
test_mode=IUGU_TEST_MODE,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
now_ = now()
|
||||
order_id = new_image['id']
|
||||
r = dyn.collection.get_items(
|
||||
TransactKey(order_id)
|
||||
+ SortKey('ADDRESS', rename_key='address')
|
||||
+ SortKey('ITEMS', path_spec='items', rename_key='items'),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
doc = {
|
||||
'id': order_id,
|
||||
'sk': 'IUGU',
|
||||
'invoice_id': '',
|
||||
}
|
||||
payment_method = new_image['payment_method']
|
||||
is_pix = payment_method == 'PIX'
|
||||
is_bank_slip = payment_method == 'BANK_SLIP'
|
||||
|
||||
invoice = iugu.create_invoice(
|
||||
order=Order(
|
||||
address=r.get('address', {}),
|
||||
items=r.get('items', []),
|
||||
**new_image,
|
||||
),
|
||||
postback_url=f'{IUGU_POSTBACK_URL}/postback/{order_id}',
|
||||
)
|
||||
|
||||
try:
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'INVOICE',
|
||||
'payment_method': payment_method,
|
||||
'secure_id': invoice['secure_id'],
|
||||
'secure_url': invoice['secure_url'],
|
||||
'created_at': now_,
|
||||
# Uncomment this when adding for multiple payment providers
|
||||
# 'payment_provider': 'iugu',
|
||||
}
|
||||
| ({'bank_slip': invoice['bank_slip']} if is_bank_slip else {})
|
||||
| ({'pix': invoice['pix']} if is_pix else {}),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user