微信jsapi支付

頁面javascript

<div class="fix-bottom-bar">
        <a href="javascript:;" class="wx-pay-btn" onclick="callpay()">當即支付</a>
    </div>

    <script type="text/javascript">
        //調用微信JS api 支付
        function jsApiCall()
        {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                <?php echo $parameter; ?>,
                function(res){
                    if(res.err_msg == "get_brand_wcpay_request:ok"){
                        window.location.href="/payment/status/index/?<?php echo 'sign='.md5('11').'&ordersn='.$ordersn;?>";
                    }else{
                        //返回跳轉到訂單詳情頁面
                        window.location.href="/payment/status/?<?php echo 'sign='.md5('00');?>";
                    }
                }
            );
        }

        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>

  封裝一個方法調用他php

//mobile 微信公衆號
$arr = $obj->submit($info);html

submit方法java

<?php defined('SYSPATH') or die('No direct script access.');

/**
 *  mobile 微信掃碼支付
 * Class Pay_Mobile_WxPay
 */
class Pay_Mobile_WxPay
{
    //微信支付目錄
    private $_wxPayDir;
    //異步通知
    const NOTIFY_URL = '/callback/index/Pay_Mobile_WxPay-notify_url/';

    /**
     * 微信支付初始化
     */
    public function __construct()
    {
        $this->_wxPayDir = Common::C('interface_path') . 'mobile/wxpay/';
        //綁定支付的APPID
        define('APPID', Common::C('cfg_wxpay_appid'));
        //商戶號
        define('MCHID', Common::C('cfg_wxpay_mchid'));
        //商戶支付密鑰
        define('KEY', Common::C('cfg_wxpay_key'));
        //公衆賬號secert
        define('APPSECRET', Common::C('cfg_wxpay_appsecret'));
    }

    /**
     * 支付數據格式化
     * @param $data
     * @return array
     */
    public function submit($data)
    {
        require $this->_wxPayDir . 'jsapi/index.php';
        $jsApiPay = new JsApiPay();
        $openId = $jsApiPay->GetOpenid();
這就是官網的例子而後吧參數替換成本身的便可關鍵是微信wxpay,api等等這些文檔路徑要引入正確就行 if (!isset($_GET['code'])) { exit; } $ordersn = $this->generate_ordersn($data['ordersn']); $input = new WxPayUnifiedOrder(); $input->SetBody($ordersn); //商品描述 $input->SetAttach($data['remark']); //備註 $input->SetOut_trade_no($ordersn); //商戶訂單號 $input->SetTotal_fee($data['total'] * 100);//總金額,以分爲單位 $input->SetTime_start(date("YmdHis"));//交易起始時間 $input->SetTime_expire(date("YmdHis", time() + 6000));//交易結束時間 $input->SetGoods_tag("tag");//商品標記 $input->SetNotify_url(Common::C('base_url') . self::NOTIFY_URL); //異步通知 $input->SetTrade_type("JSAPI"); $input->SetProduct_id($data['ordersn']); $input->SetOpenid($openId); $order = WxPayApi::unifiedOrder($input); $jsApiParameters = $jsApiPay->GetJsApiParameters($order); $arr = array( 'parameter' => $jsApiParameters, 'productname' => $data['productname'], 'total_fee' => $data['total'], 'ordersn' => $data['ordersn'], 'template' => Common::C('template_dir') . 'mobile/wx_jsapi' ); return $arr; } /** * 微信支付異步通知回調地址 */ public function notify_url() { require $this->_wxPayDir . 'jsapi/notify.php'; $notify = new notify(); $notify->Handle(true); } /** * @function 生成微信支付訂單號(規則:原訂單號+當前時間time()+6位隨機數) * @param $ordersn * @return 返回32位訂單號. */ private function generate_ordersn($ordersn) { $rand_num = St_Math::get_random_number(6); return $ordersn.time().$rand_num; } }

  還有一點,在支付成功後的毀掉地址裏面notify這個文件夾裏處理你本地訂單的狀態信息就好了json

$bool = false;
        //返回狀態碼、業務結果
        if (array_key_exists("return_code", $data) && array_key_exists("result_code", $data) && $data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS')
        {
            //查詢訂單
            if (isset($data["out_trade_no"]) && $data["out_trade_no"] != "")
            {
                $input = new WxPayOrderQuery();
                $input->SetOut_trade_no($data["out_trade_no"]);//商戶訂單號
                $result = WxPayApi::orderQuery($input);
                $tip = '信息:微信公衆號交易,訂單金額與實際支付不一致';
                //這裏針對微信訂單號做特殊處理,去掉後面的16位字符
                $ordersn = substr($data['out_trade_no'],0,strlen($data['out_trade_no'])-16);
                if (isset($result['total_fee']) && Common::total_fee_confirm($ordersn, $result['total_fee'] / 100, $tip))
                {
                    $bool = true;
                    $method = Common::C('mobile');

                    Common::pay_success($ordersn, $method['method']['8']['name']);
                    $online_transaction_no = array('source'=>'wxpay','transaction_no'=>$data['transaction_id']);
                    //寫入微信訂單號
                    DB::update('member_order')->set(array('online_transaction_no'=>json_encode($online_transaction_no)))
                        ->where('ordersn','=',$ordersn)
                        ->execute();

                }
            }
            else
            {
                new Pay_Exception("信息:微信公衆號下單,未會返回商品訂單號");
            }
        }
        else
        {
            new Pay_Exception("信息:微信公衆號交易錯誤(msg_{$data['return_msg']})");
        }
        return $bool;