I am trying to integrate NGPVAN's contribution services with Gravity Forms. Their API takes a PHP array that looks something like this:
$d = new NgpDonation('your-credentials', true, array(
'FirstName' => 'John',
'LastName' => 'Doe',
'Address1' => '100 Elm Street',
'Zip' => '12345',
'Cycle' => '2012',
'Amount' => '50.00',
'CreditCardNumber' => '4111111111111111',
'ExpYear' => '13',
'ExpMonth' => '05'
));
with many other possible fields. My code for validation is:
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 information
$cycle = '2012';
/* ******************** General Web Contribute Form ******************** */
if (($validation_result['form']['id'] == 4)) {
$source = 'ccOnline';
// attempt payment -- being ready to reject if needed.
// name information
$firstName = rgpost("input_{$field['16']}");
$lastName = rgpost("input_{$field['17']}");
$email = rgpost("input_{$field['3']}");
$phone = rgpost("input_{$field['4']}");
// address information
$address1 = rgpost("input_{$field['18']}");
$address2 = rgpost("input_{$field['19']}");
$city = rgpost("input_{$field['20']}");
$state = rgpost("input_{$field['21']}");
$zip = rgpost("input_{$field['22']}");
// amount information
$amountradio = rgpost("input_{$field['6']}");
$amount = '5';
// credit card information
$ccnum = rgpost("input_{$field['8']}");
$exmonth = rgpost("input_{$field['9']}");
$exyear = rgpost("input_{$field['10']}");
// recurring information
// $recurring = rgpost("input_11');
// $recurringfreq = rgpost("input_12');
// $recurringtotal = rgpost("input_13');
// employment information
$employer = rgpost("input_{$field['14']}");
$occupation = rgpost("input_{$field['15']}");
$result = do_transaction($lastName, $firstName, $email, $phone, $address1, $address2, $city, $state, $zip, $amount, $ccnum, $expMonth, $expYear, $employer, $occupation, $cycle, $source);
if ($result !== true) {
$validation_result['is_valid'] = false;
foreach($validation_result["form"]["fields"] as &$field){
if ($field["id"] == "8"){
$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_8'] = '*****';
}
return $validation_result;
/* ******************** End General Web Contribute Form ******************** */
} else {
return $validation_result;
}
} else {
return $validation_result;
}
}
// This is the function that is called above
function do_transaction($lastName, $firstName, $email, $phone, $address1, $address2, $city, $state, $zip, $amount, $ccnum, $exmonth, $exyear, $employer, $occupation, $cycle, $source) {
require 'NgpDonation.php';
// Your string goes below. Will look something like the following:
$apiString = "*******REDACTED FOR CONFIDENTIALITY********";
$d = new NgpDonation($apiString, true, array(
'FirstName' => $firstName,
'LastName' => $lastName,
'Email' => $email,
'HomePhone' => $phone,
'Address1' => $address1,
'Address2' => $address2,
'City' => $city,
'State' => $state,
'Zip' => $zip,
'Cycle' => $cycle,
'Amount' => $amount,
'CreditCardNumber' => $ccnum,
'ExpYear' => $exyear,
'ExpMonth' => $exmonth,
'Employer' => $employer,
'Occupation' => $occupation,
'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 = 'testingerror';
return $errorString;
}
}
Every time I send the form I get the validation error. Am I pulling the variables incorrectly? Any help would be greatly appreciated.