您的位置:首页 > 移动开发 > 微信开发

微信支付 php 公众平台及移动端服务器

2016-04-28 10:10 417 查看
微信支付 公众号内支付 传送门:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

之前项目做过在写博客之前,这里不赘述。demo很全面,参照文档即可。

//=========步骤1:网页授权获取用户openid============

//=========步骤2:使用统一支付接口,获取prepay_id============

//=========步骤3:使用jsapi调起支付============

移动端服务器端thinkcmf下,thinkphp下类似。放入vendor下。



新建控制器

use Common\Controller\HomebaseController;
class WxAPIController extends HomebaseController{
public function _initialize(){
vendor('WxPayPubHelper.WxPayPubHelper');
}

public function jsApiCall(){...}
引用微信app支付流程图 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_3
服务端工作:

第一步:使用统一下单API,获取prepay_id,生成带签名,返还客户端。

public function jsApiCall(){
//=========步骤2:使用统一支付接口,获取prepay_id============
//使用统一支付接口
$unifiedOrder = new \UnifiedOrder_pub();
$WxPayConf = new \WxPayConf_pub();
//设置统一支付接口参数
//设置必填参数
//appid已填,商户无需重复填写
//mch_id已填,商户无需重复填写
//noncestr已填,商户无需重复填写
//spbill_create_ip已填,商户无需重复填写
//sign已填,商户无需重复填写

$unifiedOrder->setParameter("body","APP支付测试");//商品描述
//自定义订单号,此处仅作举例
$timeStamp = time();
$out_trade_no = $WxPayConf::APPID."$timeStamp";
$unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
$unifiedOrder->setParameter("total_fee","0.01");//总金额
$unifiedOrder->setParameter("notify_url",$WxPayConf::NOTIFY_URL);//通知地址
$unifiedOrder->setParameter("trade_type","APP");//交易类型
//非必填参数,商户可根据实际情况选填
//$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("uid","$uid");//用户标识 *
//$unifiedOrder->setParameter("product_id","XXXX");//商品ID
$respond = $unifiedOrder->postXml();
//\Think\Log::write("【接收到的获取prepay_id通知】:\n".$respond."\n",'INFO');
$responce = $unifiedOrder->xmlToArray($respond);
if($responce['return_code']=='FAIL')
$this->ajaxReturn(array("status"=>0,"info"=>'【通信出错】:'.$responce['return_msg']),'json');
if($responce['result_code']=='FAIL')
$this->ajaxReturn(array("status"=>0,"info"=>'【业务出错】:'.$responce['err_code_des'],"data"=>$responce),'json');

$newsign["appid"] = $WxPayConf::APPID;//公众账号ID
$newsign["partnerid"] = $WxPayConf::MCHID;//商户号
$newsign["nonce_str"] = $unifiedOrder->createNoncestr();//随机字符串
$newsign["package"] = 'Sign=WXPay';//
$newsign["prepayId"] = $responce['prepay_id'];//prepay_id
$newsign["timeStamp"] = time();//
$newsign["sign"] = $unifiedOrder->getSign($newsign);//签名
//return
}
第二步:异步接收通知
Public function notify_url(){
//使用通用通知接口
$notify = new \Notify_pub();

//存储微信的回调
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$notify->saveData($xml);

//验证签名,并回应微信。
//对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,
//微信会通过一定的策略(如30分钟共8次)定期重新发起通知,
//尽可能提高通知的成功率,但微信不保证通知最终能成功。
if($notify->checkSign() == FALSE){
$notify->setReturnParameter("return_code","FAIL");//返回状态码
$notify->setReturnParameter("return_msg","签名失败");//返回信息
}else{
$notify->setReturnParameter("return_code","SUCCESS");//设置返回码
}
$returnXml = $notify->returnXml();
echo $returnXml;

//==商户根据实际情况设置相应的处理流程,此处仅作举例=======
//以log文件形式记录回调信息
//$log_ = new \Log_();
//$log_name="./notify_url.log";//log文件路径
//$log_->log_result($log_name,"【接收到的notify通知】:\n".$xml."\n");
\Think\Log::write("【接收到的notify通知】:\n".$xml."\n",'INFO');
if($notify->checkSign() == TRUE)
{
if ($notify->data["return_code"] == "FAIL") {
//此处应该更新一下订单状态,商户自行增删操作
\Think\Log::write("【通信出错】:\n".$xml."\n",'INFO');
//$log_->log_result($log_name,"【通信出错】:\n".$xml."\n");
}
elseif($notify->data["result_code"] == "FAIL"){
//此处应该更新一下订单状态,商户自行增删操作
\Think\Log::write("【业务出错】:\n".$xml."\n",'INFO');
//$log_->log_result($log_name,"【业务出错】:\n".$xml."\n");
}
else{
//此处应该更新一下订单状态,商户自行增删操作
\Think\Log::write("【支付成功】:\n".$xml."\n",'INFO');
//$log_->log_result($log_name,"【支付成功】:\n".$xml."\n");
$openid=$notify->data["openid"];
$fee = $notify->data['total_fee'];
#todo
}

//商户自行增加处理流程,
//例如:更新订单状态
//例如:数据库操作
//例如:推送支付完成信息

}
}
【to be continue...】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息