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