Skip to content

Instantly share code, notes, and snippets.

@tommy-ec
Created February 6, 2024 15:16
Show Gist options
  • Save tommy-ec/e5e6cd854c1dd9ae7a237117a4016f93 to your computer and use it in GitHub Desktop.
Save tommy-ec/e5e6cd854c1dd9ae7a237117a4016f93 to your computer and use it in GitHub Desktop.
PHP script to replicate Stripe's live environment products, plans, and prices to the test environment.
/*
* This script copies products, plans, and prices from Stripe's live environment to the test environment.
* It requires the Stripe PHP library (https://github.com/stripe/stripe-php).
*
* To use this script, replace $stripeLiveSecretKey and $stripeTestSecretKey with your own Stripe keys.
*
* Note: The script handles exceptions and avoids duplication by checking if the item already exists in the test environment before attempting to create it.
*/
// Using https://github.com/stripe/stripe-php
require_once __DIR__.'/vendor/autoload.php';
function stripeCopyLiveToTest($productionKey, $testKey) {
// Set Stripe to use the production API key
\Stripe\Stripe::setApiKey($productionKey);
try {
$products = \Stripe\Product::all(["limit" => 100])->data;
\Stripe\Stripe::setApiKey($testKey);
$productMapping = [];
foreach ($products as $product) {
// Check if the product already exists in the test environment
try {
$testProduct = \Stripe\Product::retrieve($product->id);
echo "Product already exists: {$testProduct->id}<br>";
} catch (\Exception $e) {
// Product does not exist, create it
$productData = $product->toArray();
unset($productData["livemode"], $productData["object"], $productData["created"], $productData["updated"], $productData["default_price"]);
$testProduct = \Stripe\Product::create($productData);
echo "Created Product: {$testProduct->id}<br>";
}
$productMapping[$product->id] = $testProduct->id;
}
} catch (Exception $e) {
return "Error copying products: " . $e->getMessage();
}
// Switch back to the production API key to retrieve plans
\Stripe\Stripe::setApiKey($productionKey);
try {
$plans = \Stripe\Plan::all(["limit" => 100])->data;
\Stripe\Stripe::setApiKey($testKey);
foreach ($plans as $plan) {
// Check if the plan already exists in the test environment
try {
$testPlan = \Stripe\Plan::retrieve($plan->id);
echo "Plan already exists: {$testPlan->id}<br>";
} catch (\Exception $e) {
// Plan does not exist, create it
$planData = $plan->toArray();
if(isset($planData["amount"]) && isset($planData["amount_decimal"])) {
unset($planData["amount"]);
}
unset($planData["livemode"], $planData["object"], $planData["created"], $planData["updated"]);
$planData["product"] = $productMapping[$plan->product];
$testPlan = \Stripe\Plan::create($planData);
echo "Created Plan: {$testPlan->id}<br>";
}
}
} catch (Exception $e) {
return "Error copying plans: " . $e->getMessage();
}
// Switch back to the production API key to retrieve prices
\Stripe\Stripe::setApiKey($productionKey);
try {
$prices = \Stripe\Price::all(["limit" => 100])->data;
\Stripe\Stripe::setApiKey($testKey);
foreach ($prices as $price) {
// Ensure the product for this price was copied to the test environment
if (array_key_exists($price->product, $productMapping)) {
$testProductId = $productMapping[$price->product];
// Retrieve all prices for the test product to check for existence
$testPrices = \Stripe\Price::all(["product" => $testProductId, "limit" => 100])->data;
$priceExists = false;
foreach ($testPrices as $testPrice) {
// Compare relevant attributes to determine if the price already exists
if ($testPrice->unit_amount === $price->unit_amount && $testPrice->currency === $price->currency) {
echo "Price already exists for product {$testProductId}: {$testPrice->id}<br>";
$priceExists = true;
break;
}
}
if (!$priceExists) {
// Price does not exist, create it
$priceData = $price->toArray();
if(isset($priceData["unit_amount"]) && isset($priceData["unit_amount_decimal"])) {
unset($priceData["unit_amount"]);
}
unset($priceData["id"], $priceData["type"], $priceData["livemode"], $priceData["object"], $priceData["created"], $priceData["updated"]);
// Ensure the product ID is mapped to the test environment's product ID
$priceData["product"] = $testProductId;
$testPrice = \Stripe\Price::create($priceData);
echo "Created Price: {$testPrice->id} for Product: {$testProductId}<br>";
}
}
}
} catch (Exception $e) {
return "Error copying prices: " . $e->getMessage();
}
return "Products, plans, and prices copied successfully.";
}
// Replace with your own Stripe keys
$stripeLiveSecretKey = "your-live-secret-key";
$stripeTestSecretKey = "your-test-secret-key";
echo stripeCopyLiveToTest($stripeLiveSecretKey, $stripeTestSecretKey);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment