CCrypto VPN public website https://vpn.ccrypto.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

71 lines
2.4 KiB

  1. from django.core.management.base import BaseCommand, CommandError
  2. from django.conf import settings
  3. from ccvpn.common import get_price
  4. from payments.models import ACTIVE_BACKENDS, SUBSCR_PERIOD_CHOICES, period_months
  5. CURRENCY_CODE, CURRENCY_NAME = settings.PAYMENTS_CURRENCY
  6. class Command(BaseCommand):
  7. help = "Update Stripe plans"
  8. def add_arguments(self, parser):
  9. parser.add_argument('--force-run', action='store_true',
  10. help="Run even when Stripe backend is disabled")
  11. parser.add_argument('--force-update', action='store_true',
  12. help="Replace plans, including matching ones")
  13. def handle(self, *args, **options):
  14. if 'stripe' not in ACTIVE_BACKENDS and options['force-run'] is False:
  15. raise CommandError("stripe backend not active.")
  16. backend = ACTIVE_BACKENDS['stripe']
  17. stripe = backend.stripe
  18. for period_id, period_name in SUBSCR_PERIOD_CHOICES:
  19. plan_id = backend.get_plan_id(period_id)
  20. months = period_months(period_id)
  21. amount = months * get_price()
  22. kwargs = dict(
  23. id=plan_id,
  24. amount=amount,
  25. interval='month',
  26. interval_count=months,
  27. name=backend.name + " (%s)" % period_id,
  28. currency=CURRENCY_CODE,
  29. )
  30. self.stdout.write('Plan %s: %d months for %.2f %s (%s)... ' % (
  31. plan_id, months, amount / 100, CURRENCY_NAME, CURRENCY_CODE), ending='')
  32. self.stdout.flush()
  33. try:
  34. plan = stripe.Plan.retrieve(plan_id)
  35. except stripe.error.InvalidRequestError:
  36. plan = None
  37. def is_valid_plan():
  38. if not plan:
  39. return False
  40. for k, v in kwargs.items():
  41. if getattr(plan, k) != v:
  42. return False
  43. return True
  44. if plan:
  45. if is_valid_plan() and not options['force_update']:
  46. self.stdout.write(self.style.SUCCESS('[ok]'))
  47. continue
  48. plan.delete()
  49. update = True
  50. else:
  51. update = False
  52. stripe.Plan.create(**kwargs)
  53. if update:
  54. self.stdout.write(self.style.WARNING('[updated]'))
  55. else:
  56. self.stdout.write(self.style.WARNING('[created]'))