本文实例为大家分享了thinkphp整合微信支付代码,供大家参考,具体内容如下
下载:支付sdk
将微信支付sdk放在第三方类库vendor下面,请切记把wxpay.config.php里面的商户信息修改为您的公众号信息,以避免造成资金的流失。
php端代码
public function pay(){
//商户基本信息,可以写死在wxpay.config.php里面,其他详细参考wxpayconfig.php
vendor('pay.jsapi');
$tools = new jsapipay();
$openid = $tools->getopenid();
$out_trade_no=date('yhis').rand(100,1000);
$total_fee='测试';
$body='啥也不说';
$total_fee=1;
$input = new wxpayunifiedorder();
$input->setbody($body);
$input->setout_trade_no($out_trade_no);
$input->settotal_fee($total_fee);
$input->setnotify_url("http://xx.xxx.com/pay/notify.php");
$input->settrade_type("jsapi");
$input->setopenid($openid);
$order = wxpayapi::unifiedorder($input);
$this->jsapiparameters = $tools->getjsapiparameters($order);
$this->display();
}
html端代码
<!doctype html>
<head>
<meta charset="utf-8">
<title>微信安全支付</title>
<meta name="keywords" content="" />
<meta name="handheldfriendly" content="true">
<meta name="mobileoptimized" content="320">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="cache-control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="cleartype" content="on">
<meta id="viewport" name="viewport" content="width=320, initial-scale=1.0,maximum-scale=1.0, user-scalable=0," />
<script type="text/javascript">
//调用微信js api 支付
function jsapicall()
{
weixinjsbridge.invoke(
'getbrandwcpayrequest',
{$jsapiparameters},
function(res){
weixinjsbridge.log(res.err_msg);
if(res.err_msg == 'get_brand_wcpay_request:cancel') {
alert("您已取消了此次支付");
return;
} else if(res.err_msg == 'get_brand_wcpay_request:fail') {
alert("支付失败");
return;
} else if(res.err_msg == 'get_brand_wcpay_request:ok') {
alert("支付成功!");//跳转到订单页面
} else {
alert("未知错误"+res.error_msg);
return;
}
}
);
}
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>
<button type="button" class="btn btn-danger btn-lg btn-block" onclick="callpay()">
确认支付
</button>
</body>
</html>
就这么简单。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!