def lambda_handler()

in src/backend/payment/src/refund-payment/refund.py [0:0]


def lambda_handler(event, context):
    """AWS Lambda Function entrypoint to refund payment

    Parameters
    ----------
    event: dict, required
        Step Functions State Machine event

        chargeId: string
            pre-authorization charge ID

    context: object, required
        Lambda Context runtime methods and attributes
        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    -------
    string
        JSON Stringified data containing refundId ID

    Raises
    ------
    RefundException
        Refund Exception including error message upon failure
    """
    payment_token = event.get("chargeId")
    customer_id = event.get("customerId")

    if not payment_token:
        metrics.add_metric(name="InvalidRefundRequest", unit=MetricUnit.Count, value=1)
        logger.error({"operation": "input_validation", "details": event})
        raise ValueError("Invalid Charge ID")

    try:
        logger.debug(f"Refunding payment from customer {customer_id} using {payment_token} token")
        ret = refund_payment(payment_token)

        metrics.add_metric(name="SuccessfulRefund", unit=MetricUnit.Count, value=1)
        tracer.put_annotation("Refund", ret["refundId"])
        tracer.put_annotation("PaymentStatus", "REFUNDED")

        return ret
    except RefundException as err:
        metrics.add_metric(name="FailedRefund", unit=MetricUnit.Count, value=1)
        tracer.put_annotation("RefundStatus", "FAILED")
        logger.exception({"operation": "payment_refund"})
        raise