|
|
@ -1633,108 +1633,109 @@ def finalize_payment_method(): |
|
|
""" |
|
|
""" |
|
|
Finalize payment method setup after Stripe confirmation. |
|
|
Finalize payment method setup after Stripe confirmation. |
|
|
""" |
|
|
""" |
|
|
try: |
|
|
#try: |
|
|
data = request.get_json() |
|
|
data = request.get_json() |
|
|
setup_intent_id = data.get('setup_intent_id') |
|
|
setup_intent_id = data.get('setup_intent_id') |
|
|
stripe_customer_id = data.get('stripe_customer_id') |
|
|
stripe_customer_id = data.get('stripe_customer_id') |
|
|
#stripe_customer_id = "cus_SoQqMGLmCjiBDZ" |
|
|
#stripe_customer_id = "cus_SoQqMGLmCjiBDZ" |
|
|
set_as_default = data.get('set_as_default', False) |
|
|
set_as_default = data.get('set_as_default', False) |
|
|
splynx_id = data.get('splynx_id') |
|
|
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 |
|
|
|
|
|
|
|
|
if not all([setup_intent_id, stripe_customer_id]): |
|
|
# Initialize Stripe processor |
|
|
return jsonify({ |
|
|
config = Config() |
|
|
'success': False, |
|
|
processor = StripePaymentProcessor(api_key=config.STRIPE_SECRET_KEY) |
|
|
'error': 'setup_intent_id and stripe_customer_id are required' |
|
|
|
|
|
}), 400 |
|
|
|
|
|
|
|
|
|
|
|
# Initialize Stripe processor |
|
|
# Check setup intent status |
|
|
config = Config() |
|
|
setup_result = processor.get_setup_intent_status(setup_intent_id) |
|
|
processor = StripePaymentProcessor(api_key=config.STRIPE_SECRET_KEY) |
|
|
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 |
|
|
|
|
|
|
|
|
# Check setup intent status |
|
|
if setup_result['status'] != 'succeeded': |
|
|
setup_result = processor.get_setup_intent_status(setup_intent_id) |
|
|
return jsonify({ |
|
|
|
|
|
'success': False, |
|
|
|
|
|
'error': f'Setup intent not succeeded. Status: {setup_result["status"]}' |
|
|
|
|
|
}), 400 |
|
|
|
|
|
|
|
|
if not setup_result['success']: |
|
|
payment_method = setup_result.get('payment_method') |
|
|
return jsonify({ |
|
|
print(f"payment_method: {payment_method}") |
|
|
'success': False, |
|
|
if not payment_method: |
|
|
'error': f'Setup intent check failed: {setup_result.get("error")}' |
|
|
return jsonify({ |
|
|
}), 400 |
|
|
'success': False, |
|
|
|
|
|
'error': 'No payment method found in setup intent' |
|
|
|
|
|
}), 400 |
|
|
|
|
|
|
|
|
if setup_result['status'] != 'succeeded': |
|
|
# Attach payment method to customer (if not already attached) |
|
|
return jsonify({ |
|
|
attach_result = processor.attach_payment_method( |
|
|
'success': False, |
|
|
payment_method['id'], |
|
|
'error': f'Setup intent not succeeded. Status: {setup_result["status"]}' |
|
|
stripe_customer_id |
|
|
}), 400 |
|
|
) |
|
|
|
|
|
|
|
|
payment_method = setup_result.get('payment_method') |
|
|
print(f"attach_result: {attach_result}") |
|
|
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) |
|
|
if not attach_result['success']: |
|
|
attach_result = processor.attach_payment_method( |
|
|
return jsonify({ |
|
|
payment_method['id'], |
|
|
'success': False, |
|
|
stripe_customer_id |
|
|
'error': f'Failed to attach payment method: {attach_result.get("error")}' |
|
|
) |
|
|
}), 500 |
|
|
|
|
|
|
|
|
print(f"attach_result: {attach_result}") |
|
|
# Set as default if requested |
|
|
|
|
|
if set_as_default: |
|
|
|
|
|
default_result = processor.set_default_payment_method( |
|
|
|
|
|
stripe_customer_id, |
|
|
|
|
|
payment_method['id'] |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
if not attach_result['success']: |
|
|
if not default_result['success']: |
|
|
return jsonify({ |
|
|
# Log warning but don't fail the request |
|
|
'success': False, |
|
|
log_activity( |
|
|
'error': f'Failed to attach payment method: {attach_result.get("error")}' |
|
|
user_id=current_user.id, |
|
|
}), 500 |
|
|
action="set_default_payment_method_failed", |
|
|
|
|
|
entity_type="payment_method", |
|
|
# Set as default if requested |
|
|
entity_id=None, |
|
|
if set_as_default: |
|
|
details=f"Failed to set as default: {default_result.get('error')}", |
|
|
default_result = processor.set_default_payment_method( |
|
|
ip_address=request.remote_addr |
|
|
stripe_customer_id, |
|
|
|
|
|
payment_method['id'] |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
if not default_result['success']: |
|
|
# Log successful addition |
|
|
# Log warning but don't fail the request |
|
|
log_activity( |
|
|
log_activity( |
|
|
user_id=current_user.id, |
|
|
user_id=current_user.id, |
|
|
action="add_payment_method", |
|
|
action="set_default_payment_method_failed", |
|
|
entity_type="payment_method", |
|
|
entity_type="payment_method", |
|
|
entity_id=payment_method['id'], |
|
|
entity_id=None, |
|
|
details=f"Added {payment_method['type']} payment method for customer {stripe_customer_id} (Splynx ID: {splynx_id}). Set as default: {set_as_default}", |
|
|
details=f"Failed to set as default: {default_result.get('error')}", |
|
|
ip_address=request.remote_addr |
|
|
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: |
|
|
return jsonify({ |
|
|
log_activity( |
|
|
'success': True, |
|
|
user_id=current_user.id, |
|
|
'payment_method': payment_method, |
|
|
action="finalize_payment_method_error", |
|
|
'is_default': set_as_default, |
|
|
entity_type="payment_method", |
|
|
'setup_intent_id': setup_intent_id, |
|
|
details=f"Payment method finalization error: {str(e)}", |
|
|
'customer_id': stripe_customer_id |
|
|
ip_address=request.remote_addr |
|
|
}) |
|
|
) |
|
|
|
|
|
return jsonify({ |
|
|
#except Exception as e: |
|
|
'success': False, |
|
|
# log_activity( |
|
|
'error': f'Payment method finalization failed: {str(e)}' |
|
|
# user_id=current_user.id, |
|
|
}), 500 |
|
|
# 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']) |
|
|
@main_bp.route('/api/get-payment-methods', methods=['POST']) |
|
|
@login_required |
|
|
@login_required |
|
|
|