首页 > 微信 > 详细

微信jsapi支付

时间:2018-08-19 19:54:10      阅读:175      评论:0      收藏:0      [点我收藏+]

页面

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

  封装一个方法调用他

//mobile 微信公众号
$arr = $obj->submit($info);

submit方法

<?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这个文件夹里处理你本地订单的状态信息就行了

$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;

  

微信jsapi支付

原文:https://www.cnblogs.com/yszr/p/9502169.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!