I have a First Data Merchant account. I wanted to add a payment page to my WordPress website. I created the form with Gravity Forms which would allow me to pass variables for the item they are paying for, the amount, and the quantity.
I also wanted the form to allow them to fill in the amount they wanted to pay. This might come in handy if someone just wanted to donate some money because I’m a great person or if they wanted to make a partial payment on their bill, or for any number of other reasons that someone needed to pay me.
You can read about how I set up the form in Gravity Forms to make that work in my other post.
After I created the gravity form for the payment, I created a page for the form. I just used the short code for the gravity form on the page and gravity forms automatically picks up my variables from the GET variables in the url. For example, if this url is used:
// https://cullenwebservices.com/payment?user=false&product_name=Maintenance&quantity=1&amount=80.50 //
Then gravity forms will pick up the product name, quantity and amount and pre-fill the form. Cool!
I wanted to make this page secure so I installed my SSL certificate that makes everything secure and then found an https plugin that will automatically make the payment page redirect to a secure https page for the payment.
I created the receipt with Gravity Forms and customized it to say exactly what I wanted to say. I also created the thank you page with Gravity Forms. I’m loving Gravity Forms more and more every day. Made this a very quick process.
I added the credit card processing to the validation hook for Gravity Forms.
// define('FD_URL','https://api.demo.globalgatewaye4.firstdata.com/transaction/v11'); define('FD_ID','ADxxxx-xx'); define('FD_PASSWORD','xxxxxxx'); add_filter('gform_validation_3', 'validate_cc'); //my form has a form ID of 3 function validate_cc($validation_result) { if ($validation_result['is_valid']) { //make sure everything else passed validation $form = $validation_result["form"]; foreach($form['fields'] as &$field){ // get amount, if ($field['label'] == 'Total') { $amount = $_POST['input_'.$field['id']]; } if ($field['label'] == 'Credit Card') { $inputs = $field['inputs']; foreach ($inputs as $input) { //get card number, if ($input['label'] == 'Card Number') { $ccnumber_input = $input['id']; $card_number = $_POST['input_'.str_replace('.','_',$ccnumber_input)]; } //get cardholder name, if ($input['label'] == 'Cardholder\'s Name') { $ccname_input = $input['id']; $name = $_POST['input_'.str_replace('.','_',$ccname_input)]; } //get expiration date if ($input['label'] == 'Expiration Date') { $ccexp_input = $input['id']; $ccexp = $_POST['input_'.str_replace('.','_',$ccexp_input)]; $ccexp = str_pad($ccexp['0'], 2, '0', STR_PAD_LEFT).substr($ccexp['1'], -2); } } } } $data = array("gateway_id" => FD_ID, "password" => FD_PASSWORD, "transaction_type" => "00", "amount" => $amount, "cardholder_name" => $name, "cc_number" => $card_number, "cc_expiry" => $ccexp); $data_string= json_encode($data); $ch = curl_init( FD_URL ); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8;')); $result = curl_exec($ch); // Getting jSON result string $response = json_decode($result); if ($response) { if ($response->bank_resp_code == '100') { $valid = true; } else { $body = $response->bank_message; mail ('me@email.com','error with CWS transaction',$body,"From: admin@example.com"); // alert me to problems $valid=false; } } else { mail ('me@email.com','error with CWS transaction','nothing in xml',"From: admin@example.com"); // alert me to problems $valid=false; } if (!$valid) { $validation_result['is_valid'] = false; // mark the specific field that failed and add a custom validation message $field['failed_validation'] = true; $field['validation_message'] = 'Your credit card payment failed. Please try again.'; // any other housekeeping? } $validation_result['form'] = $form; } // Return the validation result return $validation_result; } //
This code is for the demo site so that you can test. When you get the code to work in your test site, you will want to change the FD_URL, FD_ID, and FD_PASSWORD to the url, ID, and password for your real First Data merchant account. The url should be:
// define('FD_URL','https://api.globalgatewaye4.firstdata.com/transaction/v11'); //
If you get an error on the form or if the credit card payment fails, it will return to the form with the error messages.
Pingback: Add a Form to Take Payments with Gravity Forms: Allowing User Defined Fields as well as Set Amounts - Cullen Web Services