Search code examples
c#authorize.net

Errors creating transactions with Authorize.Net in .Net Core


I'm trying to create a simple pre-authorization transaction using a json class. I can't get it to work, either with JSON or XML.

XML return says:

E00004The name of the requested API method is invalid.

JSON return says:

{"code":"E00001","text":"XmlNodeConverter can only convert JSON that begins with an object."}

(Both the XML and the JSON content post fine on their dev site).

I can't use the SDK because it doesn't work with .NET Core, so I'm kind stuck here.

Here's the XML object

<createTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
    <merchantAuthentication>
        <name>87Xn5GphzvA7</name>
        <transactionKey>6QSmxLHw5a647937</transactionKey>
    </merchantAuthentication>
    <refId>123456</refId>
    <transactionRequest>    
        <transactionType>authOnlyTransaction</transactionType>
        <amount>2.99</amount>
        <currencyCode>USD</currencyCode>
        <payment>
            <creditCard><cardNumber>5424000000000015</cardNumber>
            <expirationDate>2025-12</expirationDate>
            <cardCode>999</cardCode>
            </creditCard>
        </payment>
        <billTo>
            <firstName>Anonymous</firstName>
            <lastName>User</lastName>
            <company />
            <address>123 Main Street</address>
            <city>Birmingham</city>
            <state>Alabama</state>
            <zip>12345</zip>
            <country>US</country>
        </billTo>
        <authorizationIndicatorType>
        <authorizationIndicator>pre</authorizationIndicator></authorizationIndicatorType>
    </transactionRequest>
</createTransactionRequest>

Here's the JSON object

{
    "createTransactionRequest": {
        "merchantAuthentication": {
            "name": "87Xn5GphzvA7",
            "transactionKey": "6QSmxLHw5a647937"
        },
        "refId": "123456",
        "transactionRequest": {
            "transactionType": "authOnlyTransaction",
            "amount": 2.99,
            "currencyCode": "USD",
            "payment": {
                "creditCard": {
                    "cardNumber": "5424000000000015",
                    "expirationDate": "2025-12",
                    "cardCode": "999"
                }
            },
            "billTo": {
                "firstName": "Anonymous",
                "lastName": "User",
                "company": null,
                "address": "123 Main Street",
                "city": "Birmingham",
                "state": "Alabama",
                "zip": "12345",
                "country": "US"
            },
            "authorizationIndicatorType": {
                "authorizationIndicator": "pre"
            }
        }
    }
}

I've tried with sending with both json content and xml content. Details listed above.

Both samples parse and execute properly in the Authorize.Net sandbox, so I'm not sure where to go now.


Solution

  • I've resolved this issue. For whatever reason, serializing the object prior to posting was not required.

    Here's the code now: public async Task PreAuthorization(CreditCard cardInfo, decimal feeAmount) {

            var url = new Flurl.Url(BaseUrl);
            try
            {
                var paymentRequest = new PaymentRequest();
                paymentRequest.Transaction = new TransactionRequest
                {
    
                    AuthHeader = new MerchantAuthentication { AuthName = ApiLogin, TransactionKey = ApiKey },
                    ReferenceId = "123456",
                    transactionRequest = new TransactionRequestType 
                    { 
                        RequestType = "authOnlyTransaction", 
                        Amount = feeAmount, 
                        CurrencyCode = "USD", 
                        Payment = new PaymentTypeDetails { CreditCardDetails = cardInfo },
                        BillTo = _billingInfo,
                        AuthIndicator = new AuthIndicatorType()
                    }
                };
    
                var response = await url.PostJsonAsync(paymentRequest).ReceiveString();
                return response;
            }
            catch (Exception ex)
            {
                return null;
            }
        }