efps-version
elf 2 years ago
parent 6275f594cb
commit 2200f15a2b

@ -39,10 +39,17 @@ class JinlingCommand extends HyperfCommand
public function handle(): void 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 * @var PaymentService $paymentService
*/ */
$paymentService = $this->container->make(PaymentService::class); $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}'; $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); $params = json_decode($paramsJson, true);
$ret = $paymentService->notify($params); $ret = $paymentService->notify($params);

@ -30,7 +30,7 @@ class PayController extends AbstractController
public function unified() public function unified()
{ {
$payRequest = new JsapiPayRequest($request->all()); $payRequest = new JsapiPayRequest($request->all());
$order = $this->paymentService->jsapiPay($payRequest->getApp(), $payRequest->getData()); $order = $this->paymentService->js($payRequest->getApp(), $payRequest->getData());
return $this->success([ return $this->success([
'pay_url' => $order->pay_url, 'pay_url' => $order->pay_url,
'order_no' => $order->order_no, 'order_no' => $order->order_no,

@ -2,9 +2,8 @@
namespace App\Helper\Efps; namespace App\Helper\Efps;
use App\Helper\Efps\Request\AbstractRequest;
use App\Helper\Log; use App\Helper\Log;
use App\Helper\StringHelper;
use App\Helper\TimeHelper;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack; use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\CoroutineHandler; use Hyperf\Guzzle\CoroutineHandler;
@ -13,42 +12,29 @@ abstract class AbstractApi
{ {
protected static $client; 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) { public static function request(AbstractRequest $request) {
$params = self::getCommonParams(); $params = $request->getParams();
$params = array_merge($params, $request->getParams());
try { try {
Log::info('url:' . $request->getUrl(), [], 'omipay'); Log::info('url:' . $request->getUrl(), [], 'omipay');
$response = self::getClient()->post($request->getUrl(), [ $response = self::getClient()->post($request->getUrl(), [
'query' => $params 'query' => $params
]); ]);
Log::info('request:', $params, 'omipay'); Log::info('request:', $params, 'efps');
$body = (string)$response->getBody(); $body = (string)$response->getBody();
Log::info('response:' . $body, [], 'omipay'); Log::info('response:' . $body, [], 'efps');
$result = json_decode($body, true); $result = json_decode($body, true);
if (empty($result)) { if (empty($result)) {
$result = [ $result = [
'return_code' => 'FAIL', 'returnCode' => '9999',
'error_code' => 'RESPONSE_ERROR', 'returnMsg' => '返回数据异常',
'error_msg' => '返回数据异常',
]; ];
} }
return new Result($result); return new Result($result);
} catch (Exception $e) { } catch (\Exception $e) {
Log::error('error:' . $e->getMessage(), [], 'omipay'); Log::error('error:' . $e->getMessage(), [], 'efps');
return new Result([ return new Result([
'return_code' => 'FAIL', 'returnCode' => '9998',
'error_code' => 'NETWORK_ERROR', 'returnMsg' => '网络错误',
'error_msg' => '网络错误',
]); ]);
} }
@ -81,6 +67,6 @@ abstract class AbstractApi
'x-efps-timestamp' => $timestamp, 'x-efps-timestamp' => $timestamp,
'x-efps-version' => '2.0', 'x-efps-version' => '2.0',
'x-efps-enc-key' => $encKey, 'x-efps-enc-key' => $encKey,
] ];
} }
} }

@ -3,8 +3,9 @@
namespace App\Helper\Efps; namespace App\Helper\Efps;
use App\Helper\Efps\Request\UnifiedPaymentRequest; 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) public static function unifiedPayment($outTradeNo, $orderInfo, $payAmount, $notifyUrl, $redirectUrl)
{ {
@ -15,7 +16,9 @@ class Api
$request->setPayAmount($payAmount); $request->setPayAmount($payAmount);
$request->setNotifyUrl($notifyUrl); $request->setNotifyUrl($notifyUrl);
$request->setRedirectUrl($redirectUrl); $request->setRedirectUrl($redirectUrl);
$request->setTransactionStartTime(date('YmdHis'));
$request->setAreaInfo([]);
$request->setNonceStr(StringHelper::getRandomString(32));
return self::request($request); return self::request($request);
} }
} }

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace App\Helper\Efps;
class Result
{
private $data;
public function __construct($data)
{
$this->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;
}
}

@ -20,4 +20,17 @@ class Result
public function get($key) { public function get($key) {
return $this->data[$key] ?? null; 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;
}
} }

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace App\Service; namespace App\Service;
use App\Exception\BusinessException; use App\Exception\BusinessException;
use App\Helper\Efps\PayNotify;
use App\Helper\Log; use App\Helper\Log;
use App\Helper\OmiPay\Api; use App\Helper\OmiPay\Api;
use App\Helper\OmiPay\Result; use App\Helper\OmiPay\Result;
@ -51,14 +50,15 @@ class PaymentService extends AbstractService
return date('YmdHis', $now) . $incrId; return date('YmdHis', $now) . $incrId;
} }
public function updateOrderResult(Order $order, Result $result) { public function updateOrderResult(Order $order, array $result)
if ($result->isSuccess()) { {
$order->pay_order_no = $result->get('order_no'); if ($result['is_success']) {
$order->pay_url = $result->get('pay_url'); $order->pay_order_no = $result['pay_order_no'];
$order->pay_url = $result['pay_url'];
$order->status = Order::STATUS_WAIT_PAY; $order->status = Order::STATUS_WAIT_PAY;
} else { } else {
$order->error_code = $result->get('error_code'); $order->error_code = $result['error_code'];
$order->error_msg = $result->get('error_msg'); $order->error_msg = $result['error_msg'];
$order->status = Order::STATUS_FAILED; $order->status = Order::STATUS_FAILED;
} }
$order->save(); $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/notify',
'http://146.70.113.165:9501/payment/page?order_no=' . $order->order_no 'http://146.70.113.165:9501/payment/page?order_no=' . $order->order_no
); );
$this->updateOrderResult($order, $result); $this->updateOrderResult($order, $result->toArray());
return $order; return $order;
} }
@ -168,4 +168,20 @@ class PaymentService extends AbstractService
return $params; 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;
}
} }

@ -33,9 +33,8 @@
"hyperf/rpc-client": "~2.2.0", "hyperf/rpc-client": "~2.2.0",
"hyperf/rpc-server": "~2.2.0", "hyperf/rpc-server": "~2.2.0",
"hyperf/validation": "^2.2", "hyperf/validation": "^2.2",
"phpoffice/phpspreadsheet": "^1.24", "lizhichao/one-sm": "^1.10",
"ext-json": "Required to use JSON.", "phpoffice/phpspreadsheet": "^1.24"
"ext-openssl": "Required to use HTTPS."
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^3.0", "friendsofphp/php-cs-fixer": "^3.0",

60
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0497aa465f49ca6858436c805a387c26", "content-hash": "24c591932533a2bdb8d1c21b907bda5d",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",
@ -3285,6 +3285,60 @@
], ],
"time": "2022-08-24T13:56:50+00:00" "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", "name": "maennchen/zipstream-php",
"version": "2.2.1", "version": "2.2.1",
@ -9271,8 +9325,8 @@
"prefer-stable": true, "prefer-stable": true,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=7.3" "php": ">=7.4"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.2.0" "plugin-api-version": "2.3.0"
} }

Loading…
Cancel
Save