決済までのデータの流れ方

実現したいこと

決済機能の作成に関する事で質問をさせて頂きます。
・決済に関するテーブル(Payment)
・出品に関するテーブル(Post)
・決済履歴に関するテーブル(History)

1.出品の掲載ページから購入ボタンを押す'(posts/show)
2.決済ページに飛んで、決済を行う。(payments/create)
3.決済されると決済完了のbooleanに変更される。(/)
4.出品の方にも売却済みとなるbooleanに変更。(/)
5.決済履歴が作成される(histories/index)
()内は遷移されるページ

上記の解釈でデータを渡して決済機能を作成しているのですが、データの渡し方と遷移の仕方が

前提

出品(Post)と決済(Payment)のテーブル内容は下記です。

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id')->constrained(); $table->string('name'); $table->string('antique_type'); $table->string('image'); $table->decimal('price'); $table->string('old'); $table->text('explanation'); $table->boolean('is_finished')->default(false); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('posts'); } };
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('payments', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('post_id')->constrained(); $table->unsignedBigInteger('payment_method_id'); $table->boolean('payment_is_finished')->default(false); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('payments'); } };
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('histories', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('post_id')->constrained(); $table->unsignedBigInteger('payment_method_id'); $table->boolean('payment_is_finished'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('histories'); } };

エラー箇所

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'payment_method_id' cannot be null

該当のソースコード

posts/show.blade.php

1<div class="profile"> 2 <div class="profile-header"> 3 <p>掲載日:{{ $post['created_at']->format('Y年m月d日') }}</p> 4 </div> 5 <div class="profile-top"> 6 <p><img src="{{ asset('storage/' . $post['image']) }}" width="300"></p> 7 <div class="profile-id"> 8 <p>出品番号:{{ $post['id'] }}</p> 9 </div> 10 <div class="profile-name"> 11 <h2>{{ $post['name'] }}</h2> 12 </div> 13 <div class="profile-price"> 14 <h5>価格</h5> 15 <p>{{ number_format($post->price) }}円</p></br> 16 </div> 17 <div class="profile-user"> 18 <p>出品者:{{ $post->user->name }}</p> 19 </div> 20 <div class="profile-type"> 21 <p>品目:{{ $post['antique_type'] }}</p> 22 </div> 23 </div> 24 <div class="profile-old"> 25 <p>使用歴:{{ $post['old'] }}年</p> 26 </div> 27 <div class="profile-body"> 28 <h4>詳細</h4> 29 <p>{!! nl2br(e($post->explanation)) !!}</p> 30 </div> 31 <div class="profile-btn"> 32 <form onsubmit="return confirm('購入しますか?')" action="/payments" method="POST"> 33 @csrf 34 <input type="hidden" id="post_id" name="post_id" value="{{ $post->id }}" > 35 <input type="hidden" id="post_id" name="post_id" value="{{ $post->id }}" > 36 <input type="hidden" id="title" name="title" value="{{ $post->name }}" > 37 <button type="submit" class="btn btn-success">購入</button> 38 </form> 39 <button type="button" class="btn btn-primary" onClick="history.back()">戻る</button> 40 </div> 41 42 </div>

PaymentController

1<?php 2 3namespace App\Http\Controllers; 4use Illuminate\Support\Facades\Validator; 5use Stripe\Stripe; 6use Stripe\Customer; 7use Stripe\Charge; 8use App\Models\Post; 9use App\Models\User; 10use App\Models\Payment; 11use Illuminate\Http\Request; 12use Illuminate\Support\Facades\Auth; 13 14 15class PaymentController extends Controller 16{ 17 public function create() 18 { 19 return view('payment.create'); 20 } 21 public function store(Request $request) 22 { 23 //ユーザーIDを取得 24 $user_id = Auth::id(); 25 $payment = new Payment; 26 // フォームから送られてきたデータをそれぞれ代入 27 $payment->post_id = $request->post_id; 28 $payment->payment_method_id = $request->payment_method_id; 29 // データベースに保存 30 $payment->save(); 31 return redirect('/'); 32 } 33}

Payment.php

1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Factories\HasFactory; 6use Illuminate\Database\Eloquent\Model; 7 8class Payment extends Model 9{ 10 use HasFactory; 11 protected $fillable = ['id', "post_id", 'payment_method_id', 'payment_is_finished']; 12 13 public function post() { 14 return $this->belongsTo(Post::class); 15 } 16}

web.php

1//決済 2Route::resource('payment', App\Http\Controllers\PaymentController::class); 3Route::post('/payments', [App\Http\Controllers\PaymentController::class, 'store'])->name('paymentstore'); 4Route::post('/payment', [App\Http\Controllers\PaymentController::class, 'store'])->name('payment.store'); 5Route::get('/payments/{id}', [App\Http\Controllers\PaymentController::class, 'show'])->name('payment.show');

試したこと

実行してみたところ、payment_method_idがnullなのがエラーとなりました。
正直、payment_method_idに何が入るのかをはっきりしていないのですが、下記のサイトではトークンや顧客idが入ると書いてあるのですが、顧客id=user_idになるのでしょうか?
それともクレジットの情報がpayment_method_idに入ると説明をしているのでしょうか?
・テーブル作成の参考にしたサイト
Laravel PAY.JPで決済機能を実装

補足情報(FW/ツールのバージョンなど)

php8
laravel10

コメントを投稿

0 コメント