⽀付和⽀付通知基于sdk的说明前提是,有服务号(必须开通了⽀付功能,也就是说有了商户后台)
(注意商户后台安全⽬录的设置,不然即使你写的没错误,也调⽤不成功)
h5页⾯写法:  (购物车提交--我们上⼀步已经⽣成了订单,订单表⾥⾯已经有记录了)
order.php?act=wxpay&order_sn=888888 ($user_id可以从session获取到)
//查询订单详情
$orderInfo = orderDetailInfo($order_sn,$user_id);
var_dump($orderInfo);
//测试⽀付⾦额
if($user_id=="24") $orderInfo['total_fee']=0.01;
//调⽤统⼀下单api的请求参数
$jsApiParameters = wxUnifiedOrder($orderInfo['order_sn'],$orderInfo['total_fee']*100);
$smarty->assign('jsapiparameters',$jsApiParameters);
$smarty->display('wxpay.html');
//调⽤统⼀下单api的js参数
function wxUnifiedOrder($order_sn,$total_fee)
{
require_once ROOT_PATH . "wxpay/lib/WxPay.Api.php";
require_once ROOT_PATH . "wxpay/example/WxPay.JsApiPay.php";
//require_once 'log.php';
//①、获取⽤户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();
/
/②、统⼀下单
$input = new WxPayUnifiedOrder();
$input->SetOpenid($openId);
$input->SetBody("XXX购买");
$input->SetAttach("XXX购买产品$order_sn");
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($total_fee);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("XXX购买");
$input->SetNotify_url("XXX/wxshop/wxnotify.php");
$input->SetTrade_type("JSAPI");
$order = WxPayApi::unifiedOrder($input);
//printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
//③、在⽀持成功回调通知中处理成功之后的事宜,见 notify.php
/**
* 注意:
* 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认⽀付是否成功
* 2、jsapi⽀付时需要填⼊⽤户openid,WxPay.JsApiPay.php中有获取openid流程(⽂档可以参考公众平台“⽹页授权接⼝”,
* 参考mp.weixin.qq/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
*/
return$jsApiParameters;
}
html页⾯(怎么调⽤jsapi,上⼀个页⾯的$jsApiParameters参数赋值到那块了)
<a class="next btn SEND_PAY" onclick="callpay()">⽴即⽀付</a>
<script type="text/javascript">
//调⽤JS api ⽀付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{$jsapiparameters},
function(res){
_msg == "get_brand_wcpay_request:ok" ){
layer.open({
content: '⽀付成功'
,skin: 'msg'
,time: 2 //2秒后⾃动关闭
});
}
}
);
}
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>
付款后通知地址:
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/../../includes/init.php');
ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";//sdk有这个⽂件
require_once '../lib/WxPay.Notify.php';//sdk有这个⽂件
require_once 'log.php';//sdk有这个⽂件
/
/初始化⽇志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);
class PayNotifyCallBack extends WxPayNotify
{
//查询订单
public function Queryorder($transaction_id)
{
$input = new WxPayOrderQuery();
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
Log::DEBUG("query:" . json_encode($result));
if(array_key_exists("return_code", $result)
&& array_key_exists("result_code", $result)
&& $result["return_code"] == "SUCCESS"
&& $result["result_code"] == "SUCCESS")
{
return true;
}
return false;
}
//重写回调处理函数
public function NotifyProcess($data, &$msg)
{
Log::DEBUG("1");
Log::DEBUG("call back:" . json_encode($data));
$notfiyOutput = array();
Log::DEBUG("2");
if(!array_key_exists("transaction_id", $data)){
$msg = "输⼊参数不正确";
return false;
}
//查询订单,判断订单真实性
if(!$this->Queryorder($data["transaction_id"])){
$msg = "订单查询失败";
return false;
}
//如果成功,那么就更改订单状态
$res = $this->updateOrder($data["out_trade_no"],$data["transaction_id"]);
if($res)
{
return true;
}else{
return false;
}
}
public function updateOrder($order_sn,$trade_no)
{//更改订单状态--设置已付款支付分怎么开通
/* 取得订单信息 */
$sql = 'SELECT order_id, user_id, order_sn, trade_no, consignee, address, tel, shipping_id, extension_code, extension_id, goods_amount ' .            'FROM t_order_info '  .
" WHERE order_sn = '$order_sn'";
$order    = $GLOBALS['db']->getRow($sql);
$sql = 'UPDATE t_order_info '  .
" SET order_status = '1', " .
" confirm_time = '" . gmtime() . "', " .
" pay_status = '2', " .
" pay_id = '7', " .
" pay_name = '⽀付', " .
" pay_time = '".gmtime()."', " .
" money_paid = order_amount," .
" group_buy_status = group_buy_status+1," .
" trade_no = '$trade_no' ".
"WHERE order_sn = '$order_sn'";
$res = $GLOBALS['db']->query($sql);
if($res)
{
return true;
}else{
return false;
}
}
}
$notify = new PayNotifyCallBack();//实例化
$notify->Handle(false);//调⽤这个⽅法,将来就可以执⾏上⾯那个回调⽅法,并且,获取xml数据,验证签名,和最后的给输出xml,都可以完成
//$resXml = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml> ";
//echo $resXml;
//Log::DEBUG($resXml);