diff --git a/app/Http/Controllers/BillingController.php b/app/Http/Controllers/BillingController.php index 4d13b4e..cae4dfd 100644 --- a/app/Http/Controllers/BillingController.php +++ b/app/Http/Controllers/BillingController.php @@ -9,6 +9,7 @@ use Stripe\PaymentIntent; use Stripe\PaymentMethod; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Auth; +use App\Models\Transaction; class BillingController extends Controller { @@ -24,13 +25,27 @@ class BillingController extends Controller $request->packs[ 'credits50' ]*5499 + $request->packs[ 'credits100' ]*10999 + $request->packs[ 'credits1000' ]*101000; - // Pass customer and setup_future_usage values here. Maybe metadata can - // also hold pack information + $transaction = new Transaction; + $transaction->credits = $request->packs['credits10']*10 + + $request->packs['credits50']*50 + + $request->packs['credits[100]']*100 + + $request->packs['credits[1000]']*1000; + $transaction->credits_extra = + $request->packs['credits50']*5 + + $request->packs['credits[100]']*10 + + $request->packs['credits[1000]']*150; + + $transaction->user_id = Auth::user()->id; + $transaction->charge = $amount; + $transaction->status = 'processing'; + $transaction->completed = false; + $transaction->save(); + $intent = PaymentIntent::create([ 'amount' => $amount, 'currency' => 'usd', 'customer' => Auth::user()->customer_id, - 'metadata' => ['transaction_id' => 'accept_a_payment'] + 'metadata' => ['transaction_id' => $transaction->id] ]); return $intent->client_secret; diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php new file mode 100644 index 0000000..4725f7c --- /dev/null +++ b/app/Models/Transaction.php @@ -0,0 +1,16 @@ +belongsTo(User::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 26c62ae..3ccbc67 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use App\Models\Order; +use App\Models\Transaction; use Laravel\Cashier\Billable; class User extends Authenticatable implements MustVerifyEmail @@ -46,4 +47,8 @@ class User extends Authenticatable implements MustVerifyEmail public function orders() { return $this->hasMany(Order::class); } + + public function transactions() { + return $this->hasMany(Transaction::class); + } } diff --git a/database/migrations/2021_05_19_185302_create_orders_table.php b/database/migrations/2021_05_19_185302_create_orders_table.php index 64f59e7..0ecb202 100644 --- a/database/migrations/2021_05_19_185302_create_orders_table.php +++ b/database/migrations/2021_05_19_185302_create_orders_table.php @@ -19,6 +19,7 @@ class CreateOrdersTable extends Migration $table->foreignId('service_id')->constrained()->nullable(); $table->foreignId('user_id')->constrained(); $table->bigInteger('quantity'); + $table->integer('attempts')->default(0); $table->string('note')->default(''); $table->bigInteger('remaining')->default(0); $table->string('status'); diff --git a/database/migrations/2021_06_08_224029_create_transactions_table.php b/database/migrations/2021_06_08_224029_create_transactions_table.php new file mode 100644 index 0000000..647e4d4 --- /dev/null +++ b/database/migrations/2021_06_08_224029_create_transactions_table.php @@ -0,0 +1,38 @@ +id(); + $table->foreignId('user_id')->constrained(); + $table->bigInteger('charge'); + $table->integer('credits')->unsigned(); + $table->integer('credits_extra')->unsigned()->default(0); + $table->string('status'); + $table->boolean('completed'); + $table->string('note')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('transactions'); + } +}