123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- require_once 'inc.php';
- //if (_G('orderid') == '' || !_G('orderid')||!$userid||$userid=='') {
- // echo "<script>alert('网络传输中掉包,请重新支付!')</script>";
- // exit;
- //}
- //定义数据
- $email='900024';
- $userid='851811130002';
- $userkey='8a9bd64e6585c6d8016585c6c8980002';
- //1.获取数据
- $orderid = 'nanodream'.date('YmdHis',time());//_G('orderid'); //订单号
- $price = rand(1,5); //交易金额
- $NotifyUrl = 'http://' . _S('HTTP_HOST') . '/pay/sbweixin_bank/notify_url.php'; //服务端返回地址
- //$order_model = new orders_class();
- //$addtime = $order_model->fetch_one('orderlist', "orderid='" . $orderid . "'", 'addtime');
- //$addtime = $addtime['addtime'];
- //2.组装数据,用于生成签名
- $reqestdata = array(
- 'inst_no' => $email, //机构号
- 'mch_no' => $userid,//商户号
- 'pay_trace_no' => $orderid,//请求流水号
- 'pay_time' => date('YmdHis',time()),
- 'total_amount' => $price * 100,//订单总金额
- 'notify_url' => $NotifyUrl,
- 'order_body' => "客服QQ3007549789订单号" . $orderid,//支付凭证商品描述信息,不填写默认为交易订单编号
- 'pay_type' => '400',
- );
- //3.生成签名
- $buidSign = makesign($reqestdata, $userkey);
- //4.组装数据,用于发起请求支付
- $reqestdata['sign'] = $buidSign;
- //5.传递数据,请求数据
- $url = "http://demo.payplat.cn/openpay/v3/prepay";
- $respsonData = post_json($url, json_encode($reqestdata)); //发送请求
- //6.获取数据
- $respsonData = json_decode($respsonData, true);
- if ($respsonData['return_code'] != 'SUCCESS' || $respsonData['result_code'] != 'PAY_SUCCESS') {
- $error_message = "错误代码:" . $respsonData['result_code'] . " 错误描述:" . $respsonData['return_msg'] . " 错误信息" . $respsonData['return_code'];
- exit($error_message);
- }
- //7.赋值数据
- $img = $respsonData['qrcode'];
- echo $img;
- /**生成签名
- * @param $params /请求的参数数组
- * @param $key /支付密钥
- * @return string
- */
- function makesign($paras, $key)
- {
- // $sort_array = $this->array_sort(array_filter($paras)); //删除数组中的空值并排序
- $sort_array = array_sort($paras);
- $prestr = create_linkstring($sort_array); //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
- $prestr = $prestr . "&key=" . $key; //把拼接后的字符串再与安全校验码直接连接起来
- // \Think\Log::record('prestr:' . $prestr);
- $mysgin = sign($prestr, 'MD5'); //把最终的字符串签名,获得签名结果
- return $mysgin;
- }
- /**
- * 对数组排序
- * $array 排序前的数组
- * return 排序后的数组
- */
- function array_sort($array)
- {
- ksort($array); //按照key值升序排列数组
- return $array;
- }
- /**
- * 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
- * $array 需要拼接的数组
- * return 拼接完成以后的字符串
- */
- function create_linkstring($array)
- {
- $str = "";
- foreach ($array as $key => $val) {
- ;
- //键值为空的参数不参与排序,键名为key的参数不参与排序
- if ($key != "sign") {
- $str .= $key . "=" . $val . "&";
- }
- }
- $str = trim($str, "&");
- return $str; //返回参数
- }
- /**
- * 签名字符串
- * $prestr 需要签名的字符串
- * $sign_type 签名类型,也就是sec_id
- * return 签名结果
- */
- function sign($prestr, $sign_type)
- {
- $sign = '';
- if ($sign_type == 'MD5') {
- $sign = md5($prestr); //MD5加密
- } elseif ($sign_type == 'DSA') {
- //DSA 签名方法待后续开发
- die("DSA 签名方法待后续开发,请先使用MD5签名方式");
- } elseif ($sign_type == "") {
- die("sign_type为空,请设置sign_type");
- } else {
- die("暂不支持" . $sign_type . "类型的签名方式");
- }
- return strtolower($sign); //返回参数并小写
- }
- function requestAsHttpPOST($data, $service_url)
- {
- $HTTP_TIME_OUT = "20";
- $http_data = $this->array_sort(array_filter($data)); //删除数组中的空值并排序
- $post_data = http_build_query($http_data);
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- 'header' => 'Content-type:application/x-www-form-urlencoded;charset=MD5',
- 'content' => $post_data,
- 'timeout' => $HTTP_TIME_OUT * 1000 //超时时间,*1000将毫秒变为秒(单位:s)
- )
- );
- $context = stream_context_create($options);
- $result = file_get_contents($service_url, false, $context);
- return $result;
- }
- // 提交post请求
- function post_json($url, $post)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
- $s = curl_exec($ch);
- curl_close($ch);
- return $s;
- }
- ?>
|