Multi.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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('202002', $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('202004', $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. # 查询余额
  86. public function yue($config, $merchant)
  87. {
  88. $this->config = $config;
  89. //整理内容信息
  90. $content = [
  91. 'merNo' => $merchant['merno'],
  92. ];
  93. Dever::log($content, 'yspay_yue');
  94. $result = $this->curl('202006', $content, false);
  95. if (isset($result['canPayAmt'])) {
  96. return $result['canPayAmt'];
  97. }
  98. return 0;
  99. }
  100. # 提现
  101. public function tixian($config, $merchant, $cash)
  102. {
  103. $this->config = $config;
  104. $mid = $merchant['mid'];
  105. //整理内容信息
  106. $content = [
  107. 'merNo' => $merchant['merno'],
  108. 'payAmt' => $cash,
  109. 'ps' => $merchant['name'] . '划付',
  110. ];
  111. Dever::log($content, 'yspay_huafu_tixian');
  112. $result = $this->curl('202002', $content);
  113. return 'ok';
  114. }
  115. protected function getOrderNum($order_num)
  116. {
  117. $where['order_num'] = $order_num . '_' . Dever::rand(8, 0);
  118. $state = Dever::db('pay/yspay_cash_log')->one($where);
  119. if (!$state) {
  120. return $where['order_num'];
  121. } else {
  122. return $this->getOrderNum($order_num);
  123. }
  124. }
  125. protected function common($param, $code)
  126. {
  127. $param += array(
  128. 'transCode' => $code,
  129. 'verNo' => '100',
  130. 'srcReqDate' => date("Ymd"),
  131. 'srcReqTime' => date("His"),
  132. 'srcReqId' => Dever::uuid(),
  133. 'channelId' => '043',
  134. 'groupId' => $this->config['cash_groupid'],
  135. );
  136. return $param;
  137. }
  138. protected function curl($code, $param, $state = true)
  139. {
  140. $url = self::$host;
  141. $url .= $code;
  142. $param = $this->common($param, $code);
  143. $param['signature'] = $this->sign($param);
  144. $curl = Curl::getInstance($url, $param, 'post', true);
  145. $body = $curl->result();
  146. if (strstr($body, '<html><head>')) {
  147. return 'error';
  148. }
  149. $body = Dever::json_decode($body);
  150. if (isset($body['respCode'])) {
  151. if ($body['respCode'] == '99999999') {
  152. return $state ? 'ok' : $body;
  153. } elseif ($body['respCode'] == 'FAN00012') {
  154. return $this->curl($code, $param, $state);
  155. } else {
  156. return $body['respMsg'];
  157. }
  158. } else {
  159. return 'error';
  160. }
  161. }
  162. protected function sign($data)
  163. {
  164. $data = $this->getParams($data);
  165. $file = $this->config['cash_private_file'];
  166. if (!strstr($file, 'http')) {
  167. $file = Dever::local($file);
  168. }
  169. $pkcs12 = file_get_contents($file);
  170. openssl_pkcs12_read($pkcs12, $certs, $this->config['cash_private_file_password']);
  171. if (!$certs) {
  172. Dever::alert('private_key error');
  173. }
  174. $privateKey = $certs['pkey'];
  175. if (openssl_sign(utf8_encode($data), $binarySignature, $privateKey, OPENSSL_ALGO_SHA256)) {
  176. return bin2hex($binarySignature);
  177. } else {
  178. return '';
  179. }
  180. }
  181. protected function checkSign($data, $signature)
  182. {
  183. $file = $this->config['cash_public_file'];
  184. if (!strstr($file, 'http')) {
  185. $file = Dever::local($file);
  186. }
  187. $cert = file_get_contents($file);
  188. $cert = '-----BEGIN CERTIFICATE-----' . PHP_EOL
  189. . chunk_split(base64_encode($cert), 64, PHP_EOL)
  190. . '-----END CERTIFICATE-----' . PHP_EOL;
  191. $pubKeyId = openssl_get_publickey($cert);
  192. $signature = hex2bin($signature);
  193. $ok = openssl_verify($data, $signature, $pubKeyId, OPENSSL_ALGO_SHA256);
  194. if ($ok == 1) {
  195. openssl_free_key($pubKeyId);
  196. return true;
  197. }
  198. return false;
  199. }
  200. protected function getParams($param)
  201. {
  202. ksort($param);
  203. $result = array();
  204. foreach ($param as $k => $v) {
  205. if (is_array($v)) {
  206. $v = json_encode($v, JSON_UNESCAPED_UNICODE);
  207. } elseif(trim($v) == ""){
  208. continue;
  209. }
  210. if (is_bool($v)) {
  211. $result[] = $v ? "$k=true" : "$k=false";
  212. } else {
  213. $result[] = $k . '=' . $v;
  214. }
  215. }
  216. $result = implode('&', $result);
  217. return $result;
  218. }
  219. }