Added DonatorStatus model in order to know if an donator has canceled or not his monthly donation, Now we create a DonatorStatus for the user after receiving his first donation. Added DonatorStatus view. Added donator_status.html in order to allow an user view his donation status , Added action to allow user to cancel his monthly donations. Now the user can logout using navbar. added Donation model to admin.Added command make_donations_charges in order to create stripe current monthly donations from all donators
This commit is contained in:
parent
cb520f6b58
commit
4580a75f89
22 changed files with 611 additions and 232 deletions
51
nosystemd/management/commands/make_donation_charges.py
Normal file
51
nosystemd/management/commands/make_donation_charges.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Make the monthly stripe charge to all donators'
|
||||
|
||||
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')
|
||||
|
||||
# Check if the payment was approved
|
||||
if not charge:
|
||||
# TODO save error
|
||||
context = {
|
||||
'paymentError': charge_response.get('error'),
|
||||
}
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue