可乐博客

Python对支付宝微信支付回调

今儿朋友问怎么处理微信支付的回调,简单写下代码


from fleader import fleader as rq
from public.v1.business.utils.conn_mysql import conn_mysql
from public.v1.business.user.loginx import updata_order
import conf


def apipay_notify(request):
    out_trade_no = request.form.get('out_trade_no',default='')#商户订单号
    trade_no     = request.form.get('trade_no',default='')#支付宝交易号
    trade_status = request.form.get('trade_status',default='')#交易状态
    total_amount = request.form.get('total_amount',default='')#金额

    if trade_status=='TRADE_FINISHED':
        pass
    if trade_status=='TRADE_SUCCESS':
        updata_order(out_trade_no,trade_no,total_amount,1)
    return 'success'

def vxpay_notify(request):
    arr=vx_xml2dict(request.data)
    out_trade_no=rq.var(arr,'out_trade_no')#商户订单号
    trade_no=rq.var(arr,'transaction_id')#交易号
    trade_status = rq.var(arr,'result_code')#交易状态
    total_amount = rq.var(arr,'cash_fee','0')#金额
    if trade_status=='SUCCESS':
        updata_order(out_trade_no,trade_no,total_amount,2)
    return '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>'



def vx_xml2dict(xml):
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(xml, "xml")
    xml = soup.find('xml')
    if not xml:
        return {}
    data = dict([(item.name, item.text) for item in xml.find_all()])
    return data

def vx_dict2xml(data):
    xml = []
    for k in sorted(data.keys()):
        v = data.get(k)
        if k == 'detail' and not v.startswith('<![CDATA['):
            v = '<![CDATA[{}]]>'.format(v)
        xml.append('<{key}>{value}</{key}>'.format(key=k, value=v))
    return '<xml>{}</xml>'.format(''.join(xml))
随笔