diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index 4040eff2..f295e407 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/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(); + } } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 470e92eb..76a2b29d 100644 --- a/app/Models/Payment.php +++ b/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); } } diff --git a/app/Models/PaypalProduct.php b/app/Models/PaypalProduct.php index a0bfaeec..c39c0f8e 100644 --- a/app/Models/PaypalProduct.php +++ b/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); diff --git a/database/factories/PaymentFactory.php b/database/factories/PaymentFactory.php index f3d6e9cc..ad85e725 100644 --- a/database/factories/PaymentFactory.php +++ b/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' => '{}', ]; } diff --git a/database/migrations/2021_06_23_090026_update_price_to_payments_table.php b/database/migrations/2021_06_23_090026_update_price_to_payments_table.php new file mode 100644 index 00000000..a3bb56af --- /dev/null +++ b/database/migrations/2021_06_23_090026_update_price_to_payments_table.php @@ -0,0 +1,32 @@ +decimal('price')->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('payments', function (Blueprint $table) { + $table->string('price')->change()->nullable();; + }); + } +} diff --git a/database/migrations/2021_06_23_090806_add__currency_code_to_payments_table.php b/database/migrations/2021_06_23_090806_add__currency_code_to_payments_table.php new file mode 100644 index 00000000..3e6710bb --- /dev/null +++ b/database/migrations/2021_06_23_090806_add__currency_code_to_payments_table.php @@ -0,0 +1,32 @@ +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'); + }); + } +} diff --git a/resources/views/admin/payments/index.blade.php b/resources/views/admin/payments/index.blade.php index af3eee38..d22b2788 100644 --- a/resources/views/admin/payments/index.blade.php +++ b/resources/views/admin/payments/index.blade.php @@ -28,48 +28,55 @@
Payments
-
- +
+
- - - - - - - - - - + + + + + + + + + + - @foreach($payments as $payment) - - - - - - - - - - - @endforeach
IDUserTypeAmountPricePayment_IDPayer_IDCreated at
IDUserTypeAmountPricePayment_IDPayer_IDCreated at
{{$payment->id}}{{$payment->User->name}}{{$payment->type}}{{$payment->amount}}€{{$payment->Price()}}{{$payment->payment_id}}{{$payment->payer_id}}{{$payment->created_at->diffForHumans()}}
- -
- {!! $payments->links() !!} -
-
- + + + @endsection diff --git a/resources/views/mail/payment/confirmed.blade.php b/resources/views/mail/payment/confirmed.blade.php index 0f827a22..9e79855f 100644 --- a/resources/views/mail/payment/confirmed.blade.php +++ b/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}}** diff --git a/routes/web.php b/routes/web.php index fa6df30f..e0184fab 100644 --- a/routes/web.php +++ b/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');