PHPcms微信支付

基本上按照官方给的sdk修改的没有改动太多东西
微信支付有些麻烦 需要在微信公众平台上设置-支付授权目录(重要)和-[网页授权获取用户基本信息](重要)
流程已经走通 因为各个项目的不同仅供参考吧
有好的建议请留言

Paste_Image.png

1.去官网下载

PHPcms微信支付_第1张图片
Paste_Image.png

https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

2.重新建立目录结构

PHPcms微信支付_第2张图片
Paste_Image.png

3.在模块方法中建立方法

注微信支付中获取code和openid时容易丢失传递值 可以考虑cookie传值

 public function wx_pay() {
    include PHPCMS_PATH . '/wxpay/lib/WxPay.Api.php';
    include PHPCMS_PATH . '/wxpay/WxPay.JsApiPay.php';
    include PHPCMS_PATH . '/wxpay/log.php';
    $logHandler = new CLogFileHandler(PHPCMS_PATH . "'/wxpay/logs/" . date('Y-m-d') . '.log');
    $log = Log::Init($logHandler, 15);
    //①、获取用户openid
    $tools = new JsApiPay();
    $openId = $tools->GetOpenid();
    $list['cate_name'] = param::get_cookie('cate_name');
    $list['amount'] = param::get_cookie('amount');
    $list['order_sn'] = param::get_cookie('order_sn');
    //②、统一下单
    $input = new WxPayUnifiedOrder();
    $input->SetBody($list['cate_name']);
    $input->SetAttach($list['cate_name']);
    $input->SetOut_trade_no($list['order_sn']);
    $input->SetTotal_fee($list['amount'] * 100);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag("test");
    $input->SetNotify_url("http://www.www.com/wxpay/notify.php");
    $input->SetTrade_type("JSAPI");
    $input->SetOpenid($openId);
    $order = WxPayApi::unifiedOrder($input);
    $list['jsApiParameters'] = $tools->GetJsApiParameters($order);
    //var_dump($list);exit;
    include template('xxx', 'xxx', 'default');
}

4.前台页面





    
     
    支付
    



    

微信支付

    立即支付

5.回调函数

经测试回调链接不支持拼接链接 因为赶时间没有进行其他的实现方法
回调函数 /wxpay/notify.php

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("call back:" . json_encode($data));
    $notfiyOutput = array();
    
    if(!array_key_exists("transaction_id", $data)){
        $msg = "输入参数不正确";
        return false;
    }
    //查询订单,判断订单真实性
    if(!$this->Queryorder($data["transaction_id"])){
        $msg = "订单查询失败";
        return false;
    }
    $order_sn = $data["out_trade_no"];
            $transaction_id = $data["transaction_id"];
    $amount = $data["total_fee"]/100;
    $query = "SELECT ...";
    $result = mysql_query($query); 
    $row = mysql_fetch_array($result);
    $time = time();
    Log::DEBUG("v2_code_order:" . json_encode($row));
    if($amount == $row['amount']){
        $sql2 = "SELECT id,code_sn FROM ...";
        $re = mysql_query($sql2); 
        $code = mysql_fetch_array($re);
        Log::DEBUG("code_ll:" . json_encode($code));
        $sql_code="UPDATE v2_code SET..";
        $code_up = mysql_query($sql_code);

        $sql_order="UPDATE v2_code_order SET ...'";
        $order_up = mysql_query($sql_order);
        Log::DEBUG("code_up:" . json_encode($code_up));
        Log::DEBUG("order_up:" . json_encode($order_up));

    }else{
        return false;
    }
    
    
    return true;
}
}

Log::DEBUG("begin notify");
$notify = new PayNotifyCallBack();
$notify->Handle(false);

6.微信支付扫码支付(接口)

public function jsapi()
{
    include PHPCMS_PATH . '/wxpay/lib/WxPay.Api.php';
    include PHPCMS_PATH . '/wxpay/WxPay.JsApiPay.php';
    include PHPCMS_PATH . '/wxpay/WxPay.NativePay.php';
    include PHPCMS_PATH . '/wxpay/log.php';

    if ($_POST['user_name'] && $_POST['amount'] && $_POST['title'] && $_POST['user_id']) {
        $order_sn = date("YmdHis") . substr(time(), -3) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT);

        $order['order_sn']     = $order_sn;
        $order['amount']       = $_POST['amount'];
        $order['user_phone']   = $_POST['user_phone'];
        $order['user_id']      = $_POST['user_id'];
        $order['source']       = $_POST['source'];
        $order['user_address'] = $_POST['user_address'];
        $order['user_name']    = $_POST['user_name'];
        $order['cate_name']    = $_POST['title'];
        $order['cate_id']      = 101;
        $order['c_time']       = time();

        $re = 1;
        if ($re) {
            $notify = new NativePay();
            $input  = new WxPayUnifiedOrder();
            $input->SetBody($_POST['title']);
            $input->SetAttach($_POST['title']);
            $input->SetOut_trade_no($order_sn);
            $input->SetTotal_fee($_POST['amount'] * 100);
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetGoods_tag("xxxx");
            $input->SetNotify_url("http://www.xxxx.com/wxpay/notify.php");
            $input->SetTrade_type("NATIVE");
            $input->SetProduct_id(time());
            $result = $notify->GetPayUrl($input);
            $url    = $result["code_url"];

            $url = $result["code_url"];
        }
    }

你可能感兴趣的:(PHPcms微信支付)