diff --git a/app/Command/JinlingCommand.php b/app/Command/JinlingCommand.php index 6535cc6..17d77c7 100644 --- a/app/Command/JinlingCommand.php +++ b/app/Command/JinlingCommand.php @@ -39,10 +39,17 @@ class JinlingCommand extends HyperfCommand public function handle(): void { + $sm3 = new \OneSm\Sm3(); + +// 字符串签名 + echo $sm3->sign('abc') . PHP_EOL; + echo $sm3->sign(str_repeat("adfas哈哈哈", 100)) . PHP_EOL; + return; /** * @var PaymentService $paymentService */ $paymentService = $this->container->make(PaymentService::class); + $paymentService = $this->container->make(PaymentService::class); $paramsJson = '{"return_code":"SUCCESS","nonce_str":"4a40481401bb47cc81411ab3a8181bad","timestamp":1682659313039,"sign":"5AD859576B783CB44D1FAA0E7DD4C463","order_no":"TR23042830042195760003775","out_order_no":"2023042813194700001","total_amount":2,"currency":"AUD","order_time":"20230428151948","pay_time":"20230428152021","exchange_rate":459899100,"cny_amount":9}'; $params = json_decode($paramsJson, true); $ret = $paymentService->notify($params); diff --git a/app/Controller/Payment/PayController.php b/app/Controller/Payment/PayController.php index 6eb668f..cb22fc4 100644 --- a/app/Controller/Payment/PayController.php +++ b/app/Controller/Payment/PayController.php @@ -30,7 +30,7 @@ class PayController extends AbstractController public function unified() { $payRequest = new JsapiPayRequest($request->all()); - $order = $this->paymentService->jsapiPay($payRequest->getApp(), $payRequest->getData()); + $order = $this->paymentService->js($payRequest->getApp(), $payRequest->getData()); return $this->success([ 'pay_url' => $order->pay_url, 'order_no' => $order->order_no, diff --git a/app/Helper/Efps/AbstractApi.php b/app/Helper/Efps/AbstractApi.php index 3f83562..e97f64a 100644 --- a/app/Helper/Efps/AbstractApi.php +++ b/app/Helper/Efps/AbstractApi.php @@ -2,9 +2,8 @@ namespace App\Helper\Efps; +use App\Helper\Efps\Request\AbstractRequest; use App\Helper\Log; -use App\Helper\StringHelper; -use App\Helper\TimeHelper; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Hyperf\Guzzle\CoroutineHandler; @@ -13,42 +12,29 @@ abstract class AbstractApi { protected static $client; - protected static function getCommonParams() { - $params = [ - 'm_number' => Config::get('app_id'), - 'timestamp' => TimeHelper::getMillisecond(), - 'nonce_str' => StringHelper::getRandomString(32), - ]; - $params['sign'] = Signer::sign($params); - return $params; - } - public static function request(AbstractRequest $request) { - $params = self::getCommonParams(); - $params = array_merge($params, $request->getParams()); + $params = $request->getParams(); try { Log::info('url:' . $request->getUrl(), [], 'omipay'); $response = self::getClient()->post($request->getUrl(), [ 'query' => $params ]); - Log::info('request:', $params, 'omipay'); + Log::info('request:', $params, 'efps'); $body = (string)$response->getBody(); - Log::info('response:' . $body, [], 'omipay'); + Log::info('response:' . $body, [], 'efps'); $result = json_decode($body, true); if (empty($result)) { $result = [ - 'return_code' => 'FAIL', - 'error_code' => 'RESPONSE_ERROR', - 'error_msg' => '返回数据异常', + 'returnCode' => '9999', + 'returnMsg' => '返回数据异常', ]; } return new Result($result); - } catch (Exception $e) { - Log::error('error:' . $e->getMessage(), [], 'omipay'); + } catch (\Exception $e) { + Log::error('error:' . $e->getMessage(), [], 'efps'); return new Result([ - 'return_code' => 'FAIL', - 'error_code' => 'NETWORK_ERROR', - 'error_msg' => '网络错误', + 'returnCode' => '9998', + 'returnMsg' => '网络错误', ]); } @@ -81,6 +67,6 @@ abstract class AbstractApi 'x-efps-timestamp' => $timestamp, 'x-efps-version' => '2.0', 'x-efps-enc-key' => $encKey, - ] + ]; } } \ No newline at end of file diff --git a/app/Helper/Efps/Api.php b/app/Helper/Efps/Api.php index e2df1e6..adbdbf3 100644 --- a/app/Helper/Efps/Api.php +++ b/app/Helper/Efps/Api.php @@ -3,8 +3,9 @@ namespace App\Helper\Efps; use App\Helper\Efps\Request\UnifiedPaymentRequest; +use App\Helper\StringHelper; -class Api +class Api extends AbstractApi { public static function unifiedPayment($outTradeNo, $orderInfo, $payAmount, $notifyUrl, $redirectUrl) { @@ -15,7 +16,9 @@ class Api $request->setPayAmount($payAmount); $request->setNotifyUrl($notifyUrl); $request->setRedirectUrl($redirectUrl); - + $request->setTransactionStartTime(date('YmdHis')); + $request->setAreaInfo([]); + $request->setNonceStr(StringHelper::getRandomString(32)); return self::request($request); } } \ No newline at end of file diff --git a/app/Helper/Efps/Result.php b/app/Helper/Efps/Result.php new file mode 100644 index 0000000..5bb6bb5 --- /dev/null +++ b/app/Helper/Efps/Result.php @@ -0,0 +1,38 @@ +data = $data; + } + + public function isSuccess() + { + return $this->data['returnCode'] == '0000'; + } + + public function get($key) + { + return $this->data[$key] ?? null; + } + + public function toArray() + { + $data = []; + if ($this->isSuccess()) { + $data['is_success'] = true; + $data['pay_url'] = $this->get('pay_url'); + } else { + $data['error_code'] = $this->get('error_code'); + $data['error_msg'] = $this->get('error_msg'); + } + return $data; + } +} \ No newline at end of file diff --git a/app/Helper/OmiPay/Result.php b/app/Helper/OmiPay/Result.php index bb89dff..98af0b9 100644 --- a/app/Helper/OmiPay/Result.php +++ b/app/Helper/OmiPay/Result.php @@ -20,4 +20,17 @@ class Result public function get($key) { return $this->data[$key] ?? null; } + + public function toArray() + { + $data = []; + if ($this->isSuccess()) { + $data['is_success'] = true; + $data['pay_url'] = $this->get('casherUrl'); + } else { + $data['error_code'] = $this->get('returnCode'); + $data['error_msg'] = $this->get('returnMsg'); + } + return $data; + } } \ No newline at end of file diff --git a/app/Service/PaymentService.php b/app/Service/PaymentService.php index 428becd..1e6b3e2 100644 --- a/app/Service/PaymentService.php +++ b/app/Service/PaymentService.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace App\Service; use App\Exception\BusinessException; -use App\Helper\Efps\PayNotify; use App\Helper\Log; use App\Helper\OmiPay\Api; use App\Helper\OmiPay\Result; @@ -51,14 +50,15 @@ class PaymentService extends AbstractService return date('YmdHis', $now) . $incrId; } - public function updateOrderResult(Order $order, Result $result) { - if ($result->isSuccess()) { - $order->pay_order_no = $result->get('order_no'); - $order->pay_url = $result->get('pay_url'); + public function updateOrderResult(Order $order, array $result) + { + if ($result['is_success']) { + $order->pay_order_no = $result['pay_order_no']; + $order->pay_url = $result['pay_url']; $order->status = Order::STATUS_WAIT_PAY; } else { - $order->error_code = $result->get('error_code'); - $order->error_msg = $result->get('error_msg'); + $order->error_code = $result['error_code']; + $order->error_msg = $result['error_msg']; $order->status = Order::STATUS_FAILED; } $order->save(); @@ -83,7 +83,7 @@ class PaymentService extends AbstractService 'http://146.70.113.165:9501/payment/notify', 'http://146.70.113.165:9501/payment/page?order_no=' . $order->order_no ); - $this->updateOrderResult($order, $result); + $this->updateOrderResult($order, $result->toArray()); return $order; } @@ -168,4 +168,20 @@ class PaymentService extends AbstractService return $params; } + public function unifiedPay(App $app, array $params) { + $order = Order::where('app_id', $app->app_id)->where('out_order_no', $params['out_order_no'])->first(); + if ($order) { + throw new BusinessException('订单重复'); + } + $order = $this->createOrder($app, $params); + $result = \App\Helper\Efps\Api::unifiedPayment( + $order->order_no, + $order->order_name, + $order->amount, + 'http://146.70.113.165:9501/payment/notify', + 'http://146.70.113.165:9501/payment/page?order_no=' . $order->order_no + ); + $this->updateOrderResult($order, $result->toArray()); + return $order; + } } diff --git a/composer.json b/composer.json index 19919d4..77794d4 100644 --- a/composer.json +++ b/composer.json @@ -33,9 +33,8 @@ "hyperf/rpc-client": "~2.2.0", "hyperf/rpc-server": "~2.2.0", "hyperf/validation": "^2.2", - "phpoffice/phpspreadsheet": "^1.24", - "ext-json": "Required to use JSON.", - "ext-openssl": "Required to use HTTPS." + "lizhichao/one-sm": "^1.10", + "phpoffice/phpspreadsheet": "^1.24" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", diff --git a/composer.lock b/composer.lock index 9d4ebf4..908a45f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0497aa465f49ca6858436c805a387c26", + "content-hash": "24c591932533a2bdb8d1c21b907bda5d", "packages": [ { "name": "doctrine/annotations", @@ -3285,6 +3285,60 @@ ], "time": "2022-08-24T13:56:50+00:00" }, + { + "name": "lizhichao/one-sm", + "version": "1.10", + "source": { + "type": "git", + "url": "https://github.com/lizhichao/sm.git", + "reference": "687a012a44a5bfd4d9143a0234e1060543be455a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/lizhichao/sm/zipball/687a012a44a5bfd4d9143a0234e1060543be455a", + "reference": "687a012a44a5bfd4d9143a0234e1060543be455a", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "type": "library", + "autoload": { + "psr-4": { + "OneSm\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "tanszhe", + "email": "1018595261@qq.com" + } + ], + "description": "国密sm3", + "keywords": [ + "php", + "sm3" + ], + "support": { + "issues": "https://github.com/lizhichao/sm/issues", + "source": "https://github.com/lizhichao/sm/tree/1.10" + }, + "funding": [ + { + "url": "https://www.vicsdf.com/img/w.jpg", + "type": "custom" + }, + { + "url": "https://www.vicsdf.com/img/z.jpg", + "type": "custom" + } + ], + "time": "2021-05-26T06:19:22+00:00" + }, { "name": "maennchen/zipstream-php", "version": "2.2.1", @@ -9271,8 +9325,8 @@ "prefer-stable": true, "prefer-lowest": false, "platform": { - "php": ">=7.3" + "php": ">=7.4" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" }