123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- # -*- coding: utf-8 -*-
- from .type import RequestType
- from .core import Core
- class Transfer(object):
- def __init__(self, wx):
- self._core = Core(wx.agent_num, wx.cert_serial_no, wx.apiv3_key)
- def transfer_batch(self, appid, out_batch_no, batch_name, batch_remark, total_amount, total_num, transfer_detail_list=[]):
- '''
- 商户可以通过该接口同时向多个用户微信零钱进行转账操作
- :param appid string[1,32] 直连商户的appid: 申请商户号的appid或商户号绑定的appid(企业号corpid即为此appid)示例值:wxf636efh567hg4356
- :param out_batch_no string[1,32] 商家批次单号: 商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一,示例值:'plfk2020042013'
- :param batch_name string[1,32] 批次名称: 该笔批量转账的名称 示例值:2019年1月深圳分部报销单
- :param batch_remark string[1,32] 批次备注: 转账说明,UTF8编码,最多允许32个字符 示例值:2019年1月深圳分部报销单
- :param total_amount int 转账总金额: 转账金额单位为“分”。转账总金额必须与批次内所有明细转账金额之和保持一致,否则无法发起转账操作
- :param total_num int 转账总笔数: 一个转账批次单最多发起三千笔转账。转账总笔数必须与批次内所有明细之和保持一致,否则无法发起转账操作
- :param transfer_detail_list array 转账明细列表: 发起批量转账的明细列表,最多三千笔
- out_detail_no string[1,32] 商家明细单号 商户系统内部区分转账批次单下不同转账明细单的唯一标识,要求此参数只能由数字、大小写字母组成 示例值:x23zy545Bd5436
- transfer_amount int 转账金额 转账金额单位为分
- transfer_remark string[1,32] 转账备注 单条转账备注(微信用户会收到该备注),UTF8编码,最多允许32个字符 示例值:2020年4月报销
- openid string[1,128] 用户在直连商户应用下的用户标示
- 注意:
- 商户上送敏感信息时使用微信支付平台公钥加密,证书序列号包含在请求HTTP头部的Wechatpay-Serial
- 批量转账一旦发起后,不允许撤销,批次受理成功后开始执行转账
- 转账批次单中涉及金额的字段单位为 分
- 当返回错误码为 "SYSTEM_ERROR" 时,请不要更换商家批次单号, 一定要使用原商家批次单号重试,否则可能造成重复转账等资金风险。
- 不同的商家批次单号 out_batch_no 请求为一个全新的批次, 在未查询到明确的转账批次但处理结果之前,请勿修改商家批次单号 重新提交!
- 请商户在自身的系统中合理设置转账频次并做好并发控制,防范错付风险
- https://api.mch.weixin.qq.com/v3/transfer/batches
- POST
- '''
- if not appid:
- raise Exception(u'缺少参数appid')
- if not out_batch_no:
- raise Exception(u'缺少参数out_batch_no')
- if not out_batch_no:
- raise Exception(u'缺少参数batch_name')
- if not out_batch_no:
- raise Exception(u'缺少参数batch_remark')
- if not out_batch_no:
- raise Exception(u'缺少参数total_amount')
- if not out_batch_no:
- raise Exception(u'缺少参数total_num')
- if not transfer_detail_list:
- raise Exception(u'缺少参数transfer_detail_list')
- params = {
- 'appid': appid,
- 'out_batch_no': out_batch_no,
- 'batch_name': batch_name,
- 'batch_remark': batch_remark,
- 'total_amount': total_amount,
- 'total_num': total_num,
- 'transfer_detail_list': transfer_detail_list
- }
- path = '/v3/transfer/batches'
- return self._core.request(path, method=RequestType.POST, data=params, cipher_data=False)
- def transfer_query_batchid(self, batch_id, need_query_detail=False, offset=0, limit=20, detail_status='ALL'):
- """微信批次单号查询批次单
- :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
- :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
- :param offset: 请求资源起始位置,默认值为0
- :param limit: 最大资源条数,默认值为20
- :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
- """
- if batch_id:
- path = '/v3/transfer/batches/batch-id/%s' % batch_id
- else:
- raise Exception('参数错误!')
- if need_query_detail:
- path += '?need_query_detail=true'
- else:
- path += '?need_query_detail=false'
- path += '&offset=%s' % offset
- path += '&limit=%s' % limit
- path += '&detail_status=%s' % detail_status
- return self._core.request(path)
- def transfer_query_detail_id(self, batch_id, detail_id):
- """微信明细单号查询明细单
- :param batch_id: 微信批次单号,微信商家转账系统返回的唯一标识,示例值:1030000071100999991182020050700019480001
- :param detail_id: 微信明细单号,微信支付系统内部区分转账批次单下不同转账明细单的唯一标识,示例值:1040000071100999991182020050700019500100
- """
- if batch_id and detail_id:
- path = '/v3/transfer/batches/batch-id/%s/details/detail-id/%s' % (batch_id, detail_id)
- else:
- raise Exception('参数错误!')
- return self._core.request(path)
- def transfer_query_out_batch_no(self, out_batch_no, need_query_detail=False, offset=0, limit=20, detail_status='ALL'):
- """商家批次单号查询批次单
- :param out_batch_no: 商家批次单号,示例值:plfk2020042013
- :param need_query_detail: 是否查询转账明细单,枚举值:true:是;false:否,默认否。
- :param offset: 请求资源起始位置,默认值为0
- :param limit: 最大资源条数,默认值为20
- :param detail_status: 明细状态, ALL:全部。需要同时查询转账成功和转账失败的明细单;SUCCESS:转账成功。只查询转账成功的明细单;FAIL:转账失败。
- """
- if out_batch_no:
- path = '/v3/transfer/batches/out-batch-no/%s' % out_batch_no
- else:
- raise Exception('参数错误!')
- if need_query_detail:
- path += '?need_query_detail=true'
- else:
- path += '?need_query_detail=false'
- path += '&offset=%s' % offset
- path += '&limit=%s' % limit
- path += '&detail_status=%s' % detail_status
- return self._core.request(path)
- def transfer_query_out_detail_no(self, out_detail_no, out_batch_no):
- """商家明细单号查询明细单
- :param out_detail_no: 商家明细单号,示例值:x23zy545Bd5436
- :param out_batch_no: 商家批次单号,示例值:plfk2020042013
- """
- if out_detail_no and out_batch_no:
- path = '/v3/transfer/batches/out-batch-no/{}/details/out-detail-no/{}'.format(out_batch_no, out_detail_no)
- else:
- raise Exception('参数错误!')
- return self._core.request(path)
|