def handle(self, *args, **options): provider = select_provider("gocardless") self.stdout.write( self.style.NOTICE("== Gocardless customer payments ==")) Payments.objects.all().delete() payment_objects = [] # TODO create a user if they do not exist for customer in provider.fetch_customers(): user = User.objects.get(email=customer.get("email")) payment_type = customer.get("payment_type") payment_objects.append( Payments( user=user, user_reference=customer.get("user_reference"), user_email=customer.get("email"), reference=customer.get("payment_id"), amount=customer.get("amount"), type=Payments.lookup_payment_type(payment_type), date=customer.get("payment_date"), )) md = model_to_dict(payment_objects[-1]) md["payment_type"] = payment_type self.stdout.write( self.style.SUCCESS( "\t{reference} - {amount} - {date} - {payment_type} - {user_email}" .format(**md))) Payments.objects.bulk_create(payment_objects)
def handle(self, *args, **options): provider = select_provider('gocardless') self.stdout.write( self.style.NOTICE('== Gocardless customer payments ==')) Payments.objects.all().delete() payment_objects = [] for customer in provider.fetch_customers(): user = User.objects.get(email=customer.get('email')) payment_objects.append( Payments(user=user, user_reference=customer.get('user_reference'), 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( self.style.SUCCESS( '\t{reference} - {amount} - {type} - {user_email}'.format( **model_to_dict(payment_objects[-1])))) Payments.objects.bulk_create(payment_objects)
def get_redirect_url(self, *args, **kwargs): payment_provider = 'gocardless' provider = select_provider(payment_provider) print(self.request.user) membership = Membership.objects.get(user=self.request.user) name = "Membership your membership id is MH%s" % membership.reference result = provider.confirm_subscription( membership=membership, session=self.request.session.session_key, provider_response=self.request.GET, name=name ) # if something went wrong return to profile with an error result['start_date'] = timezone.now() if result.get('success') is False: messages.add_message( self.request, messages.ERROR, 'Failure something went wrong activating your membership please contact us.') return super(MembershipJoinSuccessView, self).get_redirect_url(*args, **kwargs) del(kwargs['provider']) if create_or_update_membership(user=self.request.user, signup_details=result, complete=True) is True: messages.add_message( self.request, messages.SUCCESS, 'Success your membership should now be active') kwargs['username'] = self.request.user.get_username() return super(MembershipJoinSuccessView, self).get_redirect_url(*args, **kwargs)
def form_valid(self, form): payment_provider = 'gocardless' provider = select_provider(payment_provider) app_domain = provider.get_redirect_url() user_code = str(self.request.user.id).zfill(5) # settings.PAYMENT_PROVIDERS[payment_provider]['redirect_url'] form_subscription = MembershipJoinForm(data=self.request.POST) form_subscription.is_valid() result = { 'email': self.request.user.email, 'reference': user_code, 'amount': form_subscription.cleaned_data.get('amount', 20.00), 'start_date': timezone.now() } create_or_update_membership( user=self.request.user, signup_details=result, complete=False ) success_url = '%s/membership/%s/success' % (app_domain, payment_provider) failure_url = '%s/membership/%s/failure' % (app_domain, payment_provider) url = provider.create_subscription( user=self.request.user, session=self.request.session.session_key, amount=form_subscription.cleaned_data.get('amount', 20.00), name="Membership your membership id is MH%s" % user_code, redirect_success=success_url, redirect_failure=failure_url ) return redirect(url.redirect_url)
def handle(self, *args, **options): provider = select_provider('gocardless') self.stdout.write(self.style.NOTICE('== Gocardless subscriptions ==')) for sub in provider.fetch_subscriptions(): self.stdout.write( self.style.SUCCESS( '\t{start_date} {reference} - {amount} - {status} - {email}' .format(**sub)))
def update_subscriptions(provider_name): provider = select_provider('gocardless') print(provider) Membership.objects.all().delete() for sub in provider.fetch_subscriptions(): try: user_model = User.objects.get(email=sub.get('email')) except User.DoesNotExist: user_model = None create_or_update_membership(user=user_model, signup_details=sub, complete=True)
def get_redirect_url(self, *args, **kwargs): payment_provider = 'gocardless' provider = select_provider(payment_provider) member = Membership.objects.filter(user=self.request.user).first() result = provider.cancel_subscription( user=self.request.user, reference=member.reference ) if result.get('success') is True: cancel_membership(user=self.request.user) messages.add_message( self.request, messages.SUCCESS, 'Your membership has now been cancelled') kwargs['username'] = self.request.user.get_username() return super(MembershipCancelView, self).get_redirect_url(*args, **kwargs)
def handle(self, *args, **options): provider_name = 'gocardless' self.stdout.write( self.style.NOTICE('== %s subscriptions ==' % provider_name.capitalize())) provider = select_provider('gocardless') Membership.objects.all().delete() group = Group.objects.get(name='members') for sub in provider.fetch_subscriptions(): prefix = '' sub['amount'] = sub['amount'] * 0.01 try: user_model = User.objects.get(email=sub.get('email')) if sub.get('status') == 'active': user_model.groups.add(group) except User.DoesNotExist: user_model = None prefix = 'NO USER - ' create_or_update_membership(user=user_model, signup_details=sub, complete=True) message = '\t{prefix}{date} - {reference} - {payment} - {last_payment} - {status} - {email} '.format( **{ 'prefix': prefix, 'date': sub.get('start_date'), 'reference': sub.get('reference'), 'payment': sub.get('amount'), 'last_payment': sub.get('last_payment'), 'status': sub.get('status'), 'email': sub.get('email') }) if user_model: self.stdout.write(self.style.SUCCESS(message)) else: self.stdout.write(self.style.NOTICE(message))