浏览代码

Some love to payments

Fixed a bug with the new payment changes
Improved the payments table and changes
AVMG20 4 年之前
父节点
当前提交
b445cf7003

+ 26 - 1
app/Http/Controllers/Admin/PaymentController.php

@@ -6,11 +6,14 @@ use App\Http\Controllers\Controller;
 use App\Models\Configuration;
 use App\Models\Payment;
 use App\Models\PaypalProduct;
+use App\Models\Product;
 use App\Models\User;
 use App\Notifications\ConfirmPaymentNotification;
+use Exception;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\View\Factory;
 use Illuminate\Contracts\View\View;
+use Illuminate\Http\JsonResponse;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -154,7 +157,7 @@ class PaymentController extends Controller
                     'type' => 'Credits',
                     'status' => $response->result->status,
                     'amount' => $paypalProduct->quantity,
-                    'price' => $paypalProduct->formatCurrency(),
+                    'price' => $paypalProduct->price,
                     'payer' => json_encode($response->result->payer),
                 ]);
 
@@ -192,4 +195,26 @@ class PaymentController extends Controller
     {
         return redirect()->route('store.index')->with('success', 'Payment Canceled');
     }
+
+
+    /**
+     * @return JsonResponse|mixed
+     * @throws Exception
+     */
+    public function dataTable()
+    {
+        $query = Payment::with('user');
+
+        return datatables($query)
+            ->editColumn('user', function (Payment $payment) {
+                return $payment->user->name;
+            })
+            ->editColumn('price', function (Payment $payment) {
+                return $payment->formatCurrency();
+            })
+            ->editColumn('created_at', function (Payment $payment) {
+                return $payment->created_at ? $payment->created_at->diffForHumans() : '';
+            })
+            ->make();
+    }
 }

+ 18 - 14
app/Models/Payment.php

@@ -6,6 +6,7 @@ use Hidehalo\Nanoid\Client;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use NumberFormatter;
 use Spatie\Activitylog\Traits\LogsActivity;
 
 class Payment extends Model
@@ -19,21 +20,22 @@ class Payment extends Model
      * @var string[]
      */
     protected $fillable = [
-       'id',
-      'user_id',
-      'payment_id',
-      'payer_id',
-      'payer',
-      'status',
-      'type',
-      'amount',
-      'price',
+        'id',
+        'user_id',
+        'payment_id',
+        'payer_id',
+        'payer',
+        'status',
+        'type',
+        'amount',
+        'price',
     ];
 
-    public static function boot() {
+    public static function boot()
+    {
         parent::boot();
 
-        static::creating(function(Payment $payment) {
+        static::creating(function (Payment $payment) {
             $client = new Client();
 
             $payment->{$payment->getKeyName()} = $client->generateId($size = 8);
@@ -43,12 +45,14 @@ class Payment extends Model
     /**
      * @return BelongsTo
      */
-    public function User(){
+    public function user()
+    {
         return $this->belongsTo(User::class);
     }
 
-    public function Price()
+    public function formatCurrency($locale = 'en_US')
     {
-        return number_format($this->price, 2, '.', '');
+        $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
+        return $formatter->formatCurrency($this->price, $this->currency_code);
     }
 }

+ 4 - 0
app/Models/PaypalProduct.php

@@ -39,6 +39,10 @@ class PaypalProduct extends Model
         });
     }
 
+    /**
+     * @param string $locale
+     * @return string
+     */
     public function formatCurrency($locale = 'en_US')
     {
         $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

+ 2 - 1
database/factories/PaymentFactory.php

@@ -30,7 +30,8 @@ class PaymentFactory extends Factory
             'type' => "Credits",
             'status' => "Completed",
             'amount' => $this->faker->numberBetween(10, 10000),
-            'price' => '€' . $this->faker->numerify('##.##'),
+            'price' => $this->faker->numerify('##.##'),
+            'currency_code' => ['EUR', 'USD'][rand(0,1)],
             'payer' => '{}',
         ];
     }

+ 32 - 0
database/migrations/2021_06_23_090026_update_price_to_payments_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class UpdatePriceToPaymentsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payments', function (Blueprint $table) {
+            $table->decimal('price')->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payments', function (Blueprint $table) {
+            $table->string('price')->change()->nullable();;
+        });
+    }
+}

+ 32 - 0
database/migrations/2021_06_23_090806_add__currency_code_to_payments_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddCurrencyCodeToPaymentsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('payments', function (Blueprint $table) {
+            $table->string('currency_code' , 3)->default('USD')->after('price');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('payments', function (Blueprint $table) {
+            $table->dropColumn('currency_code');
+        });
+    }
+}

