Source code for terminusgps.authorizenet.utils
from authorizenet import apicontractsv1, apicontrollers
from django import forms
from .auth import get_merchant_auth
from .controllers import AuthorizenetControllerExecutor
[docs]
def get_customer_profile_ids() -> list[int]:
"""
Returns a list of all customer profile ids in Authorizenet.
:raises ControllerExecutionError: If the API call fails.
:returns: A list of all customer profile ids in Authorizenet.
:rtype: :py:obj:`list`
"""
request = apicontractsv1.getCustomerProfileIdsRequest(
merchantAuthentication=get_merchant_auth()
)
controller = apicontrollers.getCustomerProfileIdsController(request)
response = AuthorizenetControllerExecutor.execute_controller(controller)
if response is None or "ids" not in response.getchildren():
return []
return [int(id) for id in response.ids.getchildren()]
[docs]
def generate_customer_address(form: forms.Form) -> apicontractsv1.customerAddressType:
"""
Takes a form and returns a :py:obj:`~authorizenet.apicontractsv1.customerAddressType`.
:param form: A Django form.
:type form: :py:obj:`~django.forms.Form`
:raises ValueError: If ``address`` wasn't in the form.
:raises ValueError: If ``first_name`` wasn't in the form.
:raises ValueError: If ``last_name`` wasn't in the form.
:returns: A customer address object.
:rtype: :py:obj:`~authorizenet.apicontractsv1.customerAddressType`
"""
required_fields: list[str] = ["address", "first_name", "last_name"]
for field in required_fields:
if field not in form.cleaned_data or form.cleaned_data.get(field) is None:
raise ValueError(
f"'{field}' was not provided by the form, got '{form.cleaned_data.get(field)}'."
)
address: apicontractsv1.customerAddressType = form.cleaned_data["address"]
address.firstName = form.cleaned_data["first_name"]
address.lastName = form.cleaned_data["last_name"]
if form.cleaned_data.get("phone"):
address.phone = form.cleaned_data["phone"]
return address
[docs]
def generate_customer_payment(form: forms.Form) -> apicontractsv1.paymentType:
"""
Takes a form and returns a :py:obj:`~authorizenet.apicontractsv1.paymentType`.
:param form: A Django form.
:type form: :py:obj:`~django.forms.Form`
:raises ValueError: If ``credit_card`` wasn't in the form.
:returns: A payment object.
:rtype: :py:obj:`~authorizenet.apicontractsv1.paymentType`
"""
required_fields: list[str] = ["credit_card"]
for field in required_fields:
if field not in form.cleaned_data or form.cleaned_data.get(field) is None:
raise ValueError(
f"'{field}' was not provided by the form, got '{form.cleaned_data.get(field)}'."
)
credit_card: apicontractsv1.creditCardType = form.cleaned_data["credit_card"]
return apicontractsv1.paymentType(creditCard=credit_card)