Thinkphp微信公衆號JSAPI支付,微信支付簡易實例

  1. 官方微信支付文檔 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1
  2. 下載php版本demo
    Thinkphp微信公衆號JSAPI支付,微信支付簡易實例
    3.具體調用代碼如下:

    
    <?php
    namespace app\common\library;
    class Pay
    {
    
    public function wxpay($openId,$goods,$order_sn,$total_fee,$attach='')
    {
        require_once APP_PATH."/common/library/php_sdk/lib/WxPay.Api.php";
        require_once APP_PATH."/common/library/php_sdk/lib/WxPay.JsApiPay.php";
        require_once APP_PATH."/common/library/php_sdk/example/log.php";
        // $logHandler= new CLogFileHandler(APP_ROOT."/Api/wxpay/logs/".date('Y-m-d').'.log');
        // $log = Log::Init($logHandler, 15);
    
        $tools = new \JsApiPay();
        if(empty($openId)) $openId = $tools->GetOpenid();
        $config = new \WxPayConfig();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($goods);     //商品名稱
        $input->SetAttach($attach);     //附加參數,可填可不填,填寫的話,裏邊字符串不能出現空格
        $input->SetOut_trade_no($order_sn);   //訂單號
        $input->SetTotal_fee($total_fee * 100);   //支付金額,單位:分
        $input->SetTime_start(date("YmdHis"));  //支付發起時間
        $input->SetTime_expire(date("YmdHis", time() + 600));//支付超時
        $input->SetGoods_tag("test3");
        //$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php"); //支付回調驗證地址
        $input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/web/payment/notify/WeixinPay/notify");
        $input->SetTrade_type("JSAPI");    //支付類型
        $input->SetOpenid($openId);     //用戶openID
        $order = \WxPayApi::unifiedOrder($config,$input); //統一下單
        // dump($order);exit;
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

}

    $pay = new \app\common\library\Pay();
    $res = $pay->wxpay($this->openid,'開通會員',$order['order_no'],$order['money']);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章