Multi.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php namespace Pay\Yspay;
  2. use Dever;
  3. use Dever\Http\Curl;
  4. # 银联商务商户资金自主管理系统
  5. class Multi
  6. {
  7. static $host = 'https://im.chinaums.com/channel/Business/UnifyMulti/';
  8. public function act($config, $merchant, $info, $auto = 1)
  9. {
  10. $result = $this->huafu($config, $merchant, $info);
  11. if ($result == 'ok') {
  12. //$this->fenzhang($config, $merchant, $info);
  13. }
  14. return $result;
  15. }
  16. # 划付
  17. protected function huafu($config, $merchant, $info, $auto = 1)
  18. {
  19. $this->config = $config;
  20. $mid = $merchant['mid'];
  21. $cash = $info['hf_cash'];
  22. $order_num = $this->getOrderNum($info['order_num']);
  23. //整理内容信息
  24. $content = [
  25. 'merNo' => $merchant['merno'],
  26. //'merOrderNo' => $order_num,
  27. 'payAmt' => $cash,
  28. ];
  29. Dever::log($content, 'yspay_huafu');
  30. $result = $this->curl('202002', $content);
  31. if ($result == 'ok') {
  32. $data['status'] = 1;
  33. } else {
  34. $data['status'] = 3;
  35. $data['error'] = $result;
  36. }
  37. $data['merchant_id'] = $merchant['id'];
  38. $data['mid'] = $mid;
  39. $data['order_num'] = $order_num;
  40. $data['type'] = 1;
  41. $data['cash'] = $cash;
  42. $data['tdate'] = time();
  43. $data['desc'] = $content['ps'];
  44. $data['auto'] = $auto;
  45. Dever::db('pay/yspay_cash_log')->insert($data);
  46. return 'ok';
  47. }
  48. # 分账
  49. protected function fenzhang($config, $merchant, $info, $auto = 1)
  50. {
  51. $this->config = $config;
  52. $mid = $merchant['mid'];
  53. $cash = $info['fz_cash'];
  54. if (!$cash || $cash <= 0) {
  55. return false;
  56. }
  57. $order_num = $this->getOrderNum($info['order_num']);
  58. //整理内容信息
  59. $content = [
  60. 'merNo' => $merchant['merno'],
  61. 'merOrderNo' => $order_num,
  62. 'payAmt' => $cash,
  63. 'cardNo' => hash("sha256",$this->config['card']),
  64. 'ps' => $merchant['name'] . '分账',
  65. ];
  66. Dever::log($content, 'yspay_fenzhang');
  67. $result = $this->curl('202003', $content);
  68. if ($result == 'ok') {
  69. $data['status'] = 1;
  70. } else {
  71. $data['status'] = 3;
  72. $data['error'] = $result;
  73. }
  74. $data['merchant_id'] = $merchant['id'];
  75. $data['mid'] = $mid;
  76. $data['order_num'] = $order_num;
  77. $data['type'] = 2;
  78. $data['cash'] = $cash;
  79. $data['tdate'] = time();
  80. $data['desc'] = $content['ps'];
  81. $data['auto'] = $auto;
  82. Dever::db('pay/yspay_cash_log')->insert($data);
  83. }
  84. protected function getOrderNum($order_num)
  85. {
  86. $where['order_num'] = $order_num . '_' . Dever::rand(8, 0);
  87. $state = Dever::db('pay/yspay_cash_log')->one($where);
  88. if (!$state) {
  89. return $where['order_num'];
  90. } else {
  91. return $this->getOrderNum($order_num);
  92. }
  93. }
  94. protected function header($code)
  95. {
  96. $header = array(
  97. 'transCode' => $code,
  98. 'verNo' => '100',
  99. 'srcReqDate' => date("Ymd"),
  100. 'srcReqTime' => date("His"),
  101. 'srcReqId' => Dever::uuid(),
  102. 'channelId' => '043',
  103. 'groupId' => $this->config['cash_groupid'],
  104. );
  105. return $header;
  106. }
  107. protected function curl($code, $param, $state = true)
  108. {
  109. $url = self::$host;
  110. $url .= $code;
  111. $header = $this->header($code);
  112. $header['signature'] = $this->sign($param);
  113. echo $url;
  114. print_r($header);
  115. print_r($param);
  116. $curl = Curl::getInstance($url, $param, 'post', true, $header)->setResultHeader(true);
  117. $header = $curl->header();
  118. $body = $curl->result();
  119. print_r($header);
  120. print_r($body);die;
  121. if (strstr($body, '<html><head>')) {
  122. return 'error';
  123. }
  124. $body = Dever::json_decode($body);
  125. if (isset($header['respCode'])) {
  126. if ($header['respCode'] == '99999999') {
  127. return $state ? 'ok' : $body;
  128. } elseif ($header['respCode'] == 'FAN00012') {
  129. return $this->curl($code, $param, $state);
  130. } else {
  131. return $header['respMsg'];
  132. }
  133. } else {
  134. return 'error';
  135. }
  136. }
  137. protected function sign($data)
  138. {
  139. $data = $this->getParams($data);
  140. $file = $this->config['cash_private_file'];
  141. if (!strstr($file, 'http')) {
  142. $file = Dever::local($file);
  143. }
  144. $pkcs12 = file_get_contents($file);
  145. openssl_pkcs12_read($pkcs12, $certs, $this->config['cash_private_file_password']);
  146. if (!$certs) {
  147. Dever::alert('private_key error');
  148. }
  149. $privateKey = $certs['pkey'];
  150. if (openssl_sign(utf8_encode($data), $binarySignature, $privateKey, OPENSSL_ALGO_SHA256)) {
  151. return bin2hex($binarySignature);
  152. } else {
  153. return '';
  154. }
  155. }
  156. protected function checkSign($data, $signature)
  157. {
  158. $file = $this->config['cash_public_file'];
  159. if (!strstr($file, 'http')) {
  160. $file = Dever::local($file);
  161. }
  162. $cert = file_get_contents($file);
  163. $cert = '-----BEGIN CERTIFICATE-----' . PHP_EOL
  164. . chunk_split(base64_encode($cert), 64, PHP_EOL)
  165. . '-----END CERTIFICATE-----' . PHP_EOL;
  166. $pubKeyId = openssl_get_publickey($cert);
  167. $signature = hex2bin($signature);
  168. $ok = openssl_verify($data, $signature, $pubKeyId, OPENSSL_ALGO_SHA256);
  169. if ($ok == 1) {
  170. openssl_free_key($pubKeyId);
  171. return true;
  172. }
  173. return false;
  174. }
  175. protected function getParams($param)
  176. {
  177. ksort($param);
  178. $result = array();
  179. foreach ($param as $k => $v) {
  180. if (is_array($v)) {
  181. $v = json_encode($v, JSON_UNESCAPED_UNICODE);
  182. } elseif(trim($v) == ""){
  183. continue;
  184. }
  185. if (is_bool($v)) {
  186. $result[] = $v ? "$k=true" : "$k=false";
  187. } else {
  188. $result[] = $k . '=' . $v;
  189. }
  190. }
  191. $result = implode('&', $result);
  192. return $result;
  193. }
  194. }