Refactor payment service, fix DB session, and consolidate endpoints
- Fix critical NameError in database.py by restoring Session factory - Refactor payment_service.py and crud.py to use shared constants.py and utils.py - Deduplicate state mapping and input sanitization logic - Move transaction amount calculation logic from CRUD to Router layer - Enforce type safety in schemas using IntEnum for TransactionType/Status - Move capture endpoint from transaction.py to payment.py (now /payments/capture) - Update create_customer_profile signature for clarity
This commit is contained in:
41
app/crud.py
41
app/crud.py
@@ -1,17 +1,21 @@
|
||||
## File: your_app/crud.py
|
||||
"""
|
||||
CRUD operations for the EAMCO Authorize service.
|
||||
"""
|
||||
import logging
|
||||
from sqlalchemy.orm import Session
|
||||
from . import models, schemas
|
||||
from .constants import TransactionStatus, TransactionType
|
||||
from decimal import Decimal
|
||||
from typing import Optional, List
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# --- NEW CRUD FUNCTIONS FOR CIM ---
|
||||
|
||||
def get_card_by_id(db: Session, card_id: int):
|
||||
def get_card_by_id(db: Session, card_id: int) -> Optional[models.Card]:
|
||||
return db.query(models.Card).filter(models.Card.id == card_id).first()
|
||||
|
||||
def update_customer_auth_net_profile_id(db: Session, customer_id: int, profile_id: str):
|
||||
def update_customer_auth_net_profile_id(db: Session, customer_id: int, profile_id: str) -> Optional[models.Customer]:
|
||||
db_customer = get_customer(db, customer_id)
|
||||
if db_customer:
|
||||
db_customer.auth_net_profile_id = profile_id
|
||||
@@ -19,13 +23,10 @@ def update_customer_auth_net_profile_id(db: Session, customer_id: int, profile_i
|
||||
db.refresh(db_customer)
|
||||
return db_customer
|
||||
|
||||
def create_customer_card(db: Session, customer_id: int, card_info: schemas.CardCreate, payment_profile_id: str):
|
||||
def create_customer_card(db: Session, customer_id: int, card_info: schemas.CardCreate, payment_profile_id: str) -> models.Card:
|
||||
last_four_digits = card_info.card_number[-4:]
|
||||
try:
|
||||
exp_year, exp_month = map(int, card_info.expiration_date.split('-'))
|
||||
except ValueError:
|
||||
# Handle cases like "MM/YY" if necessary, but "YYYY-MM" is better
|
||||
raise ValueError("Expiration date must be in YYYY-MM format")
|
||||
# Schema guarantees YYYY-MM format
|
||||
exp_year, exp_month = map(int, card_info.expiration_date.split('-'))
|
||||
|
||||
db_card = models.Card(
|
||||
user_id=customer_id,
|
||||
@@ -57,10 +58,11 @@ def get_customer_by_email(db: Session, email: str):
|
||||
def get_customers(db: Session, skip: int = 0, limit: int = 100):
|
||||
return db.query(models.Customer).offset(skip).limit(limit).all()
|
||||
|
||||
def create_transaction(db: Session, transaction: schemas.TransactionBase, customer_id: int, status: int, auth_net_transaction_id: str = None):
|
||||
# Using your existing logic for saving amounts
|
||||
preauthorize_amount = transaction.preauthorize_amount if status == 0 else Decimal("0.0")
|
||||
charge_amount = transaction.charge_amount if (transaction.transaction_type != 1 and status == 0) else Decimal("0.0")
|
||||
def create_transaction(db: Session, transaction: schemas.TransactionBase, customer_id: int, status: int, auth_net_transaction_id: str = None) -> models.Transaction:
|
||||
# Logic for amounts moved to routers/payment.py
|
||||
# We simply save what is passed in the transaction object
|
||||
preauthorize_amount = transaction.preauthorize_amount or Decimal("0.0")
|
||||
charge_amount = transaction.charge_amount or Decimal("0.0")
|
||||
|
||||
db_transaction = models.Transaction(
|
||||
preauthorize_amount=preauthorize_amount,
|
||||
@@ -71,6 +73,7 @@ def create_transaction(db: Session, transaction: schemas.TransactionBase, custom
|
||||
auth_net_transaction_id=auth_net_transaction_id,
|
||||
service_id=transaction.service_id,
|
||||
delivery_id=transaction.delivery_id,
|
||||
auto_id=transaction.auto_id,
|
||||
card_id=transaction.card_id,
|
||||
payment_gateway=transaction.payment_gateway,
|
||||
rejection_reason=transaction.rejection_reason
|
||||
@@ -83,8 +86,8 @@ def create_transaction(db: Session, transaction: schemas.TransactionBase, custom
|
||||
def get_transaction_by_delivery_id(db: Session, delivery_id: int):
|
||||
return db.query(models.Transaction).filter(
|
||||
models.Transaction.delivery_id == delivery_id,
|
||||
models.Transaction.transaction_type == 1,
|
||||
models.Transaction.status == 0
|
||||
models.Transaction.transaction_type == TransactionType.AUTHORIZE,
|
||||
models.Transaction.status == TransactionStatus.APPROVED
|
||||
).first()
|
||||
|
||||
def get_transaction_by_auth_id(db: Session, auth_net_transaction_id: str):
|
||||
@@ -95,8 +98,8 @@ def get_transaction_by_auth_id(db: Session, auth_net_transaction_id: str):
|
||||
def get_transaction_by_auto_id(db: Session, auto_id: int):
|
||||
return db.query(models.Transaction).filter(
|
||||
models.Transaction.auto_id == auto_id,
|
||||
models.Transaction.transaction_type.in_([1, 2]),
|
||||
models.Transaction.status == 0
|
||||
models.Transaction.transaction_type.in_([TransactionType.AUTHORIZE, TransactionType.CAPTURE]),
|
||||
models.Transaction.status == TransactionStatus.APPROVED
|
||||
).first()
|
||||
|
||||
def update_transaction_for_capture(db: Session, auth_net_transaction_id: str, charge_amount: Decimal, status: int, rejection_reason: str = None):
|
||||
@@ -104,8 +107,8 @@ def update_transaction_for_capture(db: Session, auth_net_transaction_id: str, ch
|
||||
if not transaction:
|
||||
return None
|
||||
|
||||
transaction.charge_amount = charge_amount if status == 0 else Decimal("0.0")
|
||||
transaction.transaction_type = 2
|
||||
transaction.charge_amount = charge_amount if status == TransactionStatus.APPROVED else Decimal("0.0")
|
||||
transaction.transaction_type = TransactionType.CAPTURE
|
||||
transaction.status = status
|
||||
if rejection_reason:
|
||||
transaction.rejection_reason = rejection_reason
|
||||
|
||||
Reference in New Issue
Block a user