Esempio n. 1
0
    def pay_apply(self,
                  order_id,
                  price: float,
                  subject: str,
                  content: str,
                  back_params: str = None,
                  timeout_express=None,
                  callback_url: str = callback_url):
        """
        :param timeout_express: 超时时间,分钟
        :param order_id: 订单编号
        :param price: 总价,金额
        :param subject: 标题
        :param content:
        :param back_params:公用回传参数,必须进行 UrlEncode
        :param callback_url:
        :return:
        """
        model = AlipayTradeAppPayModel()
        model.timeout_express = f'{round(timeout_express, 4)}m'
        model.total_amount = float(price)
        model.seller_id = """config.alipay_account"""
        model.product_code = "QUICK_MSECURITY_PAY"  # 固定值
        model.body = content[:128]  # 接口最长支持128个字符
        model.subject = subject
        model.out_trade_no = order_id

        if back_params:
            model.passback_params = back_params

        request = AlipayTradeAppPayRequest(biz_model=model)
        request.notify_url = callback_url
        return self.client.sdk_execute(request)
Esempio 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)