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:

Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by 3 days ago
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion

Reply




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