0

Okay My question is likewise: I am implementing a crypto payment gateway, I send all parameters along with callbackurl (which is notification url). Once I make payment, it will send a notification in json format. I don't know how to read data/parse data without knowing source url which is sending notification to my notification url.

the data likewise:

{
  "timestamp": 6716171771,
  "nonce": 5635262,
  "sign": "dasjh1276312hjhdwwghhdsuy2128781291g2",
  "body": "{\"address\":\"01HSXHSUSGNXSHGDSKJDASJ\",\"amount\":\"7917\",\"blockHigh\":\"17219\",\"coinType\":\"206\",\"decimals\":\"8\",\"fee\":\"452000\",\"mainCoinType\":\"206\",\"status\":3,\"tradeId\":\"20181024175416907\",\"tradeType\":1,\"txId\":\"31689c332536b56a2246347e206fbed2d04d461a3d668c4c1de32a75a8d436f0\"}"
}

string result_GET = null;
string url = "https://mytestweb.com/notifypayment.aspx";
// I don't know what URL we use here in case we don't know the source url, so this is my general practice for all gateways GET response.
WebResponse response = null;
StreamReader reader = null;


HttpWebRequest httpWebRequest = null;
httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "GET"; // Supports POST too


var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var get_json_data = streamReader.ReadToEnd();
    LabelJsonData.Text = get_json_data.ToString();

    //I want the json data here , so that I can parse it and use it then.

}
Raldo94
  • 328
  • 3
  • 13

1 Answers1

0

As far as I can tell you are just trying to handle JSON data.

With C# there are two main options you have, there may be others. I have used both in the past.

The Microsoft one: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to?pivots=dotnet-8-0

The NewtonSoft one: https://www.newtonsoft.com/json

Unless you need anything found in the NewtonSoft option, you may be better off using the microsoft one. That is just my oppinion. Both work well and I have no problem with either. The reason for this advise is the microsoft one is part of whats available and you don't need to use anything extra.

This is some information about serialisation/deserialistion of JSON you might find useful : What is deserialize and serialize in JSON?

If you know the structure of the JSON data (which I think you do) you can create a class structure to deserialise the JSON into using generics. Or you can use without the a specific class structure to hold the convert json.

I am showing C# 7 related code from (https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to?pivots=dotnet-7-0)

The example below is when you don't have a class structure.

using System;
using System.Text.Json;

string jsonString = "{\"name\":\"John Doe\",\"age\":30}";

JsonDocument jsonDocument = JsonDocument.Parse(jsonString);

foreach (JsonProperty element in jsonDocument.RootElement.EnumerateObject())
{
    Console.WriteLine($"Property Name: {element.Name}");
    Console.WriteLine($"Property Value: {element.Value}");
}

The other way is to use a class structure like this

Say you have this class (which matches the expected JSON)

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Where the JSON is

{
    "name": "John Doe",
    "age": 30
}

You can use this code

using System.Text.Json;

string jsonString = "{\"name\":\"John Doe\",\"age\":30}";

Person person = JsonSerializer.Deserialize<Person>(jsonString);

Console.WriteLine(person.Name); // Output: John Doe
Console.WriteLine(person.Age); // Output: 30

In the above code samples the jsonString would be get_json_data.ToString()

Obviously the C# classes are not the right ones for the JSON you have and are illustrative. So you will need to decide on which approach you want to use. The class option is nice, but you will need to write the appropriate class to support your own JSON structure.

yogibear
  • 320
  • 2
  • 13
  • Thanks for Your Reply, But my question here is how do you get the json data? Parsing I know many methods. Example, we use postman to SEND data in url "https://mytestweb.com/notifypayment.aspx" and the body in json format as : {\"name\":\"John Doe\",\"age\":30}, Here how can I fetch this data? – Biz Data Serv Apr 29 '23 at 09:19
  • Hi, no problem. It's not 100% clear to me, so yes lets clarify. So you mention a callback url. Then you also mention about making a payment. Are you suggesting the "making the payment" is something you do? or something the URL you posted to does. Is this payment once done calling your "call back url"? if so do you have an API to receive the call back URL? I think your explanation is okay if for someone familiar with what your doing but not to some one with no insight to what your doing. So if you could be really clear with what your asking that would be really helpful. – yogibear Apr 29 '23 at 12:22
  • Okay continue Step 1: I pass all parameters to generate payment address along with callbackurl. POST: gateway url and PARAMETERS: timestamp,comment, cryptotype, callbackurl. – Biz Data Serv Apr 30 '23 at 01:58
  • Step 2: I make payment on that address, which I received through step 1, Now gateway server will send me notification (on callback url I passed in step 1) everytime I make payment on this unique generated address. few points: The gateway provider server checks everytime new incoming deposit in any address and notification sent to callbackurl associated with the address. Now my question here is "How do I read that notification has been sent in json format?", coz I don't know url of sender page to read (which is common practice of GET). – Biz Data Serv Apr 30 '23 at 01:59
  • Are you saying that you provide a callback URL, the payment gateway then calls that gateway URL to send you a notification? In which case you just need to implement an API to match the callback URL, then when the callback URL is called you have a JSON body to deal with. – yogibear Apr 30 '23 at 13:22
  • You can see this link, https://www.uduncloud.com/en/developer-center/ here Trade callback interface is self explanatory, – Biz Data Serv May 02 '23 at 02:01
  • If I get time, might take a look. Is it possible you could isolate and explain the part of the process you are having trouble with, and explain it in a way I don't need to become an expert on the system you are trying to integrate with? It seems it could be simpler for you to get help from others too if you help people to understand with minimal effort on their part. You might get more replies :-) – yogibear May 02 '23 at 09:04