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.

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 \Stripe\PaymentIntent ) && ! ( $payment instanceof \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']. 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

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 \Stripe\PaymentIntent ) && ! ( $payment instanceof \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 \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.

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 ) {
	if ( absint( $form_data['id'] ) !== 2614 ) {
		return;
	}
	if ( ! ( $payment instanceof \Stripe\PaymentIntent ) && ! ( $payment instanceof \Stripe\Charge ) ) {
		return;
	}
	if ( isset( $fields[0]['value'] ) ) {
		$payment->metadata['Name'] = $fields[0]['value'];
	}
	if ( isset( $fields[1]['value'] ) ) {
		$payment->metadata['Email'] = $fields[1]['value'];
	}
	if ( isset( $fields[2]['value'] ) ) {
		$payment->metadata['Phone'] = $fields[2]['value'];
	}
	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 );

you can send metadata to stripe with multiple fields as well as for only a specific form using this code snippet

Just remember to change the form and field ID numbers to match your own IDs.