bkilshaw / ezstripe
通过依赖Stripe Checkout和账单门户轻松向您的Laravel应用程序添加订阅
Requires
- php: ^7.4
- illuminate/support: ~7
- stripe/stripe-php: ^7.39
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3|~4
- phpunit/phpunit: ^8.0
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2023-09-11 04:36:18 UTC
README
此包仍在开发中。预期会有许多重大更改。
EZStripe
通过依赖Stripe Checkout和账单门户轻松向您的Laravel应用程序添加订阅
简介
此README分为三个部分
所有步骤都应该包含在内。如果您遇到任何问题或有任何建议,请创建一个问题。
1. 安装
通过Composer
$ composer require bkilshaw/ezstripe
2. 配置您的应用程序
发布EZStripes资源
php artisan vendor:publish --tag=ezstripe.config --tag=ezstripe.views --force
运行包含的迁移以将 stripe_id
字段添加到您的 users
表中。
php artisan migrate
更新您的 VerifyCSRFToken.php
以绕过EZStripes端点的CSRF保护
protected $except = [ 'ezstripe/*', ];
通过在您的所有页面中添加包含的blade组件在 </body>
之前包含EZStripe JS
<x-ezstripe-javascript />
创建一个新的类 app\Http\Controllers\WebhookController.php
类,它扩展了EZStripe WebhookController
<?php namespace App\Http\Controllers; use bkilshaw\EZStripe\Http\Controllers\WebhookController as EZStripeController; class WebhookController extends EZStripeController { // }
在您的 routes/web.php
中创建一个路由,将 /ezstripe/webhooks
路由重定向到您新创建的 WebhookController
Route::post('ezstripe/webhooks', [App\Http\Controllers\WebhookController::class, 'webhooks'])->name('ezstripe.webhooks');
将以下环境变量添加到您的 .env
中。您可以在这里找到您的Stripe密钥
STRIPE_WEBHOOK_SECRET
将在几个步骤中生成,暂时可以忽略
所有三个URL由您决定。成功和取消URL通常返回到您的产品页面。账单门户返回URL可以指向您的用户首页/仪表板。
# Stripes Publishable key STRIPE_KEY=pk_* # Stripes Secret key STRIPE_SECRET=sk_* # Stripes Webhook Secret STRIPE_WEBHOOK_SECRET=whsec_* # The URL a user will be redirected to after they have successfully subscribed CHECKOUT_SUCCESS_URL= # The URL a user will be redirected to if they are in Stripe Checkout and hit 'cancel' or 'back' CHECKOUT_CANCEL_URL= # The URL a user will be redirected to after vising Stripes Billing Portal BILLING_PORTAL_RETURN_URL=
3. 配置Stripe
Webhooks
什么是webhooks?
Stripe使用webhooks在您的账户中发生事件时通知您的应用程序。
示例事件
- 客户创建
- 发票已支付
- 订阅创建
- 订阅即将到期
- 客户的信用卡即将到期
Stripe在其文档中很好地解释了Webhooks 这里。
Webhooks和EZStripe
为了使您的应用程序与Stripe的变化保持一致,您必须向Stripe提供一个端点来发送webhooks。您可以通过在Stripe中创建Webhook Endpoint来实现这一点:这里
在创建新端点时,URL应指向 https://yourdomain.tld/ezstripe/webhooks
,其中用您的实际域名替换 yourdomain.tld
。
在“要发送的事件”部分,选择您希望接收通知的事件,或者点击“接收所有事件
”。
注意
在Stripe中,某些操作可能会触发许多事件,每个事件都会调用您的webhook端点。最佳实践是仅发送您需要的Webhook。
一旦您创建了您的Webhook端点,将签名密钥
复制到您的.env文件中的STRIPE_WEBHOOK_SECRET
部分。
注意
如果您计划在本地测试您的应用程序,则需要配置webhook的本地端点。如果您运行Laravel Valet,则可以使用valet share
来启动ngrok会话,并使用Webhook端点中的转发地址(例如:https://dk38alk3a.ngrok.io/ezstripe/webhooks
)。每次重启ngrok隧道时,它都会生成一个新的域名,因此您需要更新Stripe中的Webhook端点。
使用ngrok的另一种方法是使用Stripe CLI
Stripe计费门户
要启用Stripe计费门户,请访问https://dashboard.stripe.com/settings/billing/portal。
启用以下选项
Billing History: Enable
Update Subscriptions: Enable
Cancel Subscriptions: Enable
在产品下,添加用户在更新他们的订阅时可以从中选择的产品和价格。
其余的设置由您决定。您的服务条款和隐私政策链接是必需的。如果您没有,请查看https://getterms.io/
使用方法
Stripe Checkout
将用户重定向到Stripe Checkout很简单。您只需要一个包含<form id='ezstripe'>
的表单,该表单包含一个name='price_id'
的输入。该price_id
是您从Stripe获取的其中一个价格ID,由EZStripe::products()
返回。
示例
<form id="ezstripe"> @csrf <select name="price_id"> @foreach(EZStripe::products() as $product) @foreach($product->prices as $price) {!-- EZStripe is designed for subscriptions, so we're excluding non-recurring prices here --} @if(isset($price->recurring)) <option value="{{ $price->id }}"> ${{ number_format($price->unit_amount/100,2) }} / {{ $price->recurring->interval }} - {{ $product->name }} </option> @endif @endforeach @endforeach </select> <button type="submit">Checkout</button> </form>
Stripe计费门户
将用户重定向到Stripe计费门户非常简单。您只需要一个指向ezstripe.billing_portal
路由的链接。当用户登录并点击链接时,他们将被重定向到Stripe计费门户。
<a href="{{ route('ezstripe.billing_portal') }}">Billing Portal</a>
处理Webhook
当用户首次添加到Stripe时,EZStripe会自动更新您的用户并添加他们的stripe_id。您希望处理的任何其他Webhook事件都可以通过向您的WebhookController添加额外的函数来完成。为了使EZStripe处理事件,您必须遵循EZStripe的函数命名约定:将事件类型首字母小写,并在前面加上handle
。
示例
Stripe Event: customer.created EZStripe Method: handleCustomerCreated(array $payload){} Stripe Event: charge.failed EZStripe Method: handleChargeFailed(array $payload){} Stripe Event: order.payment_failed EZStripe Method: handleOrderPaymentFailed(array $payload){} Stripe Event: subscription_schedule.expiring EZStripe Method: handleSubscriptionScheduleExpiring(array $payload){}
EZStripe提供以下方法与Stripe交互
// Returns a collection of your products in Stripe EZStripe::products(); // Return a collection of your products in Stripe, only if the stripe product_id matches one that's been passed in EZStripe::products(array $product_ids); // Accepts a Illuminate\Http\Request and will return a Stripe Checkout Session the client can use for the redirect EZStripe::checkout(Request $request); // Redirect the currently authorized user to Stripes Billing Portal if they have a stripe_id set EZStripe::billing_portal();
变更日志
有关最近更改的更多信息,请参阅变更日志。
测试
$ composer test
贡献
有关详细信息,请参阅contributing.md和待办事项列表。
安全
如果您发现任何与安全相关的问题,请通过作者邮箱联系,而不是使用问题跟踪器。
致谢
许可证
许可证。请参阅许可证文件以获取更多信息。