Forums >> BVSTools >> BVSTools Software Discussion >> GreenTools for PayPal (G4PP) Specific Discussion
How BVSTools Uses GreenTools for PayPal (G4PP)
by: bvstone

Jump to: 


How BVSTools Uses GreenTools for PayPal (G4PP)

How BVSTools Uses GreenTools for PayPal (G4PP)

For many years we've accepted credit cards as payment using PayPal for customers that required use of a credit card, or that were out of the country and couldn't pay using check or EFT.

Before creating GreenTools for PayPal (G4PP) and the Invoicing Addon (G4PPINV) we would manually log on to PayPal and create and send the invoice.  Not a huge task, but it did take time that is rare these days.

The first thing we did was add a "Send PayPal Invoice" button to our Customer Web Application.  I wrote this MANY years ago to keep track of customers, license keys, support and maintenance, etc using eRPG of course!

The code for this was pretty simple:

<button id="sendPayPalInvoiceButton" type="button">Send PayPal Invoice</button>

Now, when this button is clicked we want to display a popup with the customer information, and allow entering of the invoice number and invoice total.

This is done using a hidden HTML Div element and some JavaScript:

<div id="sendPayPalInvoiceDialog" title="Send PayPal Invoice" style="display: none">
  <form id="sendPayPalInvoiceForm" action="/cgi-bin/sndppinv">
    <table border=0>
        <td>Email Address:</td><td><input name="email" value="/%email%/"><br></td>
        <td>First Name:</td><td><input name="fname" value="/%firstname%/"><br></td>
        <td>Last Name:</td><td><input name="lname" value="/%lastname%/"><br></td>
        <td>Business Name</td><td> <input name="bname" value="/%customer%/"><br></td>
        <td>Address 1:</td><td><input name="add1" value="/%address1%/"><br></td>
        <td>Address 2:</td><td><input name="add2" value="/%address2%/"><br></td>
        <td>City:</td><td><input name="city" value="/%city%/"><br></td>
        <td>State:</td><td><input name="state" value="/%state%/"><br></td>
        <td>Zip:</td><td><input name="zip" value="/%zip%/"><br></td>
        <td>Invoice Number:</td><td><input name="invnbr"></td>
        <td>Invoice Total:</td><td><input name="invtot" type="number"></td>
    <button id="sendThePayPalInvoice" type="button">Send PayPal Invoice</button>
    <div id="sendPayPalInvoiceDialogError" style="color: red"></div>
  $('#sendPayPalInvoiceButton').click(function() {

Next, after the invoice number and invoice total have been entered we click the "Send PayPal Invoice" which calls an eRPG program to do the real work.

  $('#sendThePayPalInvoice').click(function(e) {
    showMessage('Sending PayPal Invoice...');
    var formData = $('#sendPayPalInvoiceForm').serialize();
    var postURL = $('#sendPayPalInvoiceForm').attr('action');
    $.post(postURL, formData)
    .done(function(data) {
    .fail(function(xhr, status, error) {
      $('#sendPayPalInvoiceDialogError').html('There was an error!  See the console.');
      console.log('status' + status);
      console.log('error:' + error);
    .always(function(data) {

The RPG program we call, SNDPPINV is as follows:

      * Imports
      * Data read in from Web Page
     D email           S            256
     D fname           S            256
     D lname           S            256
     D bname           S            256
     D add1            S            256
     D add2            S            256
     D city            S            256
     D state           S            256
     D zip             S            256
     D country         S            256
     D invnbr          S             10
     D invtot          S              9  2
     D rc              S             10i 0
     D errorMsg        S            256
     D out_file        S            256
     D out_id          S            256

       exsr $Input;
       exsr $CreateInvoice;
       exsr $SendInvoice;

       errorMsg = 'Invoice sent to ' + %trim(email) + '.';
       EXSR $return;

       //* Create Invoice
       begsr $CreateInvoice;

         rc = #g4ppinv_setValue('account':'');
         rc = #g4ppinv_setValue('merchant_email':
         rc = #g4ppinv_setValue('merchant_first_name':'Bradley');
         rc = #g4ppinv_setValue('merchant_last_name':'Stone');
         rc = #g4ppinv_setValue('merchant_business_name':'BVSTools');
         rc = #g4ppinv_setValue('merchant_phone':'5306437264');
         rc = #g4ppinv_setValue('merchant_website':'');
         rc = #g4ppinv_setValue('merchant_address1':'201 Mapleridge Drive');
         rc = #g4ppinv_setValue('merchant_country_code':'US');
         rc = #g4ppinv_setValue('merchant_postal_code':'56001');
         rc = #g4ppinv_setValue('merchant_state':'MN');
         rc = #g4ppinv_setValue('payment_term_type':'DUE_ON_RECEIPT');
         rc = #g4ppinv_setValue('logo_url':
                                '' +

         rc = #g4ppinv_createBilling();
         rc = #g4ppinv_setBillingValue('email':email);
         rc = #g4ppinv_setBillingValue('first_name':fname);
         rc = #g4ppinv_setBillingValue('last_name':lname);
         rc = #g4ppinv_setBillingValue('business_name':bname);
         rc = #g4ppinv_setBillingValue('address1':add1);
         rc = #g4ppinv_setBillingValue('address2':add2);
         rc = #g4ppinv_setBillingValue('city':city);
         rc = #g4ppinv_setBillingValue('state':state);
         rc = #g4ppinv_setBillingValue('postal_code':zip);
         rc = #g4ppinv_setBillingValue('notification_channel':'EMAIL');

         rc = #g4ppinv_createItem();
         rc = #g4ppinv_setItemValue('name':'INV-' + %trim(invnbr));
         rc = #g4ppinv_setItemValue('description':'Invoice ' + %trim(invnbr));
         rc = #g4ppinv_setItemValue('quantity':'1');
         rc = #g4ppinv_setItemValue('uom':'AMOUNT');
         rc = #g4ppinv_setItemValue('price_value':%char(invtot));
         rc = #g4ppinv_setValue('save_invoice_draft':'true');

         // Create the invoice.  If you leave out_file blank, the file name will be auto-generated.
         //  Or, you can enter your own fully qualified path to the file you want to use.  Be aware
         //  that if it already exists it will be overwritten.

         out_file = '/paypal invoices/inv' + %trim(invnbr) + '.json';
         // rc = #g4ppinv_setValue('debug':'*YES');

         rc = #g4ppinv_createInvoice(out_id:out_file:errorMsg);

         if (rc < 0);
           EXSR $return;

       //* Send Invoice
       begsr $SendInvoice;

         rc = #g4ppinv_setValue('account':'');
         rc = #g4ppinv_setValue('number':out_id);
         // rc = #g4ppinv_setValue('debug':'*YES');
         rc = #g4ppinv_sendInvoice(errorMsg);

         if (rc < 0);
           EXSR $return;

       //* Read Input
       begsr $Input;

         email = #getData('email');
         fname = #getData('fname');
         lname = #getData('lname');
         bname = #getData('bname');
         add1 = #getData('add1');
         add2 = #getData('add2');
         city = #getData('city');
         state = #getData('state');
         zip = #getData('zip');
         invnbr = #getData('invnbr');
         invtot = #CtoN(#getData('invtot'));

         if (email = ' ');
           errorMsg = 'Email Address is blank.';
           exsr $return;

         if (fname = ' ');
           errorMsg = 'First Name is blank.';
           exsr $return;

         if (lname = ' ');
           errorMsg = 'Last Name is blank.';
           exsr $return;

         if (bname = ' ');
           errorMsg = 'Business Name is blank.';
           exsr $return;

         if (invnbr = ' ');
           errorMsg = 'Invoice Number is blank.';
           exsr $return;

         if (invtot <= 0);
           errorMsg = 'Invoice Total is invalid.';
           exsr $return;

       //* Return
       begsr $return;



         *INLR = *ON;


As we can see, even this process if very simple.  First we read the data from the form, check for errors, and if there are none we create the invoice and then send it using the G4PPINV functions.  All of the OAuth 2.0 token logic is taken care of automatically by G4PP in the background, making things very simple.

When we're done both the customer and myself will receive an email automatically from PayPal and allow simply payment using a credit card without the need for having a PayPal account on the customer side.

Every little thing to make work easier and faster pays off, and this certainly did!

Last edited 08/11/2017 at 08:49:04


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