Ejemplo n.º 1
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))
Ejemplo n.º 2
0
    def get_transaction_parameters(self,
                                   basket,
                                   request=None,
                                   use_client_side_checkout=False,
                                   **kwargs):
        """
        """
        out_trade_no = create_trade_id(basket.id)
        body = "BUY {amount} {currency}".format(amount=basket.total_incl_tax,
                                                currency=basket.currency)
        order_price = basket.total_incl_tax
        total_fee = int(order_price * 100)
        attach_data = urljoin(get_ecommerce_url(),
                              reverse('wechatpay:execute'))

        wxpayconf_pub = WxPayConf_pub()
        unifiedorder_pub = UnifiedOrder_pub()
        unifiedorder_pub.setParameter("body", body)
        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()
        approval_url = get_ecommerce_url() + reverse("wechatpay:page")
        if not PaymentProcessorResponse.objects.filter(
                processor_name=self.NAME,
                basket=basket).update(transaction_id=out_trade_no):
            self.record_processor_response({},
                                           transaction_id=out_trade_no,
                                           basket=basket)

        parameters = {
            'payment_page_url':
            approval_url,
            'code_url':
            code_url,
            'basket_id':
            basket.id,
            'request_page':
            '{payurl}?out_trade_no={param}'.format(
                payurl=(get_ecommerce_url() + reverse("wechatpay:result")),
                param=out_trade_no),
        }
        return parameters
Ejemplo n.º 3
0
    def get_transaction_parameters(self,
                                   basket,
                                   request=None,
                                   use_client_side_checkout=False,
                                   **kwargs):
        """
        """
        # Get the basket, and make sure it belongs to the current user.
        try:
            basket = request.user.baskets.get(id=basket.id)
        except ObjectDoesNotExist:
            return {}

        try:
            # Freeze the basket so that it cannot be modified
            basket.strategy = request.strategy
            Applicator().apply(basket, request.user, request)
            # basket.freeze()
            if basket.total_incl_tax <= 0:
                return {}

            out_trade_no = create_trade_id(basket.id)
            try:
                course_data = get_course_info_from_catalog(
                    request.site,
                    basket.all_lines()[0].product)
                body = course_data.get('title')
            except Exception, e:
                logger.exception(e)
                body = 'buy course'
            order_price = basket.total_incl_tax
            total_fee = int(order_price * 100)
            attach_data = urljoin(get_ecommerce_url(),
                                  reverse('wechatpay:execute'))

            wxpayconf_pub = WxPayConf_pub()
            unifiedorder_pub = UnifiedOrder_pub()
            unifiedorder_pub.setParameter("body", body)
            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()
            img = qrcode.make(code_url)
            buf = BytesIO()
            img.save(buf, format="PNG")
            qrcode_img = base64.b64encode(buf.getvalue())
            if not PaymentProcessorResponse.objects.filter(
                    processor_name=self.NAME,
                    basket=basket).update(transaction_id=out_trade_no):
                self.record_processor_response({},
                                               transaction_id=out_trade_no,
                                               basket=basket)

            parameters = {
                'qrcode_img': qrcode_img,
            }
            return parameters