You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
4.3 KiB
113 lines
4.3 KiB
|
|
|
|
import pymysql
|
|
import sys
|
|
import json
|
|
import random
|
|
import threading
|
|
import logging
|
|
import stripe
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
from datetime import datetime
|
|
from typing import List, Dict, Union, Any
|
|
from stripe_payment_processor import StripePaymentProcessor
|
|
from config import Config
|
|
from app import create_app, db
|
|
from models import Payments, PaymentBatch, SinglePayments, PaymentPlans
|
|
from splynx import Splynx, SPLYNX_URL, SPLYNX_KEY, SPLYNX_SECRET
|
|
from services import (
|
|
log_script_start, log_script_completion, log_batch_created,
|
|
log_payment_intent_followup
|
|
)
|
|
from notification_service import NotificationService
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.FileHandler('payment_processing.log'),
|
|
logging.StreamHandler(sys.stdout)
|
|
]
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Initialize Splynx API
|
|
splynx = Splynx(url=SPLYNX_URL, key=SPLYNX_KEY, secret=SPLYNX_SECRET)
|
|
|
|
# Import constants from config
|
|
PAYMENT_METHOD_DIRECT_DEBIT = Config.PAYMENT_METHOD_DIRECT_DEBIT
|
|
PAYMENT_METHOD_CARD = Config.PAYMENT_METHOD_CARD
|
|
PAYMENT_METHOD_PAYMENT_PLAN = Config.PAYMENT_METHOD_PAYMENT_PLAN
|
|
PROCESS_LIVE = Config.PROCESS_LIVE
|
|
|
|
# Get Stripe API key from config
|
|
if PROCESS_LIVE:
|
|
api_key = Config.STRIPE_LIVE_API_KEY
|
|
else:
|
|
api_key = Config.STRIPE_TEST_API_KEY
|
|
#test_stripe_customers = ['cus_SoQqMGLmCjiBDZ', 'cus_SoQptxwe8hczGz', 'cus_SoQjeNXkKOdORI', 'cus_SoQiDcSrNRxbPF', 'cus_SoQedaG3q2ecKG', 'cus_SoQeTkzMA7AaLR', 'cus_SoQeijBTETQcGb', 'cus_SoQe259iKMgz7o', 'cus_SoQejTstdXEDTO', 'cus_SoQeQH2ORWBOWX', 'cus_SoQevtyWxqXtpC', 'cus_SoQekOFUHugf26', 'cus_SoPq6Zh0MCUR9W', 'cus_SoPovwUPJmvugz', 'cus_SoPnvGfejhpSR5', 'cus_SoNAgAbkbFo8ZY', 'cus_SoMyDihTxRsa7U', 'cus_SoMVPWxdYstYbr', 'cus_SoMVQ6Xj2dIrCR', 'cus_SoMVmBn1xipFEB', 'cus_SoMVNvZ2Iawb7Y', 'cus_SoMVZupj6wRy5e', 'cus_SoMVqjH7zkc5Qe', 'cus_SoMVkzj0ZUK0Ai', 'cus_SoMVFq3BUD3Njw', 'cus_SoLcrRrvoy9dJ4', 'cus_SoLcqHN1k0WD8j', 'cus_SoLcLtYDZGG32V', 'cus_SoLcG23ilNeMYt', 'cus_SoLcFhtUVzqumj', 'cus_SoLcPgMnuogINl', 'cus_SoLccGTY9mMV7T', 'cus_SoLRxqvJxuKFes', 'cus_SoKs7cjdcvW1oO']
|
|
|
|
stripe.api_key = api_key
|
|
|
|
def get_latest_payment_intent(customer_id):
|
|
"""Get the most recently created Payment Intent for a customer"""
|
|
payment_intents = stripe.PaymentIntent.list(
|
|
customer=customer_id,
|
|
limit=1 # Only get the most recent one
|
|
)
|
|
|
|
if payment_intents.data:
|
|
return payment_intents.data[0]
|
|
return None
|
|
|
|
|
|
if __name__ == "__main__":
|
|
## Payment Method:
|
|
## 2 - Direct Debit (Automatic)
|
|
## 3 - Card Payment (Automatic)
|
|
## 9 - Payment Plan
|
|
|
|
### Running Mode
|
|
## batch = Monthly Direct Debit/Credit Cards
|
|
## payintent = Check outstanding Payment Intents and update
|
|
## payplan = Check for Payment Plans to run
|
|
## refund = Check outstanding Refunds and update
|
|
|
|
start_time = datetime.now()
|
|
|
|
|
|
# Create Flask application context
|
|
app = create_app()
|
|
api_key = Config.STRIPE_LIVE_API_KEY
|
|
|
|
|
|
with app.app_context():
|
|
customers = (
|
|
db.session.query(Payments)
|
|
.filter(Payments.PaymentBatch_ID.in_((108,109)))
|
|
.all()
|
|
)
|
|
|
|
for pay in customers:
|
|
#print(pay)
|
|
pi = get_latest_payment_intent(pay.Stripe_Customer_ID)
|
|
#print(json.dumps(pi,indent=2))
|
|
if str(pay.id) in pi['description']:
|
|
print("MATCH")
|
|
pm = stripe.Customer.list_payment_methods(
|
|
customer=pay.Stripe_Customer_ID
|
|
)
|
|
#print(json.dumps(pm,indent=2))
|
|
#print(f"card: {pm['data'][0].get('card').get('brand')}")
|
|
pm2 = pm['data'][0]
|
|
if hasattr(pm2, 'card'):
|
|
print(f"card: {pm['data'][0].get('card').get('brand')}")
|
|
pay.Payment_Method = pm['data'][0].get('card').get('brand')
|
|
elif hasattr(pm2, 'au_becs_debit'):
|
|
print("au_becs_debit")
|
|
pay.Payment_Method = "au_becs_debit"
|
|
pay.Payment_Intent = pi['id']
|
|
pay.Stripe_Payment_Method = pi['payment_method']
|
|
pay.PI_FollowUp = True
|
|
db.session.commit()
|