transfer.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from ..api.base import BaseWeChatAPI
  4. from ...type import RequestType
  5. class Transfer(BaseWeChatAPI):
  6. def transfer(self, out_trade_no, user_id, amount, desc, check_name = 'FORCE_CHECK', real_name = None):
  7. """
  8. 企业付款到微信 该接口为了和v1接口兼容提供
  9. :param out_trade_no:
  10. :param user_id:
  11. :param amount:
  12. :param desc:
  13. :param check_name:
  14. :param real_name:
  15. :return:
  16. """
  17. out_batch_no = out_trade_no
  18. total_amount = amount
  19. batch_name = desc
  20. batch_remark = desc
  21. total_num = 1
  22. transfer_detail_list = [
  23. {
  24. "out_detail_no": out_batch_no,
  25. "transfer_amount": amount,
  26. "transfer_remark": desc,
  27. "openid": user_id,
  28. "user_name": real_name
  29. }]
  30. return self.transfer_batch(
  31. out_batch_no, batch_name, batch_remark, total_amount, total_num, transfer_detail_list)
  32. def query(self, out_trade_no):
  33. """
  34. 企业付款查询接口 该接口为了和v1接口兼容提供
  35. :param out_trade_no: 商户调用企业付款API时使用的商户订单号
  36. :return: 返回的结果数据
  37. """
  38. return self.transfer_query_out_detail_no(out_trade_no, out_trade_no)
  39. def transfer_batch(self, out_batch_no, batch_name, batch_remark, total_amount, total_num, transfer_detail_list):
  40. """发起商家转账
  41. :param out_batch_no: 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一,示例值:'plfk2020042013'
  42. :param batch_name: 该笔批量转账的名称,示例值:'2019年1月深圳分部报销单'
  43. :param batch_remark: 转账说明,UTF8编码,最多允许32个字符,示例值:'2019年1月深圳分部报销单'
  44. :param total_amount: 转账总金额,单位为分,必须与批次内所有明细转账金额之和保持一致,否则无法发起转账操作,示例值:'4000000'
  45. :param total_num: 转账总笔数,必须与批次内所有明细之和保持一致,否则无法发起转账操作,示例值:200
  46. :param transfer_detail_list: 发起批量转账的明细列表,最多三千笔,示例值:[{"out_detail_no": "x23zy545Bd5436", "transfer_amount": 200000, "transfer_remark": "2020年4月报销", "openid": "o-MYE42l80oelYMDE34nYD456Xoy", "user_name": "张三"}]
  47. """
  48. params = {}
  49. if out_batch_no:
  50. params.update({'out_batch_no': out_batch_no})
  51. else:
  52. raise Exception('out_batch_no is not assigned')
  53. if batch_name:
  54. params.update({'batch_name': batch_name})
  55. else:
  56. raise Exception('batch_name is not assigned')
  57. if batch_remark:
  58. params.update({'batch_remark': batch_remark})
  59. else:
  60. raise Exception('batch_remark is not assigned')
  61. if total_amount:
  62. params.update({'total_amount': total_amount})
  63. else:
  64. raise Exception('total_amount is not assigned')
  65. if total_num:
  66. params.update({'total_num': total_num})
  67. else:
  68. raise Exception('total_num is not assigned')
  69. if transfer_detail_list:
  70. params.update({'transfer_detail_list': transfer_detail_list})
  71. else:
  72. raise Exception('transfer_detail_list is not assigned')
  73. cipher_data = False
  74. for transfer_detail in params.get('transfer_detail_list'):
  75. if transfer_detail.get('user_name'):
  76. transfer_detail['user_name'] = self.client.core.encrypt(transfer_detail.get('user_name'))
  77. cipher_data = True
  78. params.update({'appid': self.client.appid})
  79. path = '/v3/transfer/batches'
  80. return self.client.core.request(path, method = RequestType.POST, data = params, cipher_data = cipher_data)
  81. def transfer_query_batchid(self, batch_id, need_query_detail = False, offset = 0, limit = 20,
  82. detail_status = 'ALL'):
  83. """微信批次单号查询批次单
  84. :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
  85. :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
  86. :param offset: 请求资源起始位置,默认值为0
  87. :param limit: 最大资源条数,默认值为20
  88. :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
  89. """
  90. if batch_id:
  91. path = '/v3/transfer/batches/batch-id/%s' % batch_id
  92. else:
  93. raise Exception('batch_id is not assigned')
  94. if need_query_detail:
  95. path += '?need_query_detail=true'
  96. else:
  97. path += '?need_query_detail=false'
  98. path += '&offset=%s' % offset
  99. path += '&limit=%s' % limit
  100. path += '&detail_status=%s' % detail_status
  101. return self.client.core.request(path)
  102. def transfer_query_detail_id(self, batch_id, detail_id):
  103. """微信明细单号查询明细单
  104. :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
  105. :param detail_id: 微信明细单号,微信支付系统内部区分转账批次单下不同转账明细单的唯一标识,示例值:1040000071100999991182020050700019500100
  106. """
  107. if batch_id and detail_id:
  108. path = '/v3/transfer/batches/batch-id/%s/details/detail-id/%s' % (batch_id, detail_id)
  109. else:
  110. raise Exception('batch_id or detail_id is not assigned')
  111. return self.client.core.request(path)
  112. def transfer_query_out_batch_no(self, out_batch_no, need_query_detail = False, offset = 0, limit = 20,
  113. detail_status = 'ALL'):
  114. """商家批次单号查询批次单
  115. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  116. :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
  117. :param offset: 请求资源起始位置,默认值为0
  118. :param limit: 最大资源条数,默认值为20
  119. :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
  120. """
  121. if out_batch_no:
  122. path = '/v3/transfer/batches/out-batch-no/%s' % out_batch_no
  123. else:
  124. raise Exception('batch_id is not assigned')
  125. if need_query_detail:
  126. path += '?need_query_detail=true'
  127. else:
  128. path += '?need_query_detail=false'
  129. path += '&offset=%s' % offset
  130. path += '&limit=%s' % limit
  131. path += '&detail_status=%s' % detail_status
  132. return self.client.core.request(path)
  133. def transfer_query_out_detail_no(self, out_detail_no, out_batch_no):
  134. """商家明细单号查询明细单
  135. :param out_detail_no: 商家明细单号,示例值:x23zy545Bd5436
  136. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  137. """
  138. if out_detail_no and out_batch_no:
  139. path = '/v3/transfer/batches/out-batch-no/%s/details/out-detail-no/%s' % (out_batch_no, out_detail_no)
  140. else:
  141. raise Exception('out_detail_no or out_batch_no is not assigned')
  142. return self.client.core.request(path)
  143. def transfer_bill_receipt(self, out_batch_no):
  144. """转账电子回单申请受理
  145. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  146. """
  147. params = {}
  148. if out_batch_no:
  149. params.update({'out_batch_no': out_batch_no})
  150. else:
  151. raise Exception('out_batch_no is assigned')
  152. path = '/v3/transfer/bill-receipt'
  153. return self.client.core.request(path, method = RequestType.POST, params = params)
  154. def transfer_query_bill_receipt(self, out_batch_no):
  155. """查询转账电子回单
  156. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  157. """
  158. if out_batch_no:
  159. path = '/v3/transfer/bill-receipt/%s' % out_batch_no
  160. else:
  161. raise Exception('out_batch_no is not assigned')
  162. return self.client.core.request(path)
  163. def transfer_detail_receipt(self, accept_type, out_detail_no, out_batch_no = None, ):
  164. """转账明细电子回单受理
  165. :param accept_type: 受理类型
  166. :param out_detail_no: 商家明细单号,示例值:x23zy545Bd5436
  167. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  168. """
  169. params = {}
  170. if accept_type:
  171. params.update({'accept_type': accept_type})
  172. else:
  173. raise Exception('accept_type is not assigned')
  174. if out_detail_no:
  175. params.update({'out_detail_no': out_detail_no})
  176. else:
  177. raise Exception('out_detail_no is not assigned')
  178. if out_batch_no:
  179. params.update({'out_batch_no': out_batch_no})
  180. path = '/v3/transfer-detail/electronic-receipts'
  181. return self.client.core.request(path, method = RequestType.POST, params = params)
  182. def transfer_query_receipt(self, accept_type, out_detail_no, out_batch_no = None):
  183. """查询转账明细电子回单受理结果
  184. :param accept_type: 受理类型
  185. :param out_detail_no: 商家明细单号,示例值:x23zy545Bd5436
  186. :param out_batch_no: 商家批次单号,示例值:plfk2020042013
  187. """
  188. if accept_type:
  189. path = '/v3/transfer-detail/electronic-receipts?accept_type=%s' % accept_type
  190. else:
  191. raise Exception('accept_type is not assigned')
  192. if out_detail_no:
  193. path += '&out_batch_no=%s' % out_detail_no
  194. else:
  195. raise Exception('out_detail_no is not assigned')
  196. if out_batch_no:
  197. path += '&out_batch_no=%s' % out_batch_no
  198. return self.client.core_request(path)