+ 37 - 30
resources/views/admin/payments/index.blade.php

@@ -28,48 +28,55 @@
                 <div class="card-header">
                     <h5 class="card-title"><i class="fas fa-money-bill-wave mr-2"></i>Payments</h5>
                 </div>
-                <div class="card-body table-responsive">
 
-                    <table class="table table-striped">
+                <div class="card-body table-responsive">
+                    <table id="datatable" class="table table-striped">
                         <thead>
-                            <tr>
-                                <th>ID</th>
-                                <th>User</th>
-                                <th>Type</th>
-                                <th>Amount</th>
-                                <th>Price</th>
-                                <th>Payment_ID</th>
-                                <th>Payer_ID</th>
-                                <th>Created at</th>
-                            </tr>
+                        <tr>
+                            <th>ID</th>
+                            <th>User</th>
+                            <th>Type</th>
+                            <th>Amount</th>
+                            <th>Price</th>
+                            <th>Payment_ID</th>
+                            <th>Payer_ID</th>
+                            <th>Created at</th>
+                        </tr>
                         </thead>
                         <tbody>
-                            @foreach($payments as $payment)
-                                <tr>
-                                    <td>{{$payment->id}}</td>
-                                    <td>{{$payment->User->name}}</td>
-                                    <td>{{$payment->type}}</td>
-                                    <td><i class="fa fa-coins mr-2"></i>{{$payment->amount}}</td>
-                                    <td>€{{$payment->Price()}}</td>
-                                    <td>{{$payment->payment_id}}</td>
-                                    <td>{{$payment->payer_id}}</td>
-                                    <td>{{$payment->created_at->diffForHumans()}}</td>
-                                </tr>
-                            @endforeach
                         </tbody>
                     </table>
-
-                    <div class="float-right">
-                        {!!  $payments->links() !!}
-                    </div>
-
                 </div>
-            </div>
 
+            </div>
         </div>
         <!-- END CUSTOM CONTENT -->
         </div>
     </section>
     <!-- END CONTENT -->
 
+    <script>
+        document.addEventListener("DOMContentLoaded", function () {
+            $('#datatable').DataTable({
+                processing: true,
+                serverSide: true,
+                stateSave: true,
+                ajax: "{{route('admin.payments.datatable')}}",
+                columns: [
+                    {data: 'id' , name : 'payments.id'},
+                    {data: 'user', sortable: false},
+                    {data: 'type'},
+                    {data: 'amount'},
+                    {data: 'price'},
+                    {data: 'payment_id'},
+                    {data: 'payer_id'},
+                    {data: 'created_at'},
+                ],
+                fnDrawCallback: function( oSettings ) {
+                    $('[data-toggle="popover"]').popover();
+                }
+            });
+        });
+    </script>
+
 @endsection

+ 1 - 1
resources/views/mail/payment/confirmed.blade.php

@@ -6,7 +6,7 @@ Your payment has been confirmed; Your credit balance has been updated.
 ___
 ### Payment ID: **{{$payment->id}}**
 ### Status:     **{{$payment->status}}**
-### Price:      **{{$payment->price}}**
+### Price:      **{{$payment->formatCurrency()}}**
 ### Type:       **{{$payment->type}}**
 ### Amount:     **{{$payment->amount}}**
 ### Balance:    **{{$payment->user->credits}}**

+ 2 - 0
routes/web.php

@@ -88,6 +88,7 @@ Route::middleware('auth')->group(function () {
             'store' => 'paypalProduct',
         ]);
 
+        Route::get('payments/datatable', [PaymentController::class, 'datatable'])->name('payments.datatable');
         Route::get('payments', [PaymentController::class, 'index'])->name('payments.index');
 
         Route::get('nodes/datatable', [NodeController::class, 'datatable'])->name('nodes.datatable');
@@ -101,6 +102,7 @@ Route::middleware('auth')->group(function () {
         Route::get('configurations/datatable', [ConfigurationController::class, 'datatable'])->name('configurations.datatable');
         Route::patch('configurations/updatevalue', [ConfigurationController::class, 'updatevalue'])->name('configurations.updatevalue');
         Route::resource('configurations', ConfigurationController::class);
+        Route::resource('configurations', ConfigurationController::class);
 
         Route::patch('settings/update/icons', [SettingsController::class , 'updateIcons'])->name('settings.update.icons');
         Route::resource('settings', SettingsController::class)->only('index');