namespace frontend\controllers;
use Yii;
use frontend\components\Controller;
class PayController extends Controller
{
private $app_id = 'xxxxxxxxx';//appid和app_secret在开通微信支付后接收到的邮件里面可看到
private $mch_id = '123456790';//商户号
private $makesign = 'sahdasfjasfdasdfasdfasdfasfdafd';//支付的签名,32位签名,微信商户后台设置
private $app_secret = 'hsisdidfdkkdkd';
private $parameters;
private $notify_url='http://www.mingtern.com';//本控制器下面的 notifyurl 方法的URL路径 记得格式 是 http://...... 【这是回调】
public $error = 0;
public $orderid = null;
#选择支付方式#
#记得填写授权目录
public function actionWxhandle()
{
$open_id = 'hsdudf67dshd12';//这里是用户的open_id
#支付前的数据配置
$reannumb = $this->randomkeys(4).time().Yii::$app->user->id.$this->randomkeys(4);
//这里写插入语句
$money = $params['money'];
$conf = $this->payconfig('mdxz'.$reannumb, $money*100, '订单支付', $open_id);
if (!$conf || $conf['return_code'] == 'FAIL') exit("");
$this->orderid = $conf['prepay_id'];
//生成页面调用参数
$jsApiObj["appId"] = $conf['appid'];
$timeStamp = time();
$jsApiObj["timeStamp"] = "$timeStamp";
$jsApiObj["nonceStr"] = $this->createNoncestr();
$jsApiObj["package"] = "prepay_id=" . $conf['prepay_id'];
$jsApiObj["signType"] = "MD5";
$jsApiObj["paySign"] = $this->MakeSign($jsApiObj);
return json_encode($jsApiObj);
}
//回调
public function actionNotifyurl()
{
$xml = file_get_contents("php://input");
$log = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$id = $log['out_trade_no']; //获取单号
//这里修改状态
exit('SUCCESS'); //打死不能去掉
}
#微信JS支付参数获取#
protected function payconfig($no, $fee, $body, $open_id)
{
$openid = $open_id;
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$data['appid'] = $this->app_id;
$data['mch_id'] = $this->mch_id; //商户号
$data['device_info'] = 'WEB';
$data['body'] = $body;
$data['out_trade_no'] = $no; //订单号
$data['total_fee'] = $fee; //金额
$data['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"];
$data['notify_url'] = $this->notify_url; //通知url
$data['trade_type'] = 'JSAPI';
$data['openid'] = $openid; //获取openid
$data['nonce_str'] = $this->createNoncestr();
$data['sign'] = $this->MakeSign($data);
$xml = $this->ToXml($data);
$curl = curl_init(); //启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); //要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置header
curl_setopt($curl, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE); //发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); //Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
$tmpInfo = curl_exec($curl); //执行操作
curl_close($curl); //关闭CURL会话
$arr = $this->FromXml($tmpInfo);
return $arr;
}
/**
* 作用:产生随机字符串,不长于32位*/
public function createNoncestr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++)
{
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 作用:产生随机字符串,不长于32位*/
public function randomkeys($length)
{
$pattern = '1234567890123456789012345678905678901234';
$key = null;
for ($i = 0; $i < $length; $i++)
{
$key .= $pattern{mt_rand(0, 30)}; //生成php随机数
}
return $key;
}
/**
* 将xml转为array
* @param string $xml
* @throws WxPayException*/
public function FromXml($xml)
{
//将XML转为array
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
/**
* 输出xml字符
* @throws WxPayException
**/
public function ToXml($arr)
{
$xml = "";
foreach ($arr as $key => $val)
{
if (is_numeric($val))
{
$xml .= "" . $val . "" . $key . ">";
}
else
{
$xml .= "" . $key . ">";
}
}
$xml .= "";
return $xml;
}
/**
* 生成签名
* @return 签名,本函数不覆盖sign成员变量,如要设置签名需要调用SetSign方法赋值*/
protected function MakeSign($arr)
{
//签名步骤一:按字典序排序参数
ksort($arr);
$string = $this->ToUrlParams($arr);
//签名步骤二:在string后加入KEY
$string = $string . "&key=" . $this->makesign;
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$result = strtoupper($string);
return $result;
}
/**
* 格式化参数格式化成url参数*/
protected function ToUrlParams($arr)
{
$buff = "";
foreach ($arr as $k => $v)
{
if ($k != "sign" && $v != "" && !is_array($v))
{
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
}