Пример #1
0
def prepay_notify_wxpay_api(request):
    wx_config = WeChatConfig(wx_channel_config)
    wx_notify = WeChatNotify(wx_config)

    xml = request.body.decode('utf-8')
    notice = wx_notify.notify_process(xml)

    print(notice)

    if notice.get('result_code') == 'SUCCESS':
        try:
            # update order
            order = PrePayOrder.objects.get(
                out_trade_no=notice.get('out_trade_no'))
            paid = order.paid
            order.paid = True
            order.updated_time = datetime.now().replace(tzinfo=pytz.utc)
            order.save()

            # update wechatpay record
            wx_order = PrePayNotifyWeChatPay.objects.create(prepay_order=order)
            wx_order.app_id = notice.get('appid')
            wx_order.mch_id = notice.get('mch_id')
            wx_order.open_id = notice.get('openid')
            wx_order.transaction_id = notice.get('transaction_id')
            wx_order.total_fee = notice.get('total_fee')
            wx_order.trade_type = notice.get('trade_type')
            wx_order.fee_type = notice.get('fee_type')
            wx_order.bank_type = notice.get('bank_type')
            wx_order.cash_fee = notice.get('cash_fee')
            wx_order.is_subscribe = notice.get('is_subscribe')
            wx_order.time_end = notice.get('time_end')
            wx_order.save()

            # update balance
            if not paid:
                user_profile = UserProfile.objects.get(user=order.user)
                balance_pre = user_profile.account_balance
                balance_updated = balance_pre + order.amount
                user_profile.account_balance = balance_updated
                user_profile.save()
        except PrePayOrder.DoesNotExist:
            print('There is NO prepay order[%s] in table PrePayOrder.' %
                  notice.get('out_trade_no'))
        except PrePayNotifyWeChatPay.DoesNotExist:
            print(
                'There is NO WeChatPay order[%s] in table PrePayNotifyWeChatPay.'
                % notice.get('out_trade_no'))
        except UserProfile.DoesNotExist:
            print(
                'The owner of prepay order[%s] NOT found in table UserProfile.'
                % notice.get('out_trade_no'))

    params = {'return_code': 'SUCCESS'}
    response_xml = wx_notify.dict2xml(params, with_sign=False)
    print(response_xml)

    return HttpResponse(response_xml, content_type="application/xml")
Пример #2
0
def prepay_close_order_wxpay_api(request):
    trade_no = request.GET.get('trade_no')

    if not trade_no:
        error_detail = {'detail': 'Please provide the prepay trade number.'}
        return Response(error_detail, status=status.HTTP_400_BAD_REQUEST)

    wx_config = WeChatConfig(wx_channel_config)
    wx_close = WeChatCloseOrder(wx_config)
    close = wx_close.post(str(trade_no))
    print(close)

    return Response(close)
Пример #3
0
def prepay_order_query_wxpay_api(request):
    trade_no = request.GET.get('trade_no')

    if not trade_no:
        error_detail = {'detail': 'Please provide the prepay trade number.'}
        return Response(error_detail, status=status.HTTP_400_BAD_REQUEST)

    wx_config = WeChatConfig(wx_channel_config)
    wx_query = WeChatOrderQuery(wx_config)
    query = wx_query.post(str(trade_no))
    print(query)

    return Response(query)
Пример #4
0
def prepay_get_order_wxpay_api(request):
    amount = request.GET.get('amount')

    if not amount:
        error_detail = {'detail': 'Please provide the prepay amount.'}
        return Response(error_detail, status=status.HTTP_400_BAD_REQUEST)

    wx_config = WeChatConfig(wx_channel_config)
    wx_pay = WeChatPay(wx_config)
    params = {}
    #params['body'] = 'prepay'#'账户充值'
    params['body'] = '哒哒停车-账户充值'
    trade_no = get_trade_no(length=6)
    params['out_trade_no'] = trade_no
    params['total_fee'] = str(amount)  #'1'
    params['spbill_create_ip'] = get_client_ip(request)
    params['notify_url'] = wx_config.notify_url
    params['trade_type'] = 'APP'

    wx_pay.set_params(params=params)
    print(wx_pay.params)
    response = wx_pay.post_xml_ssl()

    print(response)

    if response.get('return_code') != 'SUCCESS':
        error_detail = {'detail': response.get('return_msg')}
        return Response(error_detail, status=status.HTTP_400_BAD_REQUEST)

    if response.get('result_code') != 'SUCCESS':
        error_detail = {'detail': response.get('err_code')}
        return Response(error_detail, status=status.HTTP_400_BAD_REQUEST)

    wx_order = OrderedDict()
    wx_order['appid'] = response.get('appid')
    wx_order['partnerid'] = response.get('mch_id')
    wx_order['prepayid'] = response.get('prepay_id')
    wx_order['package'] = 'Sign=WXPay'
    wx_order['noncestr'] = random_str(length=32)
    now = int(time.time())
    now_str = str(now)
    wx_order['timestamp'] = now_str  #response.get()
    sign = get_sign(wx_order, wx_config.api_key)
    wx_order['sign'] = sign

    #user = User.objects.get(id=2)
    user = User.objects.get(username=request.user)
    # insert order record
    order = PrePayOrder.objects.create(user=user, amount=amount)
    order.out_trade_no = trade_no
    order.payment_channel = 'wxpay'
    order.save()

    wxpay_record = PrePayOrderWeChatPay.objects.create(
        prepay_order=order,
        app_id=wx_pay.app_id,
        mch_id=wx_pay.mch_id,
        body=params['body'],
        total_fee=params['total_fee'],
        spbill_create_ip=params['spbill_create_ip'],
        notify_url=params['notify_url'],
        trade_type=response.get('trade_type'),
        response_app_id=wx_order['appid'],
        response_mch_id=wx_order['partnerid'],
        response_trade_type=response.get('trade_type'),
        prepay_id=wx_order['prepayid'])
    wxpay_record.save()

    return Response(wx_order)