qq_40949457
2019-01-31 11:25
采纳率: 100%
浏览 1.1k
已采纳

PHP PC端用扫码支付,手机端H5支付,微信端用JSapi支付能做吗?

我现在做的一个网站要做微信支付,扫码和jsapi支付都做好了,现在PC生成了扫码支付的二维码后,不支付,在去微信端用JSAPI支付时出现了订单重复的问题,$input->SetTrade_type("JSAPI"); 只是改变了支付类型,是不是要把这个没支付的订单在重新生成个订单号才行?我想实现的是PC端用扫码,手机浏览器用H5,微信端用JSAPI现在遇到这个坑卡着了,还请各位大哥帮忙看下,谢谢了

 //调用微信jsapi支付接口
case 'wxjsapipay':
    if(empty($_GET['sn']))
    {
        die('error: no order sn');
    }
    $sn=mysqli_real_escape_string($GLOBALS['db']->conn, htmlspecialchars(stripslashes($_GET['sn'])));
    $title_sn='订单号'.$sn;
    $order=orders_get_by_sn($sn);
    ini_set('date.timezone','Asia/Shanghai');
    require_once "wxpay/lib/WxPay.Api.php";
    require_once "wxpay/example/WxPay.JsApiPay.php";
    require_once 'wxpay/example/log.php';
    //①、获取用户openid
    $tools = new JsApiPay();
    if(!isset($_SESSION[USERSESSION]['wx_openId']) && empty($_SESSION[USERSESSION]['wx_openId'])){
        $_SESSION[USERSESSION]['wx_openId'] = $tools->GetOpenid();
    }
    $money=$order['buy_total']*100;
    //②、统一下单
    $input = new WxPayUnifiedOrder();
    $input->SetBody($title_sn);
    $input->SetAttach($title_sn);
    $input->SetOut_trade_no($sn);
    $input->SetTotal_fee($money);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag($title_sn);
    $input->SetNotify_url("http://www.xxx.com/qrcode/notify.php");
    $input->SetTrade_type("JSAPI");
    $input->SetOpenid($_SESSION[USERSESSION]['wx_openId']);
    $order = WxPayApi::unifiedOrder($input);

// echo "

";
// print_r($order);die;
$jsApiParameters = $tools->GetJsApiParameters($order);
// $editAddress = $tools->GetEditAddressParameters();
$smarty->assign('money', number_format($money/100, 2, '.', ''));
$smarty->assign('jsApiParameters', $jsApiParameters);
$smarty->assign('sn', $sn);
$smarty->display("wxpay.shtml");
exit();
break;
            //调用微信扫码支付接口
case 'wxpay':
    if(empty($_GET['sn']))
    {
        die('error: no order sn');
    }
    $sn=mysqli_real_escape_string($GLOBALS['db']->conn, htmlspecialchars(stripslashes($_GET['sn'])));
    $title_sn='订单号'.$sn;
    $order=orders_get_by_sn($sn);
    ini_set('date.timezone','Asia/Shanghai');
    require_once "wxpay/lib/WxPay.Api.php";
    require_once "wxpay/example/WxPay.NativePay.php";
    require_once 'wxpay/example/log.php';
    $notify = new NativePay();
    $money=$order['buy_total']*100;
    $input = new WxPayUnifiedOrder();
    $input->SetBody($title_sn);
    $input->SetAttach($title_sn);
    $input->SetOut_trade_no($sn);
    $input->SetTotal_fee($money);
    $input->SetTime_start(date("YmdHis"));
    $input->SetTime_expire(date("YmdHis", time() + 600));
    $input->SetGoods_tag($title_sn);
    $input->SetNotify_url("http://www.xxx.com/qrcode/notify.php");
    $input->SetTrade_type("NATIVE");
    $input->SetProduct_id($order['id']);
    $result = $notify->GetPayUrl($input);

// echo "

";
// print_r($result);die;
if (!isset($result["code_url"]))
{
$content = '

Preferential code error

';
$result['content'] = 'Failed to obtain QR code. Please try again later';
$result['title'] = 'Tip';
$result['content_cn'] = '获取支付二维码失败,请稍后再试';
$result['title_cn'] = '提示';
$result['success'] = false;
$order_confirm_url = $ac->createPluginsUrl(array('plug'=>'member','ac'=>'default'));
$result['button'] = 'Back';
$result['button_cn'] = '返回';
$smarty->assign('data', $result);
$smarty->display('sys-info.shtml');
exit();
}
$url2 = $result["code_url"];
$smarty->assign('url2', $url2);
$smarty->assign('money', number_format($money/100, 2, '.', ''));
$smarty->assign('sn', $sn);
$smarty->display("wxpay.shtml");
exit();
break;
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_40949457 2019-01-31 15:36
    已采纳

    我自己研究了下改成了这个样子,不知道这样写代码行不行

            //②、统一下单
        $input = new WxPayUnifiedOrder();
        $input->SetOut_trade_no($sn);
        $is_order = WxPayApi::orderQuery($input);//查询订单
        if(isset($is_order['result_code']) && $is_order['result_code']=='SUCCESS' && isset($is_order['return_code']) && $is_order['return_code']=='SUCCESS'){
            if($is_order['trade_state_desc']=='订单未支付' || $is_order['trade_state']=='NOTPAY'){
    
                $input->SetBody($title_sn);
    
                $input->SetAttach($title_sn);
    
                $input->SetTotal_fee($money);
    
                $input->SetTime_start(date("YmdHis"));
    
                $input->SetTime_expire(date("YmdHis", time() + 600));
    
                $input->SetGoods_tag($title_sn);
    
                $input->SetNotify_url("http://www.xxx.com/qrcode/notify.php");
    
                $input->SetTrade_type("JSAPI");
    
                $input->SetOpenid($_SESSION[USERSESSION]['wx_openId']);
    
                $err_code = WxPayApi::unifiedOrder($input);
    
                if($err_code['err_code']=='INVALID_REQUEST'){
    
                    //订单号重复进来改订单号
    
                    $order_sn_new=orders_new_sn();//重新生成订单号
    
                    $res=$db->query("UPDATE $orders_table set orders_sn='{$order_sn_new}'WHERE orders_sn='{$sn}'");
    
                    $input->SetOut_trade_no($order_sn_new);
    
    
                }
    
                $order = WxPayApi::unifiedOrder($input);
    
                $jsApiParameters = $tools->GetJsApiParameters($order);
    
            }
    
        }else{
    
            //不存在的订单直接发起支付
    
            $input->SetBody($title_sn);
    
            $input->SetAttach($title_sn);
    
            $input->SetTotal_fee($money);
    
            $input->SetTime_start(date("YmdHis"));
    
            $input->SetTime_expire(date("YmdHis", time() + 600));
    
            $input->SetGoods_tag($title_sn);
    
            $input->SetNotify_url("http://www.xxx.com/qrcode/notify.php");
    
            $input->SetTrade_type("JSAPI");
    
            $input->SetOpenid($_SESSION[USERSESSION]['wx_openId']);
    
            $order = WxPayApi::unifiedOrder($input);
    
            $jsApiParameters = $tools->GetJsApiParameters($order);
    
        }
    
    打赏 评论
  • 重新生成一个订单号后调用jsapi付款,不能用同一个商户订单号

    打赏 评论

相关推荐 更多相似问题