예제 #1
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)
예제 #2
0
    def post(self, request, *args, **kwargs):
        """
        支付成功之后,接受payments app回调,进行业务逻辑的处理
        """
        try:
            log.info('********** purchase ***********')
            log.info(request.POST)

            pay_type = request.POST['trade_type']
            trade_type = self.get_trade_types()[pay_type]
            verify_srv = trade_type['verify']()
            verify_srv.saveData(json.loads(request.POST['original_data'])['data'])

            if verify_srv.checkSign():
                log.info('********** verify success ***********')
                req_data = verify_srv.getData()
                out_trade_no = req_data.get(trade_type['trade_info'][0])
                trade_no = req_data.get(trade_type['trade_info'][1])
                order_id = recovery_order_id(out_trade_no)
                order = VIPOrder.get_user_order(order_id)
                if order:
                    order.purchase(
                        order.created_by,
                        trade_type['type'],
                        refno=trade_no
                    )
                    log.info('********** purchase success ***********')
            return Response({'result': 'success'})
        except Exception, e:
            log.exception(e)
예제 #3
0
    def get(self, request, *args, **kwargs):
        """
        VIP alipay paying
        参数:package_id 套餐ID
        :param request:
        |参数|类型|是否必填|说明|:
        |package_id|int|是|套餐ID|

        ### 示例
        GET /api/v1/vip/pay/alipay/paying/?package_id=1

        :return:
        |参数|类型|说明|
        |alipay_url|string|跳转到支付宝支付页面|
        |order_id|int|订单id|
        """
        package_id = request.GET.get('package_id')
        order = VIPOrder.create_order(request.user, package_id)
        order.pay_type = VIPOrder.PAY_TYPE_BY_ALIPAY
        order.save()
        pay_html = ""
        if order:
            body = order.name
            subject = order.name
            total_fee = order.price
            http_host = request.META.get("HTTP_HOST")

            pay_html = self.get_pay_html(
                body, subject, total_fee, http_host, order.id)

        data = {
            'alipay_url': pay_html,
            'order_id': order.id
        }
        return Response(xresult(data=data))
예제 #4
0
    def post(self, request, *args, **kwargs):
        """
        create order
        """

        package_id = request.POST.get('package_id')
        order = VIPOrder.create_order(request.user, package_id)
        return Response(xresult(data={'order_id': order.id}))
예제 #5
0
    def get(self, request, *args, **kwargs):
        """
        生成订单 调起H5支付
        跳转到微信支付
        :param request:
        |参数|类型|是否必填|说明|:
        |package_id|int|是|套餐ID|

        ### 示例
        GET /api/v1/vip/pay/wechat_h5/paying/?package_id=1

        :return:
        |参数|类型|说明|
        |href_url|string|跳转微信支付页面链接|
        """

        package_id = request.GET.get('package_id')
        order = VIPOrder.create_order(request.user, package_id)

        if order:
            # 获取二维码链接
            wxh5payconf_pub = WxH5PayConf_pub()
            unifiedorderh5_pub = UnifiedOrderH5_pub()
            total_fee = int(order.price * 100)

            attach_data = settings.LMS_ROOT_URL + reverse("vip_purchase")
            unifiedorderh5_pub.setParameter("body", order.name)
            out_trade_no = create_trade_id(order.id)
            order.pay_type = VIPOrder.PAY_TYPE_BY_WECHAT_H5
            order.outtradeno = out_trade_no
            try:
                order.save()
                unifiedorderh5_pub.setParameter("out_trade_no", out_trade_no)
                unifiedorderh5_pub.setParameter("total_fee", str(total_fee))
                unifiedorderh5_pub.setParameter(
                    "notify_url", wxh5payconf_pub.NOTIFY_URL)
                unifiedorderh5_pub.setParameter("trade_type", "MWEB")
                unifiedorderh5_pub.setParameter("attach", attach_data)
                client_ip = unifiedorderh5_pub.get_client_ip(request)
                unifiedorderh5_pub.setParameter("spbill_create_ip", client_ip)

                prepay_id = unifiedorderh5_pub.getPrepayId()
                mweb_url = unifiedorderh5_pub.getMwebUrl()

                # 返回页面时不使用缓存
                random_str = str(random.randint(100000, 999999))
                redirect_url = settings.LMS_ROOT_URL + reverse("membership_card") + "?random=" +random_str
                mweb_url = mweb_url + "&redirect_url=" + quote_plus(redirect_url)
                data = {
                    'mweb_url': mweb_url
                }
                return Response(xresult(data=data))
            except Exception as ex:
                log.error(ex)
                return Response(xresult(msg='fail', code=-1))

        else:
            return Response(xresult(msg='fail', code=-1))
예제 #6
0
    def get(self, request, *args, **kwargs):
        """
        生成订单 生成微信支付二维码
        二维码链接 code_url
        跳转到微信支付二维码页面 href_url
        :param request:
        |参数|类型|是否必填|说明|:
        |package_id|int|是|套餐ID|

        ### 示例
        GET /api/v1/vip/pay/wechat/paying/?package_id=1

        :return:
        |参数|类型|说明|
        |href_url|string|跳转微信支付页面链接|
        """

        package_id = request.GET.get('package_id')
        order = VIPOrder.create_order(request.user, package_id)

        if order:
            # 获取二维码链接
            wxpayconf_pub = WxPayConf_pub()
            unifiedorder_pub = UnifiedOrder_pub()
            total_fee = int(order.price * 100)

            attach_data = settings.LMS_ROOT_URL + reverse("vip_purchase")
            unifiedorder_pub.setParameter("body", order.name)
            out_trade_no = create_trade_id(order.id)
            order.pay_type = VIPOrder.PAY_TYPE_BY_WECHAT
            order.outtradeno = out_trade_no
            order.save()

            unifiedorder_pub.setParameter("out_trade_no", out_trade_no)
            unifiedorder_pub.setParameter("total_fee", str(total_fee))
            unifiedorder_pub.setParameter(
                "notify_url", wxpayconf_pub.NOTIFY_URL)
            unifiedorder_pub.setParameter("trade_type", "NATIVE")
            unifiedorder_pub.setParameter("attach", attach_data)

            code_url = unifiedorder_pub.getCodeUrl()
            para_str = "?code_url={}&order_id={}&price={}".format(
                code_url, order.id, order.price)
            href_url = settings.LMS_ROOT_URL + \
                reverse("vip_pay_wechat_qrcode_paying") + para_str

            data = {
                'href_url': href_url,
                'order_id': order.id,
            }
            return Response(xresult(data=data))
        else:
            return Response(xresult(msg='fail', code=-1))
예제 #7
0
    def post(self, request, *args, **kwargs):
        """
        mobile vip wechat paying
        """
        try:
            package_id = request.POST.get('package_id')
            order = VIPOrder.create_order(request.user, package_id)

            if order:
                # 获取二维码链接
                wxpayconf_pub = AppWxPayConf_pub()
                unifiedorder_pub = AppUnifiedOrder_pub()
                total_fee = int(order.price * 100)

                attach_data = settings.LMS_ROOT_URL + reverse("vip_purchase")
                unifiedorder_pub.setParameter("body", _(order.name))
                out_trade_no = create_trade_id(order.id)
                order.pay_type = VIPOrder.PAY_TYPE_BY_WECHAT_APP
                order.outtradeno = out_trade_no
                order.save()

                unifiedorder_pub.setParameter("out_trade_no", out_trade_no)
                unifiedorder_pub.setParameter("total_fee", str(total_fee))
                unifiedorder_pub.setParameter("notify_url",
                                              wxpayconf_pub.NOTIFY_URL)
                unifiedorder_pub.setParameter("trade_type", "APP")
                unifiedorder_pub.setParameter("attach", attach_data)

                prepay_id = unifiedorder_pub.getPrepayId()
                data = unifiedorder_pub.getUndResult()

                app_order_pub = AppOrder_pub()
                app_order_pub.setParameter('prepayid', prepay_id)
                sign, nonce_str, timestamp, wx_package = app_order_pub.get_request_params(
                )

                result = {
                    'order_id': order.id,
                    'wechat_request': {
                        'prepay_id': prepay_id,
                        'sign': sign,
                        'appid': data['appid'],
                        'mch_id': data['mch_id'],
                        'nonce_str': nonce_str,
                        'package': wx_package,
                        'timestamp': timestamp,
                    }
                }
                return Response(result)
        except Exception, e:
            log.exception(e)
예제 #8
0
 def post(self, request, *args, **kwargs):
     '''
     apple in-app purchasing
     '''
     try:
         package_id = request.POST.get('package_id')
         order = VIPOrder.create_order(request.user, package_id)
         if order:
             if order.pay_type != order.PAY_TYPE_BY_APPLE_INAPPPURCHASE:
                 order.pay_type = order.PAY_TYPE_BY_APPLE_INAPPPURCHASE
                 order.save(update_fields=['pay_type'])
             return Response({'order_id': order.id})
     except Exception, e:
         log.exception(e)
예제 #9
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)