公众号发送红包

准备工作

  • 阅读微信文档,了解发送红包发放前的准备事项
  • 若开发者尚未在知晓云配置支付证书,请前往知晓云-支付证书页面 配置支付证书
  • 确保你账户里的资金充足

小程序和公众号需要绑定在同一个微信商户号下

发送红包

redPack.sendRedPack(params)

参数说明

参数名类型说明
params.typestring红包类型,普通红包:normal,裂变红包:fission;必填
params.re_openidstring红包接受人(裂变红包时是种子用户)在公众号下的 openid;必填
params.send_namestring商户名称,红包发送者名称;必填
params.official_account_idstring公众号 appid;必填
params.total_amountnumber付款金额,单位元,浮点;必填
params.total_numinteger红包发放总人数,整数;必填
params.act_namestring活动名称;必填
params.remarkstring备注;必填
params.wishingstring红包祝福语;必填
params.scene_idstring场景 id, 可选
params.risk_infostring活动信息, 可选
params.gateway_typeGatewayType微信支付商户类型,可选,默认为微信小程序

GatewayType:

说明
'weixin_tenpay'微信小程序
'weixin_tenpay_js'JSAPI
'weixin_tenpay_native'Native Pay
'weixin_tenpay_app'App
'weixin_tenpay_wap'H5

示例代码

  1. const redPack = new BaaS.RedPack()
  2. // 发送红包
  3. redPack.sendRedPack({
  4. "type": "normal",
  5. "official_account_id": "wxcbda96de0b165486",
  6. "send_name": "send_name",
  7. "re_openid": "onqOjjmM1tad-3ROpncN-yUfa6uI",
  8. "total_amount": 200,
  9. "total_num": 1,
  10. "wishing": "恭喜发财",
  11. "act_name": "新年红包",
  12. "remark": "新年红包",
  13. "scene_id": "PRODUCT_2",
  14. "risk_info": "posttime%3d123123412%26clientversion%3d234134%26mobile%3d122344545%26deviceid%3dIOS",
  15. "gateway_type": "weixin_tenpay_wap",
  16. }).then(res => {
  17. console.log(res.data.return_code === 'SUCCESS')
  18. }).catch(err=>{
  19. // 见下方常见错误说明
  20. })

返回示例

知晓云验证请求参数与证书成功,且微信服务器返回成功,res 结构如下:

  1. {
  2. "status": 200,
  3. "data":{
  4. "return_code": "SUCCESS",
  5. "mch_billno": "0010010404201411170000046545",
  6. "send_listid": ""
  7. }
  8. }

知晓云验证请求参数与证书成功,但微信服务器返回错误,res 结构如下:

  1. {
  2. "status": 200,
  3. "data": {
  4. "err_code": 1,
  5. "err_code_des": "系统繁忙,请稍后再试."
  6. }
  7. }

其他常见错误

状态码说明
400缺少必要的参数、参数值错误、未配置支付证书
415不允许在非云函数环境下调用

查询发送记录

redPack.getSentRedPackList(params)

参数说明

参数名类型说明
params.limitinteger限制返回资源的个数,默认为 20 条,最大可设置为 1000
params.offsetinteger设置返回资源的起始偏移值,默认为 0

示例代码

  1. const redPack = new BaaS.RedPack()
  2. redPack.getSentRedPackList({offset: 0, limit: 20}).then(res => {
  3. console.log(res.data.objects)
  4. })

返回示例

  1. {
  2. "status": 200,
  3. "data": {
  4. "meta": {
  5. "limit": 20,
  6. "next": null,
  7. "offset": 0,
  8. "previous": null,
  9. "total_count": 1
  10. },
  11. "objects": [
  12. {
  13. "act_name": "7af1dHyHQ7fyYG7m0ucFOehVxDyxhXSO",
  14. "amt_type": null,
  15. "created_at": 1548176856,
  16. "id": "5c474dd8e2aa136e58867af3",
  17. "mch_billno": "1glzWSaTi1865qJSVRey97kbRPxj",
  18. "miniapp_id": "1",
  19. "official_account_id": "4pEqBtacy4J2w3fCqfc2cdRdxYblgRRd",
  20. "pk": "5c474dd8e2aa136e58867af3",
  21. "raw_result": "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[发放成功.]]></return_msg>\n <result_code><![C",
  22. "re_openid": "PSSmXsnrTaurKspMjbHe1PepRecxTnl9",
  23. "remark": "1Tj58luM9A",
  24. "result": {
  25. "mch_billno": "0010010404201411170000046545",
  26. "send_listid": "100000000020150520314766074200",
  27. "return_code": "SUCCESS"
  28. },
  29. "risk_info": null,
  30. "scene_id": null,
  31. "send_name": "6FqAgayy90lcrJ6xXVwXyjoViyXRTqWi",
  32. "status": null,
  33. "total_amount": 1.1,
  34. "total_num": 1,
  35. "type": "fission",
  36. "updated_at": 1548176856,
  37. "wishing": "jHrmfUDqJc"
  38. }
  39. ]
  40. }
  41. }

获取单个发送记录详情

redPack.getSentRedPack(id)

参数说明

参数名类型说明
idstring红包记录 ID

示例代码

  1. const redPack = new BaaS.RedPack()
  2. redPack.getSentRedPack('i5c46d771b1b9de0ab5e3c4eed').then(() => {
  3. console.log(res.data)
  4. })

返回示例

  1. {
  2. "status": 200,
  3. "data": {
  4. "act_name": "7af1dHyHQ7fyYG7m0ucFOehVxDyxhXSO",
  5. "amt_type": null,
  6. "created_at": 1548176856,
  7. "id": "5c474dd8e2aa136e58867af3",
  8. "mch_billno": "1glzWSaTi1865qJSVRey97kbRPxj",
  9. "miniapp_id": "1",
  10. "official_account_id": "4pEqBtacy4J2w3fCqfc2cdRdxYblgRRd",
  11. "raw_result": "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[发放成功.]]></return_msg>\n <result_code><![CDATA[SUCCESS]]></result_code>\n <err_code><![CDATA[0]]></err_code>\n <err_code_des><![CDATA[发放成功.]]></err_code_des>\n <mch_billno><![CDATA[0010010404201411170000046545]]></mch_billno>\n <mch_id>10010404</mch_id>\n <wxappid><![CDATA[wx6fa7e3bab7e15415]]></wxappid>\n <re_openid><![CDATA[onqOjjmM1tad-3ROpncN-yUfa6uI]]></re_openid>\n <total_amount>1.1</total_amount>\n <send_listid><![CDATA[100000000020150520314766074200]]></send_listid>\n </xml>",
  12. "re_openid": "PSSmXsnrTaurKspMjbHe1PepRecxTnl9",
  13. "remark": "1Tj58luM9A",
  14. "result": {
  15. "mch_billno": "0010010404201411170000046545",
  16. "send_listid": "100000000020150520314766074200",
  17. "return_code": "SUCCESS"
  18. },
  19. "risk_info": null,
  20. "scene_id": null,
  21. "send_name": "6FqAgayy90lcrJ6xXVwXyjoViyXRTqWi",
  22. "status": null,
  23. "total_amount": 1.1,
  24. "total_num": 1,
  25. "type": "fission",
  26. "updated_at": 1548176856,
  27. "wishing": "jHrmfUDqJc"
  28. }
  29. }

查询发送状态

redPack.getRedPackStatus(params)

通过 订单 ID红包记录 ID 查询发送状态

参数说明

参数名类型说明
params.mch_billnostring订单 ID, mch_billno 和 id 二选一
params.idstring红包记录 ID,mch_billno 和 id 二选一

示例代码

  1. redPack.getRedPackStatus({
  2. id: "i5c46d771b1b9de0ab5e3c4eed"
  3. })

返回示例

  1. {
  2. "status": 200,
  3. "data": {
  4. "err_code": "SUCCESS",
  5. "total_num": "1",
  6. "total_amount": "100",
  7. "mch_billno": "9010080799701411170000046603",
  8. "err_code_des": "OK",
  9. "mch_id": "11475856",
  10. "result_code": "SUCCESS",
  11. "send_type": "ACTIVITY",
  12. "return_msg": "OK",
  13. "detail_id": "10000417012016080830956240040",
  14. "return_code": "SUCCESS",
  15. "hb_type": "NORMAL",
  16. "send_time": "2016-08-08 21:49:22",
  17. "status": "RECEIVED"
  18. }
  19. }