transfer.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. from .type import RequestType
  3. from .core import Core
  4. class Transfer(object):
  5. def __init__(self, wx):
  6. self._core = Core(wx.agent_num, wx.cert_serial_no, wx.apiv3_key)
  7. def transfer_batch(self, appid, out_batch_no, batch_name, batch_remark, total_amount, total_num, transfer_detail_list=[]):
  8. '''
  9. 商户可以通过该接口同时向多个用户微信零钱进行转账操作
  10. :param appid string[1,32] 直连商户的appid: 申请商户号的appid或商户号绑定的appid(企业号corpid即为此appid)示例值:wxf636efh567hg4356
  11. :param out_batch_no string[1,32] 商家批次单号: 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一,示例值:'plfk2020042013'
  12. :param batch_name string[1,32] 批次名称: 该笔批量转账的名称 示例值:2019年1月深圳分部报销单
  13. :param batch_remark string[1,32] 批次备注: 转账说明,UTF8编码,最多允许32个字符 示例值:2019年1月深圳分部报销单
  14. :param total_amount int 转账总金额: 转账金额单位为“分”。转账总金额必须与批次内所有明细转账金额之和保持一致,否则无法发起转账操作
  15. :param total_num int 转账总笔数: 一个转账批次单最多发起三千笔转账。转账总笔数必须与批次内所有明细之和保持一致,否则无法发起转账操作
  16. :param transfer_detail_list array 转账明细列表: 发起批量转账的明细列表,最多三千笔
  17. out_detail_no string[1,32] 商家明细单号 商户系统内部区分转账批次单下不同转账明细单的唯一标识,要求此参数只能由数字、大小写字母组成 示例值:x23zy545Bd5436
  18. transfer_amount int 转账金额 转账金额单位为分
  19. transfer_remark string[1,32] 转账备注 单条转账备注(微信用户会收到该备注),UTF8编码,最多允许32个字符 示例值:2020年4月报销
  20. openid string[1,128] 用户在直连商户应用下的用户标示
  21. 注意:
  22. 商户上送敏感信息时使用微信支付平台公钥加密,证书序列号包含在请求HTTP头部的Wechatpay-Serial
  23. 批量转账一旦发起后,不允许撤销,批次受理成功后开始执行转账
  24. 转账批次单中涉及金额的字段单位为 分
  25. 当返回错误码为 "SYSTEM_ERROR" 时,请不要更换商家批次单号, 一定要使用原商家批次单号重试,否则可能造成重复转账等资金风险。
  26. 不同的商家批次单号 out_batch_no 请求为一个全新的批次, 在未查询到明确的转账批次但处理结果之前,请勿修改商家批次单号 重新提交!
  27. 请商户在自身的系统中合理设置转账频次并做好并发控制,防范错付风险
  28. https://api.mch.weixin.qq.com/v3/transfer/batches
  29. POST
  30. '''
  31. if not appid:
  32. raise Exception(u'缺少参数appid')
  33. if not out_batch_no:
  34. raise Exception(u'缺少参数out_batch_no')
  35. if not out_batch_no:
  36. raise Exception(u'缺少参数batch_name')
  37. if not out_batch_no:
  38. raise Exception(u'缺少参数batch_remark')
  39. if not out_batch_no:
  40. raise Exception(u'缺少参数total_amount')
  41. if not out_batch_no:
  42. raise Exception(u'缺少参数total_num')
  43. if not transfer_detail_list:
  44. raise Exception(u'缺少参数transfer_detail_list')
  45. params = {
  46. 'appid': appid,
  47. 'out_batch_no': out_batch_no,
  48. 'batch_name': batch_name,
  49. 'batch_remark': batch_remark,
  50. 'total_amount': total_amount,
  51. 'total_num': total_num,
  52. 'transfer_detail_list': transfer_detail_list
  53. }
  54. path = '/v3/transfer/batches'
  55. return self._core.request(path, method=RequestType.POST, data=params, cipher_data=False)
  56. def transfer_query_batchid(self, batch_id, need_query_detail=False, offset=0, limit=20, detail_status='ALL'):
  57. """微信批次单号查询批次单
  58. :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
  59. :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
  60. :param offset: 请求资源起始位置,默认值为0
  61. :param limit: 最大资源条数,默认值为20
  62. :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
  63. """
  64. if batch_id:
  65. path = '/v3/transfer/batches/batch-id/%s' % batch_id
  66. else:
  67. raise Exception('参数错误!')
  68. if need_query_detail:
  69. path += '?need_query_detail=true'
  70. else:
  71. path += '?need_query_detail=false'
  72. path += '&offset=%s' % offset
  73. path += '&limit=%s' % limit
  74. path += '&detail_status=%s' % detail_status
  75. return self._core.request(path)
  76. def transfer_query_detail_id(self, batch_id, detail_id):
  77. """微信明细单号查询明细单
  78. :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
  79. :param detail_id: 微信明细单号,微信支付系统内部区分转账批次单下不同转账明细单的唯一标识,示例值:1040000071100999991182020050700019500100
  80. """
  81. if batch_id and detail_id:
  82. path = '/v3/transfer/batches/batch-id/%s/details/detail-id/%s' % (batch_id, detail_id)
  83. else:
  84. raise Exception('参数错误!')
  85. return self._core.request(path)
  86. def transfer_query_out_batch_no(self, out_batch_no, need_query_detail=False, offset=0, limit=20, detail_status='ALL'):
  87. """商家批次单号查询批次单
  88. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  89. :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
  90. :param offset: 请求资源起始位置,默认值为0
  91. :param limit: 最大资源条数,默认值为20
  92. :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
  93. """
  94. if out_batch_no:
  95. path = '/v3/transfer/batches/out-batch-no/%s' % out_batch_no
  96. else:
  97. raise Exception('参数错误!')
  98. if need_query_detail:
  99. path += '?need_query_detail=true'
  100. else:
  101. path += '?need_query_detail=false'
  102. path += '&offset=%s' % offset
  103. path += '&limit=%s' % limit
  104. path += '&detail_status=%s' % detail_status
  105. return self._core.request(path)
  106. def transfer_query_out_detail_no(self, out_detail_no, out_batch_no):
  107. """商家明细单号查询明细单
  108. :param out_detail_no: 商家明细单号,示例值:x23zy545Bd5436
  109. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  110. """
  111. if out_detail_no and out_batch_no:
  112. path = '/v3/transfer/batches/out-batch-no/{}/details/out-detail-no/{}'.format(out_batch_no, out_detail_no)
  113. else:
  114. raise Exception('参数错误!')
  115. return self._core.request(path)