Introduction
Would you like to send metadata to Stripe when payments are processed through your form? If you’d like to add some tags that you can easily identify in your Stripe payments, you can send metadata as part of the payment through WPForms. In this tutorial, we’ll show you the PHP snippet you need in order to achieve this.
Creating the form
First, you’ll need to create your form and add the Credit Card form field. You’ll also need to set up your form to accept Stripe payments.
If you need assistance in setting up Stripe on your form, please review this documentation.
Adding the snippet to send metadata to stripe
Now it’s time to add the snippet to your site that will send through the metadata when Stripe payments are processed.
If you need any assistance in how to add snippets to your site, please see this tutorial.
Adding for both single payments and initial subscription payments
/** * Send metadata to Stripe when payment is processed for single payments and initial subscription payments. * * @link https://wpforms.com/developers/how-to-send-metadata-to-stripe-payments/ */ function wpf_stripe_single_payment_and_initial_subscription_payment_custom_metadata( $fields, $form_data, $entry_id, $payment ) { if ( ! ( $payment instanceof \WPForms\Vendor\Stripe\PaymentIntent ) && ! ( $payment instanceof \WPForms\Vendor\Stripe\Charge ) ) { return; } // Check if the field with id equals 3 value exists and assign it. if ( isset( $fields[3][ 'value' ] ) ) { $payment->metadata[ 'something' ] = $fields[3][ 'value' ]; } // Assign fixed value (no additional checks required). $payment->metadata[ 'key' ] = 'some value for single payment'; $payment->save(); } add_action( 'wpforms_stripe_process_complete', 'wpf_stripe_single_payment_and_initial_subscription_payment_custom_metadata', 10, 4 );
In the above code snippet, we’re sending through two forms of metadata. One is assigned from a specific form field with $fields[3][ 'value' ]
. This means that whatever value is entered in the form field that has the ID of 3, we’ll grab that and send it to Stripe.
The metadata[ 'key' ] = 'some value for single payment';
is also being sent but rather than grab some value from the form that was filled in by the user, we’re actually setting this ourselves in the code itself.
You’ll need to remember to change each of those for what you need in your own snippet. For example, you’ll need to change the field ID 3 to match the field ID you want to send as well as the text of some value for single payment.
If you need help in where to find your field IDs or if you wanted to target a specific form and need help finding the form ID, please review this tutorial.
Adding for single payments only
/** * Send metadata to Stripe when payment is processed for single payments only. * * @link https://wpforms.com/developers/how-to-send-metadata-to-stripe-payments/ */ function wpf_stripe_single_payment_custom_metadata( $fields, $form_data, $entry_id, $payment, $subscription ) { if ( ! empty( $subscription->id ) ) { return; } if ( ! ( $payment instanceof \WPForms\Vendor\Stripe\PaymentIntent ) && ! ( $payment instanceof \WPForms\Vendor\Stripe\Charge ) ) { return; } // Check if the field with id equals 3 value exists and assign it. if ( isset( $fields[3][ 'value' ] ) ) { $payment->metadata[ 'something' ] = $fields[3][ 'value' ]; } // Assign fixed value (no additional checks required). $payment->metadata[ 'key' ] = 'some value for single payment'; $payment->save(); } add_action( 'wpforms_stripe_process_complete', 'wpf_stripe_single_payment_custom_metadata', 10, 5 );
Adding for subscription payments only (excluding single payments or initial subscription payments)
/** * Send metadata to Stripe when payment is processed for subscription payments (excluding single payments and initial subscription payments). * * @link https://wpforms.com/developers/how-to-send-metadata-to-stripe-payments/ */ function wpf_stripe_subscription_custom_metadata( $fields, $form_data, $entry_id, $payment, $subscription ) { if ( ! ( $subscription instanceof \WPForms\Vendor\Stripe\Subscription ) ) { return; } // Check if the field with id equals 3 value exists and assign it. if ( isset( $fields[3][ 'value' ] ) ) { $subscription->metadata[ 'something' ] = $fields[3][ 'value' ]; } // Assign fixed value (no additional checks required). $subscription->metadata[ 'key' ] = 'some value for subscription'; $subscription->save(); } add_action( 'wpforms_stripe_process_complete', 'wpf_stripe_subscription_custom_metadata', 10, 5 );
And that’s it! You’ve successfully sent metadata to Stripe. Would you like to change the sublabels on the Credit Card field? Take a look at our article on How to Change Sublabels for the Credit Card Field.
Related
Action Reference: wpforms_stripe_process_complete
FAQ
Q: How can I send more than one field over and target a specific form?
A: To target one form and send multiple fields of metadata over, your code snippet can look like this.
/** * Send metadata to Stripe when payment is processed based on form ID. * * @link https://wpforms.com/developers/how-to-send-metadata-to-stripe-payments/ */ function wpf_stripe_single_payment_and_initial_subscription_payment_custom_metadata_multiple( $fields, $form_data, $entry_id, $payment ) { // Only run this code on form ID 2614 if ( absint( $form_data[ 'id' ] ) !== 2614 ) { return; } if ( ! ( $payment instanceof \WPForms\Vendor\Stripe\PaymentIntent ) && ! ( $payment instanceof \WPForms\Vendor\Stripe\Charge ) ) { return; } // Capture the value listed in the field ID 0 if ( isset( $fields[0][ 'value' ] ) ) { $payment->metadata[ 'Name' ] = $fields[0][ 'value' ]; } // Capture the value listed in the field ID 1 if ( isset( $fields[1][ 'value' ] ) ) { $payment->metadata[ 'Email' ] = $fields[1][ 'value' ]; } // Capture the value listed in the field ID 2 if ( isset( $fields[2][ 'value' ] ) ) { $payment->metadata[ 'Phone' ] = $fields[2][ 'value' ]; } // Capture the value listed in the field ID 3 if ( isset( $fields[3][ 'value' ] ) ) { $payment->metadata[ 'Address' ] = $fields[3][ 'value' ]; } $payment->save(); } add_action( 'wpforms_stripe_process_complete', 'wpf_stripe_single_payment_and_initial_subscription_payment_custom_metadata_multiple', 10, 4 );
Just remember to change the form and field ID numbers to match your own IDs.