127 lines
4.2 KiB
PHP
127 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Gateway\AsanPardakht;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\Transaction;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class AsanPardakhtApi
|
|
{
|
|
public static function getGateway(Transaction $transaction)
|
|
{
|
|
$user = auth()->user();
|
|
$response = Http::connectTimeout(5)->timeout(5)/* ->retry(3, 100, throw: false) */
|
|
->acceptJson()
|
|
->contentType('application/json')
|
|
->withHeaders([
|
|
'usr' => config('asanpardakht.user'),
|
|
'pwd' => config('asanpardakht.password'),
|
|
])
|
|
->post(config('asanpardakht.url.request'), [
|
|
'merchantConfigurationId' => config('asanpardakht.merchant_config_id'),
|
|
'serviceTypeId' => 1,
|
|
'localInvoiceId' => $transaction->id,
|
|
'amountInRials' => $transaction->amount * 10,
|
|
'localDate' => now()->format('Ymd His'),
|
|
'additionalData' => json_encode([
|
|
'mobile' => $user->mobile
|
|
]),
|
|
'callbackURL' => "https://payment.vada.ir/api/asanpardakht/check-transaction?localInvoiceId=$transaction->id" . (request()->get('platform') ? '&platform=' . request()->get('platform') : ''),
|
|
'paymentId' => "0",
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
return ["message" => "failed", 'status' => false];
|
|
}
|
|
|
|
return [
|
|
'data' => [
|
|
'url' => 'https://payment.vada.ir/api/asanpardakht/pay?token=' . $response->json() . '&mobile=' . $user->mobile,
|
|
'authority' => $response->json()
|
|
],
|
|
'status' => true
|
|
];
|
|
}
|
|
|
|
public static function checkTransaction(Transaction $transaction)
|
|
{
|
|
$transactionResponse = self::transactionResult($transaction);
|
|
if (!$transactionResponse) {
|
|
return [
|
|
'status' => false,
|
|
];
|
|
}
|
|
|
|
self::verify($transactionResponse['payGateTranID']);
|
|
self::settlement($transactionResponse['payGateTranID']);
|
|
|
|
return [
|
|
'status' => true,
|
|
];
|
|
}
|
|
|
|
public static function transactionResult(Transaction $transaction)
|
|
{
|
|
$response = Http::connectTimeout(5)->timeout(5)->retry(3, 100, throw: false)
|
|
->acceptJson()
|
|
->contentType('application/json')
|
|
->withHeaders([
|
|
'usr' => config('asanpardakht.user'),
|
|
'pwd' => config('asanpardakht.password'),
|
|
])
|
|
->get(config('asanpardakht.url.result'), [
|
|
'LocalInvoiceId' => $transaction->id,
|
|
'MerchantConfigurationId' => config('asanpardakht.merchant_config_id'),
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
return false;
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
public static function verify($payGateTranId)
|
|
{
|
|
$response = Http::connectTimeout(5)->timeout(5)->retry(3, 100, throw: false)
|
|
->acceptJson()
|
|
->contentType('application/json')
|
|
->withHeaders([
|
|
'usr' => config('asanpardakht.user'),
|
|
'pwd' => config('asanpardakht.password'),
|
|
])
|
|
->post(config('asanpardakht.url.verify'), [
|
|
'merchantConfigurationId' => config('asanpardakht.merchant_config_id'),
|
|
'payGateTranId' => (int) $payGateTranId,
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
return false;
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
|
|
public static function settlement($payGateTranId)
|
|
{
|
|
$response = Http::connectTimeout(5)->timeout(5)->retry(3, 100, throw: false)
|
|
->acceptJson()
|
|
->contentType('application/json')
|
|
->withHeaders([
|
|
'usr' => config('asanpardakht.user'),
|
|
'pwd' => config('asanpardakht.password'),
|
|
])
|
|
->post(config('asanpardakht.url.settlement'), [
|
|
'merchantConfigurationId' => config('asanpardakht.merchant_config_id'),
|
|
'payGateTranId' => (int) $payGateTranId,
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
return false;
|
|
}
|
|
|
|
return $response->json();
|
|
}
|
|
}
|