From 9c22cce13bc84a7b3648820a0a08d439df65b094 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Sun, 28 Jan 2024 19:54:29 +0500 Subject: [PATCH] Working on credit card --- .../Controllers/OnlinePaymentController.php | 36 ++++++++++ app/Models/Order/Card/CardOrder.php | 19 +++++- app/Models/Payment/OnlinePaymentHistory.php | 46 +++++++++++++ app/Nova/Resources/Branch/Branch.php | 5 +- app/Nova/Resources/Order/Card/CardOrder.php | 25 +++++-- app/Providers/AppServiceProvider.php | 3 +- app/Repos/Payment/OnlinePaymentRepo.php | 67 +++++++++++++++---- config/logging.php | 7 ++ ...dd_payment_status_to_card_orders_table.php | 28 ++++++++ ..._create_online_payment_histories_table.php | 50 ++++++++++++++ routes/web.php | 3 + 11 files changed, 267 insertions(+), 22 deletions(-) create mode 100644 app/Http/Controllers/OnlinePaymentController.php create mode 100644 app/Models/Payment/OnlinePaymentHistory.php create mode 100644 database/migrations/2024_01_28_180935_add_payment_status_to_card_orders_table.php create mode 100644 database/migrations/2024_01_28_183735_create_online_payment_histories_table.php diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php new file mode 100644 index 0000000..b71d64e --- /dev/null +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -0,0 +1,36 @@ +all(); + + // $response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [ + // 'language' => 'ru', + // 'orderId' => $request->orderId, + // 'userName' => 301161000067, + // 'password' => 'E3vb2SR3dgTPdff' + // ]); + + // if ($response['depositAmount'] > 0) { + // $payment_history = OnlinePaymentHistory::where('orderId', $request->orderId)->first(); + + // $booking = Booking::where('id', $payment_history->online_paymantable_id)->update([ + // 'status' => Settings::PAID + // ]); + + // $payment_history->update([ + // 'paymentStatus' => Settings::PAID + // ]); + // } + } +} diff --git a/app/Models/Order/Card/CardOrder.php b/app/Models/Order/Card/CardOrder.php index ad36344..38dc4f7 100644 --- a/app/Models/Order/Card/CardOrder.php +++ b/app/Models/Order/Card/CardOrder.php @@ -47,6 +47,7 @@ class CardOrder extends Model 'passport_two', 'passport_three', 'passport_four', + 'paid', 'notes', 'user_id', ]; @@ -101,6 +102,22 @@ class CardOrder extends Model parent::boot(); static::creating(CardOrderRepo::creating()); - static::created(CardOrderRepo::created()); + } + + /** + * Check if order is paid + */ + public function isPaid(): bool + { + return $this->paid; + } + + /** + * Price for order + */ + public function priceAmount(): float + { + return 1; + // return floatval($this->cardType->price); } } diff --git a/app/Models/Payment/OnlinePaymentHistory.php b/app/Models/Payment/OnlinePaymentHistory.php new file mode 100644 index 0000000..8899e8f --- /dev/null +++ b/app/Models/Payment/OnlinePaymentHistory.php @@ -0,0 +1,46 @@ + + */ + protected $fillable = [ + 'refunded_amount', + 'booking_number', + 'amount', + 'depositedAmount', + 'orderNumber', + 'description', + 'orderId', + 'cardholderName', + 'pan', + 'approvalCode', + 'expiration', + 'formUrl', + 'successUrl', + 'errorUrl', + 'api_client', + 'paymentStatus', + 'callbackStatus', + 'username', + 'online_paymantable_id', + 'online_paymantable_type', + ]; +} diff --git a/app/Nova/Resources/Branch/Branch.php b/app/Nova/Resources/Branch/Branch.php index 096347a..4fc02e7 100644 --- a/app/Nova/Resources/Branch/Branch.php +++ b/app/Nova/Resources/Branch/Branch.php @@ -89,8 +89,9 @@ class Branch extends Resource Text::make(__('Billing username'), 'billing_username') ->rules('nullable', 'string', 'max:255'), - Password::make(__('Billing password'), 'billing_password') - ->rules('nullable', 'string', 'max:255'), + Text::make(__('Billing password'), 'billing_password') + ->rules('nullable', 'string', 'max:255') + ->hideFromIndex(), Textarea::make(__('Address'), 'address'), diff --git a/app/Nova/Resources/Order/Card/CardOrder.php b/app/Nova/Resources/Order/Card/CardOrder.php index 8129546..3224a35 100644 --- a/app/Nova/Resources/Order/Card/CardOrder.php +++ b/app/Nova/Resources/Order/Card/CardOrder.php @@ -10,6 +10,7 @@ use App\Nova\Forms\NovaForm; use App\Nova\Resource; use App\Nova\Resources\Order\Card\Concerns\CardOrderFieldsForDetail; use App\Nova\Resources\Order\Card\Concerns\CardOrderFieldsForIndex; +use App\Repos\Order\Card\CardOrderRepo; use App\Repos\Order\Card\CardStateRepo; use App\Repos\Order\Card\CardTypeRepo; use App\Repos\Order\OrderRepo; @@ -20,6 +21,7 @@ use App\Repos\System\Settings\Legal\PassportRepo; use App\Repos\System\Settings\Location\RegionRepo; use App\Rules\DowranAgaAllowed; use App\Rules\OnlyLetters; +use Illuminate\Database\Eloquent\Model; use Illuminate\Http\Request; use Illuminate\Support\Facades\Gate; use Laravel\Nova\Fields\Date; @@ -125,6 +127,16 @@ class CardOrder extends Resource return $query->where('user_id', $request->user()->id); } + /** + * After resource created + * @param Laravel\Nova\Http\Requests\NovaRequest $request + * @param Illuminate\Database\Eloquent\Model $model + */ + public static function afterCreate(NovaRequest $request, Model $model): void + { + CardOrderRepo::created()($model); + } + /** * Return the location to redirect the user after creation. * @@ -132,7 +144,11 @@ class CardOrder extends Resource */ public static function redirectAfterCreate(NovaRequest $request, $resource): URL|string { - return URL::remote((new OnlinePaymentRepo())->payCardOrder($resource)); + $payment = (new OnlinePaymentRepo())->payCardOrder($resource); + + return $payment['status'] === 'success' + ? URL::remote($payment['url']) + : sprintf('resources/%s/%s', static::uriKey(), $resource->getKey()); } /** @@ -143,10 +159,7 @@ class CardOrder extends Resource */ public static function redirectAfterUpdate(NovaRequest $request, $resource) { - return URL::remote( - (new OnlinePaymentRepo()) - ->payCardOrder($resource), - ); + return URL::remote((new OnlinePaymentRepo())->payCardOrder($resource)); } /** @@ -333,7 +346,7 @@ class CardOrder extends Resource ->creationRules('required') ->updateRules('nullable'), - NovaCustomHtml::make(__('Data'), 'ada') + NovaCustomHtml::make(__('Data'), 'additional_data') ->html(view('orders.cards.contract-agreement')->render()) ->fillUsing(NovaForm::fillEmpty()), ]), diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..d3fe39d 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -19,6 +20,6 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + Model::shouldBeStrict(! app()->isProduction()); } } diff --git a/app/Repos/Payment/OnlinePaymentRepo.php b/app/Repos/Payment/OnlinePaymentRepo.php index 15f620c..1fbd0fe 100644 --- a/app/Repos/Payment/OnlinePaymentRepo.php +++ b/app/Repos/Payment/OnlinePaymentRepo.php @@ -3,28 +3,71 @@ namespace App\Repos\Payment; use App\Models\Payment\ApiKeyHalkbank; +use App\Models\Payment\OnlinePaymentHistory; +use App\Repos\Order\OrderRepo; +use Exception; use Illuminate\Support\Facades\Http; +use Illuminate\Support\Facades\Log; class OnlinePaymentRepo { - public function payCardOrder($resource): string + /** + * Pay card order + * @param [type] $resource + */ + public function payCardOrder($resource): array { $orderNumber = $this->generateOrderNumber($resource); - $response = Http::get('https://mpi.gov.tm/payment/rest/register.do', [ + try { + $paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [ + 'orderNumber' => $orderNumber, + 'amount' => "00" . $resource->priceAmount(), + 'currency' => 934, + 'language' => 'ru', + 'userName' => $resource->branch->billing_username, + 'password' => $resource->branch->billing_password, + 'returnUrl' => urlencode(route('online-payment-store')), + 'pageView' => 'DESKTOP', + 'description' => urlencode('Kart tölegi'), + ]); + } catch(Exception $exception) { + Log::channel('halkbank_payment_error')->error('Payment error', [ + 'response' => [ + 'body' => $paymentResponse->body(), + ], + 'exception' => [ + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'message' => $exception->getMessage(), + ] + ]); + + return [ + 'status' => 'success', + 'url' => '' + ]; + } + + OnlinePaymentHistory::create([ + 'online_paymantable_id' => $resource->id, + 'online_paymantable_type' => $resource::$model, + 'amount' => $resource->priceAmount(), 'orderNumber' => $orderNumber, - 'amount' => $booking->total_price.'00', - 'currency' => 934, - 'language' => 'ru', - 'userName' => 301161000067, - 'password' => 'E3vb2SR3dgTPdff', - 'returnUrl' => route('online-payment-store'), - 'failUrl' => route('online-payment-store'), - 'pageView' => 'DESKTOP', - 'description' => 'Sargyt', + 'description' => 'Kart tölegi', + 'orderId' => $paymentResponse['orderId'], + 'formUrl' => $paymentResponse['formUrl'], + 'successUrl' => route('online-payment-store'), + 'errorUrl' => route('online-payment-store'), + 'api_client' => config('app.url'), + 'username' => $resource->branch->billing_username, + 'paymentStatus' => OrderRepo::PENDING ]); - return 'a'; + return [ + 'status' => 'success', + 'url' => $paymentResponse['formUrl'] + ]; } /** diff --git a/config/logging.php b/config/logging.php index c44d276..5421fe9 100644 --- a/config/logging.php +++ b/config/logging.php @@ -126,6 +126,13 @@ return [ 'emergency' => [ 'path' => storage_path('logs/laravel.log'), ], + + 'halkbank_payment_error' => [ + 'driver' => 'single', + 'path' => storage_path('logs/halkbank_payment_error.log'), + 'level' => env('LOG_LEVEL', 'error'), + 'replace_placeholders' => true, + ], ], ]; diff --git a/database/migrations/2024_01_28_180935_add_payment_status_to_card_orders_table.php b/database/migrations/2024_01_28_180935_add_payment_status_to_card_orders_table.php new file mode 100644 index 0000000..96737dc --- /dev/null +++ b/database/migrations/2024_01_28_180935_add_payment_status_to_card_orders_table.php @@ -0,0 +1,28 @@ +boolean('paid')->default(true); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('card_orders', function (Blueprint $table) { + $table->dropColumn('paid'); + }); + } +}; diff --git a/database/migrations/2024_01_28_183735_create_online_payment_histories_table.php b/database/migrations/2024_01_28_183735_create_online_payment_histories_table.php new file mode 100644 index 0000000..39cdb58 --- /dev/null +++ b/database/migrations/2024_01_28_183735_create_online_payment_histories_table.php @@ -0,0 +1,50 @@ +id(); + + $table->string('refunded_amount')->nullable(); + $table->string('booking_number')->nullable(); + $table->string('amount')->nullable(); + $table->string('depositedAmount')->nullable(); + $table->string('orderNumber')->nullable(); + $table->string('description')->nullable(); + $table->string('orderId')->nullable(); + $table->string('cardholderName')->nullable(); + $table->string('pan')->nullable(); + $table->boolean('approvalCode')->default(false); + $table->string('expiration')->nullable(); + $table->string('formUrl')->nullable(); + $table->string('successUrl')->nullable(); + $table->string('errorUrl')->nullable(); + $table->string('api_client')->nullable(); + $table->string('paymentStatus')->nullable(); + $table->string('callbackStatus')->nullable(); + $table->string('username')->nullable(); + + $table->unsignedBigInteger('online_paymantable_id')->index()->nullable(); + $table->string('online_paymantable_type')->index()->nullable(); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('online_payment_histories'); + } +}; diff --git a/routes/web.php b/routes/web.php index 2bd0ef2..cd685ce 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Auth\LoginController; use App\Http\Controllers\Auth\RegisterController; use App\Http\Controllers\Auth\ResetPasswordController; +use App\Http\Controllers\OnlinePaymentController; use Illuminate\Support\Facades\Route; /* @@ -32,4 +33,6 @@ Route::middleware(['auth', 'un_verified'])->group(function () { Route::post('sms-verification', [RegisterController::class, 'verifySmsCode']); }); +Route::get('online-payment-store', [OnlinePaymentController::class, 'store'])->name('online-payment-store'); + Route::redirect('/', config('nova.path'));