gform_stripe_session_data

gform_stripe_session_data

DescriptionUsageParametersExample1. Show product images on the checkout page with the Image Choices add-on2. Add the statement_descriptor property3. Add additional payment method types4. Send metadata before the entry is saved5. Save payment method for future usagePlacementSinceSource Code

Description

Used to set/update product/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 _autolink_array
Session data for creating the session. See the Stripe documentation for supported arguments.

$feed _autolink_array
The feed object currently being processed.

$submission_data _autolink_array
The customer and transaction data.

$form _autolink_array
The form object currently being processed.

$entry _autolink_array
The entry object currently being processed.

Example

1. 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 );

2. 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.

3. Add additional payment method types

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.

4. 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 );

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 );

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.

發表回覆

您的郵箱地址不會被公開。 必填項已用 * 標註