Description
Used to set or update product or subscription data before creating the Stripe Checkout session in Stripe. Can be used to add product images.
Usage
$session_data = apply_filters( 'gform_stripe_session_data', $session_data, $feed, $submission_data, $form, $entry );
Parameters
- $session_data array
Session data for creating the session. See the Stripe documentation for supported arguments. - $feed array
The feed object currently being processed. - $submission_data array
The customer and transaction data. - $form array
The form object currently being processed. - $entry array
The entry object currently being processed.
Example
Show product images on the checkout page with the Image Choices add-on
The following snippet shows how to add product images to the Stripe Checkout page when using the Gravity Forms Image Choices add-on.
function my_gform_stripe_session_data( $session_data, $feed, $submission_data, $form, $entry ) {
if ( $form['id'] === 4 ) { // set your form id.
foreach ( $submission_data['line_items'] as $k => $line_item ) {
$field = GFAPI::get_field( $form, $line_item['id'] );
if ( rgar( $field, 'choices' ) ) {
$image = gf_image_choices()->get_choice_image_src( $field, $line_item['name'] );
if ( $image ) {
$session_data['line_items'][ $k ]['images'][] = $image;
}
}
}
}
return $session_data;
}
add_filter( 'gform_stripe_session_data', 'my_gform_stripe_session_data', 10, 5 );
Add the statement_descriptor property
The following snippet shows how to replace the gform_stripe_charge_pre_create filter for use with the new Stripe Checkout in Stripe add-on version 3.0 and newer.
function my_gform_stripe_session_data( $session_data, $feed, $submission_data, $form, $entry ) {
if ( $form['id'] === 4 ) { // set your form id.
$session_data['payment_intent_data']['statement_descriptor'] = 'STATEMENTDESC';
}
return $session_data;
}
add_filter( 'gform_stripe_session_data', 'my_gform_stripe_session_data', 10, 5 );
To see what charge meta can be replaced with payment_intent_data
in the Stripe checkout session, see the Stripe documentation.
Add additional payment method types
The following example shows how to include additional payment methods. You can find the full list of available payment methods in Stripe’s documentation.
Note that currently our add-on is certified to work only with the following payment methods: Card, Google Pay, Apple Pay. Other payment methods added this way may not work due to additional technical requirements.
add_filter( 'gform_stripe_session_data', function( $session_data, $feed, $submission_data, $form, $entry ) {
if ( $form['id'] === 4 ) { // set your form id.
$session_data['payment_method_types'][] = 'ideal';
}
return $session_data;
}, 10, 5 );
See the Stripe API documentation for the supported payment method types and the conditions which must be met for them to be used.
Send metadata before the entry is saved
Note: This request is sent during form validation when entry properties such as the id are not available. Also, other field values could be changed after this data has been sent to Stripe.
add_filter( 'gform_stripe_session_data', function ( $session_data, $feed, $submission_data, $form, $entry ) {
if ( ! empty( $session_data['payment_intent_data'] ) && $form['id'] === 4 ) { // set your form id.
$metadata = gf_stripe()->get_stripe_meta_data( $feed, $entry, $form );
if ( ! empty( $metadata ) ) {
// Add metadata to payment intent.
$session_data['payment_intent_data']['metadata'] = $metadata;
}
}
return $session_data;
}, 10, 5 );
Save payment method for future usage
The following snippet adds the setup_future_usage
parameter to attach the payment method to the customer created at Stripe.com
add_filter( 'gform_stripe_session_data', function( $session_data, $feed, $submission_data, $form, $entry ) {
$session_data['payment_intent_data']['setup_future_usage'] = 'off_session';
return $session_data;
}, 10, 5 );
Remove customer_creation when customer has already been created
Stripe made a breaking change to their API which causes an error to be returned on session creation if both the customer
and customer_creation
parameters are included in the request.
This example shows how the customer_creation
parameter can be removed from the request when the gform_stripe_customer_id filter has set the customer
.
add_filter( 'gform_stripe_session_data', function ( $session_data ) {
if ( ! empty( $session_data['customer'] ) ) {
unset( $session_data['customer_creation'] );
}
return $session_data;
} );
Placement
This code should be placed in the functions.php file of your active theme.
Since
This filter was added in Stripe 3.0.
Source Code
This filter is located in class-gf-stripe.php.