下单代码:pay.php
<?php
namespace Wap\Controller;
use Common\Controller\WapbaseController;
Vendor('Wxpay.WxPayApi');
Vendor('Wxpay.JsApiPay');
Vendor('Wxpay.Notify');
class PayController extends WapbaseController {
function _initialize(){
parent::_initialize();
}
/**
* 立即购买
*/
public function pay(){
if(!empty($_GET)){
$id = I('get.id');
$data_info = M('data')->where('id='.$id)->find();
//获取用户信息
$info = M('users')->where("wx_openid='".$_SESSION['user']['wx_openid']."'")->find();
if(empty($info['user_email'])){
$this->success('请完善资料再进行购买',U('index/my'));
die;
}
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody($data_info['name']);
$input->SetAttach('goumai');
$trade_no = \WxPayConfig::MCHID.date("YmdHis");
$input->SetOut_trade_no($trade_no);
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://1.aigowu.cn/index.php/Wap/Notify/notify/");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
if($order){
$info = M('users')->where("wx_openid='".$_SESSION['user']['wx_openid']."'")->find();
$data['uid'] = $info['id'];
$data['trade_no'] = $trade_no;
$data['data_id'] = $data_info['id'];
$data['money'] = $data_info['price'];
$data['addtime'] = time();
M('order')->add($data);
}
$jsApiParameters = $tools->GetJsApiParameters($order);
$this->assign('jsApiParameters',$jsApiParameters);
$this->assign($data_info);
$this->display();
}
}
/**
* 结算
*/
public function jiesuan(){
if(!empty($_GET)){
$id_string = I('get.id');
$id_array = explode(',', $id_string);
//获取用户信息
$info = M('users')->where("wx_openid='".$_SESSION['user']['wx_openid']."'")->find();
if(empty($info['user_email'])){
$this->success('请完善资料再进行购买',U('index/my'));
die;
}
foreach ($id_array as $key =>$val){
$shapcar = M('shapcar')->where('id='.$val)->find();
$dataa = M('data')->where('id='.$shapcar['data_id'])->find();
$money += $dataa['price'];
}
$tools = new \JsApiPay();
$openId = $tools->GetOpenid();
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody('优点讯资源下载');
$input->SetAttach('jiesuan');
// 订单号
$trade_no = \WxPayConfig::MCHID.date("YmdHis");
$input->SetOut_trade_no($trade_no);
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://1.aigowu.cn/index.php/Wap/Notify/notify/");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = \WxPayApi::unifiedOrder($input);
if($order){
$info = M('users')->where("wx_openid='".$_SESSION['user']['wx_openid']."'")->find();
foreach ($id_array as $key =>$val){
$dafa['trade_no'] = $trade_no;
$res = M('shapcar')->where('id='.$val)->save($dafa);
$shapcar = M('shapcar')->where('id='.$val)->find();
$data_info = M('data')->where('id='.$shapcar['data_id'])->find();
$data['uid'] = $info['id'];
$data['trade_no'] = $trade_no;
$data['data_id'] = $data_info['id'];
$data['money'] = $data_info['price'];
$data['addtime'] = time();
M('order')->add($data);
}
}
$datas['name'] = '购物车结算';
$datas['price'] = $money.'.00';
$jsApiParameters = $tools->GetJsApiParameters($order);
$this->assign('jsApiParameters',$jsApiParameters);
$this->assign($datas);
$this->display('pay');
}
}
}
回调代码:
<?php
namespace Wap\Controller;
use Think\Controller;
Vendor('Wxpay.WxPayApi');
Vendor('WxPayPubHelper.WxPayPubHelper');
class NotifyController extends Controller{
private $reply;
function _initialize(){
}
/*
* 支付回调接口
*/
public function notify()
{
//使用通用通知接口
$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= "./log.log";//log文件路径
$this->log_result($log_name,"【接收到的notify通知】:\n".$returnXml."\n");
if($notify->checkSign() == TRUE)
{
if ($notify->data["return_code"] == "FAIL") {
//此处应该更新一下订单状态,商户自行增删操作
$this->log_result($log_name,"【通信出错】:\n".$xml."\n");
echo "error";
}
elseif($notify->data["result_code"] == "FAIL"){
//此处应该更新一下订单状态,商户自行增删操作
$this->log_result($log_name,"【业务出错】:\n".$xml."\n");
echo "error";
}
else{
//此处应该更新一下订单状态,商户自行增删操作
$this->log_result($log_name,"【支付成功】:\n".$xml."\n");
$type = $notify->data["attach"]; // = jiesuan 购物车结算
$order_sn = $notify->data["out_trade_no"]; //订单号
//处理业务逻辑
echo "SUCCESS";
}
$returnXml = "<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>";
echo $returnXml;
//商户自行增加处理流程,
//例如:更新订单状态
//例如:数据库操作
} //例如:推送支付完成信息
}
function log_result($file,$word)
{
$fp = fopen($file,"a");
flock($fp, LOCK_EX) ;
fwrite($fp,"执行日期:".strftime("%Y-%m-%d-%H:%M:%S",time())."\n".$word."\n\n");
flock($fp, LOCK_UN);
fclose($fp);
}
}
?>
前端代码:(js吊起支付)
<!DOCTYPE html>
<html>
<head>
<tc_include file="Wap:head" />
<link rel="stylesheet" type="text/css" href="__TMPL__/Wap/css/lanren.css">
</head>
<body>
<div class="wenx_xx">
<div class="mz" style="margin-bottom:10px;font-size:18px;">{$name}</div>
<div class="wxzf_price">¥{$price}</div>
</div>
<div class="skf_xinf">
<div class="all_w"> <span class="bt">收款方</span> <span class="fr">优点讯科技</span> </div>
</div>
<span class="ljzf_but all_w" onclick="callpay();">立即支付</span>
<tc_include file="Wap:nav" />
</body>
</html>
<script type="text/javascript">
//调用微信JS api 支付
function jsApiCall()
{
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
<?php echo $jsApiParameters; ?>,
function(res){
WeixinJSBridge.log(res.err_msg);
//alert(res.err_msg);
if(res.err_msg == 'get_brand_wcpay_request:ok'){
window.location.href="http://1.aigowu.cn/index.php/Wap/index/order";
}else{
showMsg('交易失败','center');
}
}
);
}
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>
我把代码放在压缩包,有需要的可以借鉴一下,自行融入到自身的程序当中
点击下载:https://pjax.cc/content/uploadfile/201803/2e321522051996.zip