@H_404_0@这里假设你已经申请完微信支付
@H_404_0@1. 微信后台配置 如图PHP
@H_404_0@首先访问 index.PHP 你可以看到界面PHP;">
@H_404_0@当然你也可以直接写死为自己的访问链接。
@H_404_0@4. JSAPI 支付
@H_404_0@必要代码解析:
PHP;">
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler,15);
@H_404_0@调用日志类 可以通过 $log->DEBUG(‘test‘); 打印调试信息。其实也可以直接使用 $Log::DEBUG(‘test‘); 来调试
GetOpenid();
@H_404_0@主要是为了获取 openid 其中GetOpenid() 函数定义在 文件 WxPay.JsApiPay.PHP 文件中
__CreateOauthUrlForCode($baseUrl);
Header("Location: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
return $openid;
}
}
@H_404_0@$baseUrl 其实就是为了在跳转回来这个页面。 可以继续跟踪函数__CreateOauthUrlForCode() 其实就是通过微信的Auth2.0 来获取Openid
@H_404_0@参考链接:
这就需要你把微信的 网页授权接口也设置好。
获取到 Openid 就可以调用微信支付的统一下单接口了。回到 文件 jsapi.php 如下代码
SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis",time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
echo '统一下单支付单信息
'; printf_info($order); $jsApiParameters = $tools->GetJsApiParameters($order);
'; printf_info($order); $jsApiParameters = $tools->GetJsApiParameters($order);
这里面的代码:
SetAttach("test");
如果 把值改为 $input->SetAttach("test this is attach");就会存在bug 后面再说,其实这个参数不是必须的干脆可以去掉。
代码:
SetNotify_url();
@H_404_0@是设置接收支付结果通知的Url 这里是默认的demo 链接我们可以设置成我们的:
SetNotify_url(dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/notify.PHP');
@H_404_0@当然你也可以选择直接写死。
其中的函数 unifiedOrder($input) 可以到WxPay.Api.PHP 中文件跟踪,其实就是调用统一下单接口。
@H_404_0@在 WxPay.Api.PHP 中需要更改的一处代码是:
IsNotify_urlSet()){
$inputObj->SetNotify_url(WxPayConfig::NOTIFY_URL);//异步通知url
}
@H_404_0@就是当没设置 notifyUrl 的时候回去配置文件中找,但是配置文件中根本没有设置。
@H_404_0@所以你可以选择在 配置文件WxPay.Config.PHP 中加上这个配置,也可以直接写一个默认的notify链接。
@H_404_0@函数 GetJsApiParameters() 是获取jsApi支付的参数给变量 $jsApiParameters 方便在下面的Js中调用
@H_404_0@jsapi.PHP 中js的代码:
PHP;">
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',,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();
}
}
@H_404_0@其中点击立即支付按钮调用的就是 callpay() 函数,他有会调用jsApiCall() 函数打开支付程序。
此后输入密码完成支付。
@H_404_0@在完成支付页面点击完成会回到这个支付页面,并弹出 支付成功的提示框
Handle(false);
@H_404_0@其中大部分逻辑在 Handle 函数中处理 文件 WxPay.Notify.PHP
SetReturn_code("FAIL");
$this->SetReturn_msg($msg);
$this->ReplyNotify(false);
return;
} else {
//该分支在成功回调到NotifyCallBack方法,处理完成之后流程
$this->SetReturn_code("SUCCESS");
$this->SetReturn_msg("OK");
}
$this->ReplyNotify($needSign);
}
@H_404_0@主要代码:
PHP;">
$result = WxpayApi::notify(array($this,$msg);
@H_404_0@跟踪函数 notify 文件WxPay.Api.PHP
errorMessage();
return false;
}
@H_404_0@通过 $GLOBALS[‘HTTP_RAW_POST_DATA‘]; 获取同志数据 然后 Init 函数验证签名等。验签成功运行代码
return call_user_func($callback,$result);
}
PHP;">
return call_user_func($callback,$result);
@H_404_0@即调用了一个回调函数,NotifyCallBack() 函数并传递参数 $result 在NotifyCallBack函数中会调用我们重写的NotifyProcess()函数(此函数在notify.PHP 中被重写)
@H_404_0@NotifyProcess() 判断也没有问题就会 设置返回 success的xml信息
SetReturn_code("SUCCESS");
$this->SetReturn_msg("OK");
@H_404_0@并最终调用函数 $this->ReplyNotify($needSign); echo success的结果
@H_404_0@函数ReplyNotify 需要修改一处代码:
GetReturn_code($return_code) == "SUCCESS")
{
$this->SetSign();
}
WxpayApi::replyNotify($this->ToXml());
}
@H_404_0@改为
$this->GetReturn_code($return_code) == "SUCCESS")
GetReturn_code() == "SUCCESS")
@H_404_0@即可。
@H_404_0@这样整个流程就结束了。上面提到了 传递订单参数
SetAttach("test");
@H_404_0@如果我设置 值为 test this is attach (其实只要有空格就会存在bug)
如图 传递的订单信息