Add custom fields to Stripe checkout information and send via webhook

Viewed 34

I am using wordpress to build a website that uses Stripe as its payment gateway.

Through the Paid Membership Pro plugin I have configured my Stripe connection.

The issue I am faced with is that I need to append a custom field, in this case a unique identifier for the customer, to the payment information for it to be available to send via my webhook to my server for processing.

I have come across a load of answers but none seem to be doing exactly what I am looking for.

I noticed in the PMPro plugin it allows you to add User Fields which I presumed would be a way to add custom data to the information but after checking the JSON payloads in Stripe none of the user field information is available.

I then tried adding the code from this answer to my functions.php file in word press just using a test meta key but again this information was not available in the payloads.

I am not using woocommerce.

How can I achieve this?

1 Answers

So I was able to crack this using a php script that intercepts the Stripe checkout event.

I added this as a snippet to my Wordpress site and to read in a custom field from the post submit.

Stripe allows you to add custom filed data to the metadata block and once populated this was successfully sent to my webhook.

function my_pmpro_stripe_params_send_user_id( $params, $order = null ) {
    global $my_pmpro_checkout_order;
    if ( empty( $params['metadata'] ) ) {
        $params['metadata'] = array();
    }

    if ( empty ( $order ) ) {
        // Save order while creating payment intent.
        $order = $my_pmpro_checkout_order;
    } else {
        // Use saved order while creating subscription.
        $my_pmpro_checkout_order = $order;
    }

    $params['metadata']['my_custom_field'] = $_POST['my_custom_value'];
    return $params;
}
add_filter( 'pmpro_stripe_payment_intent_params', 'my_pmpro_stripe_params_send_user_id', 10, 2 );
add_filter( 'pmpro_stripe_create_subscription_array', 'my_pmpro_stripe_params_send_user_id', 10, 1 );
Related