|
- <?php
- namespace app\controller;
- use app\BaseController;
- class Index extends BaseController
- {
- public function index()
- {
- return view();
- }
-
- function sign($data_arr) {
- return md5(join('',$data_arr));
- }
-
- public function payto()
- {
-
- # php json方式接收接口返回 自行跳转
- #开发手册:http://docs.nephalem.cn/read/zhifufm/step
- $merchantNum = '1399268136204632064'; # 商户号, 商户后台的用户中心页面查看
- $secret = '207dd2a0b659e99416cdf3cf00695044'; # 商户密钥, 商户后台的用户中心页面查看
- $amount = $_GET['amount']; # 获取充值金额
- $orderNo = '10111111111111119'; # 自己创建的本地订单号
- $api_url = 'http://api-ampr5nnsi7sw.zhifu.fm.it88168.com/api/startOrder'; # 接口地址, 商户后台的用户中心页面查看
- $payType = $_GET['payType']; # 查看支付接口文档说明payType的取值
- $notifyUrl = 'http://pay.kok.bet/index/callback'; # 修改为您自己用来接收支付成功的公网地址
- $returnUrl = 'http://www.baidu.com'; //'http://xxxx.com/return_url.php'; # 支付成功您想让页面跳转的地址
- $returnType = "json"; //接口返回方式 page为直接跳转到支付页面,不传返回json
-
- $sign = $this ->sign(array($merchantNum, $orderNo, $amount, $notifyUrl, $secret));
- $native = array(
- 'merchantNum' => $merchantNum,
- 'payType' => $payType,
- 'amount' => $amount,
- 'orderNo' => $orderNo,
- 'notifyUrl' => $notifyUrl,
- 'returnUrl' => $returnUrl,
- 'sign' => $sign,
- 'returnType' => $returnType,
- );
-
-
-
-
- $param = http_build_query($native);
- $return = $this->http_request($api_url, $param, 'application/x-www-form-urlencoded;charset=utf-8');
- // var_dump($return);die;
- if(strpos($return,'{') === 0){
- $return = json_decode($return, true);
- if($return['success']){
- //json方式展示支付链接有如下几种
- //header("location:".$return['data']['payUrl']);
- header("Refresh:0.1;url=" . $return['data']['payUrl']); //会在0.1秒后执行跳转
- }else{
- echo $return['msg'];
- }
- }else{
- echo "请求异常";
- }
- exit;
- }
-
-
-
- // 发送请求
- function http_request($url, $post_data=array()){
- $curl = curl_init(); // 启动一个CURL会话
- curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
- curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
- curl_setopt($curl, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环
- curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
- $res = curl_exec($curl); // 执行操作
- return $res;
- }
-
- public function callback(){
- /**
- * md5(付款成功状态state的值+商户号merchantNum的值+商户订单号orderNo的值+订单金额amount的值+商户秘钥)
- * +表示字符串拼接
- */
- $merchantNum = '1399268136204632064'; # 商户号, 商户后台的用户中心页面查看
- $secret = '207dd2a0b659e99416cdf3cf00695044'; # 商户密钥, 商户后台的用户中心页面查看
-
- $this->write_log($_GET);
- $sign = $this->sign(array($_GET['state'], $merchantNum, $_GET['orderNo'], $_GET['amount'], $secret));
-
- # 对比签名
- //write_log('sign'.$sign);
- if($merchantNum==$_GET['merchantNum'] && $sign == $_GET['sign']) {
- # do something update database
- $this->write_log('我要返回success啦');
- echo 'success';
- } else {
- $this->write_log('我签名没通过啊');
- echo 'fail';
- exit();
- };
- }
-
-
- public function write_log($data){
- $years = date('Y-m');
- //设置路径目录信息
- $url = './zhifufm/'.$years.'/'.date('Ymd').'_request_log.txt';
- $dir_name=dirname($url);
- //目录不存在就创建
- if(!file_exists($dir_name))
- {
- //iconv防止中文名乱码
- $res = mkdir(iconv("UTF-8", "GBK", $dir_name),0777,true);
- }
- $fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建
- fwrite($fp,date("[Y-m-d H:i:s] ").var_export($data,true)."\r\n");//写入文件
- fclose($fp);//关闭资源通道
- }
- }
复制代码
|
|