def reply(self, requset, pk=None):
        customer_id = requset.session['customer_id']
        self.queryset = mm_Demand.received(customer_id)
        obj = self.get_object()
        if obj.status == mm_Demand.Status_Refused:
            return Response()
        serializer = self.serializer_class(obj,
                                           data=requset.data,
                                           partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        if obj.demand_type == mm_Demand.Type_Ask_Wechat:
            if serializer.validated_data[
                    'status'] == mm_Demand.Status_Accepted:
                # 更新申请人微信名片
                mm_WechatCard.add_wechat(obj.customer_id,
                                         requset.user.customer.id,
                                         requset.user.customer.wechat_id)
                # 更新被申请人的名片列表
                mm_WechatCard.add_wechat(requset.user.customer.id,
                                         obj.customer_id,
                                         obj.customer.wechat_id)
            elif serializer.validated_data[
                    'status'] == mm_Demand.Status_Refused:
                pass
            else:
                pass
        else:
            if serializer.validated_data[
                    'status'] == mm_Demand.Status_Refused:  # 返回线上或线下服务卡
                service_type = mm_VirtualService.Demand_Type_2_Service_Type.get(
                    obj.demand_type, 0)
                mm_VirtualService.modify_card(obj.customer_id, service_type, 1)

        return Response()
    def add_ask(self, request):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        demand_type = serializer.validated_data['demand_type']
        if demand_type == 0:  # 申请微信
            serializer.save(customer=request.user.customer,
                            demand_type=mm_Demand.Type_Ask_Wechat)
            return Response()
        elif demand_type == 1:

            if request.user.customer.online_card_count < 1:
                data = {'detail': '无可用线上服务卡'}
                return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
        elif demand_type == 2:
            if request.user.customer.offline_card_count < 1:
                data = {'detail': '无可用线下服务卡'}
                return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
        with transaction.atomic():
            service_type = mm_VirtualService.Demand_Type_2_Service_Type.get(
                demand_type, 0)
            mm_VirtualService.modify_card(request.session['customer_id'],
                                          service_type, -1)
            serializer.save(customer=request.user.customer,
                            demand_type=demand_type)
            return Response()
Beispiel #3
0
 def save_model(self, request, obj, form, change):
     # 返回用户的服务卡
     if 'status' in form.changed_data:
         if form.initial['status'] == 0:
             if form.cleaned_data['status'] == mm_Demand.Status_Refused:
                 service_type = mm_VirtualService.Demand_Type_2_Service_Type.get(obj.demand_type, 0)
                 mm_VirtualService.modify_card(obj.customer_id, service_type, 1)
     super().save_model(request, obj, form, change)
Beispiel #4
0
    def post(self, request, format=None):
        data = request.data.dict()
        # sign 不能参与签名验证
        signature = data.pop("sign")

        print(json.dumps(data))
        print(signature)
        logger.info('CallBack Data: %s' % json.dumps(data))
        logger.info('CallBack signature: %s' % signature)
        # verify
        success = alipay_serve.verify(data, signature)
        logger.info('CallBack verify result: %s' % success)

        if success and data["trade_status"] in ("TRADE_SUCCESS",
                                                "TRADE_FINISHED"):
            try:
                out_trade_no = data['out_trade_no']
                total_amount = float(data['buyer_pay_amount'])
                order = mm_Order.filter(
                    union_trade_no=out_trade_no,
                    total_amount=total_amount).select_related(
                        'virtual_service').first()
                if order:
                    order.status = mm_Order.ORDER_STATU_DONE
                    order.trade_no = data['trade_no']
                    order.save()
                    price_info = json.loads(
                        order.virtual_service.pricelist)[order.price_index]
                    days = price_info['days']
                    service_name = order.virtual_service.name
                    price_index_name = price_info['name']
                    mm_CustomerOrder.add_order(order.customer, 1, order,
                                               out_trade_no, service_name,
                                               price_index_name, total_amount)
                    if order.virtual_service.service_type in mm_VirtualService.Service_Group_Vip:
                        mm_ServiceCertification.update_certification(
                            order.customer_id, order.virtual_service, days)
                    elif order.virtual_service.service_type in mm_VirtualService.Service_Group_Card:
                        mm_VirtualService.modify_card(
                            order.customer_id,
                            order.virtual_service.service_type, 1)
                    else:
                        pass
            except:
                logger.error('Error: %s ' % traceback.format_exc())
            finally:
                return Response('success')
        else:
            return Response('failed')
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        """
        微信异步通知
        """
        raw_data = request.body.decode("utf-8")
        logger.info('Wechatpay CallBack Data: %s' % json.dumps(raw_data))
        data = wechatpay_serve.to_dict(raw_data)
        if not wechatpay_serve.check(data):
            return wechatpay_serve.reply("签名验证失败", False)
        # 处理业务逻辑

        total_fee = int(data['total_fee'])
        out_trade_no = data['out_trade_no']
        order = mm_Order.filter(union_trade_no=out_trade_no,
                                total_amount=total_fee /
                                100).select_related('virtual_service').first()
        if order:
            order.status = mm_Order.ORDER_STATU_DONE
            order.trade_no = data['transaction_id']
            order.save()
            price_info = json.loads(
                order.virtual_service.pricelist)[order.price_index]
            days = price_info['days']
            service_name = order.virtual_service.name
            price_index_name = price_info['name']
            mm_CustomerOrder.add_order(order.customer, 2, order, out_trade_no,
                                       service_name, price_index_name,
                                       total_fee / 100)
            if order.virtual_service.service_type in mm_VirtualService.Service_Group_Vip:
                mm_ServiceCertification.update_certification(
                    order.customer_id, order.virtual_service, days)
            elif order.virtual_service.service_type in mm_VirtualService.Service_Group_Card:
                mm_VirtualService.modify_card(
                    order.customer_id, order.virtual_service.service_type, 1)
            else:
                pass
        return wechatpay_serve.reply("OK", True)