Source code for eclypse.builders.application.sock_shop.rest_services.payment
"""The `PaymentService` class.
It is responsible for handling all payment-related transactions in the SockShop.
- Key Responsibilities:
- Processes payment details and initiates transactions for placed orders.
- Communicates with external payment providers and returns transaction statuses \
(e.g., success, failure).
"""
import os
import random as rnd
from eclypse.remote.communication import rest
from eclypse.remote.service import RESTService
from eclypse.utils import format_log_kv
from eclypse.utils.constants import RND_SEED
[docs]
class PaymentService(RESTService):
"""REST service for payment processing."""
[docs]
def __init__(self, service_id: str, store_step: bool = False):
"""Initialize the PaymentService with a random number generator.
Args:
service_id (str): The ID of the service.
store_step (bool, optional): Whether to store the results of
each step. Defaults to False.
"""
super().__init__(service_id, store_step=store_step)
self.rnd = rnd.Random(os.getenv(RND_SEED))
[docs]
@rest.endpoint("/pay", "POST")
def execute_payment(self, order_id: int, amount: float, **_):
"""Process the payment for the order.
Args:
order_id (int): The order ID.
amount (float): The total amount to be paid.
Returns:
int: The HTTP status code.
dict: The response body.
Example:
.. code-block:: python
(
200,
{
"order_id": 12345,
"transaction_id": 54321,
"status": "success",
},
)
"""
self.logger.info(
"Received request | " + format_log_kv(order_id=order_id, amount=amount)
)
return 200, {
"order_id": order_id,
"amount": amount + self.rnd.randint(1, 10),
"transaction_id": self.rnd.randint(1000, 9999),
"status": "success",
}