# 微信 JSAPI 支付

> 在使用 JSAPI支付之前，请先[获得用户openid](/api/huo-qu-yong-hu-openid.md)

#### 请求URL

* `https://newapi.mbd.pub/release/wx`

#### 请求方式

* POST
* Json

#### 请求参数

| **参数名**        | **必选** | **类型** | **说明**                                                                                                                           |
| -------------- | ------ | ------ | -------------------------------------------------------------------------------------------------------------------------------- |
| openid         | 是      | string | 用户的openid，参见[获取openid](/api/huo-qu-yong-hu-openid.md)                                                                            |
| app\_id        | 是      | string | 你的 app\_id，可在控制台查看                                                                                                               |
| share\_code    | 否      | string | 分账参数，需先[开通分账权限](/kai-fa-zhe-zhi-chi/ying-yong-he-zuo-fen-zhang-quan-xian.md#shen-qing-fen-zhang-quan-xian-de-qian-zhi-tiao-jian) |
| description    | 是      | string | 支付描述，一般为商品名称                                                                                                                     |
| amount\_total  | 是      | number | 金额，**单位为分**                                                                                                                      |
| out\_trade\_no | 否      | string | 订单号，如不填，面包多将随机生成订单号                                                                                                              |
| callback\_url  | 是      | string | 支付后跳转地址                                                                                                                          |
| sign           | 是      | string | 请求签名，参照[签名算法](/api/qian-ming-suan-fa.md)                                                                                         |

#### 返回示例

```
//成功
{
    "appId": "wxa8423199a021d",
    "timeStamp": "1615441885",
    "nonceStr": "ulAuiq1uxxofwSCH",
    "package": "prepay_id=wx111351262666123e13b9da0000",
    "signType": "RSA",
    "paySign": "4QaU1C1HvjBdTTgsjwP48YWC+OlHtqWOXWPGD3hEt27S3Snha7iDjwBPOSMLOueTIxi0TAaAaeZy0QU7RU065XwlNxTH9NDP9oN+mXyy+S6bgmNHKyxHyISVeo1gCZhFKCidpaj2KgUHgfyyvYcte5oeK3jtYccAyFyJ23IugvYSggxUKM6JQKIsJ9YWAWIBqL9q9FINbKl0bjOAMYC0IAmmw8whaLC7ZdFZ9wIsHE6YYuXwnDi/O6tRpIdZf/y3w=="
}

//失败
{
    "error": "error sign"
}
```

#### 返回参数说明

| **参数名** | **类型** | **说明** |
| ------- | ------ | ------ |
| error   | string | 错误说明   |
|         |        |        |

#### 备注

* 如果返回结果不包含 error，则请求成功，返回的参数直接放入html中，即可在微信内使用微信 jsapi 唤起支付
* jsapi支付的使用请参考微信官方文档
* 根据微信规则，jsapi 支付后用户会进入「小票页面」，用户点击该页面的「继续」按钮，会跳转至 `callback_url`

#### 支付Demo

> 该 demo 参考 payjs

```
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta name="format-detection" content="email=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
</head>
<body style="padding:20px;">
<a href="javascript:;" class="weui-btn weui-btn_primary" id="payBtn">微信支付</a>
</body>
<script>
    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }
    function onBridgeReady() {
        WeixinJSBridge.call('hideOptionMenu');
    }
    $('#payBtn').on('click', function () {
        WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                    // 以下6个支付参数由本接口获取
                    // **************************
                    "appId": "wxc5205a653b0259ac",
                    "timeStamp": "15100000000",
                    "nonceStr": "9cJEu27X6KehHGM8",
                    "package": "prepay_id=wx162234040923141245861167",
                    "signType": "MD5",
                    "paySign": "D64EF3ADBCA224E435D321619710C008"
                    // **************************
                },
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        WeixinJSBridge.call('closeWindow');
                    }
                }
        );
    });
</script>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.mbd.pub/api/wei-xin-zhi-fu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
