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).

 




Latest Posts:

MAILTOOL Updated to Allow Failed Message on Invalid Recipient MAILTOOL Updated to Allow Failed Message on Invalid Recipient
Posted by May 20, 2018
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G) Non HTTPS Callbacks Removed from GreenTools for G Suite (G4G)
Posted by April 15, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
IBM i Related Survey Available IBM i Related Survey Available
Posted by April 7, 2018
IBM Power Systems >> (QGPL) IBM i
BVSTools Releases Braintree Webhook Open Source Application - Node.js BVSTools Releases Braintree Webhook Open Source Application - Node.js
Posted by April 5, 2018
Programming >> Open Source
BVSTools Now Offering Web Services (BETA) BVSTools Now Offering Web Services (BETA)
Posted by April 3, 2018
BVSTools >> BVSTools Announcements
Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2 Creating a Reverse SSL Proxy Using RPG on the IBM i - Part 2
Posted by March 29, 2018
Programming >> Web Programming
Still on V7R1 or Earlier?  Here's Why You Should Upgrade NOW! Still on V7R1 or Earlier? Here's Why You Should Upgrade NOW!
Posted by February 21, 2018
IBM Power Systems >> (QGPL) IBM i
Converting a MMDDYY date format to YYMMDD for Sorting Using SQL Converting a MMDDYY date format to YYMMDD for Sorting Using SQL
Posted by February 16, 2018
Programming >> RPG Programming
Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G) Moving All Files from a Google Drive Folder to the Trash Using GreenTools for Google Apps (G4G)
Posted by February 3, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
Removing Unwanted Characters from a String with RPG Removing Unwanted Characters from a String with RPG
Posted by February 2, 2018
Programming >> RPG Programming
GETURI Version 8.10 Released GETURI Version 8.10 Released
Posted by January 23, 2018
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools is Now a Sponsor of ALLAS400S.com BVSTools is Now a Sponsor of ALLAS400S.com
Posted by January 17, 2018
BVSTools >> BVSTools Announcements
Service Pack SP171219 Released for G4PP v3.10 Service Pack SP171219 Released for G4PP v3.10
Posted by December 20, 2017
BVSTools >> BVSTools Announcements >> GreenTools for PayPal (G4PP) Specific Announcements
GETURI Now Requires Minimum OS of V7R1 or Higher GETURI Now Requires Minimum OS of V7R1 or Higher
Posted by December 12, 2017
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
Need Help Updating Your Software Versions?  We Can Help. Need Help Updating Your Software Versions? We Can Help.
Posted by December 8, 2017
BVSTools >> BVSTools Announcements

Reply




Copyright 1983-2018 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).