online payments for card order

This commit is contained in:
2025-10-31 02:16:15 +05:00
parent 5d3ea6d787
commit 1873486d13
8 changed files with 104 additions and 34 deletions

View File

@@ -59,6 +59,16 @@ class CardOrderModule implements ModuleContract
name: 'Branch',
message: 'This module is used for branches.',
),
new ModulePackage(
type: ModulePackageType::MODULE,
name: 'OrderStatus',
message: 'Supports orders.',
),
new ModulePackage(
type: ModulePackageType::MODULE,
name: 'OnlinePayment',
message: 'Is online payable.',
),
];
}

View File

@@ -6,6 +6,7 @@ use App\Models\User;
use App\Modules\Branch\Interfaces\BelongsToBranch;
use App\Modules\Branch\Models\Branch;
use App\Modules\LoanOrder\Repositories\LoanOrderRepository;
use App\Modules\OnlinePayment\Contracts\HasOnlinePayments;
use App\Modules\OrderStatus\Interfaces\HasStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

View File

@@ -19,7 +19,7 @@ class CardOrderRepository
/** @var \App\Modules\Branch\Models\Branch */
$branch = $record->branch;
return OnlinePaymentRepository::make()
return OnlinePaymentRepository::make(relatedModel: $record)
->paymentProvider(
HalkbankOnlinePaymentRepository::make()
->setUsername($branch->billingUsername())

View File

@@ -64,7 +64,7 @@ class HalkbankOnlinePaymentRepository implements PaymentProviderContract
return new Response(new GuzzleResponse(
503,
['Content-Type' => 'application/json'],
sprintf('{"error":"Payment provider connection failed","exception_message":"%s"}', $e->getMessage())
sprintf('{"errorCode":"1","errorMessage":"%s"}', $e->getMessage())
));
}

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Modules\OnlinePayment\Contracts;
/**
* @property int $id
*/
interface HasOnlinePayments {}

View File

@@ -7,4 +7,14 @@ use Illuminate\Http\Client\Response;
interface PaymentProviderContract
{
public function sendRequest(): Response;
public function orderNumber(): int|string;
public function amount(): int|float|string;
public function description(): string;
public function returnUrl(): string;
public function username(): string;
}

View File

@@ -3,10 +3,11 @@
namespace App\Modules\OnlinePayment\Repositories;
use App\Modules\AppHelpers\Contracts\HasOnlinePaymentStatusFields;
use App\Modules\CardOrder\Models\CardOrder;
use App\Modules\Makeable;
use App\Modules\OnlinePayment\Contracts\HasOnlinePayments;
use App\Modules\OnlinePayment\Contracts\PaymentProviderContract;
use App\Modules\OnlinePayment\Models\OnlinePayment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Client\Response;
class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
@@ -28,6 +29,11 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
*/
public const PAID = 'paid';
/**
* Related model
*/
protected ?Model $relatedModel;
/**
* Payment provider
*/
@@ -48,11 +54,21 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
*/
protected bool $failed;
/**
* Payment order id
*/
protected string $orderId;
/**
* Online payment link
*/
protected string $paymentLink;
public function __construct(?Model $relatedModel)
{
$this->relatedModel = $relatedModel;
}
/**
* Status Values
*
@@ -102,17 +118,35 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
return $this->failed;
}
/**
* Set respond results
*/
public function setResponseResults(bool $result): void
{
$this->successful = $result;
$this->failed = ! $result;
}
/**
* Send request via provider
*/
public function sendRequest(): self
{
$response = $this->provider->sendRequest();
$this->response = $this->provider->sendRequest();
$this->failed = $response->failed();
$this->successful = $response->successful();
$this->failed = $this->response->failed();
$this->successful = $this->response->successful();
$this->response = $response;
if ($this->response['errorCode'] != 0) {
$this->setResponseResults(false);
}
if ($this->successful) {
$this->orderId = string($this->response['orderId']);
$this->paymentLink = string($this->response['formUrl']);
$this->createHistory();
}
return $this;
}
@@ -125,33 +159,28 @@ class OnlinePaymentRepository implements HasOnlinePaymentStatusFields
return $this->paymentLink;
}
// public function payCardOrder()
// {
// if ($paymentResponse->failed()) {
// return [
// 'status' => 'failed',
// 'url' => '',
// ];
// }
/**
* Create online payment history
*/
public function createHistory(): void
{
$data = [
'amount' => $this->provider->amount(),
'orderNumber' => $this->provider->orderNumber(),
'description' => $this->provider->description(),
'orderId' => $this->orderId,
'formUrl' => $this->paymentLink,
'successUrl' => $this->provider->returnUrl(),
'errorUrl' => $this->provider->returnUrl(),
'username' => $this->provider->username(),
'paymentStatus' => self::PENDING,
];
// $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();
if ($this->relatedModel) {
$data['online_paymantable_id'] = $this->relatedModel->id; // @phpstan-ignore-line
$data['online_paymantable_type'] = $this->relatedModel::class;
}
// return [
// 'status' => 'success',
// 'url' => $paymentResponse['formUrl'],
// ];
// }
OnlinePayment::create($data);
}
}

View File

@@ -54,6 +54,18 @@ function module_exists(string $moduleName): bool
return modular()->moduleExists($moduleName);
}
/**
* Cast to string
*/
function string(mixed $value): string
{
if (! is_string($value)) {
throw new Exception('!!!Make it string!!!');
}
return $value;
}
/**
* Create an anonymous dynamic object with both properties and callable methods.
*