How I can get authorization -> id from payment responce?

How can I get authorization -> id from this type of response

{
           "intent": "authorize",
           "payer": {
               "payment_method": "credit_card",
               "funding_instruments": [
                   {
                       "credit_card": {
                           "payer_id": "3888888888888888888",
                           "number": "xxxxxxxxxxxx8888",
                           "type": "DISCOVER",
                           "expire_month": "10",
                           "expire_year": "2028",
                           "first_name": "ABC"
                       }
                   }
               ]
           },
           "transactions": [
               {
                   "amount": {
                       "currency": "USD",
                       "total": "4.00",
                       "details": {}
                   },
                   "description": "This is the authorize payment transaction description.",
                   "related_resources": [
                       {
                           "authorization": {
                               "amount": {
                                   "currency": "USD",
                                   "total": "4.00",
                                   "details": {}
                               },
                               "create_time": "2018-10-12T11:23:51Z",
                               "update_time": "2018-10-12T11:23:53Z",
                               "state": "authorized",
                               "parent_payment": "PAY-37J37J37J37J37J37J37J37J",
                               "id": "4FR34FR34FR34FR3A",
                               "valid_until": "2018-11-10T11:23:51Z",
                               "links": [
                                   {
                                       "href": "https://api.sandbox.paypal.com/v1/payments/authorization/4FR34FR34FR34FR3A",
                                       "rel": "self",
                                       "method": "GET"
                                   },
                                   {
                                       "href": "https://api.sandbox.paypal.com/v1/payments/authorization/4FR34FR34FR34FR3A/capture",
                                       "rel": "capture",
                                       "method": "POST"
                                   },
                                   {
                                       "href": "https://api.sandbox.paypal.com/v1/payments/authorization/4FR34FR34FR34FR3A/void",
                                       "rel": "void",
                                       "method": "POST"
                                   },
                                   {
                                       "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-37J37J37J37J37J37J37J37J",
                                       "rel": "parent_payment",
                                       "method": "GET"
                                   }
                               ]
                           }
                       }
                   ]
               }
           ],
           "id": "PAY-37J37J37J37J37J37J37J37J",
           "create_time": "2018-10-12T11:23:51Z",
           "state": "approved",
           "update_time": "2018-10-12T11:23:53Z",
           "links": [
               {
                   "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-37J37J37J37J37J37J37J37J",
                   "rel": "self",
                   "method": "GET"
               }
           ]
       }
   }
}

I need to get this id “4FR34FR34FR34FR3A” to capture payment, how can I can I get this id from this response in golang?

Your response is in JSON format. Unmarshal it.

Use https://mholt.github.io/json-to-go/ to convert your json string to a go struct

really useful. Great

I am working with Rest Api, using decoded json data. We can achieve this without Unmarshal, using range loop.
for i, transactionRes := range paymentResponse.Transactions {
obj := transactionRes.RelatedResources[i]
id := obj.Authorization.ID
returnData[“paypal_auth_id”] = id

}

I don’t think that is a good idea because your code become unmaintainable. Some should work hard to understand what the loops does, instead of looking over a well organised struct.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.