74 lines
2.1 KiB
PHP
Executable File
74 lines
2.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Modules\Gateway\Digipay;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Transaction;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class DigiPayApi
|
|
{
|
|
public static function getToken()
|
|
{
|
|
$token = Cache::remember('digipay_token', 3600, function () {
|
|
return Http::connectTimeout(5)->timeout(5)->retry(3, 100, throw: false)
|
|
->asForm()
|
|
->withHeaders([
|
|
'Authorization' => 'Basic ' . config('digipay.api_key')
|
|
])
|
|
->post(config('digipay.url.login'), [
|
|
'username' => config('digipay.username'),
|
|
'password' => config('digipay.password'),
|
|
'grant_type' => 'password',
|
|
])['access_token'];
|
|
});
|
|
|
|
return $token;
|
|
}
|
|
|
|
public static function getGateway(Transaction $transaction)
|
|
{
|
|
$user = auth()->user();
|
|
|
|
$response = Http::connectTimeout(5)->timeout(5)->retry(3, 100, throw: false)
|
|
->withToken(self::getToken())
|
|
->post(config('digipay.url.request'), [
|
|
'providerId' => $transaction->uuid,
|
|
'amount' => $transaction->amount * 10,
|
|
'userType' => 0,
|
|
'redirectUrl' => 'https://payment.vada.ir/api/digipay/check-transaction',
|
|
'cellNumber' => $user->mobile,
|
|
]);
|
|
|
|
if (!isset($response['payUrl'])) {
|
|
return ["message" => "failed", 'status' => false];
|
|
}
|
|
|
|
return [
|
|
'data' => [
|
|
'url' => $response['payUrl'],
|
|
'authority' => $response['ticket']
|
|
],
|
|
'status' => true
|
|
];
|
|
}
|
|
|
|
public static function checkTransaction($trackingCode)
|
|
{
|
|
$response = Http::retry(3, 100, throw: false)
|
|
->withToken(self::getToken())
|
|
->post(str_replace(':trackingCode', $trackingCode, config('digipay.url.verify')));
|
|
|
|
if ($response['result']['status'] != 0) {
|
|
return [
|
|
'status' => false,
|
|
];
|
|
}
|
|
|
|
return [
|
|
'status' => true,
|
|
];
|
|
}
|
|
}
|