如何将Stripe测试环境数据复制到正式生产环境
Stripe测试环境数据迁移到正式环境解决方案
以下分别提供Dashboard手动操作和脚本自动迁移两种实现方式:
方案1:通过Stripe Dashboard手动迁移
- 登录Stripe Dashboard,点击右上角开关切换到测试模式
- 进入「产品」页面,勾选所有需要迁移的测试商品,点击页面顶部的「导出」按钮,选择导出为CSV格式
- 再次点击右上角开关,切换到正式模式
- 进入正式环境的「产品」页面,点击「导入」按钮,上传之前导出的CSV文件,按照页面提示校验字段映射关系,确认后提交即可完成迁移
- 如果需要同步优惠券、订阅配置、税率规则等附属数据,在对应功能页面重复上述导出/导入操作即可
方案2:通过脚本批量自动迁移
可以使用Stripe官方SDK实现数据批量拉取和写入,以下为Python版本的示例:
- 先安装Stripe Python SDK:
pip install stripe - 编写执行以下脚本,逻辑为拉取测试环境商品&价格数据,再写入正式环境:
import stripe # 替换为你的对应环境Secret Key TEST_SECRET_KEY = "sk_test_xxx" LIVE_SECRET_KEY = "sk_live_xxx" # 拉取测试环境数据 stripe.api_key = TEST_SECRET_KEY test_products = stripe.Product.list(limit=100) test_prices = stripe.Price.list(limit=100) # 若数据量超过100条,需自行处理分页逻辑 # 写入正式环境 stripe.api_key = LIVE_SECRET_KEY prod_id_mapping = {} # 先创建商品 for test_prod in test_products: new_prod = stripe.Product.create( name=test_prod.name, description=test_prod.description, images=test_prod.images, metadata=test_prod.metadata, active=test_prod.active ) prod_id_mapping[test_prod.id] = new_prod.id # 再创建商品对应的价格配置 for test_price in test_prices: if test_price.product in prod_id_mapping: stripe.Price.create( product=prod_id_mapping[test_price.product], unit_amount=test_price.unit_amount, currency=test_price.currency, recurring=test_price.recurring, metadata=test_price.metadata, active=test_price.active )
- 执行前先校验拉取的测试数据数量是否和后台一致,建议先注释正式环境写入逻辑做试运行,确认没问题再全量执行
- 若需要迁移客户、优惠券、订阅计划等其他类型数据,参考上述逻辑调用对应资源的
list和create方法即可
内容的提问来源于stack exchange,提问作者luky
相关产品推荐
相关产品推荐

