def get_user_by_auth_code(self, provider, auth_code): cls = get_provider_cls(provider) obj = cls() openid = obj.get_openid(auth_code) kw = {obj.field: openid} shop_user = self.get(**kw) return shop_user
def bind_third_account(self, provider, auth_code): cls = get_provider_cls(provider) obj = cls() openid = obj.get_openid(auth_code) val = getattr(self, obj.field) if val and val != openid: raise exceptions.AlreadyBinded else: setattr(self, obj.field, openid) self.save(update_fields=[obj.field])
def resolve_order_info(self, info, provider, order_id, **kw): shop_user = info.context.user.shop_user cls = get_provider_cls(provider) obj = cls() openid = obj.get_openid(shop_user=shop_user) res = obj.order_info(order_id, openid) if res: return OrderInfo(**res) else: return None
def mutate(self, info, params): shop_user = info.context.user.shop_user avoid_resubmit = AvoidResubmit("withdraw") try: avoid_resubmit(params.request_id, shop_user.id) except avoid_resubmit.ResubmittedError as e: raise exceptions.GQLError(e.message) try: cls = get_provider_cls(params.provider) obj = cls() openid = obj.get_openid(shop_user=shop_user) except exceptions.DoNotSupportBindType: raise exceptions.GQLError(f"Does not support {params.provider}") note = settings.LOGO_NAME + "-提现" with transaction.atomic(): try: transfer = do_withdraw(shop_user, params.amount, note="提现到零钱") except exceptions.NotEnoughBalance as e: raise exceptions.GQLError(e.message) try: res = obj.withdraw(openid, params.amount, note) except exceptions.WithdrawError as e: # FIXME: revert do_withdraw? # 提现进行中,可能会有一定延时,需要人工处理 # 提现排队中.... logger.exception(str(e)) raise exceptions.GQLError(e.message) transfer.extra_info = dict(res) transfer.order_id = res["payment_no"] transfer.save() return Result(success=True)
def mutate(self, info, params): shop_user = info.context.user.shop_user avoid_resubmit = AvoidResubmit("createPayOrder") try: avoid_resubmit(params.request_id, shop_user.id) except avoid_resubmit.ResubmittedError as e: raise exceptions.GQLError(e.message) to_user = None if params.to: to_user = ShopUser.objects.get(uuid=params.to) try: cls = get_provider_cls(params.provider) res = cls().create_pay_order(params.code, params.amount, to_user=to_user) except exceptions.DoNotSupportBindType: raise exceptions.GQLError(f"Does not support {params.provider}") return CreatePayOrder(payment=res)
def get_user_by_openid(self, provider, openid): cls = get_provider_cls(provider) obj = cls() kw = {obj.field: openid} shop_user = self.get(**kw) return shop_user