How To Send Metadata to Stripe Payments

Overview

Would you like to send through 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.

Setup

First, you’ll need to add the Credit Card form field and set up your form to accept Stripe payments.

Add the credit card field to your form

If you need assistance in setting up Stripe on your form, please review this documentation.

Next, copy and paste this code snippet to your site.

/**
 * Send metadata to Stripe when payment is processed.
 *
 * @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 ) {
	if ( ! ( $payment instanceof \Stripe\PaymentIntent ) && ! ( $payment instanceof \Stripe\Charge ) ) {
		return;
	}
	// Check if the field with id equals 16 value exists and assign it.
	if ( isset( $fields[16]['value'] ) ) {
		$payment->metadata['something'] = $fields[16]['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, 4 );

In the above code snippet, we’re sending through two forms of metadata. One is assigned from a specific form field with $fields[16]['value']. And the other is specific with the metadata['key'] = 'some value for single payment';.

You’ll just need to remember to change each of those for what you need.

You now now see the metadata sent to Stripe when the payment is processed

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.

Action Reference: wpforms_stripe_process_complete

FAQ

Q: Will this work for recurring and single payments?

A: Absolutely! The code above will work for both single payments as well as the initial subscription payment (not the subscription itself).