diff --git a/.gitignore b/.gitignore index a3c046d..03f9406 100644 --- a/.gitignore +++ b/.gitignore @@ -145,3 +145,4 @@ dmypy.json # Cython debug symbols cython_debug/ +*.csv diff --git a/blueprints/main.py b/blueprints/main.py index ac57f0e..f4f7ffc 100644 --- a/blueprints/main.py +++ b/blueprints/main.py @@ -483,7 +483,7 @@ def get_stripe_customer_id(splynx_id): with connection.cursor() as cursor: cursor.execute(query, (splynx_id,)) result = cursor.fetchone() - + print(f"MYSQL: {result}") if result and result['stripe_customer_id']: print(f"Found Stripe customer ID in MySQL: {result['stripe_customer_id']}") return result['stripe_customer_id'] @@ -1345,6 +1345,7 @@ def payment_plans_detail(plan_id): @login_required def api_stripe_customer_id(splynx_id): """Get Stripe customer ID for a Splynx customer.""" + print(f"\n\nSplynx/Stripe finding customer\n\n") try: stripe_customer_id = get_stripe_customer_id(splynx_id) if stripe_customer_id: diff --git a/emailclass.py b/emailclass.py index 6265951..d39259e 100644 --- a/emailclass.py +++ b/emailclass.py @@ -5,7 +5,7 @@ from email.mime.multipart import MIMEMultipart port = 587 # For starttls smtp_server = "smtp.fibrenet.net.au" sender_email = "alan.woodman@interphone.com.au" -receiver_email = "alan.woodman@interphone.com.au" +receiver_email = "alan.woodman@interphone.com.au,helen.barbour@interphone.com.au,suzie.mcgregor@interphone.com.au" #password = input("Type your password and press enter:") class SendEmail(): @@ -52,4 +52,4 @@ class SendEmail(): ) return True except: - return False \ No newline at end of file + return False diff --git a/query_mysql.py b/query_mysql.py index 0062f4d..b12cad9 100644 --- a/query_mysql.py +++ b/query_mysql.py @@ -447,55 +447,67 @@ def processPaymentResult(pay_id, result, key): payment = db.session.query(Payments).filter(Payments.id == pay_id).first() elif key == "singlepay": payment = db.session.query(SinglePayments).filter(SinglePayments.id == pay_id).first() - try: - if result.get('error') and not result.get('needs_fee_update'): - payment.Error = f"Error Type: {result['error_type']}\nError: {result['error']}" - payment.Success = result['success'] - payment.PI_JSON = json.dumps(result) - - # Send notification and create ticket for failed payments - handle_failed_payment_notification( - payment_record=payment, - error_details=payment.Error, - payment_type=key + #try: + if result.get('error') and not result.get('needs_fee_update'): + payment.Error = f"Error Type: {result['error_type']}\nError: {result['error']}" + payment.Success = result['success'] + payment.PI_JSON = json.dumps(result) + + # Send notification and create ticket for failed payments + handle_failed_payment_notification( + payment_record=payment, + error_details=payment.Error, + payment_type=key + ) + elif result.get('failure_details'): + payment.Error = f"Error Type: {result.get('failure_details').get('decline_code')}\nError: {result['failure_reason']}" + payment.Success = result['success'] + payment.PI_JSON = json.dumps(result) + + # Send notification and create ticket for failed payments + handle_failed_payment_notification( + payment_record=payment, + error_details=payment.Error, + payment_type=key + ) + else: + print("Payment successful!") + if result.get('needs_fee_update'): + payment.PI_FollowUp = True + # Mark invoices as pending when PI_FollowUp is set + if PROCESS_LIVE: + find_set_pending_splynx_invoices(payment.Splynx_ID) + payment.Payment_Intent = result['payment_intent_id'] + payment.Success = result['success'] + if result['success'] and PROCESS_LIVE: + find_pay_splynx_invoices(payment.Splynx_ID) + add_payment_splynx( + splynx_id=payment.Splynx_ID, + pi_id=result['payment_intent_id'], + pay_id=payment.id, + amount=payment.Payment_Amount ) + if result.get('payment_method_type') == "card": + payment.Payment_Method = result['estimated_fee_details']['card_display_brand'] + elif result.get('payment_method_type') == "au_becs_debit": + payment.Payment_Method = result['payment_method_type'] + if payment.PI_JSON: + combined = {**json.loads(payment.PI_JSON), **result} + payment.PI_JSON = json.dumps(combined) else: - if result.get('needs_fee_update'): - payment.PI_FollowUp = True - # Mark invoices as pending when PI_FollowUp is set - if PROCESS_LIVE: - find_set_pending_splynx_invoices(payment.Splynx_ID) - payment.Payment_Intent = result['payment_intent_id'] - payment.Success = result['success'] - if result['success'] and PROCESS_LIVE: - find_pay_splynx_invoices(payment.Splynx_ID) - add_payment_splynx( - splynx_id=payment.Splynx_ID, - pi_id=result['payment_intent_id'], - pay_id=payment.id, - amount=payment.Payment_Amount - ) - if result.get('payment_method_type') == "card": - payment.Payment_Method = result['estimated_fee_details']['card_display_brand'] - elif result.get('payment_method_type') == "au_becs_debit": - payment.Payment_Method = result['payment_method_type'] - if payment.PI_JSON: - combined = {**json.loads(payment.PI_JSON), **result} - payment.PI_JSON = json.dumps(combined) - else: - payment.PI_JSON = json.dumps(result) - if result.get('fee_details'): - payment.Fee_Total = result['fee_details']['total_fee'] - for fee_type in result['fee_details']['fee_breakdown']: - if fee_type['type'] == "tax": - payment.Fee_Tax = fee_type['amount'] - elif fee_type['type'] == "stripe_fee": - payment.Fee_Stripe = fee_type['amount'] - except Exception as e: - logger.error(f"processPaymentResult: {e}\nResult: {json.dumps(result)}") - payment.PI_FollowUp = True - if PROCESS_LIVE: - find_set_pending_splynx_invoices(payment.Splynx_ID) + payment.PI_JSON = json.dumps(result) + if result.get('fee_details'): + payment.Fee_Total = result['fee_details']['total_fee'] + for fee_type in result['fee_details']['fee_breakdown']: + if fee_type['type'] == "tax": + payment.Fee_Tax = fee_type['amount'] + elif fee_type['type'] == "stripe_fee": + payment.Fee_Stripe = fee_type['amount'] + #except Exception as e: + # logger.error(f"processPaymentResult: {e}\nResult: {json.dumps(result)}") + # payment.PI_FollowUp = True + # if PROCESS_LIVE: + # find_set_pending_splynx_invoices(payment.Splynx_ID) def _update_payment(): return True # Just need to trigger commit, payment is already modified @@ -721,7 +733,7 @@ def process_payintent_mode(processor): try: intent_result = processor.check_payment_intent(pi.Payment_Intent) logger.debug(f"Intent result: {json.dumps(intent_result, indent=2)}") - + print(f"\n\npayintent result: {json.dumps(intent_result, indent=2)}\n\n") if intent_result['status'] == "succeeded": pi.PI_FollowUp_JSON = json.dumps(intent_result) pi.PI_FollowUp = False @@ -729,11 +741,12 @@ def process_payintent_mode(processor): pi.Success = True # Mark invoices as paid when payment intent succeeds - if PROCESS_LIVE: - find_pay_splynx_invoices(pi.Splynx_ID) + #if PROCESS_LIVE: + # find_pay_splynx_invoices(pi.Splynx_ID) #if intent_result.get('charge_id').startswith('ch_'): # pi.Stripe_Charge_ID = intent_result.get('charge_id') + print("\nProcess payment results coz it succeeded") processPaymentResult(pay_id=pi.id, result=intent_result, key=key) succeeded_count += 1 elif intent_result['status'] == "failed":