PHPcms微信支付

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

有好的建议请留言



1.去官网下载




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

2.重新建立目录结构



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.前台页面


<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/> 
    <title>支付</title>
    <script type="text/javascript">
        //调用微信JS api 支付
        function jsApiCall() {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', 
                {$list[jsApiParameters]},
                function(res) {
                    WeixinJSBridge.log(res.err_msg);
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        window.location.href = "";
                    } else {
                        //返回跳转到订单详情页面
                        alert(支付失败);
                        window.location.href = "";
                    }
                }
            );
        }

        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>
</head>

<body>
    <div class="receive">
        <h2>微信支付</h2>
        <ul class="form-list">

            <p class="submit" type="button" οnclick="callpay()">立即支付</p>
        </ul>
    </div>
</body>

</html>


5.回调函数

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



<?php

ini_set('date.timezone','Asia/Shanghai');
error_reporting(E_ERROR);
require_once "lib/WxPay.Api.php";
require_once 'lib/WxPay.Notify.php';
require_once 'log.php';

//初始化日志
$logHandler= new CLogFileHandler("logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

class PayNotifyCallBack extends WxPayNotify
{
function __construct()
{

    // $hostname = "localhost";
    // $database = "cmsv";
    // $username = "root";
    // $password = "root";


    $conn = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); 
    mysql_select_db($database, $conn);
}
//查询订单
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("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);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值