Exemplo n.º 1
0
    def alipay_query(cls, out_trade_no, trade_no, receipt, trade_type):
        '''alipay query'''
        try:
            config_dict = {
                VIPOrder.PAY_TYPE_BY_ALIPAY_APP: settings.ALIPAY_APP_INFO,
                VIPOrder.PAY_TYPE_BY_ALIPAY: settings.ALIPAY_APP_INFO,
            }
            config = config_dict[trade_type]
            alipay_client_config = AlipayClientConfig()
            alipay_client_config.app_id = smart_str(config['basic_info']['APP_ID'])
            alipay_client_config.sign_type = smart_str(config['other_info']['SIGN_TYPE'])
            with open(config['basic_info']['APP_PRIVATE_KEY'], 'r') as fp:
                alipay_client_config.app_private_key = fp.read()
            client = DefaultAlipayClient(alipay_client_config=alipay_client_config)

            model = AlipayTradeQueryModel()
            model.out_trade_no = smart_str(out_trade_no)
            if trade_no:
                model.trade_no = smart_str(trade_no)
            request = AlipayTradeQueryRequest(biz_model=model)

            request_params = client.sdk_execute(request)
            resp = requests.get('{}?{}'.format(SERVER_URL, request_params)).json()
            log.info('********** alipay query result ***********')
            log.info(resp)
            query_resp = resp['alipay_trade_query_response']
            return {
                'trade_status': query_resp.get('trade_status') == 'TRADE_SUCCESS',
                'total_fee': float(query_resp.get('total_amount', 0)),
                'refno': query_resp.get('trade_no', ''),
            }
        except Exception, e:
            log.exception(e)
Exemplo n.º 2
0
    def post(self, request, *args, **kwargs):
        try:
            package_id = request.POST.get('package_id')
            order = VIPOrder.create_order(request.user, int(package_id))
            if order:
                log.info('****** order id: {} ******'.format(order.id))
                order.pay_type = VIPOrder.PAY_TYPE_BY_ALIPAY_APP
                out_trade_no = create_trade_id(order.id)
                order.outtradeno = out_trade_no
                order.save()
                result = {'order_id': order.id}

                alipay_client_config = AlipayClientConfig()
                alipay_client_config.app_id = smart_str(settings.ALIPAY_APP_INFO['basic_info']['APP_ID'])
                alipay_client_config.sign_type = smart_str(settings.ALIPAY_APP_INFO['other_info']['SIGN_TYPE'])
                with open(settings.ALIPAY_APP_INFO['basic_info']['APP_PRIVATE_KEY'], 'r') as fp:
                    alipay_client_config.app_private_key = fp.read()

                client = DefaultAlipayClient(alipay_client_config=alipay_client_config)

                model = AlipayTradeAppPayModel()
                model.total_amount = smart_str(str_to_specify_digits(str(order.price)))
                model.product_code = smart_str("QUICK_MSECURITY_PAY")
                model.body = smart_str(order.name)
                model.subject = smart_str(order.name)
                model.out_trade_no = smart_str(out_trade_no)
                model.passback_params = smart_str(settings.LMS_ROOT_URL + reverse("vip_purchase"))

                request = AlipayTradeAppPayRequest(biz_model=model)
                request.notify_url = smart_str(settings.ALIPAY_APP_INFO['other_info']['NOTIFY_URL'])
                result['alipay_request'] = client.sdk_execute(request)

                return Response(data=result)
        except Exception, e:
            log.exception(e)
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        try:
            package_id = request.GET.get('package_id')
            order = VIPOrder.create_order(request.user, int(package_id))
            if order:
                order.pay_type = VIPOrder.PAY_TYPE_BY_ALIPAY_APP
                out_trade_no = create_trade_id(order.id)
                order.outtradeno = out_trade_no
                order.save()
                result = {'order_id': order.id}

                alipay_client_config = AlipayClientConfig()
                alipay_client_config.app_id = smart_str(
                    settings.ALIPAY_APP_INFO['basic_info']['APP_ID'])
                alipay_client_config.sign_type = smart_str(
                    settings.ALIPAY_APP_INFO['other_info']['SIGN_TYPE'])
                with open(
                        settings.ALIPAY_APP_INFO['basic_info']
                    ['APP_PRIVATE_KEY'], 'r') as fp:
                    alipay_client_config.app_private_key = fp.read()

                client = DefaultAlipayClient(
                    alipay_client_config=alipay_client_config)

                model = AlipayTradeWapPayModel()
                model.total_amount = smart_str(
                    str_to_specify_digits(str(order.price)))
                model.product_code = smart_str("QUICK_WAP_WAY")
                model.subject = smart_str(_(order.name))
                model.out_trade_no = smart_str(out_trade_no)
                # 返回页面时不使用缓存
                quit_url = settings.LMS_ROOT_URL + reverse(
                    "vip_alipay_h5_result")
                model.quit_url = smart_str(quit_url)
                model.passback_params = smart_str(settings.LMS_ROOT_URL +
                                                  reverse("vip_purchase"))

                request = AlipayTradeWapPayRequest(biz_model=model)
                request.notify_url = smart_str(
                    settings.ALIPAY_APP_INFO['other_info']['NOTIFY_URL'])
                result['alipay_request'] = client.sdk_execute(request)
                log.error(SERVER_URL + "?" + result['alipay_request'])
                data = {
                    'alipay_url': SERVER_URL + "?" + result['alipay_request'],
                    'order_id': order.id
                }
                return Response(xresult(data=data))
        except Exception, e:
            log.exception(e)
Exemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        """
        alipay asyncnotify api view
        """
        try:
            params = {
                smart_str(k): smart_str(v)
                for k, v in request.data.iteritems()
            }
            sign = params.pop('sign')
            params.pop('sign_type')
            with open(
                    settings.ALIPAY_APP_INFO['basic_info']
                ['ALIPAY_RSA_PUBLIC_KEY'], 'r') as fp:
                public_key = fp.read()

            if verify_with_rsa(public_key, get_sign_content(params), sign):
                passback_params = request.data.get("passback_params")
                out_trade_no = request.data.get("out_trade_no", "")

                post_data = {
                    "trade_type": "alipay",
                    "out_trade_no": out_trade_no,
                    "trade_no": request.data.get("trade_no"),
                    "total_fee": request.data.get("total_amount"),
                    "buyer_email": request.data.get("buyer_email"),
                    "extra_common_param": request.data.get("passback_params"),
                }

                if out_trade_no != "":
                    rep = requests.post(passback_params, data=post_data)
                    rep_data = rep.json()
                    if rep_data.get('result') == "success":
                        return HttpResponse('success')
        except Exception, e:
            log.exception(e)