Add support for corporate tournament Stripe payments

The commit adds webhook handling for corporate tournaments using
platform account (XLR) vs connected accounts, updates payment link
creation logic, and improves logging.
mailing
Razmig Sarkissian 1 month ago
parent 22b06b4494
commit f4d8b1a536
  1. 1
      padelclub_backend/settings_local.py.dist
  2. 99
      tournaments/services/payment_service.py

@ -42,6 +42,7 @@ STRIPE_PUBLISHABLE_KEY = ''
STRIPE_SECRET_KEY = '' STRIPE_SECRET_KEY = ''
SHOP_STRIPE_WEBHOOK_SECRET = 'whsec_...' # Your existing webhook secret SHOP_STRIPE_WEBHOOK_SECRET = 'whsec_...' # Your existing webhook secret
TOURNAMENT_STRIPE_WEBHOOK_SECRET = 'whsec_...' # New webhook secret for tournaments TOURNAMENT_STRIPE_WEBHOOK_SECRET = 'whsec_...' # New webhook secret for tournaments
XLR_STRIPE_WEBHOOK_SECRET = 'whsec_...' # New webhook secret for padel club
STRIPE_FEE = 0.0075 STRIPE_FEE = 0.0075
TOURNAMENT_SETTINGS = { TOURNAMENT_SETTINGS = {
'TIME_PROXIMITY_RULES': { 'TIME_PROXIMITY_RULES': {

@ -592,20 +592,32 @@ class PaymentService:
def stripe_webhook(request): def stripe_webhook(request):
payload = request.body payload = request.body
sig_header = request.META.get('HTTP_STRIPE_SIGNATURE') sig_header = request.META.get('HTTP_STRIPE_SIGNATURE')
print("Received webhook call")
# Check if this is a Connect account webhook
stripe_account = request.META.get('HTTP_STRIPE_ACCOUNT')
print("=== WEBHOOK DEBUG ===")
print(f"Signature: {sig_header}") print(f"Signature: {sig_header}")
print(f"Connect Account: {stripe_account}")
if stripe_account:
# This is a connected account (regular umpire tournament)
webhook_secret = settings.TOURNAMENT_STRIPE_WEBHOOK_SECRET
print(f"Using umpire webhook secret for connected account: {stripe_account}")
else:
# This is platform account (corporate tournament)
webhook_secret = settings.XLR_STRIPE_WEBHOOK_SECRET
print("Using XLR company webhook secret")
try: try:
event = stripe.Webhook.construct_event( event = stripe.Webhook.construct_event(payload, sig_header, webhook_secret)
payload, sig_header, settings.TOURNAMENT_STRIPE_WEBHOOK_SECRET
)
print(f"Tournament webhook event type: {event['type']}") print(f"Tournament webhook event type: {event['type']}")
# Debug metadata
stripe_object = event['data']['object'] stripe_object = event['data']['object']
metadata = stripe_object.get('metadata', {})
# Debug: Print the object type print(f"is_corporate_tournament: {metadata.get('is_corporate_tournament', 'unknown')}")
object_type = stripe_object.get('object', 'unknown') print(f"payment_source: {metadata.get('payment_source', 'unknown')}")
print(f"Stripe object type: {object_type}")
if event['type'] == 'checkout.session.completed': if event['type'] == 'checkout.session.completed':
success = PaymentService.process_direct_payment(stripe_object) success = PaymentService.process_direct_payment(stripe_object)
@ -639,7 +651,7 @@ class PaymentService:
return HttpResponse(status=200) return HttpResponse(status=200)
except Exception as e: except Exception as e:
print(f"Tournament webhook error: {str(e)}") print(f"Webhook error: {str(e)}")
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return HttpResponse(status=400) return HttpResponse(status=400)
@ -665,12 +677,26 @@ class PaymentService:
stripe_amount = currency_service.convert_to_stripe_amount(team_fee, tournament.currency_code) stripe_amount = currency_service.convert_to_stripe_amount(team_fee, tournament.currency_code)
customer_email = team_registration.team_contact() customer_email = team_registration.team_contact()
currency_code = tournament.currency_code or 'EUR'
print(f"[PAYMENT LINK] Tournament: {tournament.name}")
print(f"[PAYMENT LINK] is_corporate_tournament: {tournament.is_corporate_tournament}")
# Base metadata (same as checkout session)
base_metadata = {
'tournament_id': str(tournament.id),
'team_registration_id': str(team_registration.id),
'customer_email': customer_email or 'Non fourni',
'payment_source': 'payment_link',
'registration_type': 'direct',
'currency_code': currency_code,
}
# Create payment link # Create payment link params
payment_link_params = { payment_link_params = {
'line_items': [{ 'line_items': [{
'price_data': { 'price_data': {
'currency': tournament.currency_code.lower(), 'currency': currency_code.lower(),
'product_data': { 'product_data': {
'name': f'{tournament.broadcast_display_name()} du {tournament.formatted_start_date()}', 'name': f'{tournament.broadcast_display_name()} du {tournament.formatted_start_date()}',
'description': f'Lieu {tournament.event.club.name}', 'description': f'Lieu {tournament.event.club.name}',
@ -679,13 +705,6 @@ class PaymentService:
}, },
'quantity': 1, 'quantity': 1,
}], }],
'metadata': {
'tournament_id': str(tournament.id),
'team_registration_id': str(team_registration.id),
'customer_email': customer_email or 'Non fourni',
'payment_source': 'payment_link',
'registration_type': 'direct',
},
'after_completion': { 'after_completion': {
'type': 'redirect', 'type': 'redirect',
'redirect': { 'redirect': {
@ -694,31 +713,53 @@ class PaymentService:
}, },
'automatic_tax': {'enabled': False}, 'automatic_tax': {'enabled': False},
'billing_address_collection': 'auto', 'billing_address_collection': 'auto',
'shipping_address_collection': None,
} }
# Add customer email if available # Handle corporate vs regular tournaments (same logic as checkout session)
if customer_email: if tournament.is_corporate_tournament:
payment_link_params['customer_creation'] = 'if_required' print(f"[PAYMENT LINK] Corporate tournament - creating on platform account")
# Note: Stripe Payment Links don't support customer_email parameter # Corporate tournament - create on platform account (no Connect account)
# but will ask for email during checkout metadata = {
**base_metadata,
'is_corporate_tournament': 'true',
'stripe_account_type': 'direct'
}
payment_link_params['metadata'] = metadata
# Handle Stripe Connect account if needed # Create payment link on platform account
stripe_account_id = tournament.event.creator.stripe_account_id if hasattr(tournament.event.creator, 'stripe_account_id') else None payment_link = stripe.PaymentLink.create(**payment_link_params)
else:
print(f"[PAYMENT LINK] Regular tournament - creating on connected account")
# Regular tournament - create on connected account
stripe_account_id = tournament.stripe_account_id
if not stripe_account_id:
print(f"[PAYMENT LINK] ERROR: No Stripe account ID for umpire")
return None
metadata = {
**base_metadata,
'is_corporate_tournament': 'false',
'stripe_account_type': 'connect',
'stripe_account_id': stripe_account_id
}
payment_link_params['metadata'] = metadata
if stripe_account_id: print(f"[PAYMENT LINK] Creating payment link for connected account: {stripe_account_id}")
# Create payment link on connected account
payment_link = stripe.PaymentLink.create( payment_link = stripe.PaymentLink.create(
**payment_link_params, **payment_link_params,
stripe_account=stripe_account_id stripe_account=stripe_account_id
) )
else:
payment_link = stripe.PaymentLink.create(**payment_link_params)
print(f"[PAYMENT LINK] Created payment link: {payment_link.url}") print(f"[PAYMENT LINK] Created payment link: {payment_link.url}")
return payment_link.url return payment_link.url
except Exception as e: except Exception as e:
print(f"[PAYMENT LINK] Error creating payment link: {str(e)}") print(f"[PAYMENT LINK] Error creating payment link: {str(e)}")
import traceback
traceback.print_exc()
return None return None
@staticmethod @staticmethod

Loading…
Cancel
Save