基于ThinkPay的微信支付,摒弃腾讯自己搞的SDK,封装的太复杂了,直接基于ThinkPay一个文件实现微信支付,加群130747567。
/**
* 微信支付驱动
*/
class Wxpay extends \Addons\Pay\ThinkPay\Pay\Pay {
protected $gateway = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
protected $orderquery = 'https://api.mch.weixin.qq.com/pay/orderquery';
protected $config = array(
'appid' => '',
'appsecret' => '',
'mchid' => '',
'key' => ''
);
public function check() {
if (!$this->config['appid'] || !$this->config['appsecret'] || !$this->config['mchid'] || !$this->config['key']) {
E("微信支付设置有误!");
}
return true;
}
public function buildRequestForm($pay_data) {
// 获取用户openId,微信公众号JSAPI支付必须
$openId = $this->GetOpenid();
$param = array(
'appid' => $this->config['appid'],
'mch_id' => $this->config['mchid'],
'nonce_str' => $this->getNonceStr(),
'body' => $pay_data['body'],
'out_trade_no' => $pay_data['out_trade_no'],
'total_fee' => $pay_data['money'] * 100,
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => $this->config['notify_url'],
'trade_type' => 'JSAPI',
'openid' => $openId,
);
// 签名
$param['sign'] = $this->MakeSign($param);
$xml_param = $this->ToXml($param);
$result = $this->FromXml($this->postXmlCurl($xml_param, $this->gateway));
if($result['return_code'] === 'SUCCESS'){
if ($this->CheckSign($result)) {
$jsApiParameters = $this->GetJsApiParameters($result);
$pay_page = <<<EOF
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>微信支付</title>
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsApiParameters},
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_code+res.err_desc+res.err_msg);
}
);
}
function callpay()
{
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', jsApiCall);
document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
}
}else{
jsApiCall();
}
}
</script>
</head>
<body>
<br/>
<font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">{$pay_data['money']}</span>元</b></font><br/><br/>
<div align="center">
<button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
</div>
</body>
</html>
EOF;
return $pay_page;
}
} else {
E("微信订单错误!" . $result['return_msg']);
}
}
}