Exemple #1
0
    def recharge(cls, amount, member, payment, operator):
        """
        会员充值
        Args:
            amount: 充值金额
            member: 充值会员
            payment: 支付方式, 'wxpay', 'alipay', 'cash'
            operator: 操作者

        Returns:

        """
        order_type = 1

        with BaseModel._meta.database.atomic() as txn:
            order = TeamOrder.create(
                order_no=TeamOrder.get_new_order_no(),
                team=member.team,
                user=member.user,
                order_type=order_type,
                payment_method=payment,
            )
Exemple #2
0
 def new_order(cls, team, activity_id, user, order_type, payment_method,
               total_fee, payment_fee, title):
     """
     Args:
         total_fee: 订单金额
         payment_fee: 实付金额
         team:
         user:
         order_type: TeamOrder.Order_TYPES 订单类型 参加活动/消费
         payment_method: 支付方式
         activity_id:
         batch_id:
     """
     order = TeamOrder.create(order_no=TeamOrder.get_new_order_no(),
                              team=team,
                              user=user,
                              title=title,
                              order_type=order_type,
                              payment_fee=payment_fee,
                              payment_method=payment_method,
                              total_fee=total_fee,
                              activity_id=activity_id,
                              state=TeamOrder.OrderState.WAIT_BUYER_PAY)
     return order
    def initial_data(self):
        self.leader = User.create(name="imleader")
        self.team = Team.create(name="test_finish_activity",
                                owner_id=self.leader.id)
        self.activity = Activity.create(
            team=self.team,
            creator=self.leader,
            leader=self.leader,
            title="test_finish_activity",
            description="test_stoped_activity",
            price=Decimal(15),
            max_members=30,
            start_time=datetime.datetime.now() - datetime.timedelta(hours=2),
            end_time=datetime.datetime.now() - datetime.timedelta(hours=1),
            payment_type=0,
            repeat_type="weekly",
            state=Activity.ActivityState.opening)

        self.online_paid_amount = 0

        for i in range(0, 5):
            user = User.create(name="test_%s" % i)
            order = TeamOrder.create(
                team=self.team,
                user=user,
                order_type=0,
                activity_id=self.activity.id,
                title="test_finish",
                order_no=TeamOrder.get_new_order_no(),
                credit_fee=0,
                total_fee=self.activity.price,
                payment_fee=self.activity.price,
                payment_method="wxpay",
                state=TeamOrder.OrderState.TRADE_BUYER_PAID,
                paid=self.activity.start_time,
                created=self.activity.start_time,
                finished=self.activity.start_time)
            ActivityMember.create(
                team=self.team,
                activity=self.activity,
                user=user,
                price=self.activity.price,
                users_count=1,
                total_fee=self.activity.price,
                payment_state=order.state,
                payment_method="wxpay",
                order_id=order.id,
                order_no=order.order_no,
                state=ActivityMember.ActivityMemberState.confirmed,
                free_times=0,
            )

            self.online_paid_amount += self.activity.price

        for i in range(0, 3):
            user = User.create(name="test2_%s" % i)
            order = TeamOrder.create(team=self.team,
                                     user=user,
                                     order_type=0,
                                     activity_id=self.activity.id,
                                     title="test_finish",
                                     order_no=TeamOrder.get_new_order_no(),
                                     credit_fee=0,
                                     total_fee=self.activity.price,
                                     payment_fee=self.activity.price,
                                     payment_method="wxpay",
                                     state=TeamOrder.OrderState.WAIT_BUYER_PAY,
                                     created=self.activity.start_time,
                                     finished=self.activity.start_time)
            ActivityMember.create(
                team=self.team,
                activity=self.activity,
                user=user,
                price=self.activity.price,
                users_count=1,
                total_fee=self.activity.price,
                payment_state=order.state,
                payment_method="wxpay",
                order_id=order.id,
                order_no=order.order_no,
                state=ActivityMember.ActivityMemberState.wait_confirm,
                free_times=0,
            )
    def post(self, match_id):
        """ 报名赛事

        :match_id: 赛事ID
        :returns:

        """

        match = Match.get_or_404(id=match_id)

        # 检查赛事是否可以报名
        result = match.can_join()
        self.has_joined(match, user_id=self.current_user.id)

        if not result['can']:
            raise ApiException(403,
                               result['reason'],
                               log_message="报名失败:{0}, {1}".format(
                                   match_id, result['reason']))

        team = Team.get_or_404(id=match.team_id)

        # 分组比赛模式
        group = None
        if match.group_type == 1:
            group_id = self.get_argument("group_id")
            group = MatchGroup.get_or_none(id=group_id, match_id=match_id)
            if group is None:
                raise ApiException(404, "赛事分组不存在")

            # 分组是否已报满
            if group.max_members <= group.members_count:
                raise ApiException(403, "赛事分组已报满")

            total_fee = group.price

        else:
            total_fee = match.price

        name = self.get_argument("name")
        mobile = self.get_argument("mobile")

        if not name:
            raise ApiException(400, "请填写名称")

        if not mobile:
            raise ApiException(400, "请填写手机号码")

        elif not is_mobile(mobile):
            raise ApiException(400, "手机号码格式有误")

        # TODO: 上线前移除
        # member = MatchMember.get_or_none(match_id=match.id,
        #                                  user_id=self.current_user.id)
        # if member is not None:
        #     raise ApiException(403, "你已报名此赛事,无需重复报名")

        extra_attrs = self.parse_options(match_id)

        with self.db.transaction() as txn:

            # 零元赛事无需支付不生成订单
            order = None
            if total_fee > 0:
                order = TeamOrder.create(
                    order_no=TeamOrder.get_new_order_no(),
                    team=team,
                    user=team.owner_id,
                    title=match.title,
                    order_type=TeamOrder.OrderType.MATCH,
                    payment_fee=total_fee,
                    total_fee=total_fee,
                    activity_id=match.id,
                    state=TeamOrder.OrderState.WAIT_BUYER_PAY)

            other_attrs = {}
            if "avatar" in match.fields:
                other_attrs['avatar_key'] = self.upload_photo("avatar", "头像")

            if "idcard_photo" in match.fields:
                other_attrs['idcard_front'] = self.upload_photo(
                    "idcard_front", "证件照片正面")
                other_attrs['idcard_back'] = self.upload_photo(
                    "idcard_back", "证件照片背面")

            gender = self.get_argument("gender", "")
            if gender in ("0", "1"):
                gender = ("f", "m")[int(gender)]
            else:
                gender = "n"

            if order and order.state == TeamOrder.OrderState.WAIT_BUYER_PAY:
                member_state = MatchMember.MatchMemberState.wait_pay
            else:
                member_state = MatchMember.MatchMemberState.normal

            team.add_member(self.current_user.id,
                            nick=name,
                            state=TeamMember.TeamMemberState.normal)
            member = MatchMember.create(
                match_id=match.id,
                group_id=group.id if group else 0,
                member_type=match.join_type,
                user_id=self.current_user.id,
                name=name,
                mobile=mobile,
                gender=gender,
                age=self.get_argument("age", "0"),
                is_leader=self.get_argument("is_leader", False),
                realname=self.get_argument("realname", ""),
                idcard_number=self.get_argument("idcard_number", ""),
                extra_attrs=extra_attrs,
                order_id=order.id if order else 0,
                total_fee=total_fee,
                state=member_state,
                **other_attrs)

            # 如果需要付费则生成支付订单
            if total_fee > 0:

                if match.refund_expire:
                    refund_expire = match.refund_expire.strftime(
                        "%Y%m%d%H%M%S")
                else:
                    refund_expire = datetime.now().strftime("%Y%m%d%H%M%S")

                resp = self.parteam_request(
                    "/match/openapi/createOrderInfo.do",
                    post_args={
                        "orderValue":
                        match.id,
                        "eachFee":
                        int(total_fee * 100),
                        "num":
                        1,
                        "totalFee":
                        int(total_fee * 100),
                        "subject":
                        match.title,
                        "userId":
                        self.current_user.id,
                        "notifyUrl":
                        urljoin(
                            self.request.full_url(),
                            self.reverse_url('rest_match_join_notify',
                                             match.id)),
                        "version":
                        1,
                        "expDatetime":
                        refund_expire,
                        "tradeType":
                        "APP" if self.device_type.lower() in ("ios", "android")
                        else "WEB",
                    })

                if "orderNo" not in resp:
                    txn.rollback()
                    raise ApiException(
                        400,
                        "创建订单失败",
                        log_message="match order fail:{0}".format(resp))

                MatchMember.update(pt_order_no=resp['orderNo']).where(
                    MatchMember.id == member.id).execute()

            # 统计赛事人数
            Match.update_members_count(match.id)
            if group:
                MatchGroup.update_members_count(group.id)

            member = MatchMember.get_or_404(id=member.id)

        member_info = member.info
        member_info['order'] = {
            "orderNo": member.pt_order_no,
            "orderValue": match.id,
            "eachFee": int(total_fee * 100),
            "num": 1,
            "totalFee": int(total_fee * 100),
            "subject": match.title,
            "userId": self.current_user.id,
        }

        self.write(member_info)