We are trying to call to the A.NET API via C# code in a website.
I installed the Authorize.Net NuGet package to the project in Visual Studio.
I went to the test form on a.net, and entered my credentials and verified they work here:
https://developer.authorize.net/api/reference/index.html#payment-transactions
It gives me back this response...
<?xml version="1.0" encoding="utf-8"?> <authenticateTestResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"> <messages> <resultCode>Ok</resultCode> <message> <code>I00001</code> <text>Successful.</text> </message> </messages> </authenticateTestResponse>
So I copied the C# demo code found here:
and changed it as necessary (filling in values from our forms/databases).
Here is my actual code:
public static Result Run(long clientid, decimal chargeAmount, customerAddressType billingAddress, creditCardType creditCard, List<lineItemType> lineItems) { Models.ClientAuthorizeNetInfo authInfo = Data.GetClientAuthorizeNetInfo(clientid); ApiOperationBase<ANetApiRequest, ANetApiResponse>.RunEnvironment = authInfo.UseSandbox ? AuthorizeNet.Environment.SANDBOX : AuthorizeNet.Environment.PRODUCTION; //standard api call to retrieve response var paymentType = new paymentType { Item = creditCard }; var transactionRequest = new transactionRequestType { transactionType = transactionTypeEnum.authCaptureTransaction.ToString(), // charge the card amount = chargeAmount, amountSpecified = true, currencyCode = "USD", payment = paymentType, billTo = billingAddress, lineItems = lineItems.ToArray() }; var request = new createTransactionRequest { transactionRequest = transactionRequest }; request.merchantAuthentication = new merchantAuthenticationType() { name = authInfo.ApiLogin, ItemElementName = ItemChoiceType.transactionKey, Item = authInfo.TransactionKey, }; Library.Log("AuthorizeNetTransaction.Run", JsonConvert.SerializeObject(request)); Library.Log("AuthorizeNetTransaction.Run", (authInfo.UseSandbox ? AuthorizeNet.Environment.SANDBOX.getBaseUrl() : AuthorizeNet.Environment.PRODUCTION.getBaseUrl())); // instantiate the controller that will call the service var controller = new createTransactionController(request); controller.Execute((authInfo.UseSandbox ? AuthorizeNet.Environment.SANDBOX : AuthorizeNet.Environment.PRODUCTION)); // get the response from the service (errors contained if any) var response = controller.GetApiResponse(); Library.Log("AuthorizeNetTransaction.Run", JsonConvert.SerializeObject(response)); Result rv = new Result(); // validate response if (response != null) { if (response.messages.resultCode == messageTypeEnum.Ok) { if (response.transactionResponse.messages != null) { rv.success = true; rv.data = $"Transaction ID: {response.transactionResponse.transId}; Authorization Code: {response.transactionResponse.authCode}"; } else { if (response.transactionResponse.errors != null) { rv.data = $"({response.transactionResponse.errors[0].errorCode}) {response.transactionResponse.errors[0].errorText}"; } else { rv.data = "Failure information unavailable."; } } } else { if (response.transactionResponse != null && response.transactionResponse.errors != null) { rv.data = $"({response.transactionResponse.errors[0].errorCode}) {response.transactionResponse.errors[0].errorText}"; } else { rv.data = $"({response.messages.message[0].code}) {response.messages.message[0].text}"; } } } else { rv.data = "Null response from merchant account."; } return rv; }
Now right off, I'll state there are a few changes to the code I had to make.
1. I had to set "amountSpecified" on the transactionRequest to true, otherwise my amount never showed up in the request.
2. I had to set the merchantAuthentication directly on the request object, otherwise it never showed up in the request.
So I'm kinda suspect on trusting the rest of the code.
When I call GetApiResponse, it always comes back with the following...
{ "transactionResponse": { "responseCode": null, "rawResponseCode": null, "authCode": null, "avsResultCode": null, "cvvResultCode": null, "cavvResultCode": null, "transId": null, "refTransID": null, "transHash": null, "testRequest": null, "accountNumber": null, "entryMode": null, "accountType": null, "splitTenderId": null, "prePaidCard": null, "messages": null, "errors": null, "splitTenderPayments": null, "userFields": null, "shipTo": null, "secureAcceptance": null, "emvResponse": null, "transHashSha2": null, "profile": null }, "profileResponse": null, "refId": null, "messages": { "resultCode": 1, "message": [ { "code": "E00007", "text": "User authentication failed due to invalid authentication values." } ] }, "sessionToken": null }
I have verified (after changing how I add them) that the correct API login and Transaction Key are being sent in the request. For example, here is one of my requests (actual login values changes)...
{ "transactionRequest": { "transactionType": "authCaptureTransaction", "amount": 600, "amountSpecified": true, "currencyCode": "USD", "payment": { "Item": { "cardCode": "987", "isPaymentTokenSpecified": false, "cryptogram": null, "cardNumber": "5424000000000015", "expirationDate": "0821" } }, "profile": null, "solution": null, "callId": null, "terminalNumber": null, "authCode": null, "refTransId": null, "splitTenderId": null, "order": null, "lineItems": [ { "itemId": "14", "name": " adminsitration", "description": " adminsitration", "quantity": 1, "unitPrice": 600, "taxableSpecified": false } ], "tax": null, "duty": null, "shipping": null, "taxExemptSpecified": false, "poNumber": null, "customer": null, "billTo": { "phoneNumber": null, "faxNumber": null, "email": null, "firstName": "Joe", "lastName": "Schmoe", "company": null, "address": "123 Sesame Street", "city": "New York", "state": "NY", "zip": "10012", "country": null }, "shipTo": null, "customerIP": null, "cardholderAuthentication": null, "retail": null, "employeeId": null, "transactionSettings": null, "userFields": null, "surcharge": null, "merchantDescriptor": null, "subMerchant": null, "tip": null }, "merchantAuthentication": { "name": "myApiLoginHere", "Item": "myTransactionKeyHere", "ItemElementName": 6, "mobileDeviceId": null }, "clientId": null, "refId": null }
I'm using one of the test credit cards shown in the Testing Guide and have verified that I am setting the RunEnvironment to SANDBOX (assuming the code to set it is working, given that the code to set the merchantAuthentication in the original sample code wasn't working).
So I'm at a loss for why our credentials DO work when tested online, but through code, they are rejected.
Solved! Go to Solution.
03-27-2018 02:36 PM
Apparently it just didn't like my transaction key for whatever reason.
I generated a new one, and now it works.
Who knows.
03-29-2018 12:18 PM
Apparently it just didn't like my transaction key for whatever reason.
I generated a new one, and now it works.
Who knows.
03-29-2018 12:18 PM