Adding authnet not tested

This commit is contained in:
2025-09-15 15:30:30 -04:00
parent 5a6bcc0700
commit 47e3fb443b
6 changed files with 285 additions and 157 deletions

View File

@@ -7,6 +7,8 @@ import enum
from .. import crud, models, schemas, database
from ..services import payment_service
import logging
AuthNetResponse = object
@@ -23,6 +25,16 @@ class TransactionType(enum.IntEnum):
CHARGE = 0
AUTHORIZE = 1
CAPTURE = 3
# --- NEW CIM CORE FUNCTIONS ---
STATE_ID_TO_ABBREVIATION = {
0: "MA",
1: "RI",
2: "NH",
3: "ME",
4: "VT",
5: "CT",
6: "NY"
}
# This helper function is perfect, keep it.
def _parse_authnet_response(response: Optional[AuthNetResponse]) -> Tuple[TransactionStatus, Optional[str], Optional[str]]:
@@ -44,36 +56,64 @@ def _parse_authnet_response(response: Optional[AuthNetResponse]) -> Tuple[Transa
rejection_reason = f"{msg.code.text}: {msg.text.text}"
return status, auth_net_transaction_id, rejection_reason
# --- NEW ENDPOINT TO ADD A CARD ---
@router.post("/customers/{customer_id}/cards", response_model=schemas.Card, summary="Add a new payment card for a customer")
@router.post("/customers/{customer_id}/cards", response_model=schemas.CustomerCardResponse, summary="Add a new payment card for a customer")
def add_card_to_customer(customer_id: int, card_info: schemas.CardCreate, db: Session = Depends(database.get_db)):
"""
Adds a new credit card to a customer.
- If the customer doesn't have an Authorize.Net profile, it creates one.
- If they do, it adds a new payment method to their existing profile.
"""
db_customer = crud.get_customer(db, customer_id=customer_id)
if not db_customer:
raise HTTPException(status_code=404, detail="Customer not found")
# We still need this schema for the payment service call
customer_schema = schemas.Customer.from_orm(db_customer)
payment_profile_id = None
if not db_customer.auth_net_profile_id:
profile_id, payment_id = payment_service.create_customer_profile(customer=customer_schema, card_info=card_info)
if not profile_id or not payment_id:
raise HTTPException(status_code=400, detail="Failed to create payment profile with Authorize.Net")
crud.update_customer_auth_net_profile_id(db, customer_id=customer_id, profile_id=profile_id)
payment_profile_id = payment_id
else:
payment_profile_id = payment_service.add_payment_profile_to_customer(
customer_profile_id=db_customer.auth_net_profile_id,
customer=customer_schema,
card_info=card_info
try:
# This part now works because the service hard-codes the state to "MA"
if not db_customer.auth_net_profile_id:
profile_id, payment_id = payment_service.create_customer_profile(
customer=customer_schema, card_info=card_info
)
crud.update_customer_auth_net_profile_id(db, customer_id=customer_id, profile_id=profile_id)
payment_profile_id = payment_id
else:
payment_profile_id = payment_service.add_payment_profile_to_customer(
customer_profile_id=db_customer.auth_net_profile_id,
customer=customer_schema,
card_info=card_info
)
# This creates the card in our local database
new_card = crud.create_customer_card(
db=db,
customer_id=customer_id,
card_info=card_info,
payment_profile_id=payment_profile_id
)
if not payment_profile_id:
raise HTTPException(status_code=400, detail="Failed to add new card to Authorize.Net profile")
# ========= THIS IS THE FIX FOR THE FRONTEND =========
# 1. Convert the newly created card object into a Pydantic model, then a dictionary.
# Make sure your schemas.Card uses `user_id` to match your model.
response_data = schemas.Card.from_orm(new_card).model_dump()
# 2. Manually add the 'customer_state' field that the frontend needs.
response_data['customer_state'] = "MA"
# 3. Return the complete dictionary. FastAPI validates it against CustomerCardResponse
# and sends it to the frontend.
return response_data
new_card = crud.create_customer_card(db=db, customer_id=customer_id, card_info=card_info, payment_profile_id=payment_profile_id)
return new_card
# --- REFACTORED CHARGE ENDPOINT ---
except ValueError as e:
# This will catch errors from the payment service
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
# This will catch any other unexpected errors, like from the database
logger.error(f"An unexpected error occurred: {e}")
raise HTTPException(status_code=500, detail="An internal server error occurred.")
@router.post("/charge/saved-card/{customer_id}", response_model=schemas.Transaction, summary="Charge a customer using a saved card")
def charge_saved_card(customer_id: int, transaction_req: schemas.TransactionCreateByCardID, db: Session = Depends(database.get_db)):
@@ -117,7 +157,7 @@ def authorize_saved_card(customer_id: int, transaction_req: schemas.TransactionA
db_customer = crud.get_customer(db, customer_id=customer_id)
db_card = crud.get_card_by_id(db, card_id=transaction_req.card_id)
if not db_customer or not db_card or db_card.customer_id != customer_id:
if not db_customer or not db_card or db_card.user_id != customer_id:
raise HTTPException(status_code=404, detail="Customer or card not found for this account")
if not db_customer.auth_net_profile_id or not db_card.auth_net_payment_profile_id: