Flask-WXPay

一个微信支付的Flask插件, 微信支付API

Installation

Install the module with one of the following commands:

pip install Flask-WXPay

Usage

initialized:

from flask_wxpay import WXPay
wxpay = WXPay()
wxpay.init_app(app)

创建订单,生成prepay data:

from flask_wxpay import now_str
from core import wxpay

out_trade_no = now_str()
order_data = wxpay.unified_order(out_trade_no, price, ip, body, openid=openid)
prepay_data = wxpay.get_jsapi_prepay_data(order_data['prepay_id'])

配置项

WXPAY_BASE_URL 默认值: https://api.mch.weixin.qq.com
WXPAY_REQUEST_TIMEOUT 默认值: 10
WX_APPID 公众账号ID
WXPAY_MCHID 商户号
WXPAY_KEY 商户支付密钥Key
WXPAY_NOTIFY_URL 默认异步通知url
WXPAY_ROOTCA_PATH rootca证书路径,对应requests的verify参数,默认为None
WXPAY_APICLIENT_CERT_PATH 客户端证书路径,默认值None
WXPAY_APICLIENT_KEY_PATH 客户端证书key的路径,默认值None
WXPAY_SANDBOX 是否使用沙箱环境,默认为 False

API

class flask_wxpay.WXPay(app=None)

微信支付类

check_data(data, check_sign=True)

检查请求结果或者支付通知数据的正确性 如果结果不合法会抛出一个WXPayError的子类

Params data:

xml转化成的dict数据

Returns:

no return

Raises:

例如处理支付回调用法:

data = xml_to_dict(xml)
try:
    wxpay.check_data(data)
except SignError:
    return wxpay.notify_response('FAIL', '签名错误')
except WXPayError:
    logger.error('微信支付数据错误', exc_info=True)
    return wxpay.notify_response('FAIL', '数据错误')

do something handle trade
...

return wxpay.notify_response()
check_sign(data)

检查微信支付回调签名是否正确

close_order(out_trade_no)

关闭订单

download_bill(bill_date, bill_type='ALL')

现在对账单

Params bill_date:
 下载对账单的日期,格式:20140603
Params bill_type:
 ALL, SUCCESS, REFUND, RECHARGE_REFUND
Returns:response.Response对象
get_app_prepay_data(prepay_id)

返回给客户端的prepay数据

Params prepay_id:
 unified_order() 接口获取到的prepay_id
Returns:prepay data
Return type:dict
get_jsapi_prepay_data(prepay_id)

返回给公众号的prepay数据

Params prepay_id:
 unified_order() 接口获取到的prepay_id
Returns:prepay data
Return type:dict
get_redpack_info(mch_billno)

查询红包信息 :param mch_billno: 商户订单号

get_sandbox_signkey()

获取验签秘钥,沙箱环境下有效

get_sign(data)

生成签名

get_transfers_info(partner_trade_no)

企业向微信用户个人付款操作进行结果查询

Params partner_trade_no:
 商户订单号
Return type:dict
static notify_response(return_code='SUCCESS', return_msg='OK')

通知结果的返回

query_order(out_trade_no=None, transaction_id=None)

查询订单

query_refund(out_trade_no)

查询退款

refund(out_trade_no, out_refund_no, total_fee, refund_fee)

退款

send_redpack(mch_billno, send_name, re_openid, total_amount, wishing, client_ip, act_name, remark)

发红包

Params mch_billno:
 商户订单号
Params send_name:
 商户名称
Params re_openid:
 用户openid
Params total_amount:
 付款金额
Params wishing:红包祝福语
Params client_ip:
 调用接口的机器IP地址
Params act_name:
 活动名称
Params remark:备注信息
transfers(partner_trade_no, openid, amount, desc, ip, check_name='NO_CHECK', re_user_name=None)

企业向微信用户个人付款到零钱

Params partner_trade_no:
 商户订单号
Params openid:用户openid
Params desc:企业付款备注
Params amount:企业付款金额,单位为分
Params ip:该IP同在商户平台设置的IP白名单中的IP没有关联,该IP可传用户端或者服务端的IP
Params check_name:
 校验用户姓名选项,NO_CHECK:不校验真实姓名 FORCE_CHECK:强校验真实姓名
Params re_user_name:
 收款用户真实姓名,如果check_name设置为FORCE_CHECK,则必填用户真实姓名
Return type:dict
unified_order(out_trade_no, total_fee, ip, body, expire_seconds, notify_url=None, trade_type='JSAPI', openid=None)

统一下单

Params out_trade_no:
 商户订单号
Params total_fee:
 总金额,单位为分
Params ip:用户端实际ip
Params body:商品描述
Params expire_seconds:
 订单失效时间,最短失效时间间隔必须大于5分钟
Params notify_url:
 微信支付异步通知回调地址, 默认使用WXPAY_NOTIFY_URL的配置
Params trade_type:
 JSAPI,NATIVE,APP, 默认值为JSAPI
Params openid:用户openid, trade_type为JSAPI时需要
Return type:dict
flask_wxpay.xml_to_dict(xml)
flask_wxpay.dict_to_xml(*args, **kwargs)

参数形式参照flask.jsonify

flask_wxpay.gen_random_str(size=16)

生成随机字符串

flask_wxpay.md5(origin)

返回小写的32位16进制的md5值

flask_wxpay.now_str(format='%Y%m%d%H%M%S%f')

当前时间的字符串形式, 例如:20170310165231887517

Exception Classes

The following error classes exist in Flask-WXPay:

exception flask_wxpay.exceptions.WXPayError

A WXPay error occurred.

exception flask_wxpay.exceptions.CertError

双向证书的接口, 使用证书的接口如果未配置证书时抛出

exception flask_wxpay.exceptions.ReturnCodeFail(return_msg)

return_code为FAIL时

exception flask_wxpay.exceptions.ResultCodeFail(err_code, err_code_des)

result_code为FAIL时 属性: err_code, err_code_des

exception flask_wxpay.exceptions.SignError

签名错误