payment providers and some stan

This commit is contained in:
2025-10-31 01:13:04 +05:00
parent 11f99caf42
commit 834527647d
11 changed files with 232 additions and 121 deletions

View File

@@ -3,8 +3,8 @@
namespace App\Filament\Clusters\Cards\CardOrders\Pages;
use App\Filament\Clusters\Cards\CardOrders\CardOrderResource;
use App\Modules\CardOrder\Repositories\CardOrderRepository;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\URL;
class CreateCardOrder extends CreateRecord
{
@@ -14,12 +14,14 @@ class CreateCardOrder extends CreateRecord
{
$defaultUrl = $this->getResource()::getUrl('index');
return $defaultUrl;
/** @var \App\Modules\CardOrder\Models\CardOrder */
$record = $this->record;
// $payment = (new OnlinePaymentRepo)->payCardOrder($resource);
$OnlinePayment = CardOrderRepository::make()
->createOnlinePaymentOrder($record);
// $payment['status'] === 'success'
// ? URL::remote($payment['url'])
// : $defaultUrl;
return $OnlinePayment->successful()
? $OnlinePayment->paymentLink()
: $defaultUrl;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Modules\AppHelpers\Contracts;
interface HasOnlinePaymentStatusFields
{
public function failed(): bool;
public function successful(): bool;
}

View File

@@ -77,4 +77,20 @@ class Branch extends Model
{
return $this->belongsToMany(User::class);
}
/**
* Get billing username
*/
public function billingUsername(): string
{
return $this->billing_username ?: '-';
}
/**
* Get billing password
*/
public function billingPassword(): string
{
return $this->billing_password ?: '-';
}
}

View File

@@ -111,4 +111,12 @@ class CardOrder extends Model implements BelongsToBranch, HasStatus
static::creating(LoanOrderRepository::creating());
static::created(LoanOrderRepository::created());
}
/**
* Price for card
*/
public function priceAmount(): int|float|string
{
return $this->cardState->price ?? 32;
}
}

View File

@@ -2,4 +2,32 @@
namespace App\Modules\CardOrder\Repositories;
class CardOrderRepository {}
use App\Modules\CardOrder\Models\CardOrder;
use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository;
use App\Modules\Makeable;
use App\Modules\OnlinePayment\Repositories\OnlinePaymentRepository;
class CardOrderRepository
{
use Makeable;
/**
* Create online payment order
*/
public function createOnlinePaymentOrder(CardOrder $record): OnlinePaymentRepository
{
/** @var \App\Modules\Branch\Models\Branch */
$branch = $record->branch;
return OnlinePaymentRepository::make()
->paymentProvider(
HalkbankOnlinePaymentRepository::make()
->setUsername($branch->billingUsername())
->setPassword($branch->billingPassword())
->setAmount($record->priceAmount())
->setReturnUrl(route('halkbank-online-payment.store'))
->setDescription('Kart tölegi')
)
->sendRequest();
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Modules\HalkbankOnlinePayment\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
class HalkbankOnlinePaymentController extends Controller
{
public function store(Request $request): View
{
$request->validate([
'orderId' => ['required', 'string', 'max:50', 'exists:online_payment_histories,orderId'],
]);
return view('welcome');
}
}

View File

@@ -3,26 +3,25 @@
namespace App\Modules\HalkbankOnlinePayment\Repositories;
use App\Modules\Makeable;
use Closure;
use App\Modules\OnlinePayment\Contracts\PaymentProviderContract;
use Exception;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class HalkbankOnlinePaymentRepository
class HalkbankOnlinePaymentRepository implements PaymentProviderContract
{
use Makeable;
public function __construct(
protected string $username = '',
protected string $password = '',
protected string $amount = '',
protected int|float|string $amount = '',
protected int|string $orderNumber = '',
protected string $returnUrl = '',
protected string $description = '',
protected ?Closure $onRequestError = null,
protected ?Closure $onResponseFail = null,
protected ?Closure $onResponseSuccess = null,
) {
$this->username = config()->string('module.halkbank-online-payment.username');
$this->password = config()->string('module.halkbank-online-payment.password');
@@ -33,7 +32,7 @@ class HalkbankOnlinePaymentRepository
/**
* Send request to gatewat
*/
public function sendRequest()
public function sendRequest(): Response
{
try {
$paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
@@ -62,7 +61,11 @@ class HalkbankOnlinePaymentRepository
],
]);
return emptyClass([]);
return new Response(new GuzzleResponse(
503,
['Content-Type' => 'application/json'],
sprintf('{"error":"Payment provider connection failed","exception_message":"%s"}', $e->getMessage())
));
}
return $paymentResponse;
@@ -95,7 +98,7 @@ class HalkbankOnlinePaymentRepository
/**
* Get amount
*/
public function amount(): string
public function amount(): int|float|string
{
return $this->amount;
}
@@ -147,7 +150,7 @@ class HalkbankOnlinePaymentRepository
/**
* Set amoutn
*/
public function setAmount(string $amount): self
public function setAmount(int|float|string $amount): self
{
$this->amount = $amount;
@@ -184,36 +187,6 @@ class HalkbankOnlinePaymentRepository
return $this;
}
/**
* Set callback on request error
*/
public function setOnRequestError(Closure $onRequestError): self
{
$this->onRequestError = $onRequestError;
return $this;
}
/**
* Set callback on Response Fail
*/
public function setOnResponseFail(Closure $onResponseFail): self
{
$this->onResponseFail = $onResponseFail;
return $this;
}
/**
* Set callback on Response Success
*/
public function setOnResponseSuccess(Closure $onResponseSuccess): self
{
$this->onResponseSuccess = $onResponseSuccess;
return $this;
}
/**
* Generate order number for payment
*/

View File

@@ -0,0 +1,7 @@
<?php
use App\Modules\HalkbankOnlinePayment\Controllers\HalkbankOnlinePaymentController;
use Illuminate\Support\Facades\Route;
Route::get('halkbank-online-payment-store', [HalkbankOnlinePaymentController::class, 'store'])
->name('halkbank-online-payment.store');

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Modules\OnlinePayment\Contracts;
use Illuminate\Http\Client\Response;
interface PaymentProviderContract
{
public function sendRequest(): Response;
}

View File

@@ -26,7 +26,7 @@ use Illuminate\Database\Eloquent\Model;
* @property string $username
* @property int $online_paymantable_id
* @property string $online_paymantable_type
* @property ?array $api_response
* @property ?array<string, mixed> $api_response
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
*/

View File

@@ -2,14 +2,17 @@
namespace App\Modules\OnlinePayment\Repositories;
use App\Modules\AppHelpers\Contracts\HasOnlinePaymentStatusFields;
use App\Modules\CardOrder\Models\CardOrder;
use App\Modules\HalkbankOnlinePayment\Repositories\HalkbankOnlinePaymentRepository;
use App\Modules\Makeable;
use App\Modules\OnlinePayment\Contracts\PaymentProviderContract;
use App\Modules\OnlinePayment\Models\OnlinePayment;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Client\Response;
class OnlinePaymentRepository
class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
{
use Makeable;
/**
* Pending online payments
*/
@@ -25,6 +28,31 @@ class OnlinePaymentRepository
*/
public const PAID = 'paid';
/**
* Payment provider
*/
protected PaymentProviderContract $provider;
/**
* Response
*/
protected Response $response;
/**
* If payment is successful
*/
protected bool $successful;
/**
* If payment has failed
*/
protected bool $failed;
/**
* Online payment link
*/
protected string $paymentLink;
/**
* Status Values
*
@@ -40,75 +68,6 @@ class OnlinePaymentRepository
];
}
/**
* Pay card order
*
* @param mixed $resource
* @return array<string, string>
*/
public function payCardOrder()
{
$orderNumber = $this->generateOrderNumber();
$response = HalkbankOnlinePaymentRepository::make()
->setUsername()
->setPassword()
->setAmount()
->setOrderNumber()
->returnUrl()
->description()
->onError()
->onResponseFail()
->onResponseSuccess()
->sendRequest();
// $paymentResponse = Http::get('https://mpi.gov.tm/payment/rest/register.do', [
// 'orderNumber' => $orderNumber,
// 'amount' => $this->getPrice($resource->priceAmount()),
// 'currency' => 934,
// 'language' => 'ru',
// 'userName' => $resource->branch->billing_username,
// 'password' => $resource->branch->billing_password,
// 'returnUrl' => route('online-payment-store'),
// 'pageView' => 'DESKTOP',
// 'description' => 'Kart tölegi',
// ])->onError(function ($response) {
// Log::channel('halkbank_payment_error')
// ->error('Payment error', [
// 'response' => [
// 'body' => $response->body(),
// ],
// ]);
// });
// if ($paymentResponse->failed()) {
// return [
// 'status' => 'failed',
// 'url' => '',
// ];
// }
// $onlinePaymentHistory = new OnlinePayment;
// $onlinePaymentHistory->online_paymantable_id = $resource->really();
// $onlinePaymentHistory->online_paymantable_type = CardOrder::class;
// $onlinePaymentHistory->amount = $resource->priceAmount();
// $onlinePaymentHistory->orderNumber = $orderNumber;
// $onlinePaymentHistory->description = 'Kart tölegi';
// $onlinePaymentHistory->orderId = $paymentResponse['orderId'];
// $onlinePaymentHistory->formUrl = $paymentResponse['formUrl'];
// $onlinePaymentHistory->successUrl = route('online-payment-store');
// $onlinePaymentHistory->errorUrl = route('online-payment-store');
// $onlinePaymentHistory->api_client = 'billing_username';
// $onlinePaymentHistory->username = $resource->branch->billing_username;
// $onlinePaymentHistory->paymentStatus = self::PENDING;
// $onlinePaymentHistory->save();
// return [
// 'status' => 'success',
// 'url' => $paymentResponse['formUrl'],
// ];
}
/**
* Generate order number for payment
*/
@@ -116,4 +75,83 @@ class OnlinePaymentRepository
{
return date('dmyHis');
}
/**
* Payment provder
*/
public function paymentProvider(PaymentProviderContract $provider): self
{
$this->provider = $provider;
return $this;
}
/**
* If payment has been successfull
*/
public function successful(): bool
{
return $this->successful;
}
/**
* If payment has been a failure
*/
public function failed(): bool
{
return $this->failed;
}
/**
* Send request via provider
*/
public function sendRequest(): self
{
$response = $this->provider->sendRequest();
$this->failed = $response->failed();
$this->successful = $response->successful();
$this->response = $response;
return $this;
}
/**
* Payment link
*/
public function paymentLink(): string
{
return $this->paymentLink;
}
// public function payCardOrder()
// {
// if ($paymentResponse->failed()) {
// return [
// 'status' => 'failed',
// 'url' => '',
// ];
// }
// $onlinePaymentHistory = new OnlinePayment;
// $onlinePaymentHistory->online_paymantable_id = $resource->really();
// $onlinePaymentHistory->online_paymantable_type = CardOrder::class;
// $onlinePaymentHistory->amount = $resource->priceAmount();
// $onlinePaymentHistory->orderNumber = $orderNumber;
// $onlinePaymentHistory->description = 'Kart tölegi';
// $onlinePaymentHistory->orderId = $paymentResponse['orderId'];
// $onlinePaymentHistory->formUrl = $paymentResponse['formUrl'];
// $onlinePaymentHistory->successUrl = route('online-payment-store');
// $onlinePaymentHistory->errorUrl = route('online-payment-store');
// $onlinePaymentHistory->api_client = 'billing_username';
// $onlinePaymentHistory->username = $resource->branch->billing_username;
// $onlinePaymentHistory->paymentStatus = self::PENDING;
// $onlinePaymentHistory->save();
// return [
// 'status' => 'success',
// 'url' => $paymentResponse['formUrl'],
// ];
// }
}