Exemple #1
0
    def query_order_wangbipay(self, *args, **kwargs):

        info = dict()
        info['title'] = '支付信息查询'
        order_id = kwargs.get('order_id')  # 获取订单号
        if not order_id:
            info['error'] = '订单号有误'
            return info

        source = kwargs.get('source', 'default')
        payment_order = get_payment_order(request.cr, order_id, 'wangbipay',
                                          source)  # 获取订单信息

        if payment_order:
            data = dict()
            data['code'] = payment_order['wangbipay_code']  # 获得*币code
            wangbi_pay_result = self.hotkid_query(data)
            if not wangbi_pay_result.get('object'):
                info['error'] = '交易不存在'
                return info
            if wangbi_pay_result['object']['status'] == 'SUCCESS':
                payment_type = 'wangbipay'
                update_payment_order(request.cr, 'paid',
                                     payment_order['payment_id'], payment_type,
                                     'done')  # 更新网关订单信息
                info['msg'] = 'SUCCESS'
            elif wangbi_pay_result['object']['status'] == 'REQUEST':
                info['msg'] = 'WAIT_PAY'
            else:
                info['error'] = '*币支付有误'
            return info
        else:
            info['error'] = '未找到该订单'
            return info
Exemple #2
0
    def pay_qrcode_wangbipay(self, *args, **kwargs):
        # *币支付请求链接
        ihyf_payment_id = kwargs.get('ihyf_payment_id')

        payment_user_info = get_payment_user_info(request.cr, ihyf_payment_id)

        payment_order = kwargs.get('payment_order')
        order_id = payment_order['order_id']
        source = kwargs.get('source', 'default')

        if get_payment_order(request.cr, order_id, 'wangbipay', source):
            return {
                'title': 'failure',
                'error': 'Payment Order is alreay exist'
            }

        payment_id = get_payment_id(order_id, 'wangbipay', source)

        create_payment_order(request.cr, payment_order,
                             payment_user_info['ihyf_payment_id'],
                             payment_user_info['ihyf_secret_key'], 'wangbipay',
                             source, payment_id)  # 在网关创建未支付订单

        info = dict()
        info['title'] = '*币支付url'
        data = dict()
        data['amount'] = payment_order['total_amount']  # 金额
        data['receiverKey'] = '1'
        data['receiver'] = '10000000000'
        data['orderCode'] = payment_order['order_id']  # 订单号
        data['channel'] = 'WEB'
        hotkid_respose = self.hotkid_order(data)  # 俱乐部返回给我们的信息

        if hotkid_respose.get('error', False):
            return {'title': 'failure', 'error': hotkid_respose.get('error')}

        if hotkid_respose:
            url = """https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx160b97fc7a08acc3&redirect_uri=http://www.hotkidclub.com/vmc/payment.html?pcode=%s&response_type=code&scope=snsapi_base#wechat_redirect""" % \
                  (hotkid_respose['object']['code'])
            update_wangbipay_code(request.cr, hotkid_respose['object']['code'],
                                  order_id)
            # 把*币code 更新至订单中
            info['code_url'] = str(url)
            info['result'] = 'SUCCESS'
        else:
            info['error'] = '获取失败'
        return info
    def query_order_alipay(self, *args, **kwargs):
        """
        **kwargs: 安卓传入的订单的状态
        return: 返回订单状态
        """
        info = dict()
        info['title'] = '支付信息查询'
        transact_num = kwargs.get('order_id')  # 获取订单号

        if transact_num:
            source = kwargs.get('source', 'default')
            payment_order = get_payment_order(
                request.cr, transact_num, 'alipay', source)
            if payment_order:
                if payment_order['payment_status'] == 'paid':
                    info['msg'] = 'SUCCESS'
                    return info
                else:
                    payment_id = payment_order['payment_id']
                    ihyf_payment_id = \
                        payment_order['ihyf_payment_id']
                    payment_user_info = get_payment_user_info(
                        request.cr, ihyf_payment_id)

                    method = "alipay.trade.query"  # 支付宝统一收单交易查询接口
                    # 回调url(现为hyf外网地址)
                    notify_url = get_notify_url(request.cr)
                    if not notify_url:
                        _logger.warn("notify_url 不存在")
                    else:
                        notify_url = notify_url + '/alipay_callback'
                    private_key = payment_user_info[
                        'alipay_private_rsa']  # 获取该订单私钥
                    app_id = payment_user_info['alipay_app_id']  # app_id
                    info = alipay_trade_query_call(
                        method, notify_url, private_key, app_id, payment_id)
                    info['title'] = '支付信息查询'
                    return info
            else:
                info['error'] = '未找到该订单'
                return info
        else:
            info['error'] = '订单号有误'
    def pay_refund_alipay(self, *args, **kwargs):
        refund_info = kwargs.get("refund_info", False)
        order_id = refund_info.get("order_id", False)
        refund_amount = refund_info.get("refund_amount", False)
        source = kwargs.get('source', 'default')

        if order_id and refund_amount:
            payment_order = get_payment_order(
                request.cr, order_id, 'alipay', source)
            if not payment_order:
                return {
                    'title': 'failure',
                    'error': 'Payment Order is not exist'
                }
            payment_status = payment_order['payment_status']  # 支付状态
            total_amount = payment_order['total_amount']  # 订单金额
            if payment_status != "paid":
                return {
                    'title': 'failure',
                    'error': 'Payment Order is not paid'
                }
            if refund_amount > total_amount:
                return {
                    'title': 'failure',
                    'error': 'Refund Amount > Order Total Amount'
                }

            payment_id = payment_order['payment_id']
            ihyf_payment_id = kwargs['ihyf_payment_id']
            payment_user_info = get_payment_user_info(
                request.cr, ihyf_payment_id)  # 获取用户配置信息
            app_id = payment_user_info['alipay_app_id']  # 获取支付宝appid
            private_key = payment_user_info['alipay_private_rsa']  # 获取支付宝私钥
            res = alipay_trade_refund(transact_num=payment_id,
                                      refund_amount=refund_amount,
                                      refund_reason=None,
                                      operator_id=None,
                                      store_id=None,
                                      terminal_id=None,
                                      app_id=app_id,
                                      private_key=private_key)
            return res
    def pay_qrcode_alipay(self, *args, **kwargs):
        """
        args:
        kwargs: 传入的订单号,总金额,终端id,产品名称
        return: 二维码url
        """
        ihyf_payment_id = kwargs.get('ihyf_payment_id', False)

        payment_user_info = get_payment_user_info(
            request.cr, ihyf_payment_id)

        payment_order = kwargs.get('payment_order')
        order_id = payment_order['order_id']
        body = payment_order.get('body', u'')
        source = kwargs.get('source', 'default')

        if get_payment_order(request.cr, order_id, 'alipay', source):
            return {
                'title': 'failure',
                'error': 'Payment Order is alreay exist'
            }

        payment_id = get_payment_id(order_id, 'alipay', source)

        create_payment_order(
            request.cr, payment_order,
            payment_user_info['ihyf_payment_id'],
            payment_user_info['ihyf_secret_key'],
            'alipay', source, payment_id
        )  # 在网关创建未支付订单
        # 组装访问上行
        data = {}
        cash = payment_order['total_amount']  # 总金额
        description = body or source
        method = "alipay.trade.precreate"  # 支付宝统一收单交易预创建接口
        # 回调url(现为hyf外网地址)
        notify_url = get_notify_url(request.cr)
        if not notify_url:
            _logger.warn("notify_url 不存在")
        else:
            notify_url += '/alipay_callback'
        alipay_seller_id = payment_user_info['alipay_seller_id']  # 卖家id
        alipay_app_id = payment_user_info['alipay_app_id']  # app_id
        alipay_private_rsa = payment_user_info['alipay_private_rsa']  # 私钥
        alipay_public_key = payment_user_info['alipay_public_key']  # 公钥

        data['payment_id'] = payment_id
        data['cash'] = cash
        data['description'] = description
        data['method'] = method
        data['notify_url'] = notify_url
        data['seller_id'] = alipay_seller_id
        data['app_id'] = alipay_app_id
        data['private_key'] = alipay_private_rsa
        if not order_id and cash:
            return {
                "title": "支付宝订单创建",
                "error": "订单id、金额不能为空"
            }

        return alipay_generate_prepay_native(data)
    def pay_qrcode_weixinpay(self, *args, **kwargs):
        ihyf_payment_id = kwargs.get('ihyf_payment_id')
        payment_user_info = get_payment_user_info(request.cr, ihyf_payment_id)

        payment_order = kwargs.get('payment_order')
        order_id = payment_order['order_id']
        body = payment_order.get('body', u'')
        source = kwargs.get('source', 'default')

        if get_payment_order(request.cr, order_id, 'weixinpay', source):
            return {
                'title': 'failure',
                'error': 'Payment Order is alreay exist'
            }

        payment_id = get_payment_id(order_id, 'weixinpay', source)

        create_payment_order(request.cr, payment_order,
                             payment_user_info['ihyf_payment_id'],
                             payment_user_info['ihyf_secret_key'], 'weixinpay',
                             source, payment_id)  # 在网关创建未支付订单

        product = dict()
        # 附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据,这里暂定售货机ID
        product['attach'] = 1
        product['body'] = body or source
        product['payment_id'] = payment_id
        product['product_id'] = 1
        product['total_fee'] = int(payment_order['total_amount'] * 100)

        app_id = payment_user_info['weixinpay_app_id']  # app_id
        mch_id = payment_user_info['weixinpay_seller_id']  # 卖家id
        key = payment_user_info['weixinpay_private_key']  # 私钥
        ip = get_conf_info('gateway_ip')

        if not ip:
            return {
                "title": "Weixin Pay",
                "error": "Gateway Ip is not configured"
            }
        # 回调url(现为hyf外网地址)
        notify_url = get_notify_url(request.cr)
        if not notify_url:
            _logger.warn("notify_url 不存在")
        else:
            notify_url += '/weixin_callback'
        try:
            weixin_pay = QRWXpay(app_id,
                                 mch_id,
                                 key,
                                 ip,
                                 notify_url=notify_url)
            weixin_result = weixin_pay.unifiedorder(product)
            result = {
                "result": "SUCCESS",
                # "returnMsg": "OK",
                # "appid": app_id,
                # "mchId": mch_id,
                # "deviceInfo": "WEB",
                # "nonceStr": weixin_result['nonce_str'],
                # "sign": weixin_result['sign'],
                # "resultCode": "SUCCESS",
                # "tradeType": "NATIVE",
                # "prepayId": weixin_result['prepay_id'],
                "code_url": weixin_result['code_url']
            }
            return result
        except Exception as e:
            return {"title": "微信订单创建", "error": e.message}
    def query_order_weixinpay(self, *args, **kwargs):
        """
        查询订单支付结果接口
        kwargs: kwargs['outTradeNo']订单ID
        return: {}
        """
        info = dict()
        info['title'] = '支付信息查询'
        order_id = kwargs.get('order_id')
        if not order_id:
            info['error'] = '订单号有误'
            return info

        source = kwargs.get('source', 'default')
        payment_order = get_payment_order(request.cr, order_id, 'weixinpay',
                                          source)
        if not payment_order:
            info['error'] = '未找到该订单'
            return info

        if payment_order and payment_order['payment_status'] == 'paid':
            info['msg'] = 'SUCCESS'
        # elif payment_order and payment_order['payment_status'] == 'not_pay':
        #     info['msg'] = 'NOT_PAY'
        else:
            ihyf_payment_id = payment_order['ihyf_payment_id']
            # 查询微信服务器
            payment_user_info = get_payment_user_info(request.cr,
                                                      ihyf_payment_id)

            app_id = payment_user_info['weixinpay_app_id']  # 获得app_id
            seller_id = payment_user_info[
                'weixinpay_seller_id']  # 卖家id 和 mch_id 是一样的东西
            private_key = payment_user_info['weixinpay_private_key']  # 获取该订单私钥
            ip = get_conf_info('gateway_ip')

            if not ip:
                return {
                    "title": "Weixin Pay",
                    "error": "Gateway Ip is not configured"
                }
            # 回调url(现为hyf外网地址)
            notify_url = get_notify_url(request.cr)
            if not notify_url:
                _logger.warn("notify_url 不存在")
            else:
                notify_url += '/weixin_callback'
            weixin_pay = QRWXpay(app_id, seller_id, private_key, ip,
                                 notify_url)

            weixin_return_result = weixin_pay.verify_order(
                out_trade_no=payment_order['payment_id'])
            logging.info("query weixinpay")
            logging.info(weixin_return_result)
            if weixin_return_result.get('trade_state') == 'SUCCESS':
                update_payment_order(request.cr, 'paid',
                                     payment_order['payment_id'], 'done')
                info['msg'] = 'SUCCESS'
            else:
                info['msg'] = 'NOT_PAY'
        return info