Multi.php 7.1 KB

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