Question
· Sep 6, 2018

HTTP POST For Authorize.net

Hello,
   We are running cache version  2012.1.4
Is it possible to make an HTTP Post passing in JSON string?

Example:
I want to send this request to Authorize.net. How can I do this in cache object script?

URL:  https://apitest.authorize.net/xml/v1/request.api

Body:

{
    "createTransactionRequest": {
        "merchantAuthentication": {
            "name": "gfufet9QVgT5P",
            "transactionKey": "8pg6FJjxuekeY62m"
        },
        "refId": "123456",
        "transactionRequest": {
            "transactionType": "authCaptureTransaction",
            "amount": "5",
            "payment": {
                "creditCard": {
                    "cardNumber": "5424000000000015",
                    "expirationDate": "2020-12",
                    "cardCode": "999"
                }
            }
        }
    }
}

NOTE - I have removed the actual name and transactionKey from the body. 

If you get this to work you should get back the message:

User authentication failed due to invalid authentication values.

Discussion (3)0
Log in or sign up to continue

Are you unable to POST anything, or are you looking for an easy way to construct JSON in 2012.1.4 ?

The class to POST something is %Net.HttpRequest, you need to send the form as a  stream.

In recent (2016.2 upward) versions, you can create a dynamic object and convert it to a JSON string, but 2012.1.4 is lacking this  JSON support,.

But you should still  be able to send a string in the body part formed (manually) as correct JSON :

    Set httprequest=##class(%Net.HttpRequest).%New()
    Set httprequest.Server="apitest.authorize.net"
    Set httprequest.Https=1
    Set httprequest.SSLConfiguration = "TEST"  ;make sure this SSL is created via mgmt security portal
    Do httprequest.EntityBody.Write("{""createTransactionRequest"":{""merchantAuthentication"":{""name"":""gfufet9QVgT5P"",""transactionKey"":""8pg6FJjxuekeY62m""},""refId"":""123456"",""transactionRequest"":{""transactionType"":""authCaptureTransaction"",""amount"":""5"",""payment"":{""creditCard"":{""cardNumber"":""5424000000000015"",""expirationDate"":""2020-12"",""cardCode"":""999""}}}}})")
    Do httprequest.Post("/xml/v1/request.api")
    Do httprequest.HttpResponse.OutputToDevice()

2012.1.4 has JSON support via %ZEN.proxyObject

set obj = ##class(%ZEN.proxyObject).%New()
set obj.createTransactionRequest= ##class(%ZEN.proxyObject).%New()
set obj.createTransactionRequest.merchantAuthentication = ##class(%ZEN.proxyObject).%New()
set obj.createTransactionRequest.merchantAuthentication.name = "gfufet9QVgT5P"
do obj.%ToJSON()

Also you can use %ZEN.Auxiliary.jsonProvider to convert object into JSON.