美文网首页PHP
phpcms微信支付(补全动态金额支付)

phpcms微信支付(补全动态金额支付)

作者: 七百年前 | 来源:发表于2016-11-30 11:52 被阅读715次

1.设置支付页面

支付页面获取openid 可存入cookie

//微信支付页面     
public function recharge() {
    $userid = $_GET['userid'];
    if ($userid) {
        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);
        //var_dump(111);
        //①、获取用户openid
        $tools = new JsApiPay();
        //var_dump(222);
        $openId = $tools->GetOpenid();
    }
    include template('xxx', 'xxxxxx', 'xxx');
}

2.拆分支付拼装

ajax 传入金额拼装支付函数变量

public function getparameters() {
    if ($_POST['userid'] && $_POST['openid'] && $_POST['money']) {
        include PHPCMS_PATH . '/wxpay/lib/WxPay.Api.php';
        include PHPCMS_PATH . '/wxpay/WxPay.JsApiPay.php';
        include PHPCMS_PATH . '/wxpay/log.php';
        $openId = $_POST['openid'];
        $userid = $_POST['userid'];
        $list['cate_name'] = '充值';
        $list['amount'] = $_POST['money'];
        $list['order_sn'] = time() . random(8) . '_' . $userid;

        $tools = new JsApiPay();
        //②、统一下单
        $input = new WxPayUnifiedOrder();
        //var_dump(444);
        $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.i-ev.com/wxpay/wechatnotify.php");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        
        $res = array('status' =>'1', 'info' => '成功', 'data' => json_decode($jsApiParameters,TRUE));
    }else{
       $res =  array('status' =>'2', 'info' => '失败');
    }
    echo json_encode($res);exit;
}

3.前台修改

<script>

function paySub(){
  var money = $('#chongzhi-num').val() || $('.current-num').attr('data-num');
  //money = 0.01;
  if(isNaN(money) || money == '' || money == undefined){
      alert('请输入正确的金额');
      return;
  }
  $.post('http://xxxxx&a=getparameters',{'money':money,'openid':'{$openId}','userid':'{$userid}'},function(data){
      console.log(data);
      if(data.status == 1){
       //吊起微信支付
        callpay(data.data);
      }else{
        alert('请重试');
      }
  },'json');
}
//调用微信JS api 支付
function jsApiCall(jsApiParameters) {
  WeixinJSBridge.invoke('getBrandWCPayRequest',jsApiParameters,function(res) {
    WeixinJSBridge.log(res.err_msg);
      if (res.err_msg == "get_brand_wcpay_request:ok") {
              window.location.href = "xxxxxx";
      } else {
              //返回跳转到订单详情页面
              alert('支付失败');
              window.location.href = "xxxx";
      }
    }
  );
}

function callpay(jsApiParameters) {
        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(jsApiParameters);
        }
}
</script>

相关文章

网友评论

本文标题:phpcms微信支付(补全动态金额支付)

本文链接:https://www.haomeiwen.com/subject/nrxtmttx.html