From e5c1e15010e7c4e8f0f073ffba136c769bce61ab Mon Sep 17 00:00:00 2001 From: Oly Date: Mon, 30 Jan 2017 17:08:37 +0000 Subject: [PATCH] more work on payment gateway --- mhackspace/members/views.py | 2 +- .../commands/list_subscription_payments.py | 41 ++++++++++++++++++ .../commands/refresh_subscriptions.py | 2 +- .../subscriptions/migrations/0001_initial.py | 32 ++++++++++++++ .../subscriptions/migrations/__init__.py | 0 mhackspace/subscriptions/models.py | 42 +++++++++++++++++++ mhackspace/subscriptions/payments.py | 33 +++++++++++++++ mhackspace/templates/pages/home.html | 2 + mhackspace/templates/users/user_detail.html | 2 +- mhackspace/users/fixtures/groups.json | 2 + 10 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 mhackspace/subscriptions/management/commands/list_subscription_payments.py create mode 100644 mhackspace/subscriptions/migrations/0001_initial.py create mode 100644 mhackspace/subscriptions/migrations/__init__.py create mode 100644 mhackspace/subscriptions/models.py create mode 100644 mhackspace/users/fixtures/groups.json diff --git a/mhackspace/members/views.py b/mhackspace/members/views.py index 34972b2..4518a32 100644 --- a/mhackspace/members/views.py +++ b/mhackspace/members/views.py @@ -16,5 +16,5 @@ class MemberListView(LoginRequiredMixin, ListView): def get_context_data(self, **kwargs): context = super(MemberListView, self).get_context_data(**kwargs) context['members'] = self.get_queryset() - context['total'] = self.get_queryset().filter(groups__name='member').count() + context['total'] = self.get_queryset().filter(groups__name='members').count() return context diff --git a/mhackspace/subscriptions/management/commands/list_subscription_payments.py b/mhackspace/subscriptions/management/commands/list_subscription_payments.py new file mode 100644 index 0000000..930cea4 --- /dev/null +++ b/mhackspace/subscriptions/management/commands/list_subscription_payments.py @@ -0,0 +1,41 @@ +from datetime import datetime +from django.utils import timezone +from django.contrib.auth.models import Group +from django.forms.models import model_to_dict +from django.core.management.base import BaseCommand +from mhackspace.subscriptions.payments import select_provider +from mhackspace.users.models import Membership, User +from mhackspace.subscriptions.models import Payments + + +class Command(BaseCommand): + help = 'Update user subscriptions' + + def handle(self, *args, **options): + provider = select_provider('gocardless') + + self.stdout.write( + self.style.NOTICE( + '== Gocardless customers ==')) + + Payments.objects.all().delete() + for customer in provider.fetch_customers(): + self.stdout.write(str(dir(customer))) + self.stdout.write(str(customer)) + + Payments.objects.create( + user=None, + user_reference=customer.get('user_id'), + user_email=customer.get('email'), + reference=customer.get('payment_id'), + amount=customer.get('amount'), + type=Payments.lookup_payment_type(customer.get('payment_type')), + date=customer.get('payment_date') + ) + # self.stdout.write(str(customer.email)) + # self.stdout.write(str(dir(customer['email']()))) + # self.stdout.write( + # self.style.SUCCESS( + # '\t{reference} - {payment} - {status} - {email}'.format(**model_to_dict(subscriptions[-1])))) + + diff --git a/mhackspace/subscriptions/management/commands/refresh_subscriptions.py b/mhackspace/subscriptions/management/commands/refresh_subscriptions.py index 9406808..ebcfddc 100644 --- a/mhackspace/subscriptions/management/commands/refresh_subscriptions.py +++ b/mhackspace/subscriptions/management/commands/refresh_subscriptions.py @@ -20,7 +20,7 @@ class Command(BaseCommand): Membership.objects.all().delete() subscriptions = [] - group = Group.objects.get(name='member') + group = Group.objects.get(name='members') for sub in provider.fetch_subscriptions(): try: diff --git a/mhackspace/subscriptions/migrations/0001_initial.py b/mhackspace/subscriptions/migrations/0001_initial.py new file mode 100644 index 0000000..4ce244f --- /dev/null +++ b/mhackspace/subscriptions/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-01-29 21:55 +from __future__ import unicode_literals + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Payments', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('user_reference', models.CharField(max_length=255)), + ('user_email', models.CharField(max_length=255)), + ('reference', models.CharField(max_length=255, unique=True)), + ('amount', models.DecimalField(decimal_places=2, default=0.0, max_digits=6)), + ('type', models.PositiveSmallIntegerField(default=0)), + ('date', models.DateTimeField()), + ('user', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='from_user', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/mhackspace/subscriptions/migrations/__init__.py b/mhackspace/subscriptions/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mhackspace/subscriptions/models.py b/mhackspace/subscriptions/models.py new file mode 100644 index 0000000..a2b7770 --- /dev/null +++ b/mhackspace/subscriptions/models.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals, absolute_import + +from django.conf import settings +from django.contrib.auth.models import AbstractUser +from django.core.urlresolvers import reverse +from django.db import models +from django.utils.encoding import python_2_unicode_compatible +from django.utils.translation import ugettext_lazy as _ +from stdimage.models import StdImageField + + +PAYMENT_TYPES = { + 'unknown': 0, + 'subscription': 1, + 'payment': 2 +} + +@python_2_unicode_compatible +class Payments(models.Model): + user = models.ForeignKey( + settings.AUTH_USER_MODEL, + null=True, blank=True, + default=None, + related_name='from_user' + ) + user_reference = models.CharField(max_length=255) + user_email = models.CharField(max_length=255) + + reference = models.CharField(max_length=255, unique=True) + amount = models.DecimalField(max_digits=6, decimal_places=2, default=0.0) + type = models.PositiveSmallIntegerField(default=0) + date = models.DateTimeField() + + def lookup_payment_type(name): + return PAYMENT_TYPES.get(name, 0) + + def get_payment_type(self): + return self.type + + def __str__(self): + return self.reference diff --git a/mhackspace/subscriptions/payments.py b/mhackspace/subscriptions/payments.py index 6a188d4..d765a42 100644 --- a/mhackspace/subscriptions/payments.py +++ b/mhackspace/subscriptions/payments.py @@ -50,6 +50,39 @@ class gocardless_provider: 'success': response.success } + + + def fetch_customers(self): + merchant = gocardless.client.merchant() + for customer in merchant.bills(): + user = customer.user() + print(dir(customer)) + print(dir(customer.reference_fields)) + print(customer.reference_fields) + print(customer.payout_id) + print(customer.reference_fields.payout_id) + result = { + 'user_id': user.id, + 'email': user.email, + 'status': customer.status, + 'payment_id': customer.source_id, + 'payment_type': customer.source_type, + 'payment_date': customer.created_at, + 'amount': customer.amount + } + yield result #customer + + + + # for customer in self.client.users(): + # result = { + # 'email': customer.email, + # 'created_date': customer.created_at, + # 'first_name': customer.first_name, + # 'last_name': customer.last_name + # } + # yield customer + def fetch_subscriptions(self): for paying_member in self.client.subscriptions(): user=paying_member.user() diff --git a/mhackspace/templates/pages/home.html b/mhackspace/templates/pages/home.html index 3220ea9..61c4caf 100644 --- a/mhackspace/templates/pages/home.html +++ b/mhackspace/templates/pages/home.html @@ -4,5 +4,7 @@ {% block content %}

