304 lines
7.7 KiB
PHP
304 lines
7.7 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\OnlinePayment\Repositories;
|
|
|
|
use App\Modules\Makeable;
|
|
use App\Modules\OnlinePayment\Contracts\PaymentProviderContract;
|
|
use App\Modules\OnlinePayment\Models\OnlinePayment;
|
|
use Exception;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Client\Response;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class OnlinePaymentRepository
|
|
{
|
|
use Makeable;
|
|
|
|
/**
|
|
* Pending online payments
|
|
*/
|
|
public const PENDING = 'pending';
|
|
|
|
/**
|
|
* Failed online payments
|
|
*/
|
|
public const FAILED = 'failed';
|
|
|
|
/**
|
|
* Paid online payments
|
|
*/
|
|
public const PAID = 'paid';
|
|
|
|
/**
|
|
* Related model
|
|
*/
|
|
protected ?Model $relatedModel;
|
|
|
|
/**
|
|
* Payment provider
|
|
*/
|
|
protected PaymentProviderContract $provider;
|
|
|
|
/**
|
|
* Response
|
|
*/
|
|
protected Response $response;
|
|
|
|
/**
|
|
* If payment is successful
|
|
*/
|
|
protected bool $successful;
|
|
|
|
/**
|
|
* If payment has failed
|
|
*/
|
|
protected bool $failed;
|
|
|
|
/**
|
|
* Payment order id
|
|
*/
|
|
protected string $orderId;
|
|
|
|
/**
|
|
* Online payment link
|
|
*/
|
|
protected string $paymentLink;
|
|
|
|
public function __construct(?Model $relatedModel = null)
|
|
{
|
|
$this->relatedModel = $relatedModel;
|
|
}
|
|
|
|
/**
|
|
* Status Values
|
|
*
|
|
* @return array<null|string, string>
|
|
*/
|
|
public static function statusValues(): array
|
|
{
|
|
return [
|
|
null => '-',
|
|
self::PENDING => __('Pending'),
|
|
self::PAID => __('Paid'),
|
|
self::FAILED => __('Cancelled'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Generate order number for payment
|
|
*/
|
|
public function generateOrderNumber(): string
|
|
{
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Set respond results
|
|
*/
|
|
public function setResponseResults(bool $result): void
|
|
{
|
|
$this->successful = $result;
|
|
$this->failed = ! $result;
|
|
}
|
|
|
|
/**
|
|
* Send request via provider
|
|
*/
|
|
public function sendRequest(): self
|
|
{
|
|
$this->response = $this->provider->sendRequest();
|
|
|
|
$this->failed = $this->response->failed();
|
|
$this->successful = $this->response->successful();
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Check payment
|
|
*/
|
|
public function checkPayment(string $orderId)
|
|
{
|
|
// Find payment order from history
|
|
$paymentHistory = OnlinePayment::where('orderId', $orderId)->first();
|
|
|
|
// Find related resource
|
|
$resource = (new $paymentHistory->online_paymantable_type)->find(id: $paymentHistory->online_paymantable_id);
|
|
|
|
// If resource could not be found or does not exist, then inform it via logs
|
|
if (! $resource) {
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed').' '.'(RESOURCE NOT FOUND)',
|
|
'pnr' => '',
|
|
'branch_name' => '',
|
|
'price_amount' => '',
|
|
'return_url' => url('/'),
|
|
'bank_branch' => null,
|
|
'resource' => null,
|
|
'paymentHistory' => null,
|
|
];
|
|
}
|
|
|
|
$resource->load('branch');
|
|
|
|
$bank_branch = $resource->branch;
|
|
|
|
if (! $bank_branch) {
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed').' '.'(BRANCH NOT FOUND)',
|
|
'pnr' => '',
|
|
'branch_name' => '',
|
|
'price_amount' => '',
|
|
'return_url' => url('/'),
|
|
'bank_branch' => null,
|
|
'resource' => null,
|
|
'paymentHistory' => null,
|
|
];
|
|
}
|
|
|
|
try {
|
|
$response = Http::asForm()->post('https://mpi.gov.tm/payment/rest/getOrderStatus.do', [
|
|
'language' => 'ru',
|
|
'orderId' => $orderId,
|
|
'userName' => $bank_branch->billing_username,
|
|
'password' => $bank_branch->billing_password,
|
|
]);
|
|
} catch (Exception $e) {
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed').' '.'(REQUEST FAILURE)',
|
|
'pnr' => '',
|
|
'branch_name' => '',
|
|
'price_amount' => '',
|
|
'return_url' => url('/'),
|
|
'bank_branch' => null,
|
|
'resource' => null,
|
|
'paymentHistory' => null,
|
|
];
|
|
}
|
|
|
|
$payment_status = $response['ErrorCode'] == '0';
|
|
$returnURL = '/';
|
|
|
|
if ($payment_status) {
|
|
$resource->update([
|
|
'paid' => true,
|
|
]);
|
|
|
|
$paymentHistory->update([
|
|
'paymentStatus' => OnlinePaymentRepository::PAID,
|
|
]);
|
|
|
|
return [
|
|
'success' => true,
|
|
'title' => __('Payment is successful'),
|
|
'pnr' => $paymentHistory->orderNumber,
|
|
'branch_name' => $bank_branch->name,
|
|
'price_amount' => $paymentHistory->amount.' TMT',
|
|
'return_url' => $returnURL,
|
|
'bank_branch' => $bank_branch,
|
|
'resource' => $resource,
|
|
'paymentHistory' => $paymentHistory,
|
|
'response' => $response,
|
|
];
|
|
}
|
|
|
|
$paymentHistory->update([
|
|
'paymentStatus' => OnlinePaymentRepository::FAILED,
|
|
]);
|
|
|
|
return [
|
|
'success' => false,
|
|
'title' => __('Payment has failed'),
|
|
'pnr' => $paymentHistory->orderNumber,
|
|
'branch_name' => $bank_branch->name,
|
|
'price_amount' => $paymentHistory->amount.' TMT',
|
|
'return_url' => $returnURL,
|
|
'paymentHistory' => $paymentHistory,
|
|
'bank_branch' => $bank_branch,
|
|
'response' => $response,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Payment link
|
|
*/
|
|
public function paymentLink(): string
|
|
{
|
|
return $this->paymentLink;
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
];
|
|
|
|
if ($this->relatedModel) {
|
|
$data['online_paymantable_id'] = $this->relatedModel->id; // @phpstan-ignore-line
|
|
$data['online_paymantable_type'] = $this->relatedModel::class;
|
|
}
|
|
|
|
OnlinePayment::create($data);
|
|
}
|
|
|
|
/**
|
|
* Show payment status
|
|
*/
|
|
public function paymentStatusView(array $data): View
|
|
{
|
|
return view('module.online-payment::payment-status', $data);
|
|
}
|
|
}
|