Skip to content

Instantly share code, notes, and snippets.

@siumhossain
Created October 28, 2024 05:02
Show Gist options
  • Save siumhossain/8994d48ef3c8551a157a558e9c896bf9 to your computer and use it in GitHub Desktop.
Save siumhossain/8994d48ef3c8551a157a558e9c896bf9 to your computer and use it in GitHub Desktop.
ipn listen
async def ipn_listener_view(
status: str,
tran_id: uuid.UUID,
val_id: str,
amount,
store_amount,
card_type: str,
card_no: str,
currency: str,
bank_tran_id: str,
card_issuer: str,
card_brand: str,
card_issuer_country: str,
card_issuer_country_code: str,
currency_type: str,
currency_amount: str,
risk_level: int,
risk_title: str,
value_a: uuid.UUID, # user id
value_b: uuid.UUID, # book id
session: Session,
html_response: bool = False,
):
# class PaymentStatus(Enum):
# PENDING = 1
# SUCCESS = 2
# FAILED = 3
try:
if status == "VALID":
status = PaymentStatus.SUCCESS.value
else:
status = PaymentStatus.FAILED.value
# check tansaction id from payment table
prepared_stmt = session.prepare(
"""
SELECT transaction_id, user_id, book_id, amount, status FROM payment WHERE transaction_id = ?
"""
)
rows = session.execute(prepared_stmt, (tran_id,))
payment_data = rows.one()
if payment_data is None:
return CustomResponse.error(message="Transaction not found")
user_id = payment_data.user_id
book_id = payment_data.book_id
amount = payment_data.amount
transaction_id = payment_data.transaction_id
discount_price = await BookCrud.get_discount_amount(book_id, session)
if discount_price:
price_after_discount = await BookCrud.calculate_discounted_price(float(amount), float(discount_price))
amount = price_after_discount
if (
value_a != user_id
or value_b != book_id
or amount != amount
or transaction_id != tran_id
):
return CustomResponse.error(
message="Provided data not match with transaction data"
)
# update payemnt by user table status
# if not payment_data.status == PaymentStatus.SUCCESS.value:
# validate ssl commerz validate api
payload_for_ssl = {
"store_id": store_id,
"store_passwd": store_passwd,
"val_id": val_id,
"format": "json", # specify the format as json
}
form_data = FormData()
for key, value in payload_for_ssl.items():
form_data.add_field(key, value)
try:
async with aiohttp.ClientSession() as aio_session:
async with aio_session.get(
ssl_validation_url, params=payload_for_ssl
) as response:
response_data = await response.json()
tran_id_from_validation_api = response_data["tran_id"]
status_from_validation_api = response_data["status"]
amount_from_validation_api = response_data["amount"]
except Exception as e:
raise e
if (
status_from_validation_api == "VALID"
and Decimal(str(amount_from_validation_api)) == Decimal(str(amount))
and str(tran_id_from_validation_api) == str(tran_id)
):
prepared_stmt = session.prepare(
"""
UPDATE payment_by_user SET status = ?, val_id = ?, store_amount = ?, card_type = ?, card_no = ?, currency = ?, bank_tran_id = ?, card_issuer = ?, card_brand = ?, card_issuer_country = ?, card_issuer_country_code = ?, currency_type = ?, currency_amount = ?, risk_level = ?, risk_title = ?, amount = ? WHERE user_id = ? AND book_id = ?
"""
)
session.execute(
prepared_stmt,
(
status,
val_id,
store_amount,
card_type,
card_no,
currency,
bank_tran_id,
card_issuer,
card_brand,
card_issuer_country,
card_issuer_country_code,
currency_type,
currency_amount,
risk_level,
risk_title,
Decimal(str(amount)),
user_id,
book_id,
),
)
# update payment table
prepared_stmt = session.prepare(
"""
UPDATE payment SET status = ?, val_id = ?, store_amount = ?, card_type = ?, card_no = ?, currency = ?, bank_tran_id = ?, card_issuer = ?, card_brand = ?, card_issuer_country = ?, card_issuer_country_code = ?, currency_type = ?, currency_amount = ?, risk_level = ?, risk_title = ?, amount = ? WHERE transaction_id = ? AND user_id = ? AND book_id = ?
"""
)
session.execute(
prepared_stmt,
(
status,
val_id,
store_amount,
card_type,
card_no,
currency,
bank_tran_id,
card_issuer,
card_brand,
card_issuer_country,
card_issuer_country_code,
currency_type,
currency_amount,
risk_level,
risk_title,
Decimal(str(amount)),
transaction_id,
user_id,
book_id,
),
)
# update counter_data_for_user in total_book_purchase counter
await AuthCurd.update_counter_data_for_user(
user_id=user_id, session=session, field_name="total_book_purchase"
)
if not html_response:
return CustomResponse.success(
message="Payment status updated successfully"
)
return """
<html>
<head>
<title>Payment Success</title>
</head>
<body>
<h1>Payment Sucess</h1>
</body>
</html>
"""
else:
return await payment_fail_view(
value_a=value_a, value_b=value_b, tran_id=tran_id, session=session
)
except Exception as e:
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment