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:

G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by 3 days ago
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming
Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) Specific Discussion
GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function GreenTools for G Suite (G4G) Updated to Include Delete and Empty Trash Function
Posted by July 24, 2018
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
What to Do If Your License Keys Don't Work What to Do If Your License Keys Don't Work
Posted by July 18, 2018
BVSTools >> BVSTools Software Discussion
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

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