Multi.php 6.1 KB

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