PHP 微信公众号,小程序获取支付参数。微信支付

首先下载微信官方demo放入项目中

地址:https://github.com/top-songshijie/WechatPay

引入其中所需的文件,复制以下代码直接使用即可

public function _initialize()
    {
        parent::_initialize();
        require_once EXTEND_PATH . "WechatPay/lib/WxPay.Api.php";
        require_once EXTEND_PATH . "WechatPay/example/WxPay.JsApiPay.php";
        require_once EXTEND_PATH . "WechatPay/lib/WxPay.Notify.php";
        require_once EXTEND_PATH . 'WechatPay/example/log.php';
    }


    public function pay($openId , $price , $order_sn , $url)
    {
        $price = intval($price * 100);
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("");
        $input->SetAttach("");
        $input->SetOut_trade_no($order_sn);
        $input->SetTotal_fee($price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("");
        $input->SetNotify_url($url);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $config = new \WxPayConfig();
        $order = \WxPayApi::unifiedOrder($config, $input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return json_decode($jsApiParameters);
    }

    //回调
    public function pay_notify()
    {
        $config = new \WxPayConfig();
        $notify = new \WxPayNotify();
        $order = new OrderModel();
        $user = new UsersModel();
        $notify->Handle($config, false);
        $xml = file_get_contents("php://input");
        $base = new \WxPayResults();
        $data = $base->FromXml($xml);
        //验签
        if ($base->CheckSign($config)) {
            $order_sn = $data['out_trade_no'];
           
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_42330073/article/details/82850803