Home  >  Article  >  Backend Development  >  微信支付 传递自定义参数

微信支付 传递自定义参数

WBOY
WBOYOriginal
2016-06-06 20:34:541604browse

各位好:
我在进行微信支付的开发过程中,遇到这样一个情景:

当我在一个购买商品的方法中,获得商户订单号,商品价格,商品名称之后,跳转到微信支付的方法中去,但因为该方法中需要获取code, 而获取code需要跳转页面,当获取到code之后,页面继续跳转回来,但是这时,我自己传递的商户订单号,商品价格,商品名称就已经没有了,所以这个方法获取到为空,导致支付失败.

希望各位可以帮忙提供下可以解决这个问题的思路,谢谢大家了.

将代码简要贴上:

<code>function buyGoods() {   // 购买方法

    $out_trade_no = "thinks";
    $body = "xiexie";
    $total_fee = "200";
    wxPay($body, $out_trade_no, $total_fee);
}


/**
 * 微信支付方法
 */
function wxPay($body, $out_trade_no, $total_fee) {

    $jsApi = new JsApi_pub();
    if (!isset($_GET['code'])) {
        // 触发微信返回code
        $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
        Header("Location:$url");
    } else {

        // 获取code码,以此获取openid
        $code = $_GET['code'];
        $jsApi->setCode($code);
        $openid = $jsApi->getOpenId();
    }
    $unifiedOrder = new UnifiedOrder_pub();
    $unifiedOrder->setParameter("openid","$openid");
    $unifiedOrder->setParameter("body", $body);//商品描述
    //自定义订单号,此处仅作举例
    $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
    $unifiedOrder->setParameter("total_fee","$total_fee");//总金额
    $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
    $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型

    $prepay_id = $unifiedOrder->getPrepayId();
    $jsApi->setPrepayId($prepay_id);
    $jsApiParameters = $jsApi->getParameters();
    return $jsApiParameters;
}
</code>

回复内容:

各位好:
我在进行微信支付的开发过程中,遇到这样一个情景:

当我在一个购买商品的方法中,获得商户订单号,商品价格,商品名称之后,跳转到微信支付的方法中去,但因为该方法中需要获取code, 而获取code需要跳转页面,当获取到code之后,页面继续跳转回来,但是这时,我自己传递的商户订单号,商品价格,商品名称就已经没有了,所以这个方法获取到为空,导致支付失败.

希望各位可以帮忙提供下可以解决这个问题的思路,谢谢大家了.

将代码简要贴上:

<code>function buyGoods() {   // 购买方法

    $out_trade_no = "thinks";
    $body = "xiexie";
    $total_fee = "200";
    wxPay($body, $out_trade_no, $total_fee);
}


/**
 * 微信支付方法
 */
function wxPay($body, $out_trade_no, $total_fee) {

    $jsApi = new JsApi_pub();
    if (!isset($_GET['code'])) {
        // 触发微信返回code
        $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
        Header("Location:$url");
    } else {

        // 获取code码,以此获取openid
        $code = $_GET['code'];
        $jsApi->setCode($code);
        $openid = $jsApi->getOpenId();
    }
    $unifiedOrder = new UnifiedOrder_pub();
    $unifiedOrder->setParameter("openid","$openid");
    $unifiedOrder->setParameter("body", $body);//商品描述
    //自定义订单号,此处仅作举例
    $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
    $unifiedOrder->setParameter("total_fee","$total_fee");//总金额
    $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
    $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型

    $prepay_id = $unifiedOrder->getPrepayId();
    $jsApi->setPrepayId($prepay_id);
    $jsApiParameters = $jsApi->getParameters();
    return $jsApiParameters;
}
</code>

我最终是这样处理的:

<code>        $jsApi = new JsApi_pub();
        if (!isset($_GET['code'])) {
            // 触发微信返回code
            $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
            $state = json_encode(array(
                "body" => $body,
                "out_trade_no" => $out_trade_no,
                "total_fee" => "$total_fee",
            ));
            $url = str_replace("STATE", $state, $url);
            Header("Location:$url");
        } else {
            // 获取code码,以此获取openid
            $code = $_GET['code'];
            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
            $state = $_GET['state'];
        }
</code>

在官方给的URL中有这么个参数state,默认值是STATE,只需要将STATE替换成你所需要的内容就可以了。

$state = $_GET['state'];之后呢,怎样把这三个参数分解出来!求大神解答!

前几天我也在开发微信支付也遇到这个问题了
原本获取code的这个链接是
const JS_API_CALL_URL = 'http://xxxxxx.cn/?c=WeixinBack&a=js_api_call_url';
发现只能获取c,a这个参数获取不到。
解决办法:
const JS_API_CALL_URL = 'http://xxxxxx.cn/?c=WeixinBack|js_api_call_url|...
只能带一个参数就把所有的参数组合成一个字符串带上了
然后在服务器上重写一个规则,将链接写成原本的格式。

你是怎么解决的呢,我也遇到这个问题现在,我用thinkphp

我使用的方法,在一切业务开始之前,先获取到openId然后存到session里。

然后开始业务逻辑,这时候,因为你微信的所有的个人信息都已经在session里了,所以就不会有任何的跳转问题。

认真看一下微信支付的文档,发现是可以传递参数的~ 现在发现另外一个问题:
就是支付完成后,用js写的location.href 不能跳转回支付成功的页面,有遇到这个问题的么??? 又是咋解决的嘞?

$state = $_GET['state'];之后呢,怎样把这三个参数分解出来

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn