added more data to create customer

This commit is contained in:
2025-09-18 13:02:52 -04:00
parent 148200dee2
commit 0ae476db02

View File

@@ -19,9 +19,8 @@ from authorizenet import apicontractsv1
from authorizenet.constants import constants from authorizenet.constants import constants
from authorizenet.apicontrollers import ( from authorizenet.apicontrollers import (
createCustomerProfileController, createCustomerProfileController,
createCustomerPaymentProfileController, createCustomerPaymentProfileController
getCustomerProfileIdsController,
getCustomerProfileController
) )
# --- ROUTER DEFINITION --- # --- ROUTER DEFINITION ---
@@ -36,7 +35,7 @@ TRANSACTION_KEY = '94s6Qy458mMNJr7G'
constants.show_url_on_request = True constants.show_url_on_request = True
constants.environment = constants.SANDBOX constants.environment = constants.SANDBOX
state_abbrevs = ['MA', 'RI', 'NH', 'ME', 'VT', 'CT', 'NY']
@router.post("/maintenance/migrate-cards-now") @router.post("/maintenance/migrate-cards-now")
def run_card_migration_synchronously(): def run_card_migration_synchronously():
@@ -78,8 +77,12 @@ def run_card_migration_synchronously():
cardCode = str(card_obj.security_number).strip() cardCode = str(card_obj.security_number).strip()
if len(cardCode) < 3 or len(cardCode) > 4 or not cardCode.isdigit(): if len(cardCode) < 3 or len(cardCode) > 4 or not cardCode.isdigit():
raise ValueError(f"Invalid CVV length {len(cardCode)} or non-numeric for card ID {card_obj.id}") raise ValueError(f"Invalid CVV length {len(cardCode)} or non-numeric for card ID {card_obj.id}")
# Get dynamic state
the_state = state_abbrevs[customer_obj.customer_state] if 0 <= customer_obj.customer_state < len(state_abbrevs) else 'MA'
# Debug print # Debug print
print(f"DEBUG SEND CREATE: cardNumber='{sanitized_card_number}', expirationDate='{expiration_date_str}', cardCode='{cardCode}', customerID={customer_obj.id}, name='{customer_obj.customer_first_name} {customer_obj.customer_last_name}', address='{customer_obj.customer_address}', city='{customer_obj.customer_town}', zip='{customer_obj.customer_zip}', email='{customer_obj.customer_email}'") print(f"DEBUG SEND CREATE: cardNumber='{sanitized_card_number}', expirationDate='{expiration_date_str}', cardCode='{cardCode}', customerID={customer_obj.id}, name='{customer_obj.customer_first_name} {customer_obj.customer_last_name}', address='{customer_obj.customer_address}', city='{customer_obj.customer_town}', state='{the_state}', zip='{customer_obj.customer_zip}', country='USA', phone='{customer_obj.customer_phone_number}', email='{customer_obj.customer_email}'")
creditCard = apicontractsv1.creditCardType( creditCard = apicontractsv1.creditCardType(
cardNumber=sanitized_card_number, cardNumber=sanitized_card_number,
expirationDate=expiration_date_str, expirationDate=expiration_date_str,
@@ -90,11 +93,15 @@ def run_card_migration_synchronously():
lastName=customer_obj.customer_last_name, lastName=customer_obj.customer_last_name,
address=customer_obj.customer_address, address=customer_obj.customer_address,
city=customer_obj.customer_town, city=customer_obj.customer_town,
zip=customer_obj.customer_zip state=the_state,
zip=customer_obj.customer_zip,
country="USA",
phoneNumber=customer_obj.customer_phone_number
) )
paymentProfile = apicontractsv1.customerPaymentProfileType( paymentProfile = apicontractsv1.customerPaymentProfileType(
billTo=billTo, billTo=billTo,
payment=apicontractsv1.paymentType(creditCard=creditCard) payment=apicontractsv1.paymentType(creditCard=creditCard),
defaultPaymentProfile=True
) )
customerProfile = apicontractsv1.customerProfileType( customerProfile = apicontractsv1.customerProfileType(
merchantCustomerId=str(customer_obj.id), merchantCustomerId=str(customer_obj.id),
@@ -133,6 +140,7 @@ def run_card_migration_synchronously():
"""Helper to add a new Payment Profile to an existing Customer Profile.""" """Helper to add a new Payment Profile to an existing Customer Profile."""
merchantAuth = apicontractsv1.merchantAuthenticationType(name=API_LOGIN_ID, transactionKey=TRANSACTION_KEY) merchantAuth = apicontractsv1.merchantAuthenticationType(name=API_LOGIN_ID, transactionKey=TRANSACTION_KEY)
expiration_date_str = f"{card_obj.expiration_year}-{str(card_obj.expiration_month).zfill(2)}" expiration_date_str = f"{card_obj.expiration_year}-{str(card_obj.expiration_month).zfill(2)}"
# State abbreviations list
# // FIX 1 (Applied here as well): Sanitize card number # // FIX 1 (Applied here as well): Sanitize card number
sanitized_card_number = ''.join(filter(str.isdigit, str(card_obj.card_number))) sanitized_card_number = ''.join(filter(str.isdigit, str(card_obj.card_number)))
@@ -142,9 +150,23 @@ def run_card_migration_synchronously():
expirationDate=expiration_date_str, expirationDate=expiration_date_str,
cardCode=str(card_obj.security_number).strip() cardCode=str(card_obj.security_number).strip()
) )
# Get dynamic state
the_state = state_abbrevs[customer_obj.customer_state] if 0 <= customer_obj.customer_state < len(state_abbrevs) else 'MA'
billTo = apicontractsv1.customerAddressType(
firstName=customer_obj.customer_first_name,
lastName=customer_obj.customer_last_name,
address=customer_obj.customer_address,
city=customer_obj.customer_town,
state=the_state,
zip=customer_obj.customer_zip,
country="USA",
phoneNumber=customer_obj.customer_phone_number
)
paymentProfile = apicontractsv1.customerPaymentProfileType( paymentProfile = apicontractsv1.customerPaymentProfileType(
billTo=apicontractsv1.customerAddressType(firstName=customer_obj.customer_first_name, lastName=customer_obj.customer_last_name), billTo=billTo,
payment=apicontractsv1.paymentType(creditCard=creditCard) payment=apicontractsv1.paymentType(creditCard=creditCard),
defaultPaymentProfile=False
) )
request = apicontractsv1.createCustomerPaymentProfileRequest( request = apicontractsv1.createCustomerPaymentProfileRequest(
merchantAuthentication=merchantAuth, merchantAuthentication=merchantAuth,
@@ -167,6 +189,8 @@ def run_card_migration_synchronously():
print(f" AUTH.NET ERROR: {error_msg}") print(f" AUTH.NET ERROR: {error_msg}")
return None return None
# --- MIGRATION SCRIPT LOGIC --- # --- MIGRATION SCRIPT LOGIC ---
print("="*60) print("="*60)
print("MIGRATION STARTED: Migrating all customer cards to Authorize.Net CIM.") print("MIGRATION STARTED: Migrating all customer cards to Authorize.Net CIM.")
@@ -181,8 +205,6 @@ def run_card_migration_synchronously():
customers_to_migrate = (session customers_to_migrate = (session
.query(Customer_Customer) .query(Customer_Customer)
.filter(Customer_Customer.auth_net_profile_id == None) .filter(Customer_Customer.auth_net_profile_id == None)
.order_by(func.random())
.limit(1)
.all()) .all())
total_customers = len(customers_to_migrate) total_customers = len(customers_to_migrate)