123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- namespace app\common\library;
- use think\Exception;
- class SuningPay
- {
- public function __construct(){
- $this->private_key = APP_PATH.'common/library/suning/private.pem';
- $this->public_key = APP_PATH.'common/library/suning/public.pem';
- $this->wxappid = 'wxdada9bc45300a359';
-
- }
-
- public function wechath5($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
-
- $url = 'https://mfg.suning.com/ftpgs/trade/aggrpay/aggrProgramPay';
-
- $noticeUrl = request()->root(true).'/api/Supay/notify';
-
- $toParams = [
- 'mchtNo' => 'SJ103596',
- 'orderName' => $params['goods_name'],
- 'amount' => $params['money'],
- 'orderId' => $params['order_no'],
- 'noticeUrl' => $noticeUrl,
- 'returnUrl' => '',
- 'remark' => $params['goods_name'],
- ];
-
- ksort($toParams);
- $newpar = json_encode($toParams,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
- $timestamp = date('YmdHms',time());
-
- $str = 'appId=yfbmSJ103596e2023092201¶ms='.$newpar.'×tamp='.$timestamp.'&version=1.0';
-
- $newstr = strtoupper(md5($str));
-
- $sign = $this->signNew($newstr);
-
-
-
- $pubParams = [
- 'version' => '1.0',
- 'appId' => 'yfbmSJ103596e2023092201',
- 'signType' => 'RSA',
- 'signkeyIndex' => '0001',
- 'sign' => $sign,
- 'timestamp' => $timestamp,
- 'params' => $newpar,
- ];
-
- $pubParams = json_encode($pubParams,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
- $res = $this->postcurl($url,$pubParams);
-
- $result['data'] = json_decode($res,true);
-
- return $result;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- private function signNew($str) {
- $file = file_get_contents($this->private_key);
- if (!$file) {
- throw new \Exception('loadPk12Cert::file
- _get_contents');
- }
- $pkeyid = openssl_pkey_get_private($file);
-
-
-
-
-
- openssl_sign($str, $sign, $pkeyid);
- $sign = base64_encode($sign);
- return $sign;
- }
-
- private function createSignaTure($dates) {
- ksort($dates);
- $str = '';
- foreach($dates as $k => $v) {
- if($v != '') {
- $str .= $k.'='.$v.'&';
- }
- }
-
- return $str;
- }
-
- private function getToStr($arr) {
-
- $info = $this -> createSignaTure($arr);
- $str = '';
- foreach($arr as $k => $v) {
- if($v != '') {
- $str .= $k.'='.urlencode($v).'&';
- }
- }
- return $str;
- }
- private function postcurl($url, $data)
- {
-
- $curl = curl_init();
-
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- if(!$data){
- return 'data is null';
- }
- if(is_array($data))
- {
- $data = json_encode($data);
- }
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_HTTPHEADER,array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length:' . strlen($data),
- 'Cache-Control: no-cache',
- 'Pragma: no-cache'
- ));
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $res = curl_exec($curl);
- $errorno = curl_errno($curl);
- if ($errorno) {
- return $errorno;
- }
- curl_close($curl);
- return $res;
-
-
- }
- private function getSignContent($params) {
- ksort($params);
- $stringToBeSigned = "";
- $i = 0;
- foreach ($params as $k => $v) {
-
- if ("@" != substr($v, 0, 1)) {
- if ($i == 0) {
- $stringToBeSigned .= "$k" . "=" . "$v";
- } else {
- $stringToBeSigned .= "&" . "$k" . "=" . "$v";
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
- private function checkEmpty($value)
- {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
- }
|