微信支付和微信支付通知基于sdk的说明
做微信支付,需要的条件是:1、开通微信服务号,2、开通微信商户后台申请支付方式(公众号支付、H5支付、APP支付、小程序支付)
(注意微信服务号里面的配置以及商户后台 安全目录 的设置,不然即使你写的没错误,也调用不成功)
公众号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("http://XXX.com/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流程 (文档可以参考微信公众平台“网页授权接口”,
* 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
*/
return $jsApiParameters;
}
html页面(怎么调用jsapi,上一个页面的$jsApiParameters参数赋值到那块了)
立即支付
付款后通知地址:
<?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 = " ";
//echo $resXml;
//Log::DEBUG($resXml);