授权获取二维码类:
<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
* Trade类
* @author xyyphp
* @date 2016/10/10
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span> TradeController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> BaseController
{
</span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$mchid</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$appid</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">protected</span> <span style="color: #800080;">$key</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> init()
{
parent</span>::<span style="color: #000000;">init();
</span><span style="color: #800080;">$this</span>->mchid = WX_MCHID; <span style="color: #008000;">//</span><span style="color: #008000;"> 微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送</span>
<span style="color: #800080;">$this</span>->appid = WX_APPID; <span style="color: #008000;">//</span><span style="color: #008000;">公众号APPID 通过微信支付商户资料审核后邮件发送</span>
<span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span> = WX_APIKEY; <span style="color: #008000;">//</span><span style="color: #008000;">https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥</span>
<span style="color: #000000;"> }
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
* @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
* @param float $totalFee 收款总费用 单位元
* @param string $outTradeNo 唯一的订单号
* @param string $orderName 订单名称
* @param string $notifyUrl 支付结果通知url 不要有问号
* https://mp.weixin.qq.com/ 微信支付-开发配置-测试目录
* 测试目录 http://mp.izhanlue.com/paytest/ 最后需要斜线,(需要精确到二级或三级目录)
* @return string
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> createJsBizPackage(<span style="color: #800080;">$totalFee</span>, <span style="color: #800080;">$outTradeNo</span>, <span style="color: #800080;">$orderName</span>, <span style="color: #800080;">$notifyUrl</span>, <span style="color: #800080;">$timestamp</span><span style="color: #000000;">)
{
</span><span style="color: #800080;">$config</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
</span>'mch_id' => <span style="color: #800080;">$this</span>->mchid,
'appid' => <span style="color: #800080;">$this</span>->appid,
'key' => <span style="color: #800080;">$this</span>-><span style="color: #008080;">key</span>,<span style="color: #000000;">
);
</span><span style="color: #800080;">$unified</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
</span>'appid' => <span style="color: #800080;">$config</span>['appid'],
'attach' => '支付', <span style="color: #008000;">//</span><span style="color: #008000;">商家数据包,原样返回</span>
'body' => <span style="color: #800080;">$orderName</span>,
'mch_id' => <span style="color: #800080;">$config</span>['mch_id'],
'nonce_str' => <span style="color: #800080;">$this</span>->createNonceStr(),
'notify_url' => <span style="color: #800080;">$notifyUrl</span>,
<span style="color: #008000;">//</span><span style="color: #008000;">'openid' => $openid, //rade_type=JSAPI,此参数必传</span>
'out_trade_no' => <span style="color: #800080;">$outTradeNo</span>,
'spbill_create_ip' => '127.0.0.1',
'total_fee' => <span style="color: #008080;">intval</span>(<span style="color: #800080;">$totalFee</span> * 100), <span style="color: #008000;">//</span><span style="color: #008000;">单位 转为分</span>
'trade_type' => 'NATIVE',<span style="color: #000000;">
);
</span><span style="color: #800080;">$unified</span>['sign'] = <span style="color: #800080;">$this</span>->getSign(<span style="color: #800080;">$unified</span>, <span style="color: #800080;">$config</span>['key'<span style="color: #000000;">]);
</span><span style="color: #800080;">$responseXml</span> = <span style="color: #800080;">$this</span>->curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', <span style="color: #800080;">$this</span>->arrayToXml(<span style="color: #800080;">$unified</span><span style="color: #000000;">));
</span><span style="color: #008000;">/*</span><span style="color: #008000;">
<xml><return_code></return_code><return_msg></return_msg><appid></appid><mch_id></mch_id><nonce_str></nonce_str><sign></sign><result_code></result_code><pre class="brush:php;toolbar:false;" pay_id>
<trade_type></trade_type>
*/
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($unifiedOrder === false) {
die('parse xml error');
}
if ($unifiedOrder->return_code != 'SUCCESS') {
die($unifiedOrder->return_msg);
}
if ($unifiedOrder->result_code != 'SUCCESS') {
die($unifiedOrder->err_code);
/*
NOAUTH 商户无此接口权限
NOTENOUGH 余额不足
ORDERPAID 商户订单已支付
ORDERCLOSED 订单已关闭
SYSTEMERROR 系统错误
APPID_NOT_EXIST APPID不存在
MCHID_NOT_EXIST MCHID不存在
APPID_MCHID_NOT_MATCH appid和mch_id不匹配
LACK_PARAMS 缺少参数
OUT_TRADE_NO_USED 商户订单号重复
SIGNERROR 签名错误
XML_FORMAT_ERROR XML格式错误
REQUIRE_POST_METHOD 请使用post方法
POST_DATA_EMPTY post数据为空
NOT_UTF8 编码格式错误
*/
}
//$unifiedOrder->trade_type 交易类型 调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP
//$unifiedOrder->prepay_id 预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时
//$unifiedOrder->code_url 二维码链接 trade_type为NATIVE是有返回,可将该参数值生成二维码展示出来进行扫码支付
$code_list = (array)$unifiedOrder->code_url;
$arr = array(
"appId" => $config['appid'],
"timeStamp" => $timestamp,
"nonceStr" => $this->createNonceStr(),
"package" => "prepay_id=" . $unifiedOrder->prepay_id,
"signType" => 'MD5',
"code_url" => $code_list[0]
);
$arr['paySign'] = $this->getSign($arr, $config['key']);
return $arr;
}
/**
* 查询订单
*/
public function orderQuery($outTradeNo)
{
$config = array(
'mch_id' => $this->mchid,
'appid' => $this->appid,
'key' => $this->key,
);
$unified = array(
'appid' => $config['appid'],
'mch_id' => $config['mch_id'],
'out_trade_no' => $outTradeNo,
'nonce_str' => $this->createNonceStr()
);
$unified['sign'] = $this->getSign($unified, $config['key']);
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/orderquery', $this->arrayToXml($unified));
/*
调取实例:
<span style="color: #800080;">$money</span> = 0.1;<span style="color: #008000;">//</span><span style="color: #008000;">订单金额</span> <span style="color: #800080;">$out_trade_no</span> = 'test_8888888';<span style="color: #008000;">//</span><span style="color: #008000;">唯一的订单号</span> <span style="color: #800080;">$order_name</span> = '测试订单';<span style="color: #008000;">//</span><span style="color: #008000;">订单名称</span> <span style="color: #800080;">$notifyUrl</span> = 'http://www.test.com/';<span style="color: #008000;">//</span><span style="color: #008000;">支付结果通知url 不要有问号</span> <span style="color: #800080;">$timeStamp</span> = <span style="color: #008080;">time</span>();<span style="color: #008000;">//</span><span style="color: #008000;">时间戳 //获取二维码信息</span> <span style="color: #800080;">$wx_result</span> = <span style="color: #800080;">$this</span>->createJsBizPackage(<span style="color: #800080;">$money</span>, <span style="color: #800080;">$out_trade_no</span>, <span style="color: #800080;">$order_name</span>, <span style="color: #800080;">$notifyUrl</span>, <span style="color: #800080;">$timeStamp</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">检测支付状态</span> <span style="color: #800080;">$str_status</span> = <span style="color: #800080;">$this</span>->orderQuery(<span style="color: #800080;">$out_trade_no</span>);










