cancel
Showing results for 
Search instead for 
Did you mean: 

Python: refundTransaction isn't working

The code below

  • Creates a customer profile
  • Creates a payment profile
  • Charges the payment profile
  • Partially refunds the transaction

Everything works great, except the refund step, which errors with

 

Error Code: 54

Error message: The referenced transaction does not meet the criteria for issuing a credit.

 

Any idea what's wrong?

 

from authorizenet import apicontractsv1
from authorizenet.constants import constants
from authorizenet.apicontrollers import *
from decimal import *
import random

# create customer profile
merchantAuth = apicontractsv1.merchantAuthenticationType()
merchantAuth.name = <name>
merchantAuth.transactionKey = <key>

createCustomerProfile = apicontractsv1.createCustomerProfileRequest()
createCustomerProfile.merchantAuthentication = merchantAuth
createCustomerProfile.profile = apicontractsv1.customerProfileType('jdoe' + str(random.randint(0, 10000)), 'John2 Doe', 'jdoe@mail.com')

controller = createCustomerProfileController(createCustomerProfile)
controller.execute()

response = controller.getresponse()

if (response.messages.resultCode=="Ok"):
    print("Successfully created a customer profile with id: %s" % response.customerProfileId)
    customerProfileId = response.customerProfileId
    
    creditCard = apicontractsv1.creditCardType()
    creditCard.cardNumber = "4111111111111111"
    creditCard.expirationDate = "2020-12"

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

    billTo = apicontractsv1.customerAddressType()
    billTo.firstName = "John"
    billTo.lastName = "Snow"

    profile = apicontractsv1.customerPaymentProfileType()
    profile.payment = payment
    profile.billTo = billTo

    createCustomerPaymentProfile = apicontractsv1.createCustomerPaymentProfileRequest()
    createCustomerPaymentProfile.merchantAuthentication = merchantAuth
    createCustomerPaymentProfile.paymentProfile = profile
    print("customerProfileId in create_customer_payment_profile. customerProfileId = %s" %customerProfileId)
    createCustomerPaymentProfile.customerProfileId = str(customerProfileId)

    controller = createCustomerPaymentProfileController(createCustomerPaymentProfile)
    controller.execute()

    response = controller.getresponse()

    if (response.messages.resultCode=="Ok"):
        print("Successfully created a customer payment profile with id: %s" % response.customerPaymentProfileId)
        customerPaymentProfileId = response.customerPaymentProfileId
        
        profileToCharge = apicontractsv1.customerProfilePaymentType()
        profileToCharge.customerProfileId = str(customerProfileId)
        profileToCharge.paymentProfile = apicontractsv1.paymentProfile()
        profileToCharge.paymentProfile.paymentProfileId = str(customerPaymentProfileId)

        transactionrequest = apicontractsv1.transactionRequestType()
        transactionrequest.transactionType = "authCaptureTransaction"
        transactionrequest.amount = Decimal ('35.00')
        transactionrequest.profile = profileToCharge


        createtransactionrequest = apicontractsv1.createTransactionRequest()
        createtransactionrequest.merchantAuthentication = merchantAuth
        createtransactionrequest.refId = "MerchantID-0001"

        createtransactionrequest.transactionRequest = transactionrequest
        createtransactioncontroller = createTransactionController(createtransactionrequest)
        createtransactioncontroller.execute()

        response = createtransactioncontroller.getresponse()

        if response is not None:
            if response.messages.resultCode == "Ok":
                if hasattr(response.transactionResponse, 'messages') == True:
                    print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId);
                    print ('Transaction Response Code: %s' % response.transactionResponse.responseCode);
                    print ('Message Code: %s' % response.transactionResponse.messages.message[0].code);
                    print ('Description: %s' % response.transactionResponse.messages.message[0].description);
                    transId = response.transactionResponse.transId
                    
                    
                    creditCard = apicontractsv1.creditCardType()
                    creditCard.cardNumber = "1111"
                    creditCard.expirationDate = "XXXX"

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

                    transactionrequest = apicontractsv1.transactionRequestType()
                    transactionrequest.transactionType = "refundTransaction"
                    transactionrequest.amount = Decimal ('15.00')
                    transactionrequest.refTransId = str(transId)
                    transactionrequest.payment = payment


                    createtransactionrequest = apicontractsv1.createTransactionRequest()
                    createtransactionrequest.merchantAuthentication = merchantAuth
                    createtransactionrequest.refId = "MerchantID-0001"

                    createtransactionrequest.transactionRequest = transactionrequest
                    createtransactioncontroller = createTransactionController(createtransactionrequest)
                    createtransactioncontroller.execute()

                    response = createtransactioncontroller.getresponse()

                    if response is not None:
                        if response.messages.resultCode == "Ok":
                            if hasattr(response.transactionResponse, 'messages') == True:
                                print ('Successfully created transaction with Transaction ID: %s' % response.transactionResponse.transId);
                                print ('Transaction Response Code: %s' % response.transactionResponse.responseCode);
                                print ('Message Code: %s' % response.transactionResponse.messages.message[0].code);
                                print ('Description: %s' % response.transactionResponse.messages.message[0].description);
                            else:
                                print ('Failed Transaction.');
                                if hasattr(response.transactionResponse, 'errors') == True:
                                    print ('Error Code:  %s' % str(response.transactionResponse.errors.error[0].errorCode));
                                    print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
                        else:
                            print ('Failed Transaction 2.');
                            if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
                                print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode));
                                print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
                            else:
                                print ('Error Code 2: %s' % response.messages.message[0]['code'].text);
                                print ('Error message 2: %s' % response.messages.message[0]['text'].text);
                    else:
                        print ('Null Response.');
                    
                    
                    
                    
                    
                else:
                    print ('Failed Transaction.');
                    if hasattr(response.transactionResponse, 'errors') == True:
                        print ('Error Code:  %s' % str(response.transactionResponse.errors.error[0].errorCode));
                        print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
            else:
                print ('Failed Transaction 2.');
                if hasattr(response, 'transactionResponse') == True and hasattr(response.transactionResponse, 'errors') == True:
                    print ('Error Code: %s' % str(response.transactionResponse.errors.error[0].errorCode));
                    print ('Error message: %s' % response.transactionResponse.errors.error[0].errorText);
                else:
                    print ('Error Code: %s' % response.messages.message[0]['code'].text);
                    print ('Error message: %s' % response.messages.message[0]['text'].text);
        else:
            print ('Null Response.');

        
    else:
        print("Failed to create customer payment profile %s" % response.messages.message[0]['text'].text)    
    
else:
    print("Failed to create customer payment profile %s" % response.messages.message[0]['text'].text)
superseed
Member
3 REPLIES 3

Hello @superseed

 

Based on your description, you're attempting a refund before it settles which is not permitted.  Instead, you must Void an unsettled transaction which cancels the entire request.

 

You can get more details in our response code lookup too: 

https://developer.authorize.net/api/reference/responseCodes.html?code=54

 

Richard

 

 

RichardH
Administrator Administrator
Administrator

Is this a timing issue, in that I need to wait until the transaction settles?

 

If so, how long does that typically take?

 

Thank you,

 

Josh

I should have included that in my response.  In both the sandbox and production, transactions settle once a day after the transaction cut-off time specified in the merchant interface.

 

And, there isn't a way to speed that up in the sandbox.

 

Richard