Skip to main content

DelayedNotify Lambda Function

Quick Reference​

  • Function Name: payment3-delayed-notify-${BRANCH}
  • Handler: delayednotify.lambda_handler
  • Runtime: Python 3.12 (Docker Image)
  • Timeout: 240 seconds
  • Trigger: Invoked by Notify function (asynchronous)
  • Template.yaml: Lines 415-457

Function Overview​

The DelayedNotify function provides delayed processing for credit card payment notifications. It performs checksum verification, saves notification records, processes paid orders twice (with a 60-second sleep between), and handles delayed payment confirmations. This function is invoked by the Notify function after initial processing completes.

Entry Point​

File: functions/callback/delayednotify.py
Handler: lambda_handler(event, _)

def lambda_handler(event, _):
"""Record credit card callback (delayed notify)."""
processor = None
try:
# Verify checksum FIRST before any other processing
checksum_valid, checksum_error, charge_id = verify_and_handle_checksum(
event=event,
function_name="delayednotify",
branch=BRANCH,
debug_mode=DEBUG_MODE
)

if not checksum_valid:
# Log warning but continue processing
print(f"[WARNING] Checksum verification failed: {checksum_error}. Continuing with processing.")

processor = CreditCardCallbackProcessor(
event=event,
branch=BRANCH,
note="delayed notify",
debug_mode=DEBUG_MODE
)
print("start saving")
processor.save()
print("start processing paid order")
r = processor.process_paid_order()
print("process paid order response", r)
print("start sleeping")
processor.sleep(60)
print("stop sleeping")
print("start processing paid order again")
r = processor.process_paid_order()
print("process paid order response 2", r)
print("stop processing paid order")
return processor.response
except Exception as e:
# Log error to callback error log table
orderId = None
chargeId = None
if processor:
try:
orderId = processor.orderId
chargeId = processor.chargeId
except:
pass
log_callback_error(
error=e,
function_name="delayednotify",
event=event,
orderId=orderId,
chargeId=chargeId,
response_data=None
)
send_callback_error_email(
error=e,
function_name="delayednotify",
orderId=orderId,
chargeId=chargeId,
event=event
)
raise

Event Structure​

Same as Notify function - receives the same event from Notify invocation.

Checksum Verification​

Same checksum verification process as Notify function.

DynamoDB Tables​

Same tables as Notify function:

  • CreditCardNotifyTable - Notification records (write)
  • CardPaymentRecordTable - Payment records (read/update)
  • order-table-dev - Order data (read)

Lambda Invocations​

ProcessPaidOrder​

Function: payment3-process-paid-order-${BRANCH}
Invoked: Twice - once immediately, once after 60-second sleep

Purpose: Double-check payment status after delay

Invocation Type: Synchronous (via processor.process_paid_order())

Processing Flow​

Main Flow​

  1. Verify Checksum: Verify webhook checksum
  2. Create Processor: Initialize CreditCardCallbackProcessor with note="delayed notify"
  3. Save Notification: Save to CreditCardNotifyTable
  4. Process Paid Order (First): Invoke ProcessPaidOrder immediately
  5. Sleep: Wait 60 seconds
  6. Process Paid Order (Second): Invoke ProcessPaidOrder again
  7. Return Response: Return HTML redirect response

Detailed Algorithm​

1. Verify checksum (same as Notify)
2. Create CreditCardCallbackProcessor with note="delayed notify"
3. Save notification to CreditCardNotifyTable
4. Invoke ProcessPaidOrder (first time)
5. Sleep for 60 seconds
6. Invoke ProcessPaidOrder (second time)
7. Return HTML redirect response

Error Handling​

Same error handling as Notify function:

  • Errors logged to CallbackErrorLogTable
  • Error email sent via SES
  • Exception re-raised

IAM Policies​

From template.yaml (lines 425-456):

  • Same policies as Notify function
  • DynamoDBWritePolicy for CreditCardNotifyTable
  • DynamoDBCrudPolicy for CardPaymentRecordTable
  • LambdaInvokePolicy for ProcessPaidOrder
  • AWSSecretsManagerGetSecretValuePolicy for kbank secrets
  • SESCrudPolicy for error emails

Dependencies​

Same as Notify function.

Testing​

Test Event Example​

Same as Notify function.

Code Structure​

File Organization:

functions/callback/
├── delayednotify.py # Main handler
└── src/
├── checksum_handler.py # Checksum verification
└── credit_card_callback_processor.py # Callback processor
  • Notify - Invokes this function after processing
  • ProcessPaidOrder - Called twice with delay

Key Differences from Notify​

  • Note Field: Uses note="delayed notify" in CreditCardNotifyTable
  • Double Processing: Calls ProcessPaidOrder twice with 60-second delay
  • Invocation: Called asynchronously by Notify, not directly via API

References​

  • Template.yaml: Lines 415-457
  • Related Functions: Notify, ProcessPaidOrder
  • Checksum Documentation: functions/callback/docs/CHECKSUM_MARKING_DOCUMENTATION.md