Laravelのドキュメントは比較的わかりやすいものが多いですが、よりわかりやすい資料があったため、参考になりそうだと思いまとめました。
公式の資料
Laravel 5.6 Laravel Cashier
stripeの簡単な仕組み
stripeを使いユーザーがカード情報を入力すると、ajaxでstripeのサーバにリクエストを送ります。
カードのバリデーションなどはstripe側で実施され、stripe側からtokenが返されるようになっています。
実装側ではこのtokenを使って処理を行うので、カード情報を持つ必要がなく安全に処理が行えます。
stripeの導入
Stripe Payments in Laravel: The Ultimate Guide
全て英語ですがlaravelのプロジェクト作成から、stripeの導入、cashierの導入まで丁寧に記載されています。
コマンドの内容もどのファイルにサンプルコードを記載するかも書かれているため、これさえあれば問題なく実装できると思います。
英語あまり読めないのですが、グーグル翻訳でページごと翻訳すれば問題なく読み進められました。。。
同じサイトにpaypalもあったので載せておきます。
PayPal payments in Laravel: The Ultimate Guide
ガイド通りに進めて出たエラー
自分の見落としだったら申し訳ないのですが、いくつかエラーが出たので、対処内容についてまとめました。
laravel/cashierをinstall時のエラー
ガイド通りに進めて行くと、単発での決済の実装後にサブスクリプションの実装を行います。
$ composer require stripe/stripe-php
Using version ^6.10 for stripe/stripe-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing stripe/stripe-php (v6.10.0): Loading from cache
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
上記のコマンドでstripe-phpをインストールした後、下記のコマンドでサブスクリプション用にcashierをインストールするとエラーが発生しました。
$ composer require "laravel/cashier":"~7.0"
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- laravel/cashier v7.1.2 requires stripe/stripe-php ~5.0 -> satisfiable by stripe/stripe-php[v5.0.0, v5.1.0, v5.1.1, v5.1.2, v5.1.3, v5.2.0, v5.2.1, v5.2.2, v5.2.3, v5.3.0, v5.4.0, v5.5.0, v5.5.1, v5.6.0, v5.7.0, v5.8.0, v5.9.0, v5.9.1, v5.9.2] but these conflict with your requirements or minimum-stability.
- laravel/cashier v7.1.1 requires stripe/stripe-php ~5.0 -> satisfiable by stripe/stripe-php[v5.0.0, v5.1.0, v5.1.1, v5.1.2, v5.1.3, v5.2.0, v5.2.1, v5.2.2, v5.2.3, v5.3.0, v5.4.0, v5.5.0, v5.5.1, v5.6.0, v5.7.0, v5.8.0, v5.9.0, v5.9.1, v5.9.2] but these conflict with your requirements or minimum-stability.
- laravel/cashier v7.1.0 requires stripe/stripe-php ~5.0 -> satisfiable by stripe/stripe-php[v5.0.0, v5.1.0, v5.1.1, v5.1.2, v5.1.3, v5.2.0, v5.2.1, v5.2.2, v5.2.3, v5.3.0, v5.4.0, v5.5.0, v5.5.1, v5.6.0, v5.7.0, v5.8.0, v5.9.0, v5.9.1, v5.9.2] but these conflict with your requirements or minimum-stability.
(省略)
stripe-phpが5.0までしか対応していないようでしたので、composer.jsonを下記のように変更して、再度cashierのインストールを行いました。
- "stripe/stripe-php": "^6.10"
+ "stripe/stripe-php": "^5.0"
$ composer update
$ composer require "laravel/cashier":"~7.0"
プランがなくてエラー
ガイドの中ではbronzeプランのサブスクリプションを行います。
$user->newSubscription('main', 'bronze')->create($request->stripeToken);
こちらもそのまま実行すると下記のエラーがブラウザに表示されました。
No such plan: bronze
こちらに関してはstripeのダッシュボードから商品を作成するか、APIでplanをあらかじめ作成しておけばOKです。
ダッシュボードから行う場合には、
Billing > 商品 の画面から新規に作成を行い、表品名、プランのニックネーム、idを全てbronzeとすることで、ガイドのコードのまま処理が完了させることができました。
APIで作成する場合にも、下記のExample Requestをそのまま実行すれば問題なく動きました。
https://stripe.com/docs/api/php#create_plan
先ほどのガイドに関してはこれで以上です。
公式のAPIリファレンス
公式の情報で、先ほどのguideには乗っていなかったplanの作成をこちらを参考にして進めました。
stripe api API Reference
中の人が書いてるQiita
サブスクリプションの仕組みやよくあるQAなど説明してくれています。
Stripe Subscription (定期支払い)101 - Part 1