bvstone

Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction

Posted:

Parsing the Response from GreenTools for PayPal (G4PP) Refund Transaction

When working with GreenTools for PayPal (G4PP) you have the option to store the data returned in a JSON format. 

The refund ID is readily available from the #g4pppay_refundPayment() function call, but what about the other information?  Well, that is where you will need to load up your favorite JSON parser (I prefer the YAJL port by Scott Klement) and parse the data to retrieve any other information.

Following is an example of the JSON data returned and available to you:

{
	"id": "5A6644430P3314355",
	"state": "completed",
	"amount": {
		"total": "112.99",
		"currency": "USD"
	},
	"refund_from_transaction_fee": {
		"currency": "USD",
		"value": "3.28"
	},
	"total_refunded_amount": {
		"currency": "USD",
		"value": "112.99"
	},
	"refund_from_received_amount": {
		"currency": "USD",
		"value": "109.71"
	},
	"sale_id": "9B888803TG745807Z",
	"invoice_number": "123456",
	"links": [{
			"href": "https://api.sandbox.paypal.com/v1/payments/refund/9B888803TG745807Z",
			"rel": "self",
			"method": "GET"
		}
	]
}

And following is the RPG source of a program I put together that shows how to parse and retrieve each piece of data.  Keep in mind in this case I am retrieving the data into the same field, so if you want all the data separated you will want to set up different variables for the data.

     H DFTACTGRP(*NO) BNDDIR('BVSTOOLS')
      ****************************************************************
      /include qcopysrc,yajl_h
      ****************************************************************
     D docNode         s                   like(yajl_val)
     D node            s                   like(yajl_val)
     D node2           s                   like(yajl_val)
     D links           s                   like(yajl_val)
     D val             s                   like(yajl_val)

     D value           s            500a   varying
     D errMsg          s            500a   varying
     D i               s             10i 0
      ****************************************************************
        docNode = yajl_stmf_load_tree('/tmp/g4pp_MyRefundDetails.json':errMsg);

        if (docNode <> *NULL);
          node = YAJL_object_find(docNode:'id');
          value = yajl_get_string(node);

          node = YAJL_object_find(docNode:'state');
          value = yajl_get_string(node);

          node2 = YAJL_object_find(docNode:'amount');
          node = YAJL_object_find(node2:'total');
          value = yajl_get_string(node);
          node = YAJL_object_find(node2:'currency');
          value = yajl_get_string(node);

          node2 = YAJL_object_find(docNode:'refund_from_transaction_fee');
          node = YAJL_object_find(node2:'currency');
          value = yajl_get_string(node);
          node = YAJL_object_find(node2:'value');
          value = yajl_get_string(node);

          node2 = YAJL_object_find(docNode:'total_refunded_amount');
          node = YAJL_object_find(node2:'currency');
          value = yajl_get_string(node);
          node = YAJL_object_find(node2:'value');
          value = yajl_get_string(node);

          node2 = YAJL_object_find(docNode:'refund_from_received_amount');
          node = YAJL_object_find(node2:'currency');
          value = yajl_get_string(node);
          node = YAJL_object_find(node2:'value');
          value = yajl_get_string(node);

          node = YAJL_object_find(docNode:'sale_id');
          value = yajl_get_string(node);

          node = YAJL_object_find(docNode:'invoice_number');
          value = yajl_get_string(node);

          i = 0;
          links = YAJL_object_find(docNode:'links');

          dow YAJL_ARRAY_LOOP(links:i:node);
            node2 = YAJL_object_find(node:'href');
            value = yajl_get_string(node2);

            node2 = YAJL_object_find(node:'rel');
            value = yajl_get_string(node2);

            node2 = YAJL_object_find(node:'method');
            value = yajl_get_string(node2);
          enddo;

        endif;

       *inlr = *on;

We start out by loading the refund JSON file which is stored in the IFS as /tmp/g4pp_MyRefundDetails.json.

If that succeeds, we then parse each of the items down the line.  The docNode pointer is used to keep a reference to the beginning of the document.

The node pointer is used to find individual items to parse (unless we are parsing items in an array, such as the links array). 

The node2 pointer is used as a sort of temporary pointer for embedded JSON data.  It's also used as the item pointer in the links loop.

Hopefully this example will help if you need to parse any information from the refund or invoicing PayPal functions included in GreenTools for PayPal (G4PP).

 





Reply




© Copyright 1983-2024 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).