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.
95 lines
4.1 KiB
95 lines
4.1 KiB
import json
|
|
import stripe
|
|
from datetime import datetime
|
|
from typing import List, Dict, Union, Any
|
|
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_activity
|
|
from config import Config
|
|
from sqlalchemy import and_
|
|
|
|
|
|
splynx = Splynx(url=SPLYNX_URL, key=SPLYNX_KEY, secret=SPLYNX_SECRET)
|
|
|
|
|
|
api_key = Config.STRIPE_LIVE_API_KEY
|
|
|
|
stripe.api_key = api_key
|
|
|
|
|
|
def find_pay_splynx_invoices(splynx_id: int, result: dict) -> List[Dict[str, Any]]:
|
|
#result = splynx.get(url=f"/api/2.0/admin/finance/invoices?main_attributes[customer_id]={splynx_id}&main_attributes[status]=not_paid&main_attributes[status]=pending")
|
|
|
|
invoice_pay = {
|
|
"status": "paid"
|
|
}
|
|
|
|
for pay in result:
|
|
#res = splynx.put(url=f"/api/2.0/admin/finance/invoices/{pay['id']}", params=invoice_pay)
|
|
print(f"Invoice: {pay['id']} marked as paid - {pay['status']}")
|
|
|
|
def add_payment_splynx(splynx_id: int, pi_id: str, pay_id: int, amount: float, invoice_id: int) -> Union[int, bool]:
|
|
stripe_pay = {
|
|
"customer_id": splynx_id,
|
|
"amount": amount,
|
|
"date": str(datetime.now().strftime('%Y-%m-%d')),
|
|
"field_1": pi_id,
|
|
"field_2": f"Payment_ID (Batch): {pay_id}",
|
|
"invoice_id": invoice_id
|
|
}
|
|
|
|
res = splynx.post(url="/api/2.0/admin/finance/payments", params=stripe_pay)
|
|
if res:
|
|
return res['id']
|
|
else:
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
app = create_app()
|
|
i = 1
|
|
#cust_bill = splynx.get(url=f"/api/2.0/admin/customers/customer-billing/31")
|
|
#print(json.dumps(cust_bill,indent=2))
|
|
with app.app_context():
|
|
custs = db.session.query(PaymentBatch,Payments)\
|
|
.join(Payments, Payments.PaymentBatch_ID == PaymentBatch.id)\
|
|
.filter(and_(PaymentBatch.id.in_((109,109)), Payments.Success == True))\
|
|
.all()
|
|
print(len(custs))
|
|
for cust in custs:
|
|
try:
|
|
cust_bill = splynx.get(url=f"/api/2.0/admin/customers/customer-billing/{cust.Payments.Splynx_ID}")
|
|
print(f"{i}/{len(custs)}")
|
|
print(f"SplynxID: {cust.Payments.Splynx_ID} - ${float(cust_bill['deposit'])} - ${cust.Payments.Payment_Amount} - {cust.Payments.Payment_Intent}")
|
|
#print(json.dumps(cust_bill,indent=2))
|
|
if float(cust_bill['deposit']) < 0:
|
|
params = {
|
|
"main_attributes": {
|
|
"customer_id": cust.Payments.Splynx_ID,
|
|
"status": ["IN", ["pending", "not_paid"]],
|
|
"date_created": ["!=", "2026-01-05"]
|
|
}
|
|
}
|
|
#result = splynx.get(url=f"/api/2.0/admin/finance/invoices?main_attributes[customer_id]={cust.Payments.Splynx_ID}&main_attributes[status]=pending")
|
|
result = splynx.get(url=f"/api/2.0/admin/finance/invoices?{splynx.build_splynx_query_params(params)}")
|
|
#print(json.dumps(result,indent=2))
|
|
if len(result) > 0:
|
|
print(f"\t{cust.Payments.Splynx_ID} - Has unpaid invoices ({len(result)})")
|
|
for res in result:
|
|
print(f"\tInvoiceID: {res['id']} - {res['status']}")
|
|
#print(json.dumps(res,indent=2))
|
|
res = add_payment_splynx(
|
|
splynx_id=cust.Payments.Splynx_ID,
|
|
pi_id=cust.Payments.Payment_Intent,
|
|
pay_id=cust.Payments.PaymentBatch_ID,
|
|
amount=cust.Payments.Payment_Amount,
|
|
invoice_id=result[0]['id']
|
|
)
|
|
#if res:
|
|
# print("\tPayment added")
|
|
# find_pay_splynx_invoices(splynx_id=cust.Payments.Splynx_ID, result=result)
|
|
#else:
|
|
# print("\tAdding payment failed")
|
|
except:
|
|
print("Fuck")
|
|
i += 1
|
|
|