其实微信支付真的没有想象中的麻烦,首先先看api和下载demo
1.下载WxPayPubHelper包
包含三个文件
WxPay.pub.config.php
我采用的是weiphp框架上二次开发的
JSAPI路径设置
function confirmPay($payMoney){
include_once("./WxPayPubHelper/WxPayPubHelper.php");
//使用jsapi接口
$jsApi = new \JsApi_pub();
$openid = get_openid();
//=========步骤2:使用统一支付接口,获取prepay_id============
//使用统一支付接口
$unifiedOrder = new \UnifiedOrder_pub();
//设置统一支付接口参数
//设置必填参数
//appid已填,商户无需重复填写
//mch_id已填,商户无需重复填写
//noncestr已填,商户无需重复填写
//spbill_create_ip已填,商户无需重复填写
//sign已填,商户无需重复填写
$unifiedOrder->setParameter("openid","$openid");//商品描述
$unifiedOrder->setParameter("body","信息费");//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$out_trade_no = \WxPayConf_pub::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee",(int)($payMoney.'00'));//总金额
$unifiedOrder->setParameter("notify_url",\WxPayConf_pub::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交 易类型
//非必填参数,商户可根据实际情况选填
//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
//$unifiedOrder->setParameter("device_info","XXXX");//设备号
//$unifiedOrder->setParameter("attach","XXXX");//附加数据
//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
//$unifiedOrder->setParameter("openid","XXXX");//用户标识
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
$prepay_id = $unifiedOrder->getPrepayId();
//=========步骤3:使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);
$jsApiParameters = $jsApi->getParameters();
$this->assign('payMoney',$payMoney);
$this->assign('jsApiParameters',$jsApiParameters);
$this->display(ONETHINK_ADDON_PATH . 'WeiSite/View/default/WeiSite/confirmPay.html');
}
demo里面的代码,有几点需要注意
1.传入$openid,标示是哪个用户在发起请求
2.金额 total_fee,由于支付金额是按照分来计量,所以(int)($payMoney.'00')
将传入的金额后面拼接两个00,换算成块.然后转换为int类型
3.发起请求,并提示用户是否确认支付
调用微信JS api 支付
<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>