def bonus(self, request, *args, **kwargs): worker_handle = request.data.get('handle') amount = request.data.get('amount') if amount is None: raise serializers.ValidationError(detail=daemo_error("Enter a valid amount.")) reason = request.data.get('reason') workers = UserProfile.objects.filter(handle=worker_handle) if workers.count() > 1: raise serializers.ValidationError(detail=daemo_error("Handle is not unique.")) if workers.first() is None: raise serializers.ValidationError(detail=daemo_error("User not found.")) worker = workers.first() if worker.user_id == request.user.id: raise serializers.ValidationError(detail=daemo_error("Cannot bonus self.")) if TaskWorker.objects.filter( status__in=[TaskWorker.STATUS_ACCEPTED, TaskWorker.STATUS_SUBMITTED, TaskWorker.STATUS_RETURNED], task__project__owner=request.user, worker=worker.user ).count() < 1 or not hasattr(worker.user, 'stripe_account'): raise serializers.ValidationError(detail=daemo_error("You cannot bonus a worker " "who hasn't done work for you.")) with transaction.atomic(): validate_account_balance(request, int(amount * 100)) stripe = Stripe() bonus = stripe.pay_bonus(worker=worker.user, user=request.user, amount=amount, reason=reason) if bonus is None: return Response({"message": "Bonus not created!"}, status=status.HTTP_400_BAD_REQUEST) return Response({"message": "Bonus created successfully."})
def update_credit_card(self, request, *args, **kwargs): stripe = Stripe() with transaction.atomic(): stripe.update_customer_source(credit_card=request.data, user=request.user) return Response({"message": "Card updated successfully"}, status.HTTP_200_OK)
def update_bank_info(self, request, *args, **kwargs): bank_data = request.data bank_data.update({'currency': 'usd'}) bank_data.update({'country': 'US'}) stripe = Stripe() with transaction.atomic(): stripe.update_external_account(bank=bank_data, user=request.user) return Response({"message": "Bank information updated successfully"}, status.HTTP_200_OK)
def create(self, request, *args, **kwargs): stripe = Stripe() amount = int(request.data.get('amount', 0) * 100) if amount < 100: return Response(data={"detail": "Amount must be greater than $1"}, status=status.HTTP_400_BAD_REQUEST) with transaction.atomic(): charge = stripe.create_charge(amount, user=request.user) return Response(data={"id": charge.id})
def pay_workers(): workers = User.objects.all() payment = Stripe() # total = 0 # for worker in workers: task_workers = models.TaskWorker.objects.prefetch_related('task__project') \ .filter(worker=worker, status=models.TaskWorker.STATUS_ACCEPTED, is_paid=False) for tw in task_workers: payment.pay_worker(tw)
def stripe(self, request, *args, **kwargs): from crowdsourcing.serializers.utils import CreditCardSerializer, BankSerializer from crowdsourcing.payment import Stripe is_worker = request.data.get('is_worker', False) is_requester = request.data.get('is_requester', False) ssn_last_4 = request.data.get('ssn_last_4') bank_data = None credit_card = None if not is_worker and not is_requester: raise serializers.ValidationError(detail=daemo_error( "Please set either worker or requester to true.")) if is_requester: card_serializer = CreditCardSerializer( data=request.data.get('credit_card', {})) if not card_serializer.is_valid(): raise serializers.ValidationError( detail=card_serializer.errors) credit_card = request.data.get('credit_card') if is_worker: if not UserViewSet.is_whitelisted(request.user): raise serializers.ValidationError(detail=daemo_error( "You are not allowed to sign up as a worker at this time.") ) # TODO add support for other countries bank_data = request.data.get('bank', {}) bank_data.update({'currency': 'usd'}) bank_data.update({'country': 'US'}) bank_serializer = BankSerializer(data=bank_data) if not bank_serializer.is_valid(): raise serializers.ValidationError( detail=bank_serializer.errors) profile = request.user.profile account, customer = Stripe().create_account_and_customer( user=request.user, country_iso=profile.address.city.country.code, ip_address='8.8.8.8', is_worker=is_worker, is_requester=is_requester, credit_card=credit_card, bank=bank_data, ssn_last_4=ssn_last_4) if account is not None: profile.is_worker = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_worker', 1) if customer is not None: profile.is_requester = True update_worker_cache([profile.user_id], constants.ACTION_UPDATE_PROFILE, 'is_requester', 1) if account is not None or customer is not None: profile.save() return Response(data={"message": "Accounts and customer created"}, status=status.HTTP_201_CREATED) raise serializers.ValidationError(detail=daemo_error( "No accounts were created, something went wrong!"))
def create_account_and_customer(user_id, ip_address): from crowdsourcing.payment import Stripe try: user = User.objects.get(pk=user_id) Stripe().create_account_and_customer(user=user, country_iso=user.profile.address.city.country.code, ip_address=ip_address) except User.DoesNotExist: return 'User does not exist' return 'SUCCESS'
def bonus(self, request, *args, **kwargs): worker_handle = request.data.get('handle') amount = request.data.get('amount') if amount is None: raise serializers.ValidationError( detail=daemo_error("Enter a valid amount.")) reason = request.data.get('reason') workers = UserProfile.objects.filter(handle=worker_handle) if workers.count() > 1: raise serializers.ValidationError( detail=daemo_error("Handle is not unique.")) if workers.first() is None: raise serializers.ValidationError( detail=daemo_error("User not found.")) worker = workers.first() if worker.user_id == request.user.id: raise serializers.ValidationError( detail=daemo_error("Cannot bonus self.")) if TaskWorker.objects.filter( status__in=[ TaskWorker.STATUS_ACCEPTED, TaskWorker.STATUS_SUBMITTED, TaskWorker.STATUS_RETURNED ], task__project__owner=request.user, worker=worker.user).count() < 1 or not hasattr( worker.user, 'stripe_account'): raise serializers.ValidationError( detail=daemo_error("You cannot bonus a worker " "who hasn't done work for you.")) with transaction.atomic(): validate_account_balance(request, int(amount * 100)) stripe = Stripe() bonus = stripe.pay_bonus(worker=worker.user, user=request.user, amount=amount, reason=reason) if bonus is None: return Response({"message": "Bonus not created!"}, status=status.HTTP_400_BAD_REQUEST) return Response({"message": "Bonus created successfully."})
def refund_charges_before_expiration(): from crowdsourcing.payment import Stripe charges = models.StripeCharge.objects.filter(expired=False, balance__gt=50, created_at__gt=timezone.now() - settings.STRIPE_CHARGE_LIFETIME) for charge in charges: try: Stripe().refund(charge=charge, amount=charge.balance) charge.expired = True charge.expired_at = timezone.now() charge.save() except Exception: pass