diff --git a/hosting/views.py b/hosting/views.py index acd53d0c..1a6cbeb8 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -347,8 +347,6 @@ class PaymentVMView(LoginRequiredMixin, FormView): order.set_approved() # Send notification to ungleich as soon as VM has been booked - # TODO send email using celery - context = { 'vm': plan, 'order': order, diff --git a/nosystemd/management/commands/make_donation_charges.py b/nosystemd/management/commands/make_donation_charges.py index 210b04be..70f95d64 100644 --- a/nosystemd/management/commands/make_donation_charges.py +++ b/nosystemd/management/commands/make_donation_charges.py @@ -1,51 +1,72 @@ from django.core.management.base import BaseCommand -from nosystem.models import DonatorStatus, Donation from datetime import datetime from utils.stripe_utils import StripeUtils -from .forms import DonationForm + +from nosystemd.models import DonatorStatus, Donation +from nosystemd.forms import DonationForm class Command(BaseCommand): help = 'Make the monthly stripe charge to all donators' + CURRENCY = 'usd' def handle(self, *args, **options): donators = DonatorStatus.objects.filter(status=DonatorStatus.ACTIVE) current_month = datetime.now().month current_year = datetime.now().year - for donator in donators: - current_month_donation = Donation.objects.get(created_at__month=current_month, - created_at__year=current_year) - if not current_month_donation: - last_donation = Donation.objects.filter(donator=donator).last() - donation_amount = last_donation.donation - # Make stripe charge to a customer - stripe_utils = StripeUtils() - stripe_utils.CURRENCY = 'usd' - charge_response = stripe_utils.make_charge(amount=donation_amount, - customer=donator.stripe_id) - charge = charge_response.get('response_object') + print("--------- STARTING DONATIONS SCRIPT ---------") + print("Donations date: %s-%s" % (current_month, current_year)) - # Check if the payment was approved - if not charge: - # TODO save error - context = { - 'paymentError': charge_response.get('error'), + for donator_status in donators: + donator = donator_status.user.stripecustomer + try: + Donation.objects.get(created_at__month=current_month, + created_at__year=current_year, + donator=donator) + except Donation.DoesNotExist: + try: + # Get donator last donation amount + last_donation = Donation.objects.filter(donator=donator).last() + donation_amount = last_donation.donation + + # Make stripe charge to a customer + stripe_utils = StripeUtils() + stripe_utils.CURRENCY = self.CURRENCY + charge_response = stripe_utils.make_charge(amount=donation_amount, + customer=donator.stripe_id) + charge = charge_response.get('response_object') + + # Check if the payment was approved + if not charge: + # There is an error trying to creating the stripe charge + context = { + 'paymentError': charge_response.get('error'), + } + print("--------- STRIPE PAYMENT ERROR ---------") + print(context) + print("-------------------------") + # Create a donation + charge = charge_response.get('response_object') + donation_data = { + 'cc_brand': charge.source.brand, + 'stripe_charge_id': charge.id, + 'last4': charge.source.last4, + 'billing_address': last_donation.billing_address.id, + 'donator': donator.id, + 'donation': donation_amount } - print(context) - # Create a donation - charge = charge_response.get('response_object') - donation_data = { - 'cc_brand': charge.source.brand, - 'stripe_charge_id': charge.id, - 'last4': charge.source.last4, - 'billing_address': last_donation.billing_address.id, - 'donator': donator.id, - 'donation': donation_amount - } - donation_form = DonationForm(donation_data) - if donation_form.is_valid(): - donation = donation_form.save() - print(donation) + donation_form = DonationForm(donation_data) + if donation_form.is_valid(): + donation = donation_form.save() + print("--------- PAYMENT DONATION SUCCESSFULL ---------") + print("Donator: %s" % donation.donator.user.email) + print("Amount: %s %s" % (donation.donation, self.CURRENCY)) + print("-----------------------------------------------") + except Exception as e: + print("--------- ERROR ---------") + print(e) + print("-------------------------") + continue diff --git a/nosystemd/models.py b/nosystemd/models.py index 45c3948a..786eb8d7 100644 --- a/nosystemd/models.py +++ b/nosystemd/models.py @@ -14,6 +14,9 @@ class DonatorStatus(models.Model): user = models.OneToOneField(CustomUser) status = models.CharField(choices=STATUS_CHOICES, max_length=10, default=ACTIVE) + def __str__(self): + return "%s - %s " % (self.user.email, self.status) + @classmethod def create(cls, user): cls.objects.get_or_create(user=user) @@ -34,6 +37,10 @@ class Donation(models.Model): obj = cls.objects.create(**data) return obj + @classmethod + def get_total_donations_amount(cls): + return sum(cls.objects.all().values_list('donation', flat=True)) + def set_stripe_charge(self, stripe_charge): self.stripe_charge_id = stripe_charge.id self.last4 = stripe_charge.source.last4 diff --git a/nosystemd/templates/nosystemd/base.html b/nosystemd/templates/nosystemd/base.html index b9691186..4bd841f4 100644 --- a/nosystemd/templates/nosystemd/base.html +++ b/nosystemd/templates/nosystemd/base.html @@ -10,7 +10,7 @@ -