Using Gravity Forms ‘gform_paypal_hash_matches’ PHP filter

The gform_paypal_hash_matches filter allows additional validation of the hash to be performed in Gravity Forms.

Usage

To use this filter, add the following code to your theme’s functions.php file:

add_filter('gform_paypal_hash_matches', 'your_function_name', 10, 4);

Parameters

  • $hash_matches (bool): Indicates if the hash matches.
  • $entry_id (int): The ID of the current entry.
  • $hash (string): The hash.
  • $custom_field (string): The pipe-delimited string in the IPN from PayPal. This contains the entry id and the hash.

More information

See Gravity Forms Docs: gform_paypal_hash_matches

Examples

Basic hash validation

This example shows a simple validation of the hash, returning false if the entry ID is greater than 100.

add_filter('gform_paypal_hash_matches', 'validate_hash', 10, 4);

function validate_hash($hash_matches, $entry_id, $hash, $custom_field) {
    // Perform further checks on the hash or force failure
    if ($entry_id > 100) {
        return false;
    }
    return true;
}

Custom hash validation

This example demonstrates a custom hash validation using a secret key to compare with the received hash.

add_filter('gform_paypal_hash_matches', 'custom_hash_validation', 10, 4);

function custom_hash_validation($hash_matches, $entry_id, $hash, $custom_field) {
    // Your secret key
    $secret_key = "your_secret_key";

    // Calculate the expected hash
    $expected_hash = md5($entry_id . $secret_key);

    // Compare the expected hash with the received hash
    return $hash === $expected_hash;
}

Logging hash validation results

This example logs the hash validation results to a custom log file.

add_filter('gform_paypal_hash_matches', 'log_hash_validation', 10, 4);

function log_hash_validation($hash_matches, $entry_id, $hash, $custom_field) {
    // Log the validation result to a file
    file_put_contents('hash_validation_log.txt', "Entry ID: $entry_id, Hash Matches: " . ($hash_matches ? 'true' : 'false') . "\n", FILE_APPEND);

    return $hash_matches;
}

Hash validation based on custom field

This example validates the hash based on a custom field value.

add_filter('gform_paypal_hash_matches', 'custom_field_hash_validation', 10, 4);

function custom_field_hash_validation($hash_matches, $entry_id, $hash, $custom_field) {
    // Check if the custom field contains a specific value
    if (strpos($custom_field, 'custom_value') !== false) {
        // Perform additional validation or force failure
        return false;
    }
    return $hash_matches;
}