Example #1
0
def alipay_callback(request):
    # 支付宝支付回调,先检查签名是否正确,再检查是否来自支付宝的请求。
    # 有效的回调,将更新用户的资产。
    keys = request.REQUEST.keys()
    data = {}
    for key in keys:
        data[key] = request.REQUEST[key]
    notify_id = data['notify_id']
    sign_type = data['sign_type']
    sign = data['sign']
    order_id = data['out_trade_no']

    log.info(u'alipay callback, order_id: %s , data: %s' % (order_id, data))

    nid = cache.get('ali_nid_' + hashlib.sha1(notify_id).hexdigest())
    if nid:
        log.info('duplicated notify, drop it')
        return HttpResponse('error')

    if verify_notify_id(notify_id) \
            and verify_alipay_signature(sign_type, sign, data) \
            and Charge.recharge(data, provider='alipay'):
        cache.set('ali_nid_' + hashlib.sha1(notify_id).hexdigest(),
                  order_id, 90000)  # notify_id 保存25小时。
        log.info('ali callback success')
        return HttpResponse('success')
    log.info('not a valid callback, ignore')
    return HttpResponse('error')
Example #2
0
def wechat_pay_notify(request):
    if request.method != 'POST':
        logging.error('equest.method != "POST"')
        return HttpResponse('fail')
    if not _wechatpay_verify_notify(request.GET):
        return HttpResponse('fail')

    log.debug(u'type trade_state = %s' % type(request.GET['trade_state']))

    notify_id = request.GET['notify_id']
    order_id = request.GET['out_trade_no']
    log.info(u'request.GET = %s' % request.GET)
    log.info(u'wechatpay callback, order_id: %s' % order_id)
    log.info(u'request.body = %s' % request.body)

    nid = cache.get('wechatpay_nid_' + hashlib.sha1(notify_id).hexdigest())
    if nid:
        log.info(u'duplicated notify, drop it')
        return HttpResponse('fail')
    body_dict = _wechatpay_xml_to_dict(request.body)
    data = {}
    for key, item in request.GET.items():
        data[key] = item
    for key, item in body_dict.iteritems():
        data[key] = item
    data['trade_state'] = str(data['trade_state'])
    data['total_fee'] = data['total_fee']
    log.debug(u'Charge.recharge data = %s' % data)
    if Charge.recharge(data, provider='wechatpay'):
        cache.set('wechatpay_nid_' + hashlib.sha1(notify_id).hexdigest(),
                  order_id, 90000)  # notify_id 保存25小时。
        log.info(u'wechatpay callback success')
        return HttpResponse('success')

    log.info(u'not a valid callback, ignore')
    return HttpResponse('fail')