Note: As of Stripe version 3.0 this filter is not available for use with the Stripe Checkout payment collection method. Use the gform_stripe_session_data filter instead if using Stripe Checkout.
Description
The gform_stripe_charge_pre_create filter allows the charge properties to be overridden before the charge is created by the Stripe API. This filter should not be used to override the $charge_meta[‘metadata’] property.
Note: This filter applies only to Products and Services feeds. When “Use Stripe’s Payment Element” is enabled in the Stripe field settings you need to use this filter along with gform_stripe_payment_element_initial_payment_information
Usage
The hook which would run for all Stripe product and service feeds can be used like so:
add_filter( 'gform_stripe_charge_pre_create', 'your_function_name', 10, 5 );
Parameters
Parameter | Type | Description |
---|---|---|
$charge_meta | array | The properties for the charge to be created. |
$feed | Feed Object | The feed object currently being processed. |
$submission_data | Submission Data | Contains the form title, payment amount, setup fee amount, trial amount, line items created using the submitted pricing field values and any discounts from coupons. |
$form | Form Object | The Form which is currently being processed. |
$entry | Entry Object | The entry from which the subscription was created. |
Examples
1. Add the statement_descriptor property.
The following example shows how you can add the statement_descriptor property to the charge meta. Please check Stripe’s documentation for requirements.
add_filter( 'gform_stripe_charge_pre_create', 'stripe_charge_pre_create', 10, 5 );
function stripe_charge_pre_create( $charge_meta, $feed, $submission_data, $form, $entry ) {
$charge_meta['statement_descriptor'] = 'STATEMENTDESC';
return $charge_meta;
}
2. Attach payment method to the customer.
The following example shows how you can tell Stripe.com to attach the payment method to the customer for forms using the Stripe Card field.
If the Enable additional payment methods setting is not active in the Stripe field, you would need to use it in combination with gform_stripe_customer_id to create the customer object.
// This sets setup_future_usage parameter to off_session to save the payment method.
add_filter( 'gform_stripe_charge_pre_create', function( $charge_meta, $feed, $submission_data, $form, $entry ) {
gf_stripe()->log_debug( __METHOD__ . '(): Adding setup_future_usage for feed ' . rgars( $feed, 'meta/feedName' ) );
$charge_meta['setup_future_usage'] = 'off_session';
return $charge_meta;
}, 10, 5 );
3. Add the description property.
The following example shows how you can add the description property to the charge meta.
add_filter( 'gform_stripe_charge_pre_create', function ( $charge_meta, $feed, $submission_data, $form, $entry ) {
if ( empty( $charge_meta['description'] ) ) {
$charge_meta['description'] = gf_stripe()->get_payment_description( $entry, $submission_data, $feed );
}
return $charge_meta;
}, 10, 5 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
This hook was added in Stripe version 2.2.2.
Source Code
$charge_meta = apply_filters( 'gform_stripe_charge_pre_create', $charge_meta, $feed, $submission_data, $form, $entry );
This hook is located in GFStripe::authorize_product() in class-gf-stripe.php.