319 lines
10 KiB
Python
319 lines
10 KiB
Python
"""
|
|
|
|
AUTHOR:
|
|
|
|
Khushal P Soonderji
|
|
|
|
DATE:
|
|
|
|
Monday, 16th Dec., 2024.
|
|
|
|
OBJECTIVE:
|
|
|
|
To provide a structure to receive payment request details.
|
|
|
|
REFERENCES:
|
|
|
|
N/A
|
|
|
|
DOWNLOADS:
|
|
|
|
N/A
|
|
|
|
"""
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** IMPORT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# To make sibling directories accessible for imports:
|
|
import sys
|
|
sys.path.append(".")
|
|
sys.path.append("..")
|
|
|
|
# For making data behaviour_models:
|
|
from pydantic import BaseModel, Field, field_validator, PastDatetime
|
|
from typing import Optional, Literal, Union, List, Any
|
|
|
|
# My utils:
|
|
from utils_v2.string import json
|
|
from utils_v2.string import regex
|
|
|
|
# To work with MongoDB:
|
|
from bson.objectid import ObjectId
|
|
|
|
# To work with currencies:
|
|
import pycountry
|
|
|
|
# For random strings:
|
|
import random
|
|
import string
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MACROS / ONE-TIME INIT ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# RegEx Patterns:
|
|
REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$"
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** VARIABLES ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
# --- Nothing Yet
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** FUNCTIONS ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
class PGPaymentRequestHeaders(BaseModel):
|
|
|
|
sessionToken: str = Field(
|
|
description = "the session token of the user who is requesting the service",
|
|
pattern = REGEX_SESSION_TOKEN,
|
|
frozen = True,
|
|
alias = "X-Session-Token"
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
|
|
def model_dump(self, *args, **kwargs):
|
|
return super().model_dump(*args, by_alias = True, **kwargs)
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
class PaymentRequestMetadata(BaseModel):
|
|
|
|
"""
|
|
These are things that the UI will send you, and you must use them as-is for adding receipts. Their internal workings
|
|
and use-cases are not known and are not of this code's direct concern except that you must ensure that these details
|
|
get delivered through the mechanism of adding receipts.
|
|
"""
|
|
|
|
idUser: int | None = Field(frozen = False, default = None, validate_default = True)
|
|
idClient: int = Field(frozen = True)
|
|
clientName: str | None = Field(frozen = True, default = None, validate_default = True)
|
|
inAccount: int = Field(frozen = True)
|
|
reference: str | None = Field(frozen = True, default = None, validate_default = True)
|
|
againstReference: dict | None = Field(frozen = True, default = None, validate_default = True)
|
|
documentUrl: str | None = Field(frozen = False, default = None, validate_default = True)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
|
|
# ┓┏ ┓• ┓ •
|
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
|
|
|
@field_validator("reference")
|
|
def validate_reference(cls, value):
|
|
if not value: value = "On Account"
|
|
return value
|
|
|
|
@field_validator("againstReference", mode = "before")
|
|
def validate_against_reference(cls, value):
|
|
if not value: value = None
|
|
if isinstance(value, str): value = json.from_string(value)
|
|
return value
|
|
|
|
@field_validator("documentUrl")
|
|
def validate_document_url(cls, value):
|
|
if not value: value = None
|
|
return value
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
class PGPaymentRequestData(BaseModel):
|
|
|
|
tokenKey: ObjectId = Field(
|
|
description = "the auth token of the account that must be used to request the payment",
|
|
frozen = True,
|
|
)
|
|
|
|
paymentId: ObjectId | None = Field(
|
|
description = "if you had queued the payment request separately, pass the id here",
|
|
frozen = True,
|
|
default = None
|
|
)
|
|
|
|
customerName: str | None = Field(
|
|
description = "the name of the registered customer who must make the payment",
|
|
frozen = True,
|
|
default = None
|
|
)
|
|
|
|
customerNo: str = Field(
|
|
description = "the contact no. of the registered customer who must make the payment",
|
|
frozen = True
|
|
)
|
|
|
|
payerNo: str | None = Field(
|
|
description = (
|
|
"the phone no. to which the payment request will go;"
|
|
"if not specified, the value of 'customerNo' should be used"
|
|
),
|
|
frozen = True,
|
|
default = None
|
|
)
|
|
|
|
email: str | None = Field(
|
|
description = "the e-mail id of the registered customer",
|
|
pattern = regex.REGEX_EMAIL_ID,
|
|
frozen = True,
|
|
default = None
|
|
)
|
|
|
|
amount: float | int = Field(
|
|
description = "the amount of money to be requested",
|
|
ge = 0.0,
|
|
frozen = True
|
|
)
|
|
|
|
currencyCode: str = Field(
|
|
description = "the three-letter iso 4217 code to identify the currency",
|
|
frozen = True,
|
|
examples = ["INR", "USD", "KES"]
|
|
)
|
|
|
|
description: str = Field(
|
|
description = "a short, human-readable description of the payment",
|
|
frozen = True
|
|
)
|
|
|
|
metadata: PaymentRequestMetadata = Field(
|
|
description = "any extra information about this payment",
|
|
frozen = True
|
|
)
|
|
|
|
tags: List[Any] | None = Field(
|
|
description = "any no. of tags to apply to this payment record to make search and filtering easy later",
|
|
default = None,
|
|
validate_default = True
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "forbid"
|
|
arbitrary_types_allowed = True
|
|
|
|
# ┓┏ ┓• ┓ •
|
|
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
|
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
|
|
|
@field_validator("tokenKey", "paymentId", mode = "before")
|
|
def parse_oid(cls, value):
|
|
try: value = ObjectId(value)
|
|
except: pass
|
|
return value
|
|
|
|
@field_validator("currencyCode", mode = "before")
|
|
def validate_currency(cls, value):
|
|
currency = pycountry.currencies.get(alpha_3 = value)
|
|
if currency is None: raise ValueError("invalid currency code, please use iso 4217 standard")
|
|
return value
|
|
|
|
@field_validator("description", mode = "before")
|
|
def ensure_description(cls, value):
|
|
if not isinstance(value, str):
|
|
value = "rndm_" + "".join(random.choices(string.ascii_letters + string.digits, k = 20))
|
|
return value
|
|
|
|
@field_validator("tags", mode = "after")
|
|
def null_to_empty_list(cls, value):
|
|
return [] if value is None else value
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
class PaymentRequestOneResult(BaseModel):
|
|
|
|
success: bool = Field(
|
|
description = "whether, or not, the payment was successfully requested",
|
|
default = False
|
|
)
|
|
|
|
message: str | None = Field(
|
|
description = "a brief message to summarize the result of the process",
|
|
default = None
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "forbid"
|
|
|
|
|
|
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
class PaymentCallbackResult(BaseModel):
|
|
|
|
success: bool = Field(
|
|
description = "whether, or not, the payment event was noted",
|
|
default = False
|
|
)
|
|
|
|
message: str | None = Field(
|
|
description = "a brief message to summarize the result of the process",
|
|
default = None
|
|
)
|
|
|
|
# ┏┓ ┏•
|
|
# ┃ ┏┓┏┓╋┓┏┓
|
|
# ┗┛┗┛┛┗┛┗┗┫
|
|
# ┛
|
|
|
|
class Config:
|
|
extra = "forbid"
|
|
|
|
|
|
# *****************************************************************************************************************
|
|
# ***** ****
|
|
# *** MAIN PROGRAM ***
|
|
# ***** ****
|
|
# *****************************************************************************************************************
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|