Files
api_arrahn/app/Services/ShorakaService.php
T
2026-09-17 15:08:43 +08:00

138 lines
3.4 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use RuntimeException;
class ShorakaService
{
protected $baseUrl;
protected $apiId;
protected $secretKey;
protected $timeout;
public function __construct()
{
$this->baseUrl = rtrim(
config('services.shoraka.base_url'),
'/'
);
$this->apiId =
config('services.shoraka.api_id');
$this->secretKey =
config('services.shoraka.secret_key');
$this->timeout =
config('services.shoraka.timeout', 60);
}
public function submitOrder(array $data): array
{
$payload = [
'api_id' => $this->apiId,
'product_type' =>
$data['product_type'],
'commodity_type' =>
$data['commodity_type'],
'ownership' =>
$data['ownership'],
'value_date' =>
$data['value_date'],
'order_currency' =>
$data['order_currency'],
'order_amount' =>
$this->formatAmount(
$data['order_amount']
),
'murabaha_amount' =>
$this->formatAmount(
$data['murabaha_amount']
),
'tenor' =>
$data['tenor'],
'tenor_other' =>
$data['tenor_other'] ?? '',
'tenor_other_unit' =>
$data['tenor_other_unit'] ?? '',
'order_type' =>
$data['order_type'],
'ownership_doc_no' =>
$data['ownership_doc_no'] ?? '',
];
$payload['signature'] =
$this->makeSubmitSignature($payload);
$response = Http::asForm()
->acceptJson()
->timeout($this->timeout)
->post(
$this->baseUrl . '/api/submitorder',
$payload
);
return $this->getJsonResponse($response);
}
protected function makeSubmitSignature(
array $payload
): string {
$values = [
$this->secretKey,
$this->apiId,
$payload['product_type'],
$payload['commodity_type'],
$payload['ownership'],
$payload['value_date'],
$payload['order_currency'],
$payload['order_amount'],
$payload['murabaha_amount'],
$payload['tenor'],
$payload['tenor_other'],
$payload['tenor_other_unit'],
$payload['order_type'],
];
return hash(
'sha256',
implode(';', $values)
);
}
protected function formatAmount($amount): string
{
return number_format(
(float) $amount,
2,
'.',
''
);
}
protected function getJsonResponse(
Response $response
): array {
if (!$response->successful()) {
throw new RuntimeException(
'Shoraka request failed. HTTP ' .
$response->status() .
': ' .
$response->body()
);
}
$data = $response->json();
if (!is_array($data)) {
throw new RuntimeException(
'Shoraka returned an invalid response.'
);
}
return $data;
}
}