Skip to content

Instantly share code, notes, and snippets.

@vikasprogrammer
Last active February 7, 2020 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikasprogrammer/81c8c43e92c3284812b1319b27c66d55 to your computer and use it in GitHub Desktop.
Save vikasprogrammer/81c8c43e92c3284812b1319b27c66d55 to your computer and use it in GitHub Desktop.
Copy stripe subscriptions after migration
<?php
/*
Author: Vikas Singhal (@vikasprogrammer)
Pre-reqs:
Create plans on target account with same plan id.
Description:
It simply copies over the old subscription in source account to new account mapping the customer and plans. It spits out the new sub_id which you can copy to EDD or whatever billing system you are using.
It can also cancel plans in the old account.
/*
<?php
$old_api_key = '';
$new_api_key = '';
$pretend = true;
$cancel_old_plans = false;
function start($starting_after = '') {
global $old_api_key, $new_api_key, $pretend, $cancel_old_plans;
if($starting_after != '') {
$starting_after = "--starting-after $starting_after";
}
$old_subs = json_decode(shell_exec("stripe --api-key $old_api_key subscriptions list --status active $starting_after"));
$count = count($old_subs->data);
echo "Found $count subs, hasmore? {$old_subs->has_more}\n";
foreach ($old_subs->data as $d) {
$trial_end = $d->current_period_end;
// echo $trial_end;
echo "Creating new sub for {$d->customer} plan {$d->plan->id} trial_end $trial_end ... ";
if($pretend != true) {
$sub = shell_exec("stripe --api-key $new_api_key subscriptions create --customer={$d->customer} -d \"items[0][plan]\"={$d->plan->id} -d trial_end=$trial_end");
error_log($sub);
$sub = json_decode($sub);
// var_dump($sub);
if(property_exists($sub, "error")) {
echo "Error .. \n";
var_dump($sub);
exit;
} else {
echo "done! ";
echo "new sub_id {$sub->id}\n";
}
} else {
echo "\n";
}
if($cancel_old_plans) {
json_decode(shell_exec("stripe --api-key $old_api_key subscriptions cancel {$d->id}"));
echo "old plan {$d->id} cancelled!\n";
// exit;
continue;
}
}
if ($old_subs->has_more) {
if($cancel_old_plans)
start(); //as we don't have the last object id anymore
else
start($d->id);
}
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment