Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkomando/e756e858afac40b082520e2b468f56cc to your computer and use it in GitHub Desktop.
Save dkomando/e756e858afac40b082520e2b468f56cc to your computer and use it in GitHub Desktop.
Enables the credit card fields for Gravity Forms and provides a function to get the credit card details which are not available through the filter $form or $entry
<?php
/**
* Enable CC Field & Send through email or the GF Webhooks Addon.
*
* @package GF - DK Extended.
*/
/**
* Security: Block direct access.
*/
defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
* This originated from a GitHub Gist. Which you can see from the fork.
*
* @link https://gist.github.com/DeveloperWil/2a380bf7e428cd962084
*/
// Enable the GF Credit Card field for admin and front-end.
add_action( 'gform_enable_credit_card_field', '__return_true', 11 );
/**
* Email encoded card details when the form is submitted.
*
* Note: You may want to use 'gform_pre_submission' instead of 'gform_after_submission' if altering fields before any notifications/webhooks/etc!
*
* @param Object $entry The entry that was just created.
* @param Object $form The current form.
*/
function email_encoded_cc( $entry, $form ) {
// This should be a required field in which it will always exist.
$cc_fields = GFCommon::get_fields_by_type( $form, array('creditcard') )[0];
$card_number = rgpost( "input_{$cc_fields['id']}_1" );
$expiration_date = rgpost( "input_{$cc_fields['id']}_2" );
$expire_month = ( 10 > (int) $expiration_date[0] ) ? '0' . $expiration_date[0] : $expiration_date[0];
$expire_year = $expiration_date[1];
$security_code = rgpost( "input_{$cc_fields['id']}_3" );
$card_name = rgpost( "input_{$cc_fields['id']}_5" );
$cc_detail_string = "CardName: $card_name CardNum: $card_number CardExp: $expire_month/$expire_year CardCCV: $security_code";
// Remember base 64 is not encryption - just encoding!
$encoded_cc_detail_string = base64_encode( $cc_detail_string );
// Send encoded CC details via email. (Sending this info by email really is a bad idea since email isn't secure!)
$to = 'whoever@wherever.com'; // Email here.
$subject = '[Website Name] Submission';
$body = $cc_detail_string; // Encoded string.
$message = "Here are the CC details:\n$body";
$from = 'noreply@website-name.com';
$headers = "From:" . $from;
// Send message.
mail( $to, $subject, $message, $headers );
}
add_action( 'gform_after_submission', 'email_encoded_cc', 10, 2 );
/**
* ----------------------------------------------------------------
* ----------------------------------------------------------------
* Using with GF Webhooks Addon instead.
* ----------------------------------------------------------------
* ----------------------------------------------------------------
*/
/**
* Manipulate Webhook values on submit after validation occurs.
*
* @param Object $form The current form.
*/
function pre_submission_handler( $form ) {
/**
* Check for form ID #18 submission.
*/
if ( 18 === $form['id'] ) {
// Update CC info to additional fields, since GF isn't passing them by default.
// This will automatically grab the CC field and the fields ID for pulling data.
$cc_fields = GFCommon::get_fields_by_type( $form, array( 'creditcard' ) )[0];
// $card_number = rgpost( "input_{$cc_fields['id']}_1" ); // GF collects this field without issue.
// $card_type = $cc_fields['creditCards'][ rgpost( "input_{$cc_fields['id']}_3" ) ]; // GF collects this field without issue.
$exp_date_arr = rgpost( "input_{$cc_fields['id']}_2" );
// Adjust month.
$exp_month = ( 10 > (int) $exp_date_arr[0] ) ? '0' . $exp_date_arr[0] : $exp_date_arr[0];
// Update hidden fields (that you will need to add to your form) that the Webhooks Addon can actually reference!
$_POST['input_39'] = $exp_month . '/' . $exp_date_arr[1];
$_POST['input_40'] = rgpost( "input_{$cc_fields['id']}_3" );
$_POST['input_41'] = rgpost( "input_{$cc_fields['id']}_5" );
}
}
add_action( 'gform_pre_submission', 'pre_submission_handler' );
/**
* ----------------------------------------------------------------
* ----------------------------------------------------------------
* Finally, don't forget to wipe these fields with a 'gform_after_submission'!
* ----------------------------------------------------------------
* ----------------------------------------------------------------
*/
/**
* Gravity Forms:
* - Remove submitted data from saving to site form entries.
*/
add_action(
'gform_after_submission',
/**
* Overwrite specific field entry.
* - Remove Social Security Numbers from the local GF database.
*
* @param Object $entry The entry that was just created.
* @param Object $form The current form.
*/
function( $entry, $form ) {
// Note: This code is only checking GF form ID: 18!
if ( '18' === $entry['form_id'] ) {
/**
* Update Entry Field.
*
* @param int $entry_id The ID of the entry the field or input value is to be updated for.
* @param int|string $input_id The ID of the field or input the value is to be updated for.
* @param null|int|string|float $value The new value of the field or input.
* @param string $item_index The item index if the field or input is a child of a Repeater field.
*/
// NOTE: GF automatically obfuscates the CC number. However, we will need to obfuscate the fields we add around the CC Number.
// Remove CC Exp Data.
GFAPI::update_entry_field( $entry['id'], 39, 'data_not_stored' );
// Remove CC CCV Data.
GFAPI::update_entry_field( $entry['id'], 40, 'data_not_stored' );
// Remove CC Card Name Data.
GFAPI::update_entry_field( $entry['id'], 41, 'data_not_stored' );
}
},
10,
2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment