6
0
Fork 0
jiqu-library-server/app/Services/YeePayService.php

83 lines
1.8 KiB
PHP

<?php
namespace App\Services;
use App\Exceptions\YeePayException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class YeePayService
{
public function __construct(
protected array $config,
) {
}
/**
* @param string $service
* @param array $data
* @return array
*/
public function request(string $service, array $data = []): array
{
$data = array_merge([
'requestNo' => Str::uuid()->getHex()->toString(),
'service' => $service,
'partnerId' => $this->config['partner_id'],
'signType' => 'MD5',
'notifyUrl' => route('yeepay.notify'),
], $data);
$data['sign'] = $this->sign($data);
$response = Http::asForm()->post($this->config['gateway_url'], $data);
$response->throw();
$result = $response->json();
if ($result['success'] && in_array($result['resultCode'], ['EXECUTE_SUCCESS', 'EXECUTE_PROCESSING'])) {
return $result;
}
$message = $result['resultDetail'] ?? $result['resultMessage'];
throw new YeePayException($message);
}
/**
* 生成签名
*
* @param array $params
* @return string
*/
protected function sign(array $params): string
{
unset($params['sign']);
ksort($params);
$str = '';
foreach ($params as $key => $value) {
if (blank($value)) {
continue;
}
if ($str !== '') {
$str .= '&';
}
if (is_array($value)) {
$value = json_encode($value);
}
$str .= "{$key}={$value}";
}
$str .= $this->config['secret_key'];
return md5($str);
}
}