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:

BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command BVSTools Releases Send Job Log to BVSTools (SNDLOG2BVS) Command
Posted by August 28, 2023
BVSTools >> BVSTools Announcements
MAILTOOL Now Allows Email Redirection for Development and Testing MAILTOOL Now Allows Email Redirection for Development and Testing
Posted by May 27, 2023
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email GreenTools for Microsoft Apps (G4MS) Now Supports Footers When Sending Email
Posted by March 29, 2023
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
QuickBooks Online - Subtotals and Discounts Frustration QuickBooks Online - Subtotals and Discounts Frustration
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic Making the Switch From QuickBooks Desktop to QuickBooks Online - No Picnic
Posted by March 16, 2023
QuickBooks >> QuickBooks Online
BVSTools Software Verified on V7R5 and Power10 BVSTools Software Verified on V7R5 and Power10
Posted by December 9, 2022
BVSTools >> BVSTools Announcements
Microsoft Office 365 Servers and Random Errors Issue Microsoft Office 365 Servers and Random Errors Issue
Posted by November 14, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending/Resending Emails Using a MIME File with MAILTOOL Sending/Resending Emails Using a MIME File with MAILTOOL
Posted by November 8, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Sending an HTML Email on Your IBM i Using MAILTOOL Sending an HTML Email on Your IBM i Using MAILTOOL
Posted by November 1, 2022
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Transferring License Keys from One System to Another Transferring License Keys from One System to Another
Posted by October 31, 2022
BVSTools >> BVSTools Software Discussion
Calculating the Size of a File Before Base64 Encoding Calculating the Size of a File Before Base64 Encoding
Posted by August 13, 2022
Programming >> RPG Programming
GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File GreenTools for Microsoft Apps (G4MS) v9.12 Now Includes Function to Send Emails using MIME File
Posted by August 11, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts GreenTools for Google Apps (G4G) v15.20 Now Supports Shortcuts
Posted by August 6, 2022
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions GreenTools for Microsoft Apps (G4MS) Groups Admin Authority Instructions
Posted by July 26, 2022
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives GreenTools for Microsoft Apps (G4MS) v9.10 Now Includes OneDrive Functions that Work With Groups/Shared Drives
Posted by July 19, 2022
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements

Reply




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