AuthorizeNet / sdk-python

Python SDK for the Authorize.Net API
Other
53 stars 82 forks source link

Document production constants settings #153

Open buddha314 opened 2 years ago

buddha314 commented 2 years ago

To switch to production, the README states

# For PRODUCTION use
    createtransactioncontroller.setenvironment(constants.PRODUCTION)

However, the API documentation does not provide the format or list of variables to be held in constants.py.

markcerv commented 1 year ago

Hi Buddha314 -- this has been bugging/confusing me for a while as today. Today I dug into the code and found pull request 26 which also refers to constant. I found the actual file at: https://github.com/AuthorizeNet/sdk-python/blob/master/authorizenet/constants.py

I can see now that the import should be: from authorizenet import constants

then later on in the code, you can have the line you use above createtransactioncontroller.setenvironment(constants.PRODUCTION)

I hope this helps you.

markcerv commented 1 year ago

My above comment is close, but not functional. Once I deployed the above code fragment to production and tried running a transaction, I got an error. :(

Bottom line, here's the correct way to call:

from authorizenet.constants import constants
from authorizenet.apicontrollers import createTransactionController

    if some_test_of_yours():
        createtransactioncontroller.setenvironment(constants.PRODUCTION)
    else:
        createtransactioncontroller.setenvironment(constants.SANDBOX)

    createtransactioncontroller.execute()

And here is a more complete example that might help others:

# related third party imports
from authorizenet import apicontractsv1
from authorizenet.constants import constants
from authorizenet.apicontrollers import createTransactionController

# Are there any settings you need?
from django.conf import settings

MERCHANTAUTH_NAME = settings.MERCHANTAUTH_NAME
MERCHANTAUTH_TRANS_KEY = settings.MERCHANTAUTH_TRANS_KEY

def process_card_payment(data_from_user):

    transactionType = "authCaptureTransaction"

    creditCard = apicontractsv1.creditCardType()
    creditCard.cardNumber = data_from_user["card_number"]
    creditCard.expirationDate = data_from_user["expiration_date"]
    creditCard.cardCode = data_from_user["card_code"]

    # Create order information
    order = apicontractsv1.orderType()
    order.invoiceNumber = data_from_user["res_id"]
    order.description = data_from_user["description"]

    # Set the customer's identifying information
    customerData = apicontractsv1.customerDataType()
    customerData.type = "individual"
    customerData.id = data_from_user["my_txn_id"]

    payment = apicontractsv1.paymentType()
    payment.creditCard = creditCard

    # Set the customer's Bill To address
    customerAddress = apicontractsv1.customerAddressType()
    customerAddress.firstName = data_from_user["first_name"]
    customerAddress.lastName = data_from_user["last_name"]
    # more fields like, address, city, state,zip, country

    transactionrequest = apicontractsv1.transactionRequestType()
    transactionrequest.transactionType = transactionType  # set above via parameter
    transactionrequest.amount = data_from_user["amount"]
    transactionrequest.payment = payment  # defined above in payment = apicontractsv1.paymentType()
    transactionrequest.billTo = customerAddress  # defined above in customerAddress = apicontractsv1.customerAddressType()
    transactionrequest.order = order  # defined above in order = apicontractsv1.orderType()
    transactionrequest.customer = customerData  # defined above in customerData = apicontractsv1.customerDataType()

    createtransactionrequest = apicontractsv1.createTransactionRequest()
    createtransactionrequest.merchantAuthentication = merchantAuth
    createtransactionrequest.refId = data_from_user["ref_id"]

    createtransactionrequest.transactionRequest = transactionrequest
    createtransactioncontroller = createTransactionController(createtransactionrequest)

    if this_is_a_prod_server():
        logger.debug("Setting the environment to be PRODUCTION")
        createtransactioncontroller.setenvironment(constants.PRODUCTION)
    else:
        logger.debug("Setting the environment to be SANDBOX")
        createtransactioncontroller.setenvironment(constants.SANDBOX)

    createtransactioncontroller.execute()

    response = createtransactioncontroller.getresponse()

    if response.messages.resultCode == "Ok":
        result = f"OK: Transaction ID : {response.transactionResponse.transId}"
        logger.info(result)
    else:
        result = (
            f"ERROR: response code: [{response.messages.resultCode}]"
            f" error code [{response.messages.message.code}]"
        )

        logger.error(result)

    return response

Hope this helps.

dhanashreeyadav1 commented 5 days ago

TCS-Dev Dhanashree- The constants.py file is already present in the SDK. If you want to use the constants.py file inside the SDK, you have to include the below line in your code. “from authorizenet.constants import constants”