diff --git a/digitalglarus/forms.py b/digitalglarus/forms.py
index cb7ede7a..4084ced5 100644
--- a/digitalglarus/forms.py
+++ b/digitalglarus/forms.py
@@ -7,7 +7,7 @@ from datetime import datetime
from utils.models import BillingAddress
from utils.forms import LoginFormMixin, SignupFormMixin, BillingAddressForm
-from .models import MembershipType
+from .models import MembershipType, MembershipOrder
from .models import Booking
@@ -39,6 +39,23 @@ class MembershipBillingForm(BillingAddressForm):
}
+class MembershipOrderForm(forms.ModelForm):
+
+ class Meta:
+ model = MembershipOrder
+ fields = ['membership', 'customer', 'billing_address',
+ 'last4', 'cc_brand', 'stripe_charge_id', 'amount']
+
+ # def save(self, commit=True):
+ # instance = super(MembershipOrderForm, self).save(commit=False)
+
+ # # if commit:
+ # # DonatorStatus.create(self.cleaned_data['donator'].user)
+ # # instance.save()
+
+ # return instance
+
+
class BookingBillingForm(BillingAddressForm):
token = forms.CharField(widget=forms.HiddenInput())
start_date = forms.DateField(widget=forms.HiddenInput())
diff --git a/digitalglarus/management/commands/make_membership_charge.py b/digitalglarus/management/commands/make_membership_charge.py
new file mode 100644
index 00000000..c727c734
--- /dev/null
+++ b/digitalglarus/management/commands/make_membership_charge.py
@@ -0,0 +1,166 @@
+from django.core.management.base import BaseCommand
+
+from datetime import datetime
+from django.utils import translation
+
+from utils.stripe_utils import StripeUtils
+
+from utils.mailer import BaseEmail
+from digitalglarus.models import MembershipOrder
+from digitalglarus.forms import MembershipOrderForm
+# from membership.U
+# 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):
+ translation.activate('en-us')
+ memberships_orders = MembershipOrder.objects.filter(membership__active=True)
+ current_month = datetime.now().month
+ current_year = datetime.now().year
+
+ print("--------- STARTING MEMBERSHIP CHARGING SCRIPT ---------")
+ print("Memberhips date: %s-%s" % (current_month, current_year))
+
+ for membership_order in memberships_orders:
+ member = membership_order.customer
+ try:
+ MembershipOrder.objects.get(created_at__month=current_month,
+ created_at__year=current_year,
+ customer=member)
+ except MembershipOrder.DoesNotExist:
+ try:
+ current_membership_price = membership_order.membership.type.price
+
+ last_membership_order = MembershipOrder.objects.filter(customer=member).last()
+
+ # Make stripe charge to a customer
+ stripe_utils = StripeUtils()
+ stripe_utils.CURRENCY = self.CURRENCY
+ charge_response = stripe_utils.make_charge(amount=current_membership_price,
+ customer=member.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("-------------------------")
+ continue
+
+ # Create a donation
+ charge = charge_response.get('response_object')
+ membership_order_data = {
+ 'cc_brand': charge.source.brand,
+ 'stripe_charge_id': charge.id,
+ 'last4': charge.source.last4,
+ 'membership': last_membership_order.membership.id,
+ 'billing_address': last_membership_order.billing_address.id,
+ 'customer': member.id,
+ 'amount': current_membership_price
+ }
+ membership_order_form = MembershipOrderForm(membership_order_data)
+ import pdb
+ pdb.set_trace()
+ if membership_order_form.is_valid():
+ membership_order = membership_order_form.save()
+
+ context = {
+ 'order': membership_order,
+ 'base_url': "{0}://{1}".format('https', 'dynamicweb.ungleich.ch')
+
+ }
+ email_data = {
+ 'subject': 'Your monthly membership has been charged',
+ 'to': member.user.email,
+ 'context': context,
+ 'template_name': 'membership_monthly_charge',
+ 'template_path': 'digitalglarus/emails/'
+ }
+ email = BaseEmail(**email_data)
+ email.send()
+
+ print("--------- PAYMENT DONATION SUCCESSFULL ---------")
+ print("Member: %s" % member.user.email)
+ print("Amount: %s %s" % (current_membership_price, self.CURRENCY))
+ print("-----------------------------------------------")
+
+ except Exception as e:
+ print("--------- ERROR ---------")
+ print(e)
+ print("-------------------------")
+ continue
+ # 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("-------------------------")
+ # continue
+ # # 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()
+
+ # context = {
+ # 'donation': donation,
+ # 'base_url': "{0}://{1}".format('https', 'dynamicweb.ungleich.ch')
+
+ # }
+ # email_data = {
+ # 'subject': 'Your donation have been charged',
+ # 'to': donation.donator.user.email,
+ # 'context': context,
+ # 'template_name': 'donation_charge',
+ # 'template_path': 'nosystemd/emails/'
+ # }
+ # email = BaseEmail(**email_data)
+ # email.send()
+
+ # 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/digitalglarus/models.py b/digitalglarus/models.py
index 0147d53b..17621bfd 100644
--- a/digitalglarus/models.py
+++ b/digitalglarus/models.py
@@ -63,6 +63,11 @@ class Membership(models.Model):
type = models.ForeignKey(MembershipType)
active = models.BooleanField(default=True)
+ @classmethod
+ def create(cls, data):
+ instance = cls.objects.create(**data)
+ return instance
+
@classmethod
def is_digitalglarus_active_member(cls, user):
past_month = (datetime.today() - relativedelta(months=1)).month
@@ -74,11 +79,6 @@ class Membership(models.Model):
return cls.objects.filter(has_booking_past_month | has_booking_current_month).\
filter(active_membership).exists()
- @classmethod
- def create(cls, data):
- instance = cls.objects.create(**data)
- return instance
-
def deactivate(self):
self.active = False
self.save()
diff --git a/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.html b/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.html
new file mode 100644
index 00000000..68347768
--- /dev/null
+++ b/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.html
@@ -0,0 +1,136 @@
+{% load static from staticfiles %}
+
+
+
+
+
+
+Oxygen Invoice
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+ Your monthly membership for {{order.created_at|date:"F"}} has been charged. You can view your invoice clicking on the button below.
+ |
+
+
+
+
+ |
+
+
+
+ |
+
+
+
+
+
+
+ |
+
+
+
+
+
diff --git a/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.txt b/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.txt
new file mode 100644
index 00000000..68347768
--- /dev/null
+++ b/digitalglarus/templates/digitalglarus/emails/membership_monthly_charge.txt
@@ -0,0 +1,136 @@
+{% load static from staticfiles %}
+
+
+
+
+
+
+Oxygen Invoice
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+
+
+
+
+
+
+
+ Your monthly membership for {{order.created_at|date:"F"}} has been charged. You can view your invoice clicking on the button below.
+ |
+
+
+
+
+ |
+
+
+
+ |
+
+
+
+
+
+
+ |
+
+
+
+
+
diff --git a/digitalglarus/templates/digitalglarus/membership_orders_detail.html b/digitalglarus/templates/digitalglarus/membership_orders_detail.html
index 7da92048..817c818a 100644
--- a/digitalglarus/templates/digitalglarus/membership_orders_detail.html
+++ b/digitalglarus/templates/digitalglarus/membership_orders_detail.html
@@ -17,7 +17,7 @@
-
Your Membership Order Detail
+ Your membership invoice for {{order.created_at|date:"F"}}
InvoiceGet PDF
diff --git a/membership/models.py b/membership/models.py
index 88e3e902..21dea515 100644
--- a/membership/models.py
+++ b/membership/models.py
@@ -24,7 +24,6 @@ def get_anonymous_user_instance(User):
validation_slug=make_password(None))
-
class MyUserManager(BaseUserManager):
def create_user(self, email, name, password=None):
"""
@@ -89,6 +88,10 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
else:
return None
+ @classmethod
+ def get_all_members(cls):
+ return cls.objects.filter(stripecustomer__membershiporder__isnull=False)
+
@classmethod
def validate_url(cls, validation_slug):
user = cls.objects.filter(validation_slug=validation_slug).first()
diff --git a/nosystemd/management/commands/make_donation_charges.py b/nosystemd/management/commands/make_donation_charges.py
index 12f4e884..04d00bf6 100644
--- a/nosystemd/management/commands/make_donation_charges.py
+++ b/nosystemd/management/commands/make_donation_charges.py
@@ -9,7 +9,6 @@ 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'