PFinal\Wechat\Service\PayService::createJsBizPackage PHP Метод

createJsBizPackage() публичный статический Метод

https://pay.weixin.qq.com/wiki/doc/api/wap.php?chapter=9_1
public static createJsBizPackage ( string $openid, float $totalFee, string $outTradeNo, string $orderName, string $notifyUrl, $timestamp ) : string
$openid string 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
$totalFee float 收款总费用 单位元
$outTradeNo string 唯一的订单号
$orderName string 订单名称
$notifyUrl string 支付结果通知url 不要有问号 https://mp.weixin.qq.com/ 微信支付-开发配置-测试目录 测试目录 http://www.example.com/paytest/ 最后需要斜线,(需要精确到二级或三级目录)
Результат string
    public static function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp)
    {
        $config = self::getConfig();
        $unified = array('appid' => $config['appid'], 'attach' => '支付', 'body' => $orderName, 'mch_id' => $config['mch_id'], 'nonce_str' => self::createNonceStr(), 'notify_url' => $notifyUrl, 'openid' => $openid, 'out_trade_no' => $outTradeNo, 'spbill_create_ip' => '127.0.0.1', 'total_fee' => sprintf('%.0f', self::calc($totalFee, 100, '*', 2)), 'trade_type' => 'JSAPI');
        $unified['sign'] = self::getSign($unified, $config['key']);
        $responseXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));
        /*
        <xml>
        <return_code><![CDATA[SUCCESS]]></return_code>
        <return_msg><![CDATA[OK]]></return_msg>
        <appid><![CDATA[wx00e5904efec77699]]></appid>
        <mch_id><![CDATA[1220647301]]></mch_id>
        <nonce_str><![CDATA[1LHBROsdmqfXoWQR]]></nonce_str>
        <sign><![CDATA[ACA7BC8A9164D1FBED06C7DFC13EC839]]></sign>
        <result_code><![CDATA[SUCCESS]]></result_code>
        <prepay_id><![CDATA[wx2015032016590503f1bcd9c30421762652]]></prepay_id>
        <trade_type><![CDATA[JSAPI]]></trade_type>
        </xml>
        */
        $unifiedOrder = @simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        //Log::info($responseXml);
        if ($unifiedOrder === false) {
            die('parse xml error');
        }
        if ("{$unifiedOrder->return_code}" !== 'SUCCESS') {
            die($unifiedOrder->return_msg);
        }
        if ("{$unifiedOrder->result_code}" !== 'SUCCESS') {
            die($unifiedOrder->err_code);
            /*
             NOAUTH  商户无此接口权限
             NOTENOUGH  余额不足
             ORDERPAID  商户订单已支付
             ORDERCLOSED  订单已关闭
             SYSTEMERROR  系统错误
             APPID_NOT_EXIST     APPID不存在
             MCHID_NOT_EXIST  MCHID不存在
             APPID_MCHID_NOT_MATCH appid和mch_id不匹配
             LACK_PARAMS 缺少参数
             OUT_TRADE_NO_USED 商户订单号重复
             SIGNERROR 签名错误
             XML_FORMAT_ERROR XML格式错误
             REQUIRE_POST_METHOD 请使用post方法
             POST_DATA_EMPTY post数据为空
             NOT_UTF8 编码格式错误
            */
        }
        //$unifiedOrder->trade_type  交易类型  调用接口提交的交易类型,取值如下:JSAPI,NATIVE,APP
        //$unifiedOrder->prepay_id  预支付交易会话标识 微信生成的预支付回话标识,用于后续接口调用中使用,该值有效期为2小时
        //$unifiedOrder->code_url 二维码链接 trade_type为NATIVE是有返回,可将该参数值生成二维码展示出来进行扫码支付
        $arr = array("appId" => $config['appid'], "timeStamp" => $timestamp, "nonceStr" => self::createNonceStr(), "package" => "prepay_id=" . $unifiedOrder->prepay_id, "signType" => 'MD5');
        $arr['paySign'] = self::getSign($arr, $config['key']);
        return $arr;
    }

Usage Example

Пример #1
0
         mch_id          //微信支付分配的商户号
         appid           //微信分配的公众账号ID
         openid          //用户在商户appid下的唯一标识
         transaction_id  //微信支付订单号
         out_trade_no    //商户订单号
         total_fee       //订单总金额单位默认为分,已转为元
         is_subscribe    //用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效
         attach          //商家数据包,原样返回
         time_end        //支付完成时间
    ]
*/
$notifyUrl = 'http://xxx.com/index.php/wxpay/notify';
// 订单信息
$order = array('totalFee' => 0.01, 'tradeNo' => uniqid(), 'name' => '测试订单');
// 业务签名
$bizPackage = \PFinal\Wechat\Service\PayService::createJsBizPackage($openid, $order['totalFee'], $order['tradeNo'], $order['name'], $notifyUrl, $signPackage['timestamp']);
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>微信支付demo</title>
    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<br>
<br>