HuijuH5Pay.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\library;
  3. use think\Controller;
  4. //汇聚银联h5、支付宝h5
  5. class HuijuH5Pay extends Controller
  6. {
  7. public function __construct(){
  8. $this->mch_no_trade = "888117500004672";
  9. //报备商户号
  10. $this->TradeMerchantNo = "777171600367612";
  11. //商户私钥
  12. $this->mchPrivateKey = "40c310e1a575470cb660f08937bf6313";
  13. }
  14. //银联h5
  15. public function ylh5Pay($order_no,$ReturnUrl,$notifyurl,$good_name,$amount,$m_id,$type='1'){
  16. if ($type==1){
  17. $q1_fr = "ALIPAY_H5";
  18. }elseif ($type==2){
  19. $q1_fr = "UNIONPAY_H5";
  20. }
  21. //1.版本号
  22. $params["p0_Version"] = "2.1";
  23. //2.商户编号
  24. $params["p1_MerchantNo"] = $this->mch_no_trade;
  25. //3.商户订单号
  26. $params["p2_OrderNo"] = $order_no;
  27. //4.订单金额
  28. $params["p3_Amount"] = $amount;
  29. //5.交易币种
  30. $params["p4_Cur"] = "1";
  31. //6.商品名称
  32. $params["p5_ProductName"] = $good_name;
  33. //7.商品描述
  34. $params["p6_ProductDesc"] = "";
  35. //8.公用回传参数
  36. $params["p7_Mp"] = $m_id;
  37. //9.商户页面通知地址
  38. $params["p8_ReturnUrl"] = $ReturnUrl;
  39. //10.服务器异步通知地址
  40. $params["p9_NotifyUrl"] = $notifyurl;
  41. //11.交易类型
  42. $params["q1_FrpCode"] = $q1_fr;
  43. //18.微信 H5 模 式
  44. //$params["q9_TransactionModel"] = "MODEL1"; //新增
  45. //19.报备商户号
  46. $params["qa_TradeMerchantNo"] = $this->TradeMerchantNo; //新增
  47. $hmacVal = urlencode($this->hmacRequest($params,$this->mchPrivateKey));
  48. $params['hmac'] = $hmacVal;
  49. $url = 'https://www.joinpay.com/trade/uniPayApi.action';
  50. $result = http_post($url,$params);
  51. $result = json_decode($result,true);
  52. //dump($result);die;
  53. return $result;
  54. }
  55. /**
  56. * 生成签名
  57. * @param $params
  58. * @param $key
  59. * @param string $encryptType
  60. * @return string
  61. */
  62. function hmacRequest($params, $key, $encryptType = "1")
  63. {
  64. if ("1" == $encryptType) {
  65. return md5(implode("", $params) . $key);
  66. } else {
  67. $private_key = openssl_pkey_get_private($key);
  68. $params = implode("", $params);
  69. openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
  70. openssl_free_key($private_key);
  71. $sign = base64_encode($sign);
  72. return $sign;
  73. }
  74. }
  75. /**
  76. * 批量代付生成签名,由于批量代付包含details的字段,需要特殊处理
  77. * @param $params
  78. * @param $key
  79. * @param string $encryptType
  80. * @return string
  81. */
  82. function batchHmacRequest($params, $key, $encryptType = "1")
  83. {
  84. $details=$params['details'];
  85. unset($params['details']);
  86. $detailsStr='';
  87. foreach ($details as $k=>$value){
  88. $detailsStr.= implode("", $value);
  89. }
  90. if ("1" == $encryptType) {
  91. return md5(implode("", $params) . $detailsStr . $key);
  92. } else {
  93. $private_key = openssl_pkey_get_private($key);
  94. $params = implode("", $params).$detailsStr;
  95. openssl_sign($params, $sign, $private_key, OPENSSL_ALGO_MD5);
  96. openssl_free_key($private_key);
  97. $sign = base64_encode($sign);
  98. return $sign;
  99. }
  100. }
  101. //生成订单号
  102. function getOrderId()
  103. {
  104. return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  105. }
  106. /**
  107. * 生成批量代付detail
  108. *
  109. *$detail=array(
  110. * 0=>array(
  111. * 1, //detailId
  112. * '张三',//payee
  113. * '6228480402564890018',//'bankacount'
  114. * '0.1',// 'money'
  115. * '代付接口测试1',//'desc'
  116. * '广州市',// 'city'
  117. * 0,//'isVerify'
  118. * 1,//'PublicOrPrivate'
  119. * 102902500019,// 'num'
  120. * )
  121. *
  122. * )
  123. * return str
  124. *
  125. * example
  126. * 代付明细序号(必填)|收款人姓名(必填)|收款银行帐号(必填)|代付金额(必填)|代付说明|收款银行所在城市(必填)|是否审核(1:审核 0:不审核)|对公对私(1:对公 2:对私)|联行号(对公必填)
  127. * 1001|张三|6228480402564890018|10.01|代付接口测试1|广州市|1|1|102902500019^1001|李四|6228480402564890018|10.01|代付接口测试2|广州市|1|1|102902500019
  128. **/
  129. function getProxyPayDetail(Array $detail)
  130. {
  131. $result = '';
  132. foreach ($detail as $key => $item) {
  133. if ($key != 0) {
  134. $result .= '^';
  135. }
  136. $result .= implode('|', array_values($item));
  137. }
  138. return $result;
  139. }
  140. function decodeUnicode($str)
  141. {
  142. return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
  143. create_function(
  144. '$matches',
  145. 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
  146. ),
  147. $str);
  148. }
  149. function output($str){
  150. echo '<pre>'.decodeUnicode(json_encode(json_decode($str), JSON_PRETTY_PRINT)).'</pre>';
  151. }
  152. /**
  153. * 判断文件是是否是图片
  154. * @param $fileName
  155. * @param $fileTmpName
  156. * @return bool true|false
  157. */
  158. function checkIsImage($fileName,$fileTmpName){
  159. if( !in_array(substr(strrchr ( $fileName , "." ),1), array('jpg','png','bmp','jpeg')) || !getimagesize($fileTmpName)){
  160. return false;
  161. }
  162. return true;
  163. }
  164. /**
  165. * 获取图片的base64
  166. * @param $file $_FILES["file"]["tmp_name"]
  167. * @param $type png/
  168. * @return string
  169. */
  170. function getImageBase64($file,$type){
  171. $bin_string = file_get_contents($file);
  172. return 'data:' . $type . ';base64,' . base64_encode($bin_string);
  173. }
  174. /**
  175. * 生成批次id
  176. * @param $length
  177. * @return string
  178. */
  179. function getBatchId()
  180. {
  181. return "batch".getOrderId();
  182. }
  183. }