> For the complete documentation index, see [llms.txt](https://payment.gitbook.io/mpay/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://payment.gitbook.io/mpay/summary/notify.md).

# 1.2.4 异步通知

### 异步通知

**异步通知**

对于转账产生的交易，赤龙支付会根据原始转账请求接口API中传入的异步通知地址`notify_url`，通过`POST`请求的形式将支付结果作为参数通知到商户系统。

**通知字段说明**

| 参数                | 类型     | 是否必填 | 描述                       |
| ----------------- | ------ | :--: | ------------------------ |
| mch\_id           | string |   是  | 商户号                      |
| sub\_merchant\_id | string |   是  | 子商户号                     |
| order\_no         | string |   是  | 平台订单号                    |
| mch\_order\_no    | string |   是  | 商户订单唯一订单号                |
| goods             | string |   是  | 商品名                      |
| total\_fee        | string |   是  | 代付金额                     |
| rate              | string |   是  | 费率                       |
| pay\_type         | string |   是  | alipay 支付宝  wechat 微信    |
| service\_charge   | string |   是  | 代付服务费                    |
| status            | string |   是  | 交易状态  -1支付失败  0未支付  1已支付 |
| create\_time      | string |   是  | 订单创建时间                   |
| pay\_time         | string |   是  | 订单支付时间                   |
| sign              | string |   是  | 签名字符串                    |

**通知信息**

```javascript
{
    "mch_id":"10000",
    "sub_merchant_id":"",
    "order_no":"S100001809271542554317",
    "mch_order_no":"74471538034175",
    "goods":"支付宝企业接口支付",
    "total_fee":"0.01",
    "rate":"0.2000",
    "pay_type":"alipay",
    "service_charge":"0.000",
    "status":"1",
    "create_time":"2018-09-27 15:42:55",
    "pay_time":"2018-09-27 15:43:25",
    "sign":"d73ab7ce4d43338cb793b1d34484b495"
}
```

**异步返回结果的验签**

* 第一步： 在通知返回参数列表中，除去`sign`参数外，凡是通知返回回来的参数皆是待验签的参数。
* 第二步： 通知中所有参数全部使用了 `urlencode`编码，使用前请先 `urldecode`解码
* 第三步： 将剩下参数进行 url\_decode, 然后进行字典排序，组成字符串，得到待签名字符串
* 第四步：商户接收程序执行完后必须打印输出`“success”`（不包含引号）。如果商户反馈给赤龙支付的字符不是`success`这7个字符，赤龙支付服务器会不断重发通知，直到超过24小时22分钟。一般情况下，25小时以内完成8次通知（通知的间隔频率一般是：4m,10m,10m,1h,2h,6h,15h）；

**参考DEMO**

异步回调地址请求接口为`notity()`方法

> 例如回调地址为：`https://cn.bing.com/notity`

```php
// 异步回调地址
public function transferNotify()
{
    exit('success');
    $post = request()->post();
    $orderInfo = Order::get(['order_no' => $post['order_no']]);
    $mch_id = $orderInfo->mch_id;
    if ($post['sign'] != $this->sign($post, $mch_id)) {
        exit('verify sign fail');
    }
    //解码
    foreach ($post as $key => &$value) {
        $value = urldecode($value);
    }
    unset($value);
    //先检测系统订单是否已经成功
    $info = Db::name('test_order')
        ->where(['third_order_id' => $post['mch_order_no']])
        ->find();
    if (empty($info)) {
        exit('Fail');
    }
    if ($info['status'] == 1) {
        exit('success');
    }
    if ($post['status'] == 1) {
        $row = Db::name('test_order')
            ->where(['third_order_id' => $post['mch_order_no']])
            ->update([
                'status' => 1,
                'pay_time' => strtotime($post['pay_time'])
            ]);
        if ($row >= 0) {
            //处理成功！
            exit('success');
        }
    }
}

// 签名方法
private function sign($data, $mch_id)
{
    // POST参数解码
    foreach ($data as $key => &$value) {
        $value = urldecode($value);
    }
    unset($value);
    // 过滤掉无效的参数
    if (isset($data['sign'])) {
        unset($data['sign']);
    }
    // 商户秘钥key，可以通过商户管理平台【商户管理】=》【商户资料】
    $key = "4845c24ee71e78f8e394afae1ce4bc36";
    ksort($data);
    $params_str = urldecode(http_build_query($data));
    $params_str = $params_str . '&key=' . $key;
    return md5($params_str);
}
```

> 由于异步使用`POST`方式发送通知信息，因此该页面中获取参数的方式，如：`request.Form("order_no")`、`$_POST["order_no"]`；
