(20241216) Payment auth bug fix.

This commit is contained in:
2024-12-16 16:00:52 +05:30
parent 064495163f
commit 0b9976fadb
19 changed files with 589 additions and 473 deletions
+163 -19
View File
@@ -36,13 +36,16 @@ sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime
from pydantic import BaseModel, Field, field_validator, PastDatetime, AwareDatetime, model_validator
from typing import Optional, Literal, Union, List, Any
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# Other core models:
from models.core.user import CoreUserInfoModel
# To work with MongoDB:
from bson.objectid import ObjectId
@@ -80,6 +83,57 @@ import pycountry
# *****************************************************************************************************************
class CustomerDetails(BaseModel):
name: str | None = Field(
description = "the name of the registered customer who must make the payment",
frozen = True,
default = None
)
contactNo: 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 'contactNo' will be used"
),
frozen = False,
default = None
)
email: str | None = Field(
description = "the e-mail id of the registered customer",
pattern = regex.REGEX_EMAIL_ID,
frozen = True,
default = None
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "allow"
arbitrary_types_allowed = True
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@model_validator(mode = "after")
def validate_model(cls, values):
if values.payerNo is None: values.payerNo = values.contactNo
return values
# ---------------------------------------------------------------------------------------------------------------------
class PaymentEvent(BaseModel):
eventTs: AwareDatetime = Field(
@@ -87,6 +141,19 @@ class PaymentEvent(BaseModel):
frozen = True
)
paymentStatus: Literal[
"initFailed", # ... When we tried to initiate the request, but the payment gateway (PG) rejected it.
"initiated", # .... When we made a successful payment request, or the customer initiated one from the PG.
"failed", # ....... When the customer tried paying, but it failed (e.g.: because of an incorrect pin).
"rejected", # ..... When the customer explicitly rejected the payment.
"authorized", # ... When the customer made the payment (but it hasn't been settled in your account yet).
"settled", # ...... When the PG sends the money to your account.
"refunded", # ..... When the money was refunded to the client.
] = Field(
description = "the status of the payment request to see what stage of the process we are in",
frozen = False
)
initByPG: bool = Field(
description = "to figure out whether the payment gateway initiated this event or we did",
frozen = True
@@ -98,6 +165,10 @@ class PaymentEvent(BaseModel):
examples = [200, 400, 401]
)
headers: dict = Field(
description = "the headers that came in with the event; useful when trying to decode who generated the event"
)
payload: dict = Field(
description = "the json payload or set of query params received from an event from the payment gateway",
frozen = True
@@ -133,35 +204,44 @@ class CorePaymentModel(BaseModel):
alias = "_id"
)
paymentStatus: Literal[
"initFailed", # ... When we tried to initiate the request, but the payment gateway (PG) rejected it.
"initiated", # .... When we made a successful payment request, or the customer initiated one from the PG.
"failed", # ....... When the customer tried paying, but it failed (e.g.: because of an incorrect pin).
"rejected", # ..... When the customer explicitly rejected the payment.
"authorized", # ... When the customer made the payment (but it hasn't been settled in your account yet).
"settled", # ...... When the PG sends the money to your account.
"refunded", # ..... When the money was refunded to the client.
] = Field(
description = "the status of the payment request to see what stage of the process we are in",
frozen = False
user: CoreUserInfoModel = Field(
description = "how you identify your user",
frozen = True
)
customer: CustomerDetails = Field(
description = "how you identify your user's customer",
frozen = True
)
ts: AwareDatetime = Field(
description = "the time (utc) at which the payment was first initiated",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
lastEventTs: AwareDatetime = Field(
description = "the time (utc) at which the latest payment event occurred",
description = "the time (utc) at which the last event occurred",
frozen = True
)
lastPaymentStatus: str = Field(
description = "the status of the payment request to see what stage of the process we are in",
frozen = False
)
tokenId: ObjectId = Field(
description = "the id of the auth token that is associated with this payment",
frozen = True
)
amount: float | int = Field(
description = "the amount of money being requested"
description = "the amount of money being requested",
frozen = True
)
currencyCode: str = Field(
description = "the three-letter ISO 4217 code to identify the currency",
description = "the three-letter iso 4217 code to identify the currency",
frozen = True,
examples = ["INR", "USD", "KES"]
)
@@ -206,6 +286,51 @@ class CorePaymentModel(BaseModel):
def model_dump(self, *args, **kwargs):
return super().model_dump(*args, by_alias = True, **kwargs)
# ┏┓ •
# ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
# ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
# ┛
@property
def full(self):
payment_json = {
"paymentId": str(self.messageId),
"user": self.user,
"ts": self.ts.isoformat(),
"lastEventTs": self.lastEventTs,
"lastPaymentStatus": self.lastPaymentStatus,
"amount": self.amount,
"currencyCode": self.currencyCode,
"client": self.client,
"clientPaymentReferenceId": self.clientPaymentReferenceId,
"tags": self.tags,
"metadata": self.metadata,
"events": []
}
for e in self.events:
payment_json["events"].append({
"eventTs": e.eventTs,
"paymentStatus": e.paymentStatus,
"initByPG": e.initByPG
})
return payment_json
@property
def preview(self):
return {
"paymentId": str(self.messageId),
"user": self.user,
"ts": self.ts.isoformat(),
"lastEventTs": self.lastEventTs,
"lastPaymentStatus": self.lastPaymentStatus,
"amount": self.amount,
"currencyCode": self.currencyCode,
"client": self.client,
"clientPaymentReferenceId": self.clientPaymentReferenceId,
"tags": self.tags,
"metadata": self.metadata
}
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@@ -248,22 +373,37 @@ if __name__ == "__main__":
now = date_time.get_current_utc_date_time(as_string = False)
payment = CorePaymentModel(
paymentStatus = "authorized",
tokenId = "67519cf3a7804fcbc6f12452",
user = CoreUserInfoModel(
userId = 1
),
customer = CustomerDetails(
name = "Bhopli Narangi",
contactNo = "9876543210",
email = "bhopli@orange.com"
),
amount = 1.00,
currencyCode = "INR",
metadata = {
"userId": 1,
"name": "Bhopli"
"key": "value"
},
tags = [
"some",
"tags"
],
client = "razorpay",
clientPaymentReferenceId = "txn_123_abc",
lastPaymentStatus = "settled",
lastEventTs = now,
events = [
PaymentEvent(
eventTs = now - datetime.timedelta(minutes = 1, seconds = 12),
initByPG = True,
paymentStatus = "initiated",
initByPG = False,
httpCode = None,
headers = {
"some": "header"
},
payload = {
"status": "captured",
"from": "Barfi",
@@ -271,8 +411,12 @@ if __name__ == "__main__":
),
PaymentEvent(
eventTs = now,
paymentStatus = "settled",
initByPG = True,
httpCode = None,
headers = {
"some": "header"
},
payload = {
"status": "authorized",
"from": "Barfi",