Skip to content

Instantly share code, notes, and snippets.

@dparker1005
Last active October 4, 2024 18:45
Show Gist options
  • Save dparker1005/aef8a67864cd4a5658cb0c14b6c03a6e to your computer and use it in GitHub Desktop.
Save dparker1005/aef8a67864cd4a5658cb0c14b6c03a6e to your computer and use it in GitHub Desktop.
Have users update their billing information in Stripe Customer Portal.
<?php
// Copy from below here...
/**
* Have users update their billing information in Stripe Customer Portal.
*
* NOTE: Before using Stripe Customer Portal, you must first enable it by saving the
* customer portal settings page here:
* https://dashboard.stripe.com/settings/billing/portal (For live mode)
* https://dashboard.stripe.com/test/settings/billing/portal (For test mode)
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
add_action( 'pmpro_billing_preheader', 'my_pmpro_billing_preheader_redirect_to_stripe' );
function my_pmpro_get_customer_portal_url( $customer_id ) {
try {
$session = \Stripe\BillingPortal\Session::create([
'customer' => $customer_id,
'return_url' => get_site_url(),
]);
return $session->url;
} catch ( Exception $e ) {
return '';
}
}
function my_pmpro_billing_preheader_redirect_to_stripe() {
global $besecure, $gateway, $show_paypal_link, $show_check_payment_instructions;
$user_order = new MemberOrder();
$user_order->getLastMemberOrder( null, array( 'success', 'pending' ) );
$stripe = new PMProGateway_stripe();
$customer = $stripe->get_customer_for_user( $user_order->user_id );
if ( empty( $customer->id ) ) {
return;
}
$customer_portal_url = my_pmpro_get_customer_portal_url( $customer->id );
if ( ! empty( $customer_portal_url ) ) {
wp_redirect( $customer_portal_url );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment