在 belling-> 产品下的条带仪表板上创建新产品,例如 prod-1,将定价计划例如 plan-1(条带将为该计划生成 ID)添加到该产品(prod-1),同时添加定价计划在计费间隔下选择或自定义您想要的间隔。
现在让我们在 laravel 应用端工作。我会推荐使用 Laravel Cashier。
运行作曲家composer require laravel/cashier
更新您的 USER 迁移表:
Schema::table('users', function ($table) {
$table->string('stripe_id')->nullable()->collation('utf8mb4_bin');
$table->string('card_brand')->nullable();
$table->string('card_last_four', 4)->nullable();
$table->timestamp('trial_ends_at')->nullable();
});
Schema::create('subscriptions', function ($table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->string('name');
$table->string('stripe_id')->collation('utf8mb4_bin');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
更新您的用户模型:
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
在您的控制器中。 MyController.php:
use Cartalyst\Stripe\Laravel\Facades\Stripe;
use Cartalyst\Stripe\Exception\CardErrorException;
use Session;
use Auth;
public function store(Request $request)
{
$token = $_POST['stripeToken'];
$user = Auth::user();
try {
$user->newSubscription('prod-1', 'ID of plan-1')->create($token);
Session::flash('success', 'You are now a premium member');
return redirect()->back();
} catch (CardErrorException $e) {
return back()->withErrors('Error!'. $e->getMessage());
}
}
在你看来:
<form action="{{ route('subscribe')}}" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_0000000000000000000" // Your api key
data-image="/images/marketplace.png" // You can change this image to your image
data-name="My App Name"
data-description="Subscription for 1 weekly box"
data-amount="2000" //the price is in cents 2000 = 20.00
data-label="Sign Me Up!">
</script>
</form>
创建路线:
Route::POST('subscription', 'MyController@store')->name('susbcribe');
让我知道它是否有效。