Did some other modifications

This commit is contained in:
Joan
2025-09-16 23:00:28 +02:00
parent 2bfdb539be
commit 65b8542791
4 changed files with 80 additions and 7 deletions

View File

@@ -16,9 +16,10 @@ from database import (
get_remaining_numbers_amount,
get_main_message_id,
store_update_message_id, get_update_message_id,
get_last_n_other_participants, get_remaining_numbers
get_last_n_other_participants, get_remaining_numbers,
delete_reservation_timestamp, cancel_reservation_by_id
)
from config import BOT_TOKEN, BOT_NAME, WEBHOOK_ID, TYC_DOCUMENT_URL, REVERSE_CHANNELS, NEWRELIC_API_KEY, PAYPAL_URL
from config import BOT_TOKEN, BOT_NAME, WEBHOOK_ID, TYC_DOCUMENT_URL, REVERSE_CHANNELS, NEWRELIC_API_KEY, PAYPAL_URL, ADMIN_IDS
from newrelic_telemetry_sdk import Log, LogClient
app = Flask(__name__)
@@ -411,7 +412,12 @@ def paypal_webhook():
# Capture payment
resource = event["resource"]
invoice_id = resource.get("id") # capture ID
capture_order(invoice_id, access_token)
delete_reservation_timestamp(invoice_id)
response = capture_order(invoice_id, access_token)
if response.get("status") == "COMPLETED":
logger.info(f"Payment for order {invoice_id} captured successfully.")
else:
logger.error(f"Failed to capture payment for order {invoice_id}. Response: {response}")
elif event_type == "PAYMENT.CAPTURE.COMPLETED":
logger.info(f"✅ Payment completed: {event['resource']['id']}")
resource = event["resource"]
@@ -426,6 +432,55 @@ def paypal_webhook():
# Process the valid payment
receive_paypal_payment(invoice_id, payment_status, payment_amount)
elif event_type == "PAYMENT.CAPTURE.PENDING":
logger.info(f"⏳ Payment pending: {event['resource']['id']}. Awaiting completion.")
# Optionally notify user about pending status
resource = event["resource"]
invoice_id = resource["supplementary_data"]["related_ids"]["order_id"]
payment_status = resource.get("status")
payment_amount = resource["amount"]["value"]
status_details = resource["status_details"]["reason"]
delete_reservation_timestamp(invoice_id)
if status_details == "PENDING_REVIEW":
logger.info(f"Payment for invoice {invoice_id} is pending review. Notifying admins.")
for admin_id in ADMIN_IDS:
send_telegram_message(
admin_id,
f"⚠️ El pago para la factura {invoice_id} está pendiente. "
f"Estado actual: '{payment_status}'. "
f"Detalles: '{status_details}'. "
f"Por favor, revisa manualmente si es necesario."
)
elif status_details == "REFUNDED":
logger.info(f"Payment for invoice {invoice_id} has been refunded. Notifying admins.")
for admin_id in ADMIN_IDS:
send_telegram_message(
admin_id,
f"⚠️ El pago para la factura {invoice_id} ha sido reembolsado. "
f"Estado actual: '{payment_status}'. "
f"Detalles: '{status_details}'. "
f"Por favor, revisa manualmente si es necesario."
)
elif event_type == "PAYMENT.CAPTURE.DENIED":
logger.info(f"❌ Payment denied: {event['resource']['id']}. Notifying user.")
resource = event["resource"]
invoice_id = resource["supplementary_data"]["related_ids"]["order_id"]
payment_status = resource.get("status")
payment_amount = resource["amount"]["value"]
participant_data = get_user_by_invoice_id(invoice_id)
if participant_data:
user_id = participant_data['user_id']
cancel_reservation_by_id(participant_data['id'])
send_telegram_message(
user_id,
f"❌ Tu pago para la factura {invoice_id} ha sido denegado. "
f"Por favor, inténtalo de nuevo o contacta con un administrador si crees que es un error."
)
else:
logger.warning(f"Could not find participant data for denied payment invoice {invoice_id}.")
else:
logger.info(f" Received event: {event_type}")
else: