Claes Gustavsson
Claes Gustavsson

Reputation: 5689

How to receive a webhook in asp classic?

Im totally new to webhooks but I think I understand how it basically should work. But I need some help to really understand it and to see if I am totally wrong :-)

And I wonder how to receive a webhook? If I´m missing something, or can I just do a request.form("something") to receive it?

This is what I have now. Im sending a json formated post with XmlHttp from sendorder.asp to a payment service and in it I declare a web hook in the formated json like this along with other order details.

webhooks"": [{" &_
      """eventName"": ""payment.checkout.completed"","&_
      """url"": ""https://mydomain.se/getorderinfo.asp"","&_
      """authorization"": ""xxxxxxxx"""

And in the payment service docs it say "Easy will send the event as a HTTP POST request to the URL specified by the merchant."

So I guess that in my getorderinfo.asp page I just have a theinfo=request.form("something") to be able to receive the information?

But then their docs says that "When you subscribe to a webhook, Easy will try to send the request to the specified URL until the server responds with a 200 OK status code."

Does this mean that I have to send something back, or is the 200 OK sent as a response to them automatically?

The json data that I´m suppose to get back looks like this.

{
    "id" : "f3d5043af4094d6887ee95bf16073958",
    "merchantId" : "e718004345cc48cba095a235de85c359",
    "timestamp" : "2018-01-12T09:40:19.8919+00:00",
    "event" : "payment.refund.completed",
    "data" : {
        ...
        ...
    }
}

Im using aspJSON1.17.asp to be able to convert the json data with this code.

theinfo=request.form("something")
jsonstring = theinfo
        'response.write jsonstring 
        Set oJSON = New aspJSON

        'Load JSON string
        oJSON.loadJSON(jsonstring)
        theevent=oJSON.data("event")

So if I then just do this, will that work?

if theevent="payment.checkout.completed" then
response.write "Yepp the payment is completed, insert it into the db now!"
end if

And also, how can I test if the webhook is being send to the getorderinfo.asp page?

As you can see, I´m totally new, so any input really appreciated, thanks.

Just to see if I getting anything sent to my getorderinfo.asp page I have this.

theinfo = CStr(Request.Form)
if theinfo<>"" then 
testing=123
    sql= "INSERT INTO ordrar(userId) VALUES ("&testing&");"
    conn.Execute (sql)
end if

But this is not inserting anything in my db?

Upvotes: 2

Views: 1463

Answers (1)

Yair Yaya Tendler
Yair Yaya Tendler

Reputation: 159

all JSON is transmitted as Binary data, so instead of request.form or request.querystring you request.BinaryRead(Request.TotalBytes) <-- then convert to ASCII see code sample below

hope that helps

<%
If Request.TotalBytes=0 then 
response.write("Form Data:" & request.form &"<br>---<br>")
response.write("QueryString Data:" & request.querystring&"<br>---<br>")
else 
response.write("Binary Data Converted to string for WEbhooks or receive JSON data<br>---- <br>" )
response.write(Request.TotalBytes & "-Total Bytes ")

    Dim lngBytesCount, post
    lngBytesCount = Request.TotalBytes
    post = BytesToStr(Request.BinaryRead(lngBytesCount))
    Response.ContentType = "text/plain"
    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " & post
End If

Function BytesToStr(bytes)
    Dim Stream
    Set Stream = Server.CreateObject("Adodb.Stream")
        Stream.Type = 1 'adTypeBinary
        Stream.Open
        Stream.Write bytes
        Stream.Position = 0
        Stream.Type = 2 'adTypeText
        Stream.Charset = "iso-8859-1"
        BytesToStr = Stream.ReadText
        Stream.Close
    Set Stream = Nothing
End Function
%>

Upvotes: 2

Related Questions