send_weixin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. require_once 'inc.php';
  3. //if (_G('orderid') == '' || !_G('orderid')||!$userid||$userid=='') {
  4. // echo "<script>alert('网络传输中掉包,请重新支付!')</script>";
  5. // exit;
  6. //}
  7. //定义数据
  8. $email='900024';
  9. $userid='851811130002';
  10. $userkey='8a9bd64e6585c6d8016585c6c8980002';
  11. //1.获取数据
  12. $orderid = 'nanodream'.date('YmdHis',time());//_G('orderid'); //订单号
  13. $price = rand(1,5); //交易金额
  14. $NotifyUrl = 'http://' . _S('HTTP_HOST') . '/pay/sbweixin_bank/notify_url.php'; //服务端返回地址
  15. //$order_model = new orders_class();
  16. //$addtime = $order_model->fetch_one('orderlist', "orderid='" . $orderid . "'", 'addtime');
  17. //$addtime = $addtime['addtime'];
  18. //2.组装数据,用于生成签名
  19. $reqestdata = array(
  20. 'inst_no' => $email, //机构号
  21. 'mch_no' => $userid,//商户号
  22. 'pay_trace_no' => $orderid,//请求流水号
  23. 'pay_time' => date('YmdHis',time()),
  24. 'total_amount' => $price * 100,//订单总金额
  25. 'notify_url' => $NotifyUrl,
  26. 'order_body' => "客服QQ3007549789订单号" . $orderid,//支付凭证商品描述信息,不填写默认为交易订单编号
  27. 'pay_type' => '400',
  28. );
  29. //3.生成签名
  30. $buidSign = makesign($reqestdata, $userkey);
  31. //4.组装数据,用于发起请求支付
  32. $reqestdata['sign'] = $buidSign;
  33. //5.传递数据,请求数据
  34. $url = "http://demo.payplat.cn/openpay/v3/prepay";
  35. $respsonData = post_json($url, json_encode($reqestdata)); //发送请求
  36. //6.获取数据
  37. $respsonData = json_decode($respsonData, true);
  38. if ($respsonData['return_code'] != 'SUCCESS' || $respsonData['result_code'] != 'PAY_SUCCESS') {
  39. $error_message = "错误代码:" . $respsonData['result_code'] . " 错误描述:" . $respsonData['return_msg'] . " 错误信息" . $respsonData['return_code'];
  40. exit($error_message);
  41. }
  42. //7.赋值数据
  43. $img = $respsonData['qrcode'];
  44. echo $img;
  45. /**生成签名
  46. * @param $params /请求的参数数组
  47. * @param $key /支付密钥
  48. * @return string
  49. */
  50. function makesign($paras, $key)
  51. {
  52. // $sort_array = $this->array_sort(array_filter($paras)); //删除数组中的空值并排序
  53. $sort_array = array_sort($paras);
  54. $prestr = create_linkstring($sort_array); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  55. $prestr = $prestr . "&key=" . $key; //把拼接后的字符串再与安全校验码直接连接起来
  56. // \Think\Log::record('prestr:' . $prestr);
  57. $mysgin = sign($prestr, 'MD5'); //把最终的字符串签名,获得签名结果
  58. return $mysgin;
  59. }
  60. /**
  61. * 对数组排序
  62. * $array 排序前的数组
  63. * return 排序后的数组
  64. */
  65. function array_sort($array)
  66. {
  67. ksort($array); //按照key值升序排列数组
  68. return $array;
  69. }
  70. /**
  71. * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  72. * $array 需要拼接的数组
  73. * return 拼接完成以后的字符串
  74. */
  75. function create_linkstring($array)
  76. {
  77. $str = "";
  78. foreach ($array as $key => $val) {
  79. ;
  80. //键值为空的参数不参与排序,键名为key的参数不参与排序
  81. if ($key != "sign") {
  82. $str .= $key . "=" . $val . "&";
  83. }
  84. }
  85. $str = trim($str, "&");
  86. return $str; //返回参数
  87. }
  88. /**
  89. * 签名字符串
  90. * $prestr 需要签名的字符串
  91. * $sign_type 签名类型,也就是sec_id
  92. * return 签名结果
  93. */
  94. function sign($prestr, $sign_type)
  95. {
  96. $sign = '';
  97. if ($sign_type == 'MD5') {
  98. $sign = md5($prestr); //MD5加密
  99. } elseif ($sign_type == 'DSA') {
  100. //DSA 签名方法待后续开发
  101. die("DSA 签名方法待后续开发,请先使用MD5签名方式");
  102. } elseif ($sign_type == "") {
  103. die("sign_type为空,请设置sign_type");
  104. } else {
  105. die("暂不支持" . $sign_type . "类型的签名方式");
  106. }
  107. return strtolower($sign); //返回参数并小写
  108. }
  109. function requestAsHttpPOST($data, $service_url)
  110. {
  111. $HTTP_TIME_OUT = "20";
  112. $http_data = $this->array_sort(array_filter($data)); //删除数组中的空值并排序
  113. $post_data = http_build_query($http_data);
  114. $options = array(
  115. 'http' => array(
  116. 'method' => 'POST',
  117. 'header' => 'Content-type:application/x-www-form-urlencoded;charset=MD5',
  118. 'content' => $post_data,
  119. 'timeout' => $HTTP_TIME_OUT * 1000 //超时时间,*1000将毫秒变为秒(单位:s)
  120. )
  121. );
  122. $context = stream_context_create($options);
  123. $result = file_get_contents($service_url, false, $context);
  124. return $result;
  125. }
  126. // 提交post请求
  127. function post_json($url, $post)
  128. {
  129. $ch = curl_init();
  130. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  131. curl_setopt($ch, CURLOPT_URL, $url);
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  134. curl_setopt($ch, CURLOPT_HEADER, false);
  135. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  136. curl_setopt($ch, CURLOPT_POST, 1);
  137. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  138. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  139. $s = curl_exec($ch);
  140. curl_close($ch);
  141. return $s;
  142. }
  143. ?>