diff --git a/app/Command/JinlingCommand.php b/app/Command/JinlingCommand.php index db697a0..8c7fa5f 100644 --- a/app/Command/JinlingCommand.php +++ b/app/Command/JinlingCommand.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace App\Command; +use App\Helper\Efps\Api; use App\Helper\Signer; use App\Service\AppService; use App\Service\MerchantService; use App\Service\PaymentService; +use Hyperf\Command\Annotation\Command; use Hyperf\Command\Command as HyperfCommand; use Hyperf\Contract\ContainerInterface; @@ -38,6 +40,29 @@ class JinlingCommand extends HyperfCommand public function handle(): void { + /*Api::register([ + 'merId' => "100", // 562238003185933 + 'backUrl' => 'http://www.baidu.com', + 'certificateName' => '测试', + 'lawyerCertType' => 0, + 'lawyerCertNo' => '430481198104234557', + 'lawyerCertPhotoFront' => 'http://5b0988e595225.cdn.sohucs.com/images/20191031/e6a31c2611f042bbbfd39297243437a7.png', + 'lawyerCertPhotoBack' => 'http://5b0988e595225.cdn.sohucs.com/images/20191031/e6a31c2611f042bbbfd39297243437a7.png', + 'certificateTo' => '20500211', + 'contactPhone' => '18888888888', + 'accountType' => 3, + 'canDownType' => '01', + ]);*/ + Api::bindCard([ + 'memberId' => '562238003185933', + 'mchtOrderNo' => time() . rand(1000, 9999), + 'userName' => '测试', + 'phoneNum' => '18888888888', + 'bankCardNo' => '6214835911385365', + 'bankCardType' => 'debit', + 'certificatesNo' => '430481198104234557', + ]); + return; echo \App\Helper\Efps\Signer::sign('sss'); $sm3 = new \OneSm\Sm3(); diff --git a/app/Controller/Payment/PayController.php b/app/Controller/Payment/PayController.php index cb22fc4..08bb9aa 100644 --- a/app/Controller/Payment/PayController.php +++ b/app/Controller/Payment/PayController.php @@ -4,7 +4,15 @@ declare(strict_types=1); namespace App\Controller\Payment; +use App\Exception\BusinessException; +use App\Helper\Efps\Api; +use App\Request\BindCardConfirmRequest; +use App\Request\BindCardRequest; use App\Request\JsapiPayRequest; +use App\Request\ProtocolPayConfirmRequest; +use App\Request\ProtocolPayPreRequest; +use App\Request\RegisterRequest; +use App\Request\UnBindCardRequest; use Hyperf\HttpServer\Contract\RequestInterface; use App\Service\PaymentService; @@ -27,13 +35,63 @@ class PayController extends AbstractController ]); } - public function unified() + public function register(RequestInterface $request) { - $payRequest = new JsapiPayRequest($request->all()); - $order = $this->paymentService->js($payRequest->getApp(), $payRequest->getData()); - return $this->success([ - 'pay_url' => $order->pay_url, - 'order_no' => $order->order_no, - ]); + $req = new RegisterRequest($request->all()); + $result = Api::register($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); + } + + public function bindCard(RequestInterface $request) + { + $req = new BindCardRequest($request->all()); + $result = Api::bindCard($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); + } + + public function bindCardConfirm(RequestInterface $request) + { + $req = new BindCardConfirmRequest($request->all()); + $result = Api::bindCardConfirm($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); + } + + public function unBindCard(RequestInterface $request) + { + $req = new UnBindCardRequest($request->all()); + $result = Api::unBindCard($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); + } + + public function protocolPayPreRequest(RequestInterface $request) + { + $req = new ProtocolPayPreRequest($request->all()); + $result = Api::protocolPayPre($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); + } + + public function protocolPayConfirm(RequestInterface $request) + { + $req = new ProtocolPayConfirmRequest($request->all()); + $result = Api::protocolPayConfirm($req->getData()); + if (!$result->isSuccess()) { + throw new BusinessException($result->getMessage()); + } + return $this->success($result->getData()); } } diff --git a/app/Helper/Efps/AbstractApi.php b/app/Helper/Efps/AbstractApi.php index e97f64a..a03d850 100644 --- a/app/Helper/Efps/AbstractApi.php +++ b/app/Helper/Efps/AbstractApi.php @@ -12,12 +12,20 @@ abstract class AbstractApi { protected static $client; - public static function request(AbstractRequest $request) { - $params = $request->getParams(); + protected static $env = 'dev'; + + public static function getConfig($key) + { + $config = Config::get(self::$env); + return $config ? ($config[$key] ?? null) : null; + } + + public static function request($uri, $params, $sign) { try { - Log::info('url:' . $request->getUrl(), [], 'omipay'); - $response = self::getClient()->post($request->getUrl(), [ - 'query' => $params + Log::info('url:' . $uri, [], 'efps'); + $response = self::getClient()->post($uri, [ + 'json' => $params, + 'headers' => self::getXEfpsHeaders($sign), ]); Log::info('request:', $params, 'efps'); $body = (string)$response->getBody(); @@ -43,11 +51,8 @@ abstract class AbstractApi protected static function getClient(): Client { if (!self::$client) { self::$client = new Client([ - 'base_uri' => Config::get('base_url'), + 'base_uri' => self::getConfig('baseUrl'), 'handler' => HandlerStack::create(new CoroutineHandler()), - 'headers' => [ - 'Content-Type' => 'application/json', - ], 'timeout' => 5, 'swoole' => [ 'timeout' => 10, @@ -58,15 +63,13 @@ abstract class AbstractApi return self::$client; } - protected static function getHeaders($sign, $encKey, $timestamp) + protected static function getXEfpsHeaders($sign) { return [ - 'x-efps-sign-no' => '', - 'x-efps-sign-type' => 'RSAwithSHA256', + 'x-efps-sign-no' => self::getConfig('signNo'), + 'x-efps-sign-type' => 'SHA256withRSA', 'x-efps-sign' => $sign, - 'x-efps-timestamp' => $timestamp, - 'x-efps-version' => '2.0', - 'x-efps-enc-key' => $encKey, + 'x-efps-timestamp' => date('YmdHis') ]; } } \ No newline at end of file diff --git a/app/Helper/Efps/Api.php b/app/Helper/Efps/Api.php index adbdbf3..233dd7d 100644 --- a/app/Helper/Efps/Api.php +++ b/app/Helper/Efps/Api.php @@ -7,18 +7,154 @@ use App\Helper\StringHelper; class Api extends AbstractApi { - public static function unifiedPayment($outTradeNo, $orderInfo, $payAmount, $notifyUrl, $redirectUrl) + public static function register($params) { - $request = new UnifiedPaymentRequest(); - $request->setVersion('3.0'); - $request->setOutTradeNo($outTradeNo); - $request->setOrderInfo($orderInfo); - $request->setPayAmount($payAmount); - $request->setNotifyUrl($notifyUrl); - $request->setRedirectUrl($redirectUrl); - $request->setTransactionStartTime(date('YmdHis')); - $request->setAreaInfo([]); - $request->setNonceStr(StringHelper::getRandomString(32)); - return self::request($request); + $merId = $params['merId'] ?? ''; + $backUrl = $params['backUrl'] ?? ''; + $certificateName = $params['certificateName'] ?? ''; + $lawyerCertType = $params['lawyerCertType'] ?? 0; + $lawyerCertNo = $params['lawyerCertNo'] ?? ''; + $lawyerCertPhotoFront = $params['lawyerCertPhotoFront'] ?? ''; + $lawyerCertPhotoBack = $params['lawyerCertPhotoBack'] ?? ''; + $certificateTo = $params['certificateTo'] ?? ''; + $contactPhone = $params['contactPhone'] ?? ''; + $accountType = $params['accountType'] ?? 3; + $canDownType = $params['canDownType'] ?? '01'; + + $uri = '/api/cust/SP/Personal/apply'; + $params = [ + 'version' => '3.0', + 'acqSpId' => self::getConfig('customerCode'), + 'merId' => $merId, + 'backUrl' => $backUrl, + 'certificateName' => $certificateName, + 'lawyerCertType' => $lawyerCertType, + 'lawyerCertNo' => $lawyerCertNo, + 'lawyerCertPhotoFront' => $lawyerCertPhotoFront, + 'lawyerCertPhotoBack' => $lawyerCertPhotoBack, + 'certificateTo' => $certificateTo, + 'contactPhone' => $contactPhone, + 'accountType' => $accountType, + 'canDownType' => $canDownType, + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } + + public static function bindCard($params) + { + $memberId = $params['memberId'] ?? ''; + $mchtOrderNo = $params['mchtOrderNo'] ?? StringHelper::generateOrderNo(); + $userName = $params['userName'] ?? ''; + $phoneNum = $params['phoneNum'] ?? ''; + $bankCardNo = $params['bankCardNo'] ?? ''; + $bankCardType = $params['bankCardType'] ?? ''; + $certificatesNo = $params['certificatesNo'] ?? ''; + $expired = $params['expired'] ?? ''; + $cvn = $params['cvn'] ?? ''; + + $uri = '/api/txs/protocol/bindCard'; + $params = [ + 'version' => '3.0', + 'customerCode' => self::getConfig('customerCode'), + 'mchtOrderNo' => $mchtOrderNo, + 'memberId' => $memberId, + 'userName' => Signer::publicEncrypt($userName), + 'phoneNum' => Signer::publicEncrypt($phoneNum), + 'bankCardNo' => Signer::publicEncrypt($bankCardNo), + 'bankCardType' => $bankCardType, + 'certificatesType' => '01', + 'certificatesNo' => Signer::publicEncrypt($certificatesNo), + 'nonceStr' => StringHelper::getRandomString(32), + ]; + if ($bankCardType == 'credit') { + $params['expired'] = Signer::publicEncrypt($expired); + $params['cvn'] = Signer::publicEncrypt($cvn); + } + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } + + public static function bindCardConfirm($params) + { + $smsNo = $params['smsNo'] ?? ''; + $smsCode = $params['smsCode'] ?? ''; + $memberId = $params['memberId'] ?? ''; + $uri = '/api/txs/protocol/bindCardConfirm'; + $params = [ + 'version' => '3.0', + 'customerCode' => self::getConfig('customerCode'), + 'smsNo' => $smsNo, + 'memberId' => $memberId, + 'smsCode' => $smsCode, + 'nonceStr' => StringHelper::getRandomString(32), + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } + + public static function unBindCard($params) + { + $protocol = $params['protocol'] ?? ''; + $memberId = $params['memberId'] ?? ''; + $uri = '/api/txs/protocol/unBindCard'; + $params = [ + 'version' => '3.0', + 'customerCode' => self::getConfig('customerCode'), + 'protocol' => $protocol, + 'memberId' => $memberId, + 'nonceStr' => StringHelper::getRandomString(32), + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } + + public static function protocolPayPre($params) + { + $outTradeNo = $params['outTradeNo'] ?? StringHelper::generateOrderNo(); + $protocol = $params['protocol'] ?? ''; + $smsNo = $params['smsNo'] ?? ''; + $smsCode = $params['smsCode'] ?? ''; + $payAmount = $params['payAmount'] ?? 0; + $orderInfo = []; + $orderInfo['Id'] = $outTradeNo; + $orderInfo['businessType'] = '130001'; + $orderInfo['goodsList'] = [['name' => 'pay', 'number' => 'one', 'amount' => $$payAmount]]; + + $uri = '/api/txs/protocol/protocolPayPre'; + $params = [ + 'version' => '3.0', + 'customerCode' => self::getConfig('customerCode'), + 'outTradeNo' => $outTradeNo, + 'protocol' => $protocol, + // 'smsNo' => $smsNo, + // 'smsCode' => $smsCode, + 'orderInfo' => $orderInfo, + 'payAmount' => $payAmount, + 'payCurrency' => 'CNY', + 'isInstalments' => 0, + 'transactionStartTime' => date('YmdHis'), + 'nonceStr' => StringHelper::getRandomString(32), + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); + } + + public static function protocolPayConfirm($params) + { + $smsCode = $params['smsCode'] ?? ''; + $protocol = $params['protocol'] ?? ''; + $token = $params['token'] ?? ''; + $uri = '/api/txs/protocol/protocolPayConfirm'; + $params = [ + 'version' => '3.0', + 'customerCode' => self::getConfig('customerCode'), + 'token' => $token, + 'protocol' => $protocol, + 'smsCode' => $smsCode, + 'nonceStr' => StringHelper::getRandomString(32), + ]; + $sign = Signer::sign(json_encode($params)); + return self::request($uri, $params, $sign); } } \ No newline at end of file diff --git a/app/Helper/Efps/Config.php b/app/Helper/Efps/Config.php index e142f13..baab405 100644 --- a/app/Helper/Efps/Config.php +++ b/app/Helper/Efps/Config.php @@ -4,42 +4,26 @@ namespace App\Helper\Efps; class Config { - private static $params = [ - 'app_id' => '', - 'secret_key' => 'cea6f34bea8640dea91fd8b7a926a9a5', - 'base_url' => 'https://efps.epylinks.cn', - 'public_key' => '', - 'private_key' => '-----BEGIN PRIVATE KEY----- -MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7ibPHSAc2glW5 -ZHKhSJR8SHTwkAlN9H0KhiX+NzxuZirUd8VhyPKFcOeSvf+lRo8P7EHd2Hsfly39 -SuTZv4LUqsU0ODw0bXuC7ys1P6kctUc8Pi5Zg7l2FpVQZIwxkszevuYmjSQ7NTA7 -GAS/4pZX6C4yyDObQBV5nmSnhGXzKk/z3c6O829jgVlbFMAKKF//Q4TBiooOeZpY -rgkGtaZSN50puM+WDn7S/ETwZELBi2jRDqg6bJKYsCVPHW/yPdnMS0BMQc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ccDRz/3eCmUpyisMoXYlbLtx4GF0mPnyC -iRpQ37IYx5ZDkq4rrGvAcX9I+uMMDccAQcjvrKUn9tkCgYAcx/Usx7dsJyq9qt0x -nPryFkGoDjUg4U5WFvN4d4et4oMDAvA4iFkPxdLlGokYtLC0cEsAmKEqVKVs/Zfa -FyqcOZZSfgy6c90YmbXTmEHIayR05IxVRrL0T+/CGtnYGsNPLGNCSL9eU7zxA+QW -HeJnufFo3VmRzqOQcqrLfi/scQ== ------END PRIVATE KEY-----' + private static array $params = [ + 'dev' => [ + 'customerCode' => '562265003122220', + 'signNo' => '562265003122220003', + 'baseUrl' => 'http://test-efps.epaylinks.cn', + 'privateKeyFilePath' => BASE_PATH . '/certs/dev/user.pfx', + 'publicKeyFilePath' => BASE_PATH . '/certs/dev/efps.cer', + 'privateKeyPassword' => '123456', + ], + 'prod' => [ + 'customerCode' => '562276004021027', + 'signNo' => '562276004021027002', + 'baseUrl' => 'https://efps.epaylinks.cn', + 'privateKeyFilePath' => BASE_PATH . '/certs/prod/user.pfx', + 'publicKeyFilePath' => BASE_PATH . '/certs/prod/efps.cer', + 'privateKeyPassword' => 'iUixTxtl8N2Ntlx1LqZ', + ] ]; - public static function get($key) { - return self::$params[$key] ?: null; + public static function get($env): ?array { + return self::$params[$env] ?: null; } } \ No newline at end of file diff --git a/app/Helper/Efps/Request/AbstractRequest.php b/app/Helper/Efps/Request/AbstractRequest.php deleted file mode 100644 index 8469291..0000000 --- a/app/Helper/Efps/Request/AbstractRequest.php +++ /dev/null @@ -1,32 +0,0 @@ -uri; - } - - public function getParams() { - return $this->_params; - } - - public function __get($name) - { - return $this->_params[$name]; - } - - public function __set($name, $value) - { - $this->_params[$name] = $value; - } -} \ No newline at end of file diff --git a/app/Helper/Efps/Request/UnifiedPaymentRequest.php b/app/Helper/Efps/Request/UnifiedPaymentRequest.php deleted file mode 100644 index 068aee6..0000000 --- a/app/Helper/Efps/Request/UnifiedPaymentRequest.php +++ /dev/null @@ -1,216 +0,0 @@ -version = $version; - } - - /** - * @param mixed $outTradeNo - */ - public function setOutTradeNo($outTradeNo): void - { - $this->outTradeNo = $outTradeNo; - } - - /** - * @param mixed $customerCode - */ - public function setCustomerCode($customerCode): void - { - $this->customerCode = $customerCode; - } - - /** - * @param mixed $clientIp - */ - public function setClientIp($clientIp): void - { - $this->clientIp = $clientIp; - } - - /** - * @param mixed $orderInfo - */ - public function setOrderInfo($orderInfo): void - { - $this->orderInfo = $orderInfo; - } - - /** - * @param mixed $payAmount - */ - public function setPayAmount($payAmount): void - { - $this->payAmount = $payAmount; - } - - /** - * @param mixed $payCurrency - */ - public function setPayCurrency($payCurrency): void - { - $this->payCurrency = $payCurrency; - } - - /** - * @param mixed $noCreditCards - */ - public function setNoCreditCards($noCreditCards): void - { - $this->noCreditCards = $noCreditCards; - } - - /** - * @param mixed $notifyUrl - */ - public function setNotifyUrl($notifyUrl): void - { - $this->notifyUrl = $notifyUrl; - } - - /** - * @param mixed $redirectUrl - */ - public function setRedirectUrl($redirectUrl): void - { - $this->redirectUrl = $redirectUrl; - } - - /** - * @param mixed $attachData - */ - public function setAttachData($attachData): void - { - $this->attachData = $attachData; - } - - /** - * @param mixed $transactionStartTime - */ - public function setTransactionStartTime($transactionStartTime): void - { - $this->transactionStartTime = $transactionStartTime; - } - - /** - * @param mixed $transactionEndTime - */ - public function setTransactionEndTime($transactionEndTime): void - { - $this->transactionEndTime = $transactionEndTime; - } - - /** - * @param mixed $payMethod - */ - public function setPayMethod($payMethod): void - { - $this->payMethod = $payMethod; - } - - /** - * @param mixed $subAppId - */ - public function setSubAppId($subAppId): void - { - $this->subAppId = $subAppId; - } - - /** - * @param mixed $channelMchtNo - */ - public function setChannelMchtNo($channelMchtNo): void - { - $this->channelMchtNo = $channelMchtNo; - } - - /** - * @param mixed $enablePayChannels - */ - public function setEnablePayChannels($enablePayChannels): void - { - $this->enablePayChannels = $enablePayChannels; - } - - /** - * @param mixed $instalmentsNum - */ - public function setInstalmentsNum($instalmentsNum): void - { - $this->instalmentsNum = $instalmentsNum; - } - - /** - * @param mixed $storeId - */ - public function setStoreId($storeId): void - { - $this->storeId = $storeId; - } - - /** - * @param mixed $alipayStoreId - */ - public function setAlipayStoreId($alipayStoreId): void - { - $this->alipayStoreId = $alipayStoreId; - } - - /** - * @param mixed $extUserInfo - */ - public function setExtUserInfo($extUserInfo): void - { - $this->extUserInfo = $extUserInfo; - } - - /** - * @param mixed $terminalInfo - */ - public function setTerminalInfo($terminalInfo): void - { - $this->terminalInfo = $terminalInfo; - } - - /** - * @param mixed $areaInfo - */ - public function setAreaInfo($areaInfo): void - { - $this->areaInfo = $areaInfo; - } - - /** - * @param mixed $extendParams - */ - public function setExtendParams($extendParams): void - { - $this->extendParams = $extendParams; - } - - /** - * @param mixed $coupons - */ - public function setCoupons($coupons): void - { - $this->coupons = $coupons; - } - - /** - * @param mixed $nonceStr - */ - public function setNonceStr($nonceStr): void - { - $this->nonceStr = $nonceStr; - } -} \ No newline at end of file diff --git a/app/Helper/Efps/Result.php b/app/Helper/Efps/Result.php index 5bb6bb5..b0861dd 100644 --- a/app/Helper/Efps/Result.php +++ b/app/Helper/Efps/Result.php @@ -23,16 +23,18 @@ class Result return $this->data[$key] ?? null; } - public function toArray() + public function getMessage() { - $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 $this->data['returnMsg']; + } + + public function getData() + { + $data = $this->data; + unset($data['returnCode']); + unset($data['returnMsg']); + unset($data['nonceStr']); + unset($data['customerCode']); return $data; } } \ No newline at end of file diff --git a/app/Helper/Efps/Signer.php b/app/Helper/Efps/Signer.php index eaebfdb..98024ec 100644 --- a/app/Helper/Efps/Signer.php +++ b/app/Helper/Efps/Signer.php @@ -2,82 +2,70 @@ namespace App\Helper\Efps; +use App\Exception\BusinessException; use App\Helper\StringHelper; class Signer { - private static $privateKey = '-----BEGIN PRIVATE KE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ZfPThNk3SnVLvTPBD9cli4vbsU73YhvQp/BDA -3aw3RhscBuUCgYEA5AJ4nL/L/nLBDNuqi30FQIXCGsbAVjkC7bpVoye5b+emBXhT -S5NZ6u66dtKI+eREj2DgVIHKNS+Wsw2vHe7V4LsMKEi1X39LmsgCYMKLw7E38aiX -TmbtTPKBGIrd1QqA58LOTIvcviwDDCnuP3DWkQAa12momuPP5OdWzkqdJjECgYEA -w1kbURRUO2MWtX0jymCXim1ZhEQXhOP/EcV1WF6CbhrLiZc5tNXF6qCBdgUVjP8H -1YyiGNmy+3P4sBSzAkFOv+mcf68hl9bccDRz/3eCmUpyisMoXYlbLtx4GF0mPnyC -iRpQ37IYx5ZDkq4rrGvAcX9I+uMMDccAQcjvrKUn9tkCgYAcx/Usx7dsJyq9qt0x -nPryFkGoDjUg4U5WFvN4d4et4oMDAvA4iFkPxdLlGokYtLC0cEsAmKEqVKVs/Zfa -FyqcOZZSfgy6c90YmbXTmEHIayR05IxVRrL0T+/CGtnYGsNPLGNCSL9eU7zxA+QW -HeJnufFo3VmRzqOQcqrLfi/scQ== ------END PRIVATE KEY-----'; - - private static $publicKey = ''; - - public static function sign($params, $timestamp, $version = 2) + protected static $env = 'dev'; + + private static function getConfig($key) { - ksort($params); - $paramRows = []; - foreach ($params as $key => $value) { - $paramRows[] = $key . '=' . $value; - } - $queryString = implode('&', $paramRows); - if ($version == 1) { - return self::rsaWithSHA256Sign($queryString, self::$privateKey); - } - $randomKey = StringHelper::getRandomString(32); - $encKey = self::rsaWithSHA256Encypt($randomKey, self::$publicKey); - $sm3 = new \OneSm\Sm3(); - $signBody = $sm3->sign($queryString . $timestamp . $encKey); - return self::rsaWithSHA256Sign($signBody, self::$privateKey); + $config = Config::get(self::$env); + return $config ? ($config[$key] ?? null) : null; } - public static function rsaWithSHA256Sign($content, $privateKey) - { - $key = openssl_get_privatekey($privateKey); - // openssl_private_encrypt($content, $signature, $privateKey, OPENSSL_PKCS1_PADDING); - openssl_sign($content, $signature, $key, OPENSSL_ALGO_SHA256); - openssl_free_key($key); - return base64_encode($signature); + public static function sign($data) { + $certs = []; + openssl_pkcs12_read( + file_get_contents(self::getConfig('privateKeyFilePath')), + $certs, + self::getConfig('privateKeyPassword') + ); //其中password为你的证书密码 + + if (empty($certs)) { + throw new BusinessException('请检查RSA私钥配置'); + } + + openssl_sign($data, $sign, $certs['pkey'],OPENSSL_ALGO_SHA256); + $sign = base64_encode($sign); + return $sign; } - public static function rsaWithSHA256Encypt($content, $publicKey) - { - $key = openssl_get_publickey($publicKey); - openssl_public_decrypt($content, $signature, $key, OPENSSL_PKCS1_PADDING); - openssl_free_key($key); - return base64_encode($signature); + public static function verify($data, $sign) { + + //读取公钥文件 + $pubKey = file_get_contents(self::getConfig('publicKeyFilePath')); + + $res = openssl_get_publickey($pubKey); + + if (empty($res)) { + throw new BusinessException('RSA公钥错误, 请检查公钥文件格式是否正确'); + } + //调用openssl内置方法验签,返回bool值 + + $result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256); + // 释放资源 + openssl_free_key($res); + + return $result; } - public function verify($content, $sign, $publicKey) + public static function publicEncrypt($data) { - $key = openssl_get_publickey($publicKey); - $ok = openssl_verify($content, base64_decode($sign), $key, 'SHA256'); - openssl_free_key($key); - return $ok; - } + //读取公钥文件 + $pubKey = file_get_contents(self::getConfig('publicKeyFilePath')); + $res = openssl_get_publickey($pubKey); + + if (empty($res)) { + throw new BusinessException('RSA公钥错误, 请检查公钥文件格式是否正确'); + } + + $crypttext = ""; + openssl_public_encrypt($data,$crypttext, $res ); + openssl_free_key($res); + + return(base64_encode($crypttext)); + } } \ No newline at end of file diff --git a/app/Helper/StringHelper.php b/app/Helper/StringHelper.php index 122849c..6aff791 100644 --- a/app/Helper/StringHelper.php +++ b/app/Helper/StringHelper.php @@ -35,4 +35,15 @@ class StringHelper return $password; } + + public static function generateOrderNo() { + $now = time(); + $key = RedisKey::getGenerateOrderNoKey($now); + $incrId = Redis::incr($key); + $incrId = '' . $incrId; + Redis::expire($key, 5*60); + $padLength = 6 - strlen($incrId); + $incrId = str_pad($incrId, $padLength, '0', STR_PAD_LEFT); + return date('YmdHis', $now) . $incrId; + } } \ No newline at end of file diff --git a/app/Request/BindCardConfirmRequest.php b/app/Request/BindCardConfirmRequest.php new file mode 100644 index 0000000..ee328c4 --- /dev/null +++ b/app/Request/BindCardConfirmRequest.php @@ -0,0 +1,11 @@ +