Created
December 13, 2018 05:36
-
-
Save mrubiosan/ec0a848f4e49dc79f0298a674fe968c4 to your computer and use it in GitHub Desktop.
Migrate Stripe Products and Plans
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$fromToken = 'rk_live_abc'; | |
$toToken = 'rk_test_def'; | |
$prodContext = stream_context_create([ | |
'http' => [ | |
'header' => "Authorization: Bearer $fromToken\r\n" | |
] | |
]); | |
$testContext = stream_context_create([ | |
'http' => [ | |
'method' => 'POST', | |
'header' => "Authorization: Bearer $toToken\r\nContent-type: application/x-www-form-urlencoded\r\n" | |
] | |
]); | |
$products = []; | |
$qs = ''; | |
do { | |
$result = file_get_contents('https://api.stripe.com/v1/products?active=true'.$qs, false, $prodContext); | |
$decoded = json_decode($result, true); | |
$products = array_merge($products, $decoded['data']); | |
$lastProduct = end($products); | |
$qs = $lastProduct ? '&starting_after='.urlencode($lastProduct['id']) : ''; | |
} while(!empty($decoded['has_more'])); | |
foreach ($products as $product) { | |
$fields = ['id', 'name', 'type','statement_descriptor','unit_label', 'active']; | |
$payload = array_intersect_key($product, array_flip($fields)); | |
array_walk($payload, function(&$val) { | |
if (is_bool($val)) { $val = $val ? 'true' : 'false';} | |
}); | |
$encodedPayload = http_build_query($payload); | |
$ch = curl_init('https://api.stripe.com/v1/products'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
"Authorization: Bearer $toToken" | |
]); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedPayload); | |
var_dump(curl_exec($ch)); | |
} | |
$plans = []; | |
$qs = ''; | |
do { | |
$result = file_get_contents('https://api.stripe.com/v1/plans?active=true&limit=100'.$qs, false, $prodContext); | |
$decoded = json_decode($result, true); | |
$plans = array_merge($plans, $decoded['data']); | |
$lastProduct = end($plans); | |
$qs = $lastProduct ? '&starting_after='.urlencode($lastProduct['id']) : ''; | |
} while(!empty($decoded['has_more'])); | |
function is_not_null($val) { | |
return $val !== null; | |
} | |
foreach ($plans as $product) { | |
$fields = ['id', 'currency', 'interval','product','active', 'amount', 'billing_scheme', 'interval_count', 'nickname', 'tiers', 'tiers_mode', 'trial_period_days', 'usage_type']; | |
$payload = array_intersect_key($product, array_flip($fields)); | |
$payload = array_filter($payload, 'is_not_null'); | |
array_walk($payload, function(&$val) { | |
if (is_bool($val)) { $val = $val ? 'true' : 'false';} | |
}); | |
if (!empty($payload['tiers'])) { | |
foreach($payload['tiers'] as &$val) { | |
$val['unit_amount'] = $val['amount']; | |
unset($val['amount']); | |
if (!isset($val['up_to'])) { | |
$val['up_to'] = 'inf'; | |
} | |
$val = array_filter($val, 'is_not_null'); | |
} | |
} | |
$encodedPayload = http_build_query($payload); | |
$ch = curl_init('https://api.stripe.com/v1/plans'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
"Authorization: Bearer $toToken" | |
]); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedPayload); | |
var_dump(curl_exec($ch)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment