home pageabout moiagroupour servicesour workhow we workcontact usstuff we like

Gravity Forms + NGP API for Payment Processing

I recently had a client switch over to NGP from straight PayPal and they wanted to retain the ability to easily process transactions through forms done with Gravity Forms, but NGP’s famously vague documentation made it a bit more of a challenge. Here’s how you can do it. First, create your form in Gravity Forms. Then, add these items to your functions.php:

Be sure to download the New Media Campaigns NGP API toolkit and put the NgpDonation.php in your theme’s “includes” folder. Or wherever you like.

add_action("gform_validation", "process_credit_card_data");

/*
* Courtsey of
* http://www.newmediacampaigns.com/blog/accept-campaign-donations-with-the-ngp-contribution-api
* http://www.gravityhelp.com/documentation/page/Gform_validation
*/
function process_credit_card_data($validation_result) {
    // print_r($form); // for debugging
    // print_r($validation_result);
    // print_r($_POST);
    if ($validation_result['is_valid']) {
        $cycle = strftime("%Y", time());
        /* ******************** FORM ID 2-3 ******************** */
        if (($validation_result['form']['id'] == 2)) {
            $source = "12LunchPhx";
            // attempt payment -- being ready to reject if needed.
            // prepare fields
            $lastName = $_POST['input_1_6'];
            $firstName = $_POST['input_1_3'];
            // My form has a checkbox to say "same as billing?"
            if ($_POST['input_28_1'] == "Yes") {
                // use reg. address
                $address1 = $_POST['input_2_1'];
                $zip = $_POST['input_2_5'];
                } else {
                // use billing address
                $address1 = $_POST['input_27_1'];
                $zip = $_POST['input_27_5'];
            }
            // this makes testing a lot less expensive.
            // note that we're using the Gravity Forms auto-totalling here.
            if (trim($lastName) == "test") {
                $amount = 1;
                } else {
                $amount = $_POST['input_29'];
            }
            $ccnum = $_POST['input_30'];
            list($expMonth, $expYear) = explode('/', $_POST['input_31']);
            $result = do_transaction($lastName, $firstName, $address1, $zip, $amount, $ccnum, $expMonth, $expYear, $cycle, $source);
            if ($result !== true) {
                $validation_result['is_valid'] = false;
                foreach($validation_result["form"]["fields"] as &$field){
                    //NOTE: replace 30 with the field you would like to show the validation on.
                    if ($field["id"] == "30"){
                        $field["failed_validation"] = true;
                        $field["validation_message"] = $result;
                        break;
                    }
                }
                } else {
                // I like to blank credit card data so it's not stored locally.
                $_POST['input_30'] = 'Blanked for liability reasons.';
            }
            return $validation_result;
            /* ******************** END FORM ID 2 ******************** */
            } else {
            return $validation_result;
        }
        } else {
        return $validation_result;
    }
}

function do_transaction($lastName, $firstName, $address1, $zip, $amount, $ccnum, $expMonth, $expYear, $cycle, $source) {
    require 'includes/NgpDonation.php';
    // Your string goes below. Will look something like the following:
    $apiString = "W8YnwP77BY4I6kSwm2xJ09n3F1J+4hGffs321fsar1n8LPMYsOrx1jPP2cKquKYeaRVqxpgJ8KkhGdQfj4Q==";
    // $amount = 1;
    //echo("do_transaction($lastName, $firstName, $address1, $zip, $amount, $ccnum, $expMonth, $expYear, $cycle)\n\n");
    $d = new NgpDonation($apiString, true, array(
    'FirstName' => $firstName,
    'LastName' => $lastName,
    'Address1' => $address1,
    'Zip' => $zip,
    'Cycle' => $cycle,
    'Amount' => $amount,
    'CreditCardNumber' => $ccnum,
    'ExpYear' => $expYear,
    'ExpMonth' => $expMonth,
    'Source' => $source
    ));
    $result = $d->save();
    if ($result) {
        return true;
        } else {
        if ( $d->hasErrors() ) {
            $errors = $d->getErrors();
            //print_r($errors);
        }
        if ( $d->hasFault() ) {
            $fault = $d->getFault();
            //print_r($fault);
        }
        $result = $d->getResult();
        $errorString = $result->VendorResult->Message;
        return $errorString;
    }
}

 

Thanks to New Media Campaigns  for their great toolkit for NGP.

If all  you want to do is add emails, you can pretty easily get that going by taking the above and mixing it with this:

Using the NGP API to add email addresses

And yeah, it’d be even better to make this into a true Gravity Forms module/plugin/whatever since you have to manually re-write the function every time you add a new form. If you do this, let me know!

Comments are closed.