skip to Main Content

I’m attempting to use my API to retrieve the payment type. The attempt to obtain the payment status did not result in an error, but the attempt to obtain the payment type does.

Here is my data-gathering code (payment type)

Root model = JsonConvert.DeserializeObject<Root>(response.Content);
string paymentType = model.data.attributes.source.type;

Here is the response that I want to get

"source": {
     "id": "src_5LVgh5SpdcgdGuJn1sweL9YeFP9XZ",
     "type": "gcash"
}

Here is the whole response from API

{
  "data": {
    "id": "link_tov7i3iR5U9wZfwoMSaHY8H6",
    "type": "link",
    "attributes": {
      "amount": 350000,
      "archived": false,
      "currency": "PHP",
      "description": "JUAN DELA CRUZ - 221222011659",
      "livemode": false,
      "fee": 8750,
      "remarks": "Delivery Date: Dec. 31, 2022",
      "status": "paid",
      "tax_amount": null,
      "taxes": [],
      "checkout_url": "-",
      "reference_number": "ikPzIi2",
      "created_at": 1671643021,
      "updated_at": 1671643021,
      "payments": [
        {
          "data": {
            "id": "-",
            "type": "payment",
            "attributes": {
              "access_url": null,
              "amount": 350000,
              "balance_transaction_id": "-",
              "billing": {
                "address": {
                  "city": "Taguig",
                  "country": "PH",
                  "line1": "12th floor The Trade and Financial Tower u1206",
                  "line2": "32nd street and 7th Avenue",
                  "postal_code": "1630",
                  "state": "Bonifacio Global City"
                },
                "email": "-",
                "name": "-",
                "phone": "-"
              },
              "currency": "PHP",
              "description": "- - -",
              "disputed": false,
              "external_reference_number": "ikPzIi2",
              "fee": 8750,
              "livemode": false,
              "net_amount": 341250,
              "origin": "links",
              "payment_intent_id": null,
              "payout": null,
              "source": {
                "id": "-",
                "type": "gcash"
              },
              "statement_descriptor": "Vapers Creed (Cavite, PH)",
              "status": "paid",
              "tax_amount": null,
              "metadata": {
                "pm_reference_number": "ikPzIi2"
              },
              "refunds": [],
              "taxes": [],
              "available_at": 1672045200,
              "created_at": 1671695870,
              "paid_at": 1671695870,
              "updated_at": 1671695870
            }
          }
        }
      ]
    }
  }
}

JSON-Properties.cs

 public partial class Address
{
    [JsonProperty("city")]
    public string city { get; set; }

    [JsonProperty("country")]
    public string country { get; set; }

    [JsonProperty("line1")]
    public string line1 { get; set; }

    [JsonProperty("line2")]
    public string line2 { get; set; }

    [JsonProperty("postal_code")]
    public string postal_code { get; set; }

    [JsonProperty("state")]
    public string state { get; set; }
}

public partial class Attributes
{
    [JsonProperty("amount")]
    public int? amount { get; set; }

    [JsonProperty("archived")]
    public bool? archived { get; set; }

    [JsonProperty("currency")]
    public string currency { get; set; }

    [JsonProperty("description")]
    public string description { get; set; }

    [JsonProperty("livemode")]
    public bool? livemode { get; set; }

    [JsonProperty("fee")]
    public int? fee { get; set; }

        [JsonProperty("remarks")]
    public string remarks { get; set; }

    [JsonProperty("status")]
    public string status { get; set; }

    [JsonProperty("tax_amount")]
    public object tax_amount { get; set; }

    [JsonProperty("taxes")]
    public List<object> taxes { get; set; }

    [JsonProperty("checkout_url")]
    public string checkout_url { get; set; }

    [JsonProperty("reference_number")]
    public string reference_number { get; set; }

    [JsonProperty("created_at")]
    public int? created_at { get; set; }

    [JsonProperty("updated_at")]
    public int? updated_at { get; set; }

    [JsonProperty("payments")]
    public List<Payment> payments { get; set; }

    [JsonProperty("access_url")]
    public object access_url { get; set; }

    [JsonProperty("balance_transaction_id")]
    public string balance_transaction_id { get; set; }

    [JsonProperty("billing")]
    public Billing billing { get; set; }

    [JsonProperty("disputed")]
    public bool? disputed { get; set; }

    [JsonProperty("external_reference_number")]
    public string external_reference_number { get; set; }

    [JsonProperty("net_amount")]
    public int? net_amount { get; set; }

    [JsonProperty("origin")]
    public string origin { get; set; }

    [JsonProperty("payment_intent_id")]
    public object payment_intent_id { get; set; }

    [JsonProperty("payout")]
    public object payout { get; set; }

    [JsonProperty("source")]
    public Source source { get; set; }

    [JsonProperty("statement_descriptor")]
    public string statement_descriptor { get; set; }

    [JsonProperty("metadata")]
    public Metadata metadata { get; set; }

    [JsonProperty("refunds")]
    public List<object> refunds { get; set; }

    [JsonProperty("available_at")]
    public int? available_at { get; set; }

    [JsonProperty("paid_at")]
    public int? paid_at { get; set; }
}

public partial class Billing
{
    [JsonProperty("address")]
    public Address address { get; set; }

    [JsonProperty("email")]
    public string email { get; set; }

    [JsonProperty("name")]
    public string name { get; set; }

    JsonProperty("phone")]
    public string phone { get; set; }
}

public partial class Data
{
    [JsonProperty("id")]
    public string id { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }

    [JsonProperty("attributes")]
    public Attributes attributes { get; set; }
}

public partial class Metadata
{
    [JsonProperty("pm_reference_number")]
    public string pm_reference_number { get; set; }
}

public partial class Payment
{
    [JsonProperty("data")]
    public Data data { get; set; }
}

public partial class Root
{
    [JsonProperty("data")]
     public Data data { get; set; }
}

public partial class Source
{
    [JsonProperty("id")]
    public string id { get; set; }

    [JsonProperty("type")]
    public string type { get; set; }
}

I’ve only tried this code but it gaves me that API.Attributes.source.get returned null

string paymentType = model.data.attributes.source.type;

I’m expecting to get the payment type in API response, but thus far no such luck.

2

Answers


  1. The Model
    using System.Collections.Generic;

    namespace Nop.Web.Models.Extension
    {
        public class Root
        {
            public Data data { get; set; }
        }
        // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
        public class Address
        {
            public string city { get; set; }
            public string country { get; set; }
            public string line1 { get; set; }
            public string line2 { get; set; }
            public string postal_code { get; set; }
            public string state { get; set; }
        }
    
        public class Attributes
        {
            public int amount { get; set; }
            public bool archived { get; set; }
            public string currency { get; set; }
            public string description { get; set; }
            public bool livemode { get; set; }
            public int fee { get; set; }
            public string remarks { get; set; }
            public string status { get; set; }
            public object tax_amount { get; set; }
            public List<object> taxes { get; set; }
            public string checkout_url { get; set; }
            public string reference_number { get; set; }
            public int created_at { get; set; }
            public int updated_at { get; set; }
            public List<Payment> payments { get; set; }
            public object access_url { get; set; }
            public string balance_transaction_id { get; set; }
            public Billing billing { get; set; }
            public bool disputed { get; set; }
            public string external_reference_number { get; set; }
            public int net_amount { get; set; }
            public string origin { get; set; }
            public object payment_intent_id { get; set; }
            public object payout { get; set; }
            public Source source { get; set; }
            public string statement_descriptor { get; set; }
            public Metadata metadata { get; set; }
            public List<object> refunds { get; set; }
            public int available_at { get; set; }
            public int paid_at { get; set; }
        }
    
        public class Billing
        {
            public Address address { get; set; }
            public string email { get; set; }
            public string name { get; set; }
            public string phone { get; set; }
        }
    
        public class Data
        {
            public string id { get; set; }
            public string type { get; set; }
            public Attributes attributes { get; set; }
        }
    
        public class Metadata
        {
            public string pm_reference_number { get; set; }
        }
    
        public class Payment
        {
            public Data data { get; set; }
        }
    
        public class Source
        {
            public string id { get; set; }
            public string type { get; set; }
        }
    
    
    }
    

    The Controller

          [HttpPost]
                public IActionResult GetTheResponse([FromBody] Root root)
                {
    var paymentType = root.data.attributes.payments.FirstOrDefault().data.attributes.source.type;
                return Json(new { paymentType });
    
                    return Json(new { paymentType });
                }
    
    Login or Signup to reply.
  2. If the Root starts from the response, then to get the payment type should be like this,

    var model = JsonConvert.DeserializeObject<dynamic>(response.Content);
    
    string paymentType = model.data.attributes.payments[0].data.attributes.source.type;
    

    I used the type dynamic to prove the path is correct. So you just need to make sure that your strongly-typed objects are correct.

    UPDATE

    Tested with your classes using Root and it works as well.

    var model = JsonConvert.DeserializeObject<Root>(response.Content);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search