From 39a332150d3b56d81be6b1e4fb152e5e1edeb4b3 Mon Sep 17 00:00:00 2001 From: Alan Woodman Date: Tue, 26 Aug 2025 14:20:49 +0800 Subject: [PATCH] Fixed BECS Payment Method --- blueprints/main.py | 197 +++++++++++++------------ config.py | 2 +- stripe_payment_processor.py | 6 +- templates/main/add_payment_method.html | 2 +- 4 files changed, 105 insertions(+), 102 deletions(-) diff --git a/blueprints/main.py b/blueprints/main.py index 0bb9a6f..4247687 100644 --- a/blueprints/main.py +++ b/blueprints/main.py @@ -1633,108 +1633,109 @@ def finalize_payment_method(): """ Finalize payment method setup after Stripe confirmation. """ - try: - data = request.get_json() - setup_intent_id = data.get('setup_intent_id') - stripe_customer_id = data.get('stripe_customer_id') - #stripe_customer_id = "cus_SoQqMGLmCjiBDZ" - set_as_default = data.get('set_as_default', False) - splynx_id = data.get('splynx_id') - - if not all([setup_intent_id, stripe_customer_id]): - return jsonify({ - 'success': False, - 'error': 'setup_intent_id and stripe_customer_id are required' - }), 400 - - # Initialize Stripe processor - config = Config() - processor = StripePaymentProcessor(api_key=config.STRIPE_SECRET_KEY) - - # Check setup intent status - setup_result = processor.get_setup_intent_status(setup_intent_id) - - if not setup_result['success']: - return jsonify({ - 'success': False, - 'error': f'Setup intent check failed: {setup_result.get("error")}' - }), 400 - - if setup_result['status'] != 'succeeded': - return jsonify({ - 'success': False, - 'error': f'Setup intent not succeeded. Status: {setup_result["status"]}' - }), 400 - - payment_method = setup_result.get('payment_method') - if not payment_method: - return jsonify({ - 'success': False, - 'error': 'No payment method found in setup intent' - }), 400 - - # Attach payment method to customer (if not already attached) - attach_result = processor.attach_payment_method( - payment_method['id'], - stripe_customer_id - ) - - print(f"attach_result: {attach_result}") - - if not attach_result['success']: - return jsonify({ - 'success': False, - 'error': f'Failed to attach payment method: {attach_result.get("error")}' - }), 500 - - # Set as default if requested - if set_as_default: - default_result = processor.set_default_payment_method( - stripe_customer_id, - payment_method['id'] - ) - - if not default_result['success']: - # Log warning but don't fail the request - log_activity( - user_id=current_user.id, - action="set_default_payment_method_failed", - entity_type="payment_method", - entity_id=None, - details=f"Failed to set as default: {default_result.get('error')}", - ip_address=request.remote_addr - ) - - # Log successful addition - log_activity( - user_id=current_user.id, - action="add_payment_method", - entity_type="payment_method", - entity_id=payment_method['id'], - details=f"Added {payment_method['type']} payment method for customer {stripe_customer_id} (Splynx ID: {splynx_id}). Set as default: {set_as_default}", - ip_address=request.remote_addr - ) - + #try: + data = request.get_json() + setup_intent_id = data.get('setup_intent_id') + stripe_customer_id = data.get('stripe_customer_id') + #stripe_customer_id = "cus_SoQqMGLmCjiBDZ" + set_as_default = data.get('set_as_default', False) + splynx_id = data.get('splynx_id') + + if not all([setup_intent_id, stripe_customer_id]): return jsonify({ - 'success': True, - 'payment_method': payment_method, - 'is_default': set_as_default, - 'setup_intent_id': setup_intent_id, - 'customer_id': stripe_customer_id - }) - - except Exception as e: - log_activity( - user_id=current_user.id, - action="finalize_payment_method_error", - entity_type="payment_method", - details=f"Payment method finalization error: {str(e)}", - ip_address=request.remote_addr - ) + 'success': False, + 'error': 'setup_intent_id and stripe_customer_id are required' + }), 400 + + # Initialize Stripe processor + config = Config() + processor = StripePaymentProcessor(api_key=config.STRIPE_SECRET_KEY) + + # Check setup intent status + setup_result = processor.get_setup_intent_status(setup_intent_id) + print(f"setup_result: {setup_result}") + if not setup_result['success']: + return jsonify({ + 'success': False, + 'error': f'Setup intent check failed: {setup_result.get("error")}' + }), 400 + + if setup_result['status'] != 'succeeded': return jsonify({ 'success': False, - 'error': f'Payment method finalization failed: {str(e)}' + 'error': f'Setup intent not succeeded. Status: {setup_result["status"]}' + }), 400 + + payment_method = setup_result.get('payment_method') + print(f"payment_method: {payment_method}") + if not payment_method: + return jsonify({ + 'success': False, + 'error': 'No payment method found in setup intent' + }), 400 + + # Attach payment method to customer (if not already attached) + attach_result = processor.attach_payment_method( + payment_method['id'], + stripe_customer_id + ) + + print(f"attach_result: {attach_result}") + + if not attach_result['success']: + return jsonify({ + 'success': False, + 'error': f'Failed to attach payment method: {attach_result.get("error")}' }), 500 + + # Set as default if requested + if set_as_default: + default_result = processor.set_default_payment_method( + stripe_customer_id, + payment_method['id'] + ) + + if not default_result['success']: + # Log warning but don't fail the request + log_activity( + user_id=current_user.id, + action="set_default_payment_method_failed", + entity_type="payment_method", + entity_id=None, + details=f"Failed to set as default: {default_result.get('error')}", + ip_address=request.remote_addr + ) + + # Log successful addition + log_activity( + user_id=current_user.id, + action="add_payment_method", + entity_type="payment_method", + entity_id=payment_method['id'], + details=f"Added {payment_method['type']} payment method for customer {stripe_customer_id} (Splynx ID: {splynx_id}). Set as default: {set_as_default}", + ip_address=request.remote_addr + ) + + return jsonify({ + 'success': True, + 'payment_method': payment_method, + 'is_default': set_as_default, + 'setup_intent_id': setup_intent_id, + 'customer_id': stripe_customer_id + }) + + #except Exception as e: + # log_activity( + # user_id=current_user.id, + # action="finalize_payment_method_error", + # entity_type="payment_method", + # details=f"Payment method finalization error: {str(e)}", + # ip_address=request.remote_addr + # ) + # return jsonify({ + # 'success': False, + # 'error': f'Payment method finalization failed: {str(e)}' + # }), 500 @main_bp.route('/api/get-payment-methods', methods=['POST']) @login_required diff --git a/config.py b/config.py index c2ac91a..edecd8e 100644 --- a/config.py +++ b/config.py @@ -32,7 +32,7 @@ class Config: # Threading configuration - MAX_PAYMENT_THREADS = 5 # Number of concurrent payment processing threads + MAX_PAYMENT_THREADS = 15 # Number of concurrent payment processing threads THREAD_TIMEOUT = 60 # Timeout in seconds for payment processing threads # Stripe API Keys diff --git a/stripe_payment_processor.py b/stripe_payment_processor.py index 04f959e..6b55710 100644 --- a/stripe_payment_processor.py +++ b/stripe_payment_processor.py @@ -1083,7 +1083,8 @@ class StripePaymentProcessor: 'created': payment_method.created } - if payment_method.card: + #if payment_method.card: + if pm_details['type'] == "card": pm_details['card'] = { 'brand': payment_method.card.brand, 'last4': payment_method.card.last4, @@ -1091,7 +1092,8 @@ class StripePaymentProcessor: 'exp_month': payment_method.card.exp_month, 'exp_year': payment_method.card.exp_year } - elif payment_method.au_becs_debit: + #elif payment_method.au_becs_debit: + elif pm_details['type'] == "au_becs_debit": pm_details['au_becs_debit'] = { 'bsb_number': payment_method.au_becs_debit.bsb_number, 'last4': payment_method.au_becs_debit.last4 diff --git a/templates/main/add_payment_method.html b/templates/main/add_payment_method.html index a68b88a..df517d5 100644 --- a/templates/main/add_payment_method.html +++ b/templates/main/add_payment_method.html @@ -187,7 +187,7 @@