# 微信公众号支付

## 1. 使用场景

商户已有H5商城网站，用户通过消息或扫描二维码在微信内打开网页时，可以调用微信支付完成下单购买的流程。

步骤（1）：如图7.1，商户下发图文消息或者通过自定义菜单吸引用户点击进入商户网页。

步骤（2）：如图7.2，进入商户网页，用户选择购买，完成选购流程。

![图7.3 用户确认支付，输入密码](/files/cVrYZIWmBCxyNNQk0O9B)![图7.4 用户支付成功提示](/files/XBgCgBaWLv7GZOaDAHil)

步骤（3）：如图7.3，调起微信支付控件，用户开始输入支付密码。

步骤（4）：如图7.4，密码验证通过，支付成功。商户后台得到支付成功的通知。

![](/files/zQpdOPrjam5XXfMyOQFw)![](/files/wuFhcjeHQUJ9K49b5z6J)

## 2. 开发步骤

### 2.1. 设置支付授权目录

提供需要调用支付的域名给UseePay进行配置

### 2.2. 设置页面授权域名

开发公众号支付时，在下单接口中要求必传用户openid，而获取openid则需要您在公众平台设置获取openid的域名，只有被设置过的域名才是一个有效的获取openid的域名，否则将获取失败。具体界面如图7.8所示：

<figure><img src="/files/ALyInlgvNR4CBZIHvj5O" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/V9Ezr2MkhCDSyOwQBCwI" alt=""><figcaption></figcaption></figure>

### 2.3. 业务流程时序图

<figure><img src="/files/rpWnDrUQ5cPvn9QmzvaR" alt=""><figcaption></figcaption></figure>

商户与UseePay系统主要交互：

1. 商户Server 调用 [pay 接口请求订单](#wei-xin-gong-zhong-hao-zhi-fu-wechatofficeaccount)
2. 商户Server接受[支付通知](/developer/reference/notify/notifyurl.md)
3. 商户Server[查询支付结果 ](/developer/reference/api-reference-xin-yong-ka/order/order-detail.md)

## 3. Pay接口

请查看[Jsapi支付下单接口](/developer/reference/local-pay-ben-di-hua/ben-di-hua-jie-kou/dian-zi-qian-bao/wechat-hk/jsapi.md)

## 4. 如何获取openId

请查看[微信授权](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html)

## 5. 微信H5调起支付

> 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
>
> 注意：WeixinJSBridge内置对象在其他浏览器中无效。
>
> **列表中参数名区分大小，大小写错误签名验证会失败。**&#x20;
>
> getBrandWCPayRequest参数以及返回值定义如下：

网页内支付接口err\_msg返回结果值说明：

```javascript
 WeixinJSBridge.invoke(
       'getBrandWCPayRequest',{
             "appId":"wx2421b1c4370ec43b",//公众号名称，由商户传入
             "timeStamp":"1395712654", //时间戳，自1970年以来的秒数
             "nonceStr":"e61463f8efa94090b1f366cccfbbb444",//随机串
             "package":"prepay_id=u802345jgfjsdfgsdg888",
             "signType":"RSA", //微信签名方式：
             "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89"//微信签名
         },
        function(res){
            if(res.err_msg === "get_brand_wcpay_request:ok" ) {
                // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                alert("支付成功");
            }
        }
    )

```

| 返回值                               | 描述        |
| --------------------------------- | --------- |
| get\_brand\_wcpay\_request:ok     | 支付成功      |
| get\_brand\_wcpay\_request:cancel | 支付过程中用户取消 |
| get\_brand\_wcpay\_request:fail   | 支付失败      |

JS API的返回结果get\_brand\_wcpay\_request:ok仅在用户成功完成支付时返回。由于前端交互复杂，get\_brand\_wcpay\_request:cancel或者get\_brand\_wcpay\_request:fail可以统一处理为用户遇到错误或者主动放弃，不必细化区分


---

# 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://useepay.gitbook.io/developer/reference/local-pay-ben-di-hua/ben-di-hua-jie-kou/dian-zi-qian-bao/wechat-hk/office-account.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.
