代扣
代扣功能描述
和用户完成签约后即可直接从用户签约钱包完成扣款.
代扣规则
- 同一个合约同一个用户只能签约一次, 重复签约会返回签约失败
- 签约后商户和用户都可以解约
- 扣款后全额退款不会返还周期内剩余扣款次数
- 代扣目前仅支持微信支付
- 微信代扣限制(请咨询BD具体的天/月/次额度限制)
代扣接口
1.代扣签约
请求参数说明
| 参数名 | 是否必须 | 类型 | 说明 | 示例值 |
|---|---|---|---|---|
| appId | 否 | string | 应用ID,如果开通了多个为了区分则必传 | wx123567 |
| agreementId | 是 | string | 签约协议ID,微信场景即plan_id | 202301010001 |
| username | 是 | string(32) | 签约用户的名称,用于页面展示,不需要对值进行URL编码,参数值不支持UTF8非3字节编码的字符 | 张三 |
| vendor | 是 | string(16) | 支付渠道,如:wechatpay(微信支付) | wechatpay |
| currency | 是 | string(3) | 货币代码,如:USD(美元) | USD |
| notifyUrl | 是 | string | 异步通知URL,用于接收签约结果通知 | https://api.example.com/notify |
| returnUrl | 否 | string | 同步返回URL,(H5,JSAPI时必须)用户签约完成后跳转的URL | https://api.example.com/return |
| userId | 否 | string | 用户标识,(JSAPI,MINI_APP时必须),vendor=wechatpay时填充openid | oSC6Tw6SrhMMzMguby9cCqVzOo |
| platform | 是 | string | 平台类型,如:WEB/H5(网页)、APP(移动应用)、MINI_APP(小程序)、JSAPI(微信公众号) | WEB |
| clientIp | 否 | string | 客户端IP地址(H5时必须),用于风控校验 | 127.0.0.1 |
| reference | 是 | string(64) | 商户唯一标识号,用于标识本次签约请求 | 202301010001 |
| remark | 否 | string | 备注信息,用于记录本次签约的附加信息 | 测试签约 |
响应参数说明
| 参数名 | 是否必须 | 类型 | 说明 | 示例值 |
|---|---|---|---|---|
| code | 是 | string | 返回码,00表示成功,其他表示失败 | 00 |
| label | 是 | string | 返回码标签,用于标识返回码类型 | 00 |
| message | 是 | string | 返回信息,描述操作结果 | success |
| agreementId | 是 | string | 签约协议ID,微信场景即plan_id | 202301010001 |
| contractNo | 是 | string | 合约编号,nihaopay合约唯一标识 | 202301010001 |
| signId | 是 | string | 签约URL,APP/小程序返回signId,WEB/H5返回signUrl | 202501111159364508440 |
| signUrl | 是 | string | 签约ID,APP/小程序返回signId,WEB/H5返回signUrl | https://api.example.com/sign |
请求示例场景1:WEB签约
shell
curl --location 'https://api.nihaopay.com/v1.2/contract/sign' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"appId": "your appId",
"agreementId": "your agreementId",
"usersame": "your customer name",
"vendor": "wechatpay",
"currency": "USD",
"notifyUrl": "https://YOUR_HOST/notify",
"returnUrl": "https://YOUR_HOST/callback",
"platform": "WEB",
"clientIp": "127.0.0.1",
"reference":"your unique reference",
"remark": "remark",
"userId": "your_user_id"
}'请求失败响应示例
json
{
"code": "01",
"label": "01",
"message": "failed"
}请求成功响应示例场景1:APP/小程序
json
{
"code": "00",
"label": "00",
"message": "success",
"agreementId": "your agreementId",
"contractNo": "our contractNo",
"signId": "xxxxxxxx"
}场景2:JSAPI/WEB/H5
json
{
"code": "00",
"label": "00",
"message": "success",
"agreementId": "your agreementId",
"contractNo": "our contractNo",
"signUrl": "https://api.mch.weixin.qq.com/global/papay/contracts/login?os_session_id=xxx#wechat_pay"
}2.代扣解约
请求参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| agreementId | 可选 | string | 签约协议ID,微信场景即plan_id |
| reference | 可选 | string(64) | 商户唯一标识符 |
| reason | 必须 | string | 解约原因 |
| contractNo | 可选 | string | 合约编号,nihaopay合约唯一标识,reference+agreementId 和 contractNo 不能同时为空 |
响应参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| code | 不为空 | string | 返回码,00表示成功 |
| label | 不为空 | string | 返回码标签 |
| message | 不为空 | string | 返回信息 |
| contractId | 不为空 | string | 合约ID |
| contractNo | 不为空 | string | 合约编号,nihaopay合约唯一标识 |
| reference | 不为空 | string | 商户唯一标识号 |
请求示例使用agreementId + reference
shell
curl --location 'https://api.nihaopay.com/v1.2/contract/terminate' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"agreementId": "your agreementId",
"reference": "your unique reference",
"reason": "your terminate reason"
}'使用contractNo
shell
curl --location 'https://api.nihaopay.com/v1.2/contract/terminate' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"contractNo": "d649252400014ea7a8a890704cc600d6",
"reason": "your terminate reason"
}'成功响应示例
json
{
"code": "00",
"label": "00",
"message": "success",
"contractId": "contract id",
"contractNo": "our contract number",
"reference": "your unique reference"
}失败响应示例
json
{
"code": "01",
"label": "01",
"message": "failed"
}3.代扣合约查询
请求参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| agreementId | 可选 | string | 签约协议ID,微信场景即plan_id |
| reference | 可选 | string(64) | 商户唯一标识符 |
| contractNo | 可选 | string | 合约编号,nihaopay合约唯一标识,reference+agreementId 和 contractNo 不能同时为空 |
响应参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| code | 不为空 | string | 返回码,00表示成功 |
| label | 不为空 | string | 返回码标签 |
| message | 不为空 | string | 返回信息 |
| contractId | 不为空 | string | 合约ID |
| contractNo | 不为空 | string | 合约编号,nihaopay合约唯一标识 |
| contractStatus | 不为空 | string | 合约状态,Signed(已签约)、Terminated(已解约),Processing(待签约),FAIL(签约失败) |
| signedTime | 可能为空 | string | 签约时间 |
| terminateTime | 可能为空 | string | 解约时间 |
| terminateMode | 可能为空 | string | 解约模式,USER(用户主动解约)、MERCHANT(商户解约)、PLATFORM(商户解约平台解约) |
请求示例场景: agreementId + reference
shell
curl --location 'https://api.nihaopay.com/v1.2/contract/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"agreementId": "your agreementId",
"reference": "your unique reference"
}'场景: contractNo
shell
curl --location 'https://api.nihaopay.com/v1.2/contract/query' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"contractNo": "d649252400014ea7a8a890704cc600d6"
}'返回示例
json
{
"code": "00",
"label": "00",
"message": "success",
"contractId": "contract id",
"contractNo": "our contract number",
"contractStatus": "Signed/Terminated",
"signedTime": "2025-01-15 19:15:14",
"terminateTime": "2025-01-15 19:15:14",
"terminateMode": "USER/MERCHANT/PLATFORM",
}4.发起扣款
请求参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| amount | 不为空 | string | 扣款金额 |
| currency | 不为空 | string(3) | 货币代码,如USD |
| note | 可选 | string | 订单备注 |
| description | 不为空 | string | 扣款描述信息(用户端能看到的扣款内容) |
| ipn_url | 不为空 | string | 异步通知URL |
| reference | 不为空 | string(64) | 商户唯一标识符 |
| vendor | 不为空 | string(16) | 支付渠道,如wechatpay |
| contract_id | 不为空 | string | 合约ID,可以从签约回调中的通知获得,也可以主动调用query查询得到 |
响应参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| status | 不为空 | string | 交易状态,可能值: success/failure |
| reference | 不为空 | string | 商户订单号 |
| currency | 不为空 | string(3) | 交易货币,如 USD |
| amount | 不为空 | decimal | 交易金额 |
| rmb_amount | 不为空 | decimal | 人民币金额 |
| id | 不为空 | string | 交易流水号 |
| note | 允许为空 | string | 订单备注 |
| time | 不为空 | string | 交易时间,UTC格式 |
| message | 不为空 | string | 交易结果描述 |
请求示例
shell
curl --location 'https://api.nihaopay.com/v1.2/transactions/autodebit' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <TOKEN>' \
--data '{
"amount": "1",
"currency": "USD",
"note": "your order note",
"description": "<your debit description>",
"ipn_url": "<your ipn url>",
"reference": "<your unique reference>",
"vendor": "wechatpay",
"contract_id": "<your contract id>"
}'成功返回示例
json
{
"status": "success",
"reference": "<your unique reference>",
"currency": "USD",
"amount": 1,
"rmb_amount": 0,
"id": "20250111220054100012",
"note": "autodebit test",
"time": "2025-01-11T22:00:54Z",
"message": ""
}错误返回示例
json
{
"status": "failure",
"reference": "<your unique reference>",
"currency": "USD",
"amount": 1,
"rmb_amount": 0,
"id": "20250111220054100012",
"note": "autodebit test",
"time": "2025-01-11T22:00:54Z",
"message": "用户账户支付已达上限"
}签约状态异步通知
通知参数说明
| 参数 | 是否必须 | 类型 | 说明 |
|---|---|---|---|
| agreementId | 不为空 | string | 签约协议ID,微信场景即plan_id |
| userId | 不为空 | string | 用户ID,微信场景即open_id |
| contractId | 不为空 | string | 合约ID,发起扣款时需要 |
| contractNo | 不为空 | string | 合约编号,nihaopay合约唯一标识 |
| reference | 不为空 | string | 商户唯一标识符 |
| contractStatus | 不为空 | string | 状态:Signed(已签约)、Terminated(已解约),Processing(待签约),FAIL(签约失败) |
| signedAt | 可能为空 | string | 签约时间,签约通知时必须 |
| terminateAt | 可能为空 | string | 解约时间,解约通知时必须 |
| terminateMode | 可能为空 | string | 解约模式,解约通知时必须,USER(用户主动解约)、MERCHANT(商户解约)、PLATFORM(商户解约平台解约) |
签约通知示例
shell
curl -X POST ${YOUR_NOTIFY_URL} \
--header 'Content-Type: application/json' \
--header 'Signature: ${signature}' \
--data '{
"agreementId": "your agreementId",
"userId": "contract user id",
"contractId": "contract id",
"contractNo": "our contract number",
"reference": "your unique reference",
"contractStatus": "Signed/Terminated",
"signedAt": "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ",
"terminateAt": "yyyy-MM-dd'T'HH:mm:ss.SSSZZZ",
"terminateMode": "USER/MERCHANT/PLATFORM",
}'签约通知签名
signature = MD5(body + '&' + MD5(token))
回调通知重试机制
- 最多通知7次
- 通知的间隔频率为:0m,1m,2m,4m,8m,20m,30m