Introduction

Hackspaces are a shared space where artists, designers, makers, hackers, programmers, tinkerers, professionals and hobbyists can work on their projects, share knowledge and collaborate.We are in the process of developing Maidstone Hackspace. We're previous members of (ICMP) and looking to form a new space in the future. At the moment, communication is via google groups, email, and the website. If you're at all intrested please join our mailing list and make yourself known! + + Click here to chat with us https://hangouts.google.com/group/oDcAL0nDfQYfO3qq1 {% show_feeds %} {% endblock content %} diff --git a/mhackspace/templates/users/user_detail.html b/mhackspace/templates/users/user_detail.html index 054fb0e..f5954f2 100644 --- a/mhackspace/templates/users/user_detail.html +++ b/mhackspace/templates/users/user_detail.html @@ -36,7 +36,7 @@
-
Joined
+
Joined {{membership.start_date}}

MHS{{ user.id|stringformat:"05d" }}

{{user.name}}{{user.last_name}}

diff --git a/mhackspace/users/fixtures/groups.json b/mhackspace/users/fixtures/groups.json new file mode 100644 index 0000000..ab3af61 --- /dev/null +++ b/mhackspace/users/fixtures/groups.json @@ -0,0 +1,2 @@ +Postgres is up - continuing... +[{"model": "auth.group", "pk": 1, "fields": {"name": "members", "permissions": []}}] \ No newline at end of file