Przeglądaj źródła

Add a transactions table and model

tags/v0.1.0
Immanuel Onyeka 3 lat temu
rodzic
commit
6c2aae5f5a
5 zmienionych plików z 78 dodań i 3 usunięć
  1. +18
    -3
      app/Http/Controllers/BillingController.php
  2. +16
    -0
      app/Models/Transaction.php
  3. +5
    -0
      app/Models/User.php
  4. +1
    -0
      database/migrations/2021_05_19_185302_create_orders_table.php
  5. +38
    -0
      database/migrations/2021_06_08_224029_create_transactions_table.php

+ 18
- 3
app/Http/Controllers/BillingController.php Wyświetl plik

@@ -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;


+ 16
- 0
app/Models/Transaction.php Wyświetl plik

@@ -0,0 +1,16 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;

class Transaction extends Model
{
use HasFactory;

public function user() {
return $this->belongsTo(User::class);
}
}

+ 5
- 0
app/Models/User.php Wyświetl plik

@@ -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);
}
}

+ 1
- 0
database/migrations/2021_05_19_185302_create_orders_table.php Wyświetl plik

@@ -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');


+ 38
- 0
database/migrations/2021_06_08_224029_create_transactions_table.php Wyświetl plik

@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTransactionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('transactions', function (Blueprint $table) {
$table->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');
}
}

Ładowanie…
Anuluj
Zapisz