dominservice / laravel-stripe
Laravel 对 Stripe 的集成。
1.2.3
2024-03-08 10:49 UTC
Requires
- php: ^8.1
- ext-json: *
- dominservice/data_locale_parser: ^1.0
- illuminate/console: ^10.0
- illuminate/contracts: ^10.0
- illuminate/database: ^10.0
- illuminate/http: ^10.0
- illuminate/queue: ^10.0
- illuminate/routing: ^10.0
- illuminate/support: ^10.0
- psr/log: ^3.0
- stripe/stripe-php: ^13.10
README
laravel 10+ 和 PHP 8.1+ 的 Stripe 集成
安装
composer require dominservice/laravel-stripe
在 .env
中添加您的 Stripe 凭据
STRIPE_KEY=pk_live_XxxXXxXXX
STRIPE_SECRET=sk_live_XxxXXxXXX
STRIPE_WEBHOOK_CHECKOUT=whsec_XxxXXxXXX
发布配置
php artisan vendor:publish --tag=stripe
发布迁移
php artisan vendor:publish --tag=stripe-migrations
配置
在配置文件中,您可以更改允许的货币列表。检查您的货币是否在此列表中,如果不是,请添加它,否则您将无法使用此包。
同时检查您的 User 模型是否与 config/stripe.php
文件中指定的模型相同
用法
路由
您必须将中间件 stripe.verify:{name}
绑定到具有 支付 webhook 引用的路由,以确保路径安全。
Route::group(['middleware' => ['stripe.verify:checkout'], 'namespace' => '\App\Http\Controllers'], function () { Route::get('/webhook/payments', 'WebhookController@payments'); (...)
{name}
表示应使用哪个 webhook 密钥。在上面的例子中,它是 checkout
。
这意味着将使用 config("stripe.webhooks.signing_secrets.checkout")
来验证 webhook。
代码
use Dominservice\LaraStripe\Client as StripeClient; (...) $stripe = (new StripeClient()); // Create product with price $product = \App\Models\Product::find(1); $productStripe = $stripe->products() ->setName($product->name) ->setActive(true) ->setExtendPricesCurrency('pln') ->setExtendPricesUnitAmount((float)$amount) ->setExtendPricesBillingScheme('per_unit') ->setExtendPricesRecurring(['interval' => 'month']) ->create($product); // Create customer $customer = $stripe->customers() ->setName($user->name) ->setEmail($user->email) ->setPhone($user->phone) ->setAddress([ 'country' => 'PL', 'city' => 'Warszawa', 'postal_code' => '00-000', 'line1' => 'ul. kopernika 1/2', ]) ->create($user); // Checkout session $session = $stripe->checkoutSessions() ->setSuccessUrl(route('payment.afterTransaction', $order->ulid) . '?session_id={CHECKOUT_SESSION_ID}') ->setCancelUrl(route('payment.canceled', $order->ulid)) ->setMode('subscription') ->setClientReferenceId($order->ulid) ->setCustomer($customer->id) ->setLineItems([ [ 'price' => $productStripe->default_price->id, 'quantity' => 1, ] ]) ->create();
待续... ;)
此存储库尚未完成