Skip to content

Instantly share code, notes, and snippets.

@thomasfw
Last active December 22, 2016 10:29
Show Gist options
  • Save thomasfw/a9bc0fdee6f628d2db0e733cc22ff57e to your computer and use it in GitHub Desktop.
Save thomasfw/a9bc0fdee6f628d2db0e733cc22ff57e to your computer and use it in GitHub Desktop.
Restrict Content Pro: Update a member's billing cycle in Stripe when their subscription's expiration is changed in WP
<?php // https://github.com/thomasfw
// Add to your functions.php, or somewhere else..
// The 'rcp_set_expiration_date' hook is used instead of 'rcp_edit_member' so we can access the $old_date
add_action( 'rcp_set_expiration_date', 'my_update_stripe_billing_cycle', PHP_INT_MAX, 3 );
function my_update_stripe_billing_cycle( $member_id, $new_date, $old_date )
{
// Check/verify we're in the edit_member form (given the limitations on error output, changing the expiry elsewhere will not fire the request to Stripe)
if( !isset($_POST['rcp_edit_member_nonce']) || !wp_verify_nonce( $_POST['rcp_edit_member_nonce'], 'rcp_edit_member_nonce' ) ) return;
// Check Stripe is enabled
$gateways = new RCP_Payment_Gateways();
if ( !is_object($gateways) || !$gateways->is_gateway_enabled('stripe') ) return;
if( !class_exists( 'Stripe\Stripe' ) ) require_once RCP_PLUGIN_DIR . 'includes/libraries/stripe/init.php';
// Get the member
$member = new RCP_Member( $member_id );
if ( !is_object($member) || !$member->is_recurring() ) return;
// Send request to Stripe (if new date is not 'none')
// Also see https://stripe.com/docs/subscriptions/billing-cycle
$errors = array();
if ( $new_date !== 'none' )
{
// Get the merchant subscription ID, update the subscription
$new_date_unix = strtotime( strtok($new_date,' ') . ' 23:59:59' );
$merchant_sub_id = $member->get_merchant_subscription_id();
$subscription = \Stripe\Subscription::retrieve( $merchant_sub_id );
if( !is_object($subscription) || $subscription->status == 'canceled' ) return;
// Update the subscription
$subscription->trial_end = $new_date_unix;
$subscription->prorate = false;
try {
$subscription->save();
} catch (\Stripe\Error\Base $e) {
$errors[] = $e->getMessage();
} catch (Exception $e) {
$errors[] = $e->getMessage();
}
}
else
{
$errors[] = "The member has an auto-renewing subscription in Stripe, expiration date must be set to a valid future date in order to update their billing period - cannot be 'none'";
}
// Handle any errors
if( !empty($errors) )
{
// Remove this hook so we don't get an infinite loop, and set the member's expiry date back to $old_date
remove_action( 'rcp_set_expiration_date', 'ilts_update_stripe_billing_cycle', PHP_INT_MAX );
$str = '';
foreach ( $errors as $msg ) { $str .= $msg; };
$member->add_note( "Member\'s Stripe billing period could not be updated ($str)." );
$member->set_expiration_date( $old_date );
$str = "<strong>Failed to update the member's expiration date: </strong>" . $str;
$str .= "</p><a href='".home_url(add_query_arg( NULL, NULL ))."'>Try Again</a>";
wp_die( $str ); exit;
}
else
{
$member->add_note( sprintf("Member\'s Stripe billing period end date was set to %s.", date('Y-m-d H:i:s',$new_date_unix) ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment