Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dparker1005/0e51f3ba133a55a1884a18a4417d2691 to your computer and use it in GitHub Desktop.

Select an option

Save dparker1005/0e51f3ba133a55a1884a18a4417d2691 to your computer and use it in GitHub Desktop.
Remove application fees from all Stripe subscriptions.
<?php
/**
* Remove application fees from all Stripe subscriptions.
* In order to run this script, the pmpro_stripe_remove_application_fees URL parameter must be set.
* To reset the last processed subscription ID, the pmpro_stripe_remove_application_fees_reset URL parameter can also be set.
*
* ex. http://yoursite.com/?pmpro_stripe_remove_application_fees=1
*
* In order for this script to work, your site must either:
* - Be connected to the Stripe platform account that created the subscriptions, OR
* - Be completely disconnected from the platform account and use API keys.
*
* 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/
*/
function my_pmpro_stripe_remove_all_application_fees() {
// Only run for admins.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Check whether we should run.
if ( empty( $_GET['pmpro_stripe_remove_application_fees'] ) ) {
return;
}
// Set up Stripe.
new PMProGateway_stripe();
// Get the last subscription ID that was processed.
$last_subscription_id = get_option( 'my_pmpro_stripe_remove_all_application_fees_last_subscription_id', '' );
$args = array(
'limit' => 10, // Accepts a maximum of 100. Adjust as needed based on your hosting setup can handle.
);
if ( ! empty( $last_subscription_id && empty( $_REQUEST['pmpro_stripe_remove_application_fees_reset'] ) ) ) {
$args['starting_after'] = $last_subscription_id;
}
$subscriptions = \Stripe\Subscription::all( $args );
if ( empty( $subscriptions->data ) ) {
echo "All subscriptions have been processed.";
wp_die();
}
foreach ( $subscriptions->data as $subscription ) {
// Update the subscription to remove the application_fee_percent
try {
\Stripe\Subscription::update(
$subscription->id,
[
'application_fee_percent' => 0
]
);
echo "Updated subscription: " . esc_html( $subscription->id ) . "<br>";
} catch (Exception $e) {
echo "Error updating subscription " . esc_html( $subscription->id ) . ": " . esc_html( $e->getMessage() ) . "<br>";
}
update_option( 'my_pmpro_stripe_remove_all_application_fees_last_subscription_id', $subscription->id );
}
wp_die();
}
add_action( 'init', 'my_pmpro_stripe_remove_all_application_fees' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment