Working API CHARGING!
This commit is contained in:
@@ -29,6 +29,10 @@ class Service_Service(db.Model):
|
||||
description = db.Column(db.TEXT())
|
||||
|
||||
service_cost = db.Column(db.Numeric(10, 2), nullable=False)
|
||||
|
||||
payment_type = db.Column(db.INTEGER, nullable=True)
|
||||
payment_card_id = db.Column(db.INTEGER, nullable=True)
|
||||
payment_status = db.Column(db.INTEGER, nullable=True)
|
||||
|
||||
|
||||
class Service_Service_schema(ma.SQLAlchemyAutoSchema):
|
||||
|
||||
@@ -5,6 +5,7 @@ from app.classes.customer import Customer_Customer
|
||||
from app.classes.cards import Card_Card, Card_Card_schema
|
||||
from app.classes.transactions import Transaction
|
||||
from app.classes.delivery import Delivery_Delivery
|
||||
from app.classes.service import Service_Service, Service_Service_schema
|
||||
|
||||
|
||||
|
||||
@@ -438,3 +439,107 @@ def get_customer_transactions(customer_id, page):
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@payment.route("/service/payment/<int:service_id>/<int:payment_type>", methods=["PUT"])
|
||||
def process_service_payment_tiger(service_id, payment_type):
|
||||
service = db.session.query(Service_Service).filter(Service_Service.id == service_id).first()
|
||||
if not service:
|
||||
return jsonify({"ok": False, "error": "Service not found"}), 404
|
||||
|
||||
# Set payment columns as specified
|
||||
service.payment_type = payment_type # 1 for Tiger
|
||||
service.payment_status = 2 # As specified
|
||||
# payment_card_id retains the selected card's ID if set in the service record
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True}), 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@payment.route("/authorize/service/<int:service_id>", methods=["PUT"])
|
||||
def update_service_payment_authorize(service_id):
|
||||
service = db.session.query(Service_Service).filter(Service_Service.id == service_id).first()
|
||||
if not service:
|
||||
return jsonify({"error": "Service not found"}), 404
|
||||
|
||||
data = request.get_json()
|
||||
card_id = data.get('card_id')
|
||||
status = data.get('status', 0)
|
||||
|
||||
service.payment_type = 11 # CC - Authorize
|
||||
service.payment_status = status
|
||||
if card_id:
|
||||
service.payment_card_id = card_id
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True}), 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@payment.route("/capture/service/<int:service_id>", methods=["PUT"])
|
||||
def update_service_payment_capture(service_id):
|
||||
service = db.session.query(Service_Service).filter(Service_Service.id == service_id).first()
|
||||
if not service:
|
||||
return jsonify({"error": "Service not found"}), 404
|
||||
|
||||
data = request.get_json()
|
||||
card_id = data.get('card_id')
|
||||
status = data.get('status', 3) # Default to 3 for capture
|
||||
|
||||
if service.payment_type is None or service.payment_type != 11:
|
||||
service.payment_type = 11 # CC - Authorize
|
||||
service.payment_status = status # 3 for capture
|
||||
if card_id:
|
||||
service.payment_card_id = card_id
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True}), 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@payment.route("/transactions/service/<int:service_id>", methods=["GET"])
|
||||
def get_service_transactions(service_id):
|
||||
"""
|
||||
Gets all transactions for a specific service ID
|
||||
"""
|
||||
try:
|
||||
query = (
|
||||
db.session
|
||||
.query(Transaction)
|
||||
.filter(Transaction.service_id == service_id)
|
||||
.order_by(Transaction.created_at.desc())
|
||||
)
|
||||
|
||||
transactions = query.all()
|
||||
|
||||
transactions_data = []
|
||||
for transaction in transactions:
|
||||
transactions_data.append({
|
||||
"id": transaction.id,
|
||||
"preauthorize_amount": float(transaction.preauthorize_amount or 0),
|
||||
"charge_amount": float(transaction.charge_amount or 0),
|
||||
"transaction_type": transaction.transaction_type,
|
||||
"status": transaction.status,
|
||||
"created_at": transaction.created_at.isoformat() if transaction.created_at else None,
|
||||
"auth_net_transaction_id": transaction.auth_net_transaction_id,
|
||||
"rejection_reason": transaction.rejection_reason,
|
||||
"delivery_id": transaction.delivery_id,
|
||||
"service_id": transaction.service_id,
|
||||
"card_id": transaction.card_id,
|
||||
})
|
||||
|
||||
return jsonify(transactions_data), 200
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error fetching transactions for service {service_id}: {str(e)}")
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
@@ -154,6 +154,51 @@ def create_service_call():
|
||||
db.session.commit()
|
||||
return jsonify({ "ok": True, "id": new_service_call.id }), 201
|
||||
|
||||
@service.route("/update-cost/<int:id>", methods=["PUT"])
|
||||
def update_service_cost(id):
|
||||
"""
|
||||
Dedicated endpoint to update only the service cost for a service call.
|
||||
This is used after payment capture/charge to update the actual amount.
|
||||
"""
|
||||
try:
|
||||
# Find the service
|
||||
service_record = Service_Service.query.get_or_404(id)
|
||||
|
||||
# Get request data - only service_cost
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"error": "No data provided"}), 400
|
||||
|
||||
# Extract and validate the service_cost
|
||||
new_cost = data.get('service_cost')
|
||||
if new_cost is None:
|
||||
return jsonify({"error": "service_cost is required"}), 400
|
||||
|
||||
# Convert to float for validation
|
||||
try:
|
||||
new_cost_float = float(new_cost)
|
||||
except (ValueError, TypeError):
|
||||
return jsonify({"error": "service_cost must be a valid number"}), 400
|
||||
|
||||
# Update the service_cost
|
||||
service_record.service_cost = new_cost_float
|
||||
|
||||
# Commit the transaction
|
||||
db.session.commit()
|
||||
|
||||
# Return success response
|
||||
return jsonify({
|
||||
"ok": True,
|
||||
"service_id": id,
|
||||
"service_cost_updated": new_cost_float,
|
||||
"message": f"Service {id} cost updated to ${new_cost_float}"
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"Error updating service cost for service {id}: {str(e)}")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@service.route("/update/<int:id>", methods=["PUT"])
|
||||
def update_service_call(id):
|
||||
service_record = Service_Service.query.get_or_404(id)
|
||||
@@ -336,3 +381,22 @@ def update_service_parts(customer_id):
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
@service.route("/payment/<int:service_id>/<int:payment_type>", methods=["PUT"])
|
||||
def process_service_payment(service_id, payment_type):
|
||||
service = db.session.query(Service_Service).filter(Service_Service.id == service_id).first()
|
||||
if not service:
|
||||
return jsonify({"ok": False, "error": "Service not found"}), 404
|
||||
|
||||
# Set payment columns as specified
|
||||
service.payment_type = payment_type # e.g., 1 for Tiger
|
||||
service.payment_status = 2 # As specified
|
||||
# payment_card_id retains the selected card's ID if set in the service record
|
||||
|
||||
try:
|
||||
db.session.commit()
|
||||
return jsonify({"ok": True}), 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user