SuningPay.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\common\library;
  3. use think\Exception;
  4. class SuningPay
  5. {
  6. public function __construct(){
  7. $this->private_key = APP_PATH.'common/library/suning/private.pem';//私钥
  8. $this->public_key = APP_PATH.'common/library/suning/public.pem';//公钥
  9. $this->wxappid = 'wxdada9bc45300a359'; //小程序id
  10. // $this->wxkey = 'UYko5ZEZHb9WrGL8Jws6Qr14kGkOBrxf';
  11. }
  12. /**
  13. * 微信支付
  14. * @return void
  15. */
  16. public function wechath5($params=[])
  17. {
  18. $result = [
  19. 'status' => 1,
  20. 'msg' => '',
  21. 'data' => [],
  22. ];
  23. try {
  24. //交易地址
  25. $url = 'https://mfg.suning.com/ftpgs/trade/aggrpay/aggrProgramPay';
  26. // $url = 'https://ftpgspre.cnsuning.com/ftpgs/trade/aggrpay/aggrProgramPay';
  27. $noticeUrl = request()->root(true).'/api/Supay/notify';
  28. //var_dump($params);exit;
  29. $toParams = [
  30. 'mchtNo' => 'SJ103596',//商户代码 SJ+六位数字(平台分配固定值)
  31. 'orderName' => $params['goods_name'],//订单名称
  32. 'amount' => $params['money'],//订单金额 元
  33. 'orderId' => $params['order_no'],//支付单号 全局唯一,不低于14位
  34. 'noticeUrl' => $noticeUrl,//回调通知地址 https地址
  35. 'returnUrl' => '',//没有可以不写
  36. 'remark' => $params['goods_name'],//备注
  37. ];
  38. ksort($toParams);
  39. $newpar = json_encode($toParams,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  40. $timestamp = date('YmdHms',time());
  41. $str = 'appId=yfbmSJ103596e2023092201&params='.$newpar.'&timestamp='.$timestamp.'&version=1.0';
  42. $newstr = strtoupper(md5($str));
  43. // echo $newstr;exit;
  44. $sign = $this->signNew($newstr);
  45. //公共参数
  46. $pubParams = [
  47. 'version' => '1.0',//接口版本号 固定送1.0
  48. 'appId' => 'yfbmSJ103596e2023092201',//应用唯一标识 提供给第三方应用的唯一标识,
  49. 'signType' => 'RSA',//签名类型 固定送RSA,签名算法SHA1withRSA
  50. 'signkeyIndex' => '0001',//公钥索引 商户应用接入前自行生成密钥对,将公钥提供苏宁后配置产生的索引号
  51. 'sign' => $sign,//签名 使用商户私钥加签后产生的签名信息
  52. 'timestamp' => $timestamp,//请求时间戳 yyyyMMddHHmmss
  53. 'params' => $newpar,//业务报文参数 业务接口请求报文参数,JSON格式。
  54. ];
  55. // $header = 'content-type=applicaton/json';
  56. $pubParams = json_encode($pubParams,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  57. $res = $this->postcurl($url,$pubParams);
  58. $result['data'] = json_decode($res,true);
  59. return $result;
  60. } catch (Exception $e) {
  61. $result['status'] = 0;
  62. $result['msg'] = $e->getMessage();
  63. }
  64. return $result;
  65. }
  66. private function signNew($str) {
  67. $file = file_get_contents($this->private_key);
  68. if (!$file) {
  69. throw new \Exception('loadPk12Cert::file
  70. _get_contents');
  71. }
  72. $pkeyid = openssl_pkey_get_private($file);
  73. // if (!openssl_pkcs12_read($file, $cert, $pkeyid)) {
  74. // throw new \Exception('loadPk12Cert::openssl_pkcs12_read ERROR');
  75. // }
  76. // $pem = $cert['pkey'];
  77. openssl_sign($str, $sign, $pkeyid);
  78. $sign = base64_encode($sign);
  79. return $sign;
  80. }
  81. //获取签名
  82. private function createSignaTure($dates) {
  83. ksort($dates);
  84. $str = '';
  85. foreach($dates as $k => $v) {
  86. if($v != '') {
  87. $str .= $k.'='.$v.'&';
  88. }
  89. }
  90. //$str .= strtolower(md5($key));
  91. return $str;
  92. }
  93. //获取参数
  94. private function getToStr($arr) {
  95. $info = $this -> createSignaTure($arr);
  96. $str = '';
  97. foreach($arr as $k => $v) {
  98. if($v != '') {
  99. $str .= $k.'='.urlencode($v).'&';
  100. }
  101. }
  102. // $str .= Config::TRADE_SIGNATURE_KEY.Config::TRADE_QSTRING_EQUAL.$info;
  103. // file_put_contents('../baowen.log', $str."\r\n");
  104. return $str;
  105. }
  106. private function postcurl($url, $data)
  107. {
  108. $curl = curl_init();
  109. curl_setopt($curl, CURLOPT_URL, $url);
  110. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  111. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  112. if(!$data){
  113. return 'data is null';
  114. }
  115. if(is_array($data))
  116. {
  117. $data = json_encode($data);
  118. }
  119. curl_setopt($curl, CURLOPT_POST, 1);
  120. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  121. curl_setopt($curl, CURLOPT_HEADER, 0);
  122. curl_setopt($curl, CURLOPT_HTTPHEADER,array(
  123. 'Content-Type: application/json; charset=utf-8',
  124. 'Content-Length:' . strlen($data),
  125. 'Cache-Control: no-cache',
  126. 'Pragma: no-cache'
  127. ));
  128. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  129. $res = curl_exec($curl);
  130. $errorno = curl_errno($curl);
  131. if ($errorno) {
  132. return $errorno;
  133. }
  134. curl_close($curl);
  135. return $res;
  136. }
  137. private function getSignContent($params) {
  138. ksort($params);
  139. $stringToBeSigned = "";
  140. $i = 0;
  141. foreach ($params as $k => $v) {
  142. //false === $this->checkEmpty($v) &&
  143. if ("@" != substr($v, 0, 1)) {
  144. if ($i == 0) {
  145. $stringToBeSigned .= "$k" . "=" . "$v";
  146. } else {
  147. $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  148. }
  149. $i++;
  150. }
  151. }
  152. unset ($k, $v);
  153. return $stringToBeSigned;
  154. }
  155. private function checkEmpty($value)
  156. {
  157. if (!isset($value))
  158. return true;
  159. if ($value === null)
  160. return true;
  161. if (trim($value) === "")
  162. return true;
  163. return false;
  164. }
  165. }