这是一个可以在本地运行的模拟Stripe服务器,用于测试目的。
项目描述
这是一个可以在本地运行的模拟Stripe服务器,用于测试目的。
这是一个您可以在本地启动的程序,用于模拟Stripe服务器,而无需触及真实的Stripe服务器或互联网。
与像 stripe-mock 这样的其他测试/模拟软件不同,localstripe是 有状态的:它跟踪执行的操作(创建客户、添加卡等),以便这些操作对后续查询产生影响。
目标是拥有一个现成的模拟服务器,用于端到端测试任何应用程序。
功能
支持任何语言:localstripe不是一个您要包含的库,而是一个真正的服务器,您可以使用常规Stripe API请求在 http://localhost:8420 查询
有状态:如果您创建一个Stripe对象(比如说,一个客户),您将在未来的请求中再次获得它
与Stripe Elements集成:localstripe包含一个JavaScript文件,可以模拟任何网页上的Stripe Elements,允许您在您的网页上从模拟服务器创建标记
支持自定义API路由的webhooks
限制
只支持 Stripe API的最新版本(尽力而为)
废弃的属性和功能可能支持也可能不支持
无Stripe Connect支持:localstripe目前仅支持Stripe支付,不支持Stripe Connect
开始使用
安装localstripe
pip install --user -U localstripe
# or, to install globally:
sudo pip install localstripe
然后只需运行命令 localstripe。模拟Stripe服务器现在正在8420端口监听。
或者使用Docker镜像启动容器
docker run -p 8420:8420 adrienverge/localstripe:latest
可以使用以下方式重建Docker镜像
docker build --no-cache -t adrienverge/localstripe -<<EOF
FROM python:3
RUN pip install localstripe
CMD ["localstripe"]
EOF
示例
在下面的示例中,让我们创建一个Plan、一个Customer,并将后者订阅到前者
curl localhost:8420/v1/plans -u sk_test_12345: \
-d id=pro-plan \
-d amount=2500 \
-d currency=eur \
-d interval=month \
-d name="Plan for professionals"
{
"amount": 2500,
"created": 1504187388,
"currency": "eur",
"id": "pro-plan",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"name": "Plan for professionals",
"object": "plan",
"statement_descriptor": null,
"trial_period_days": null
}
curl localhost:8420/v1/customers -u sk_test_12345: \
-d description="Customer for david.anderson@example.com"
{
"id": "cus_b3IecP7GlNCPMM",
"description": "Customer for david.anderson@example.com",
"account_balance": 0,
"currency": "eur",
"default_source": null,
...
}
curl localhost:8420/v1/subscriptions -u sk_test_12345: \
-d customer=cus_b3IecP7GlNCPMM \
-d items[0][plan]=pro-plan
{
"id": "sub_UJIdAleo3FnwG7",
"customer": "cus_b3IecP7GlNCPMM",
"current_period_end": 1506779564,
"current_period_start": 1504187564,
"items": {
...
}
现在,如果您再次检索该客户,它将有一个相关的订阅
curl localhost:8420/v1/customers/cus_b3IecP7GlNCPMM -u sk_test_12345:
{
"id": "cus_b3IecP7GlNCPMM",
"description": "Customer for david.anderson@example.com",
...
"subscriptions": {
"data": [
{
"id": "sub_UJIdAleo3FnwG7",
"items": {
"data": [
{
"id": "si_2y5q9Q6lvAB9cr",
"plan": {
"id": "pro-plan",
"name": "Plan for professionals",
"amount": 2500,
"currency": "eur",
"interval": "month",
...
}
与后端集成
例如,在Python应用程序中,您只需将stripe.api_base设置为http://localhost:8420
import stripe
stripe.api_key = 'sk_test_12345'
stripe.api_base = 'http://localhost:8420'
与Stripe Elements集成
如果您的应用程序在网页中使用Stripe Elements接收卡号,您可能希望将令牌发送到模拟服务器而不是真实Stripe服务器。
要实现这一点,您需要在您的页面中加载http://localhost:8420/js.stripe.com/v3/脚本。它将覆盖全局Stripe对象,因此新元素和卡表单实际上会向http://localhost:8420/v1/tokens API发送数据。
例如,如果您使用Protractor这样的测试工具,您需要在创建卡元素之前在网页中注入此JavaScript源代码
<script src="http://localhost:8420/js.stripe.com/v3/"></script>
使用webhooks
使用特殊路由/_config注册webhook
curl localhost:8420/_config/webhooks/mywebhook1 \
-d url=http://localhost:8888/api/url -d secret=whsec_s3cr3t
然后,localstripe将使用secret签名向此URL发送webhooks。可以使用events选项筛选要发送的事件。目前仅支持以下事件类型
产品:product.created
计划:plan.created
客户:customer.created、customer.updated和customer.deleted
来源:customer.source.created
订阅:customer.subscription.created和customer.subscription.deleted
发票:invoice.created、invoice.payment_succeeded和invoice.payment_failed
清除存储的数据
使用程序清除数据可以非常有用,如果您在使用任何测试框架时使用localstripe。
可以使用DELETE HTTP方法通过/_config/data路由清除存储的数据
curl -X DELETE localhost:8420/_config/data
黑客和贡献
为了快速从源运行localstripe并在文件更改时重新加载
find -name '*.py' | entr -r python -m localstripe --from-scratch
为了快速从源构建并运行localstripe
python -m build
pip install --user --upgrade dist/localstripe-*.tar.gz
localstripe
如果您计划提交一个改进localstripe的pull请求,那真是太酷了!为了使审查过程顺利,您应遵循我们的贡献指南。
许可证
本程序根据GNU通用公共许可证版本3授权。
项目详情
下载文件
下载适用于您平台的应用程序文件。如果您不确定选择哪个,请了解更多关于安装包的信息。