Recurring payments!
parent
957737be1a
commit
ffa2f00f67
@ -0,0 +1,70 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.conf import settings
|
||||
|
||||
from payments.models import ACTIVE_BACKENDS, SUBSCR_PERIOD_CHOICES, period_months
|
||||
|
||||
CURRENCY_CODE, CURRENCY_NAME = settings.PAYMENTS_CURRENCY
|
||||
MONTHLY_PRICE = settings.PAYMENTS_MONTHLY_PRICE
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Update Stripe plans"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--force-run', action='store_true',
|
||||
help="Run even when Stripe backend is disabled")
|
||||
parser.add_argument('--force-update', action='store_true',
|
||||
help="Replace plans, including matching ones")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if 'stripe' not in ACTIVE_BACKENDS and options['force-run'] is False:
|
||||
raise CommandError("stripe backend not active.")
|
||||
|
||||
backend = ACTIVE_BACKENDS['stripe']
|
||||
stripe = backend.stripe
|
||||
|
||||
for period_id, period_name in SUBSCR_PERIOD_CHOICES:
|
||||
plan_id = backend.get_plan_id(period_id)
|
||||
months = period_months(period_id)
|
||||
amount = months * MONTHLY_PRICE
|
||||
|
||||
kwargs = dict(
|
||||
id=plan_id,
|
||||
amount=months * MONTHLY_PRICE,
|
||||
interval='month',
|
||||
interval_count=months,
|
||||
name=backend.name + " (%s)" % period_id,
|
||||
currency=CURRENCY_CODE,
|
||||
)
|
||||
|
||||
self.stdout.write('Plan %s: %d months for %.2f %s (%s)... ' % (
|
||||
plan_id, months, amount / 100, CURRENCY_NAME, CURRENCY_CODE), ending='')
|
||||
self.stdout.flush()
|
||||
|
||||
try:
|
||||
plan = stripe.Plan.retrieve(plan_id)
|
||||
except stripe.error.InvalidRequestError:
|
||||
plan = None
|
||||
|
||||
def is_valid_plan():
|
||||
if not plan:
|
||||
return False
|
||||
for k, v in kwargs.items():
|
||||
if getattr(plan, k) != v:
|
||||
return False
|
||||
return True
|
||||
|
||||
if plan:
|
||||
if is_valid_plan() and not options['force_update']:
|
||||
self.stdout.write(self.style.SUCCESS('[ok]'))
|
||||
continue
|
||||
plan.delete()
|
||||
update = True
|
||||
else:
|
||||
update = False
|
||||
|
||||
stripe.Plan.create(**kwargs)
|
||||
if update:
|
||||
self.stdout.write(self.style.WARNING('[updated]'))
|
||||
else:
|
||||
self.stdout.write(self.style.WARNING('[created]'))
|
@ -0,0 +1,49 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.5 on 2016-09-04 00:48
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import jsonfield.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('payments', '0003_auto_20151209_0440'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Subscription',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('backend_id', models.CharField(choices=[('bitcoin', 'Bitcoin'), ('coinbase', 'Coinbase'), ('manual', 'Manual'), ('paypal', 'PayPal'), ('stripe', 'Stripe')], max_length=16)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('period', models.CharField(choices=[('3m', 'Every 3 months'), ('6m', 'Every 6 months'), ('12m', 'Every year')], max_length=16)),
|
||||
('last_confirmed_payment', models.DateTimeField(blank=True, null=True)),
|
||||
('status', models.CharField(choices=[('new', 'Waiting for payment'), ('active', 'Active'), ('cancelled', 'Cancelled')], default='new', max_length=16)),
|
||||
('backend_extid', models.CharField(blank=True, max_length=64, null=True)),
|
||||
('backend_data', jsonfield.fields.JSONField(blank=True, default=dict)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='recurringpaymentsource',
|
||||
name='user',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='payment',
|
||||
name='recurring_source',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='RecurringPaymentSource',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='payment',
|
||||
name='subscription',
|
||||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='payments.Subscription'),
|
||||
),
|
||||
]
|
Loading…
Reference in New Issue