Multi.php 7.4 KB

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