Home / Admin / WP Simple Pay: Add User Meta After Payment
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Add User Meta After Payment

Use the payment email address to look up a WordPress user and add custom meta information.

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/qo9xxmjo/
 * 
 * @param \Stripe\Event $event Stripe Event.
 * @param \Stripe\Subscription|\Stripe\PaymentIntent $object Stripe Subscription or PaymentIntent
 */
function simpay_update_wp_user( $event, $object ) {
	$email_address = $object->customer->email;
	$wp_user = get_user_by( 'email', $email_address );
	// User cannot be found, do nothing.
	if ( false === $wp_user ) {
		return;
	}
	// Add the Subscription or PaymentIntent ID as User meta.
	update_user_meta( $wp_user->ID, 'txn_id', $object->id );
}
add_action( 'simpay_webhook_subscription_created', 'simpay_update_wp_user', 10, 2 );
add_action( 'simpay_webhook_payment_intent_succeeded', 'simpay_update_wp_user', 10, 2 );

Comments

Add a Comment