def handle(self, *args, **options): existed_ids = [] for plan in Plan.api_list(): stripe_id = plan.get('id') if Plan.objects.filter(stripe_id=stripe_id).exists(): print('Plan id=%s updated.' % stripe_id) else: print('Plan id=%s created.' % stripe_id) existed_ids.append(stripe_id) Plan.sync_from_stripe_data(plan)
def remote_plan_cleanup(): """Go through the remote plans and remove thos that are not in the local list of plans.""" for remote_plan in Plan.api_list(): if remote_plan['nickname'] in PLAN_NAMES: active = True else: active = False stripe.Plan.modify(remote_plan['id'], api_key=djstripe_settings.STRIPE_SECRET_KEY, active=active) LOGGER.info('Remote Plan %s made %s', remote_plan['nickname'], 'active' if active else 'inactive')
def setup_remote_plan(product: stripe.Product, plan_data: dict) -> stripe.Product: plan_nickname = product_name_to_plan_name(product['name']) plan_to_save = None create = True for remote_plan in Plan.api_list(): if remote_plan['nickname'] == plan_nickname: plan_to_save = remote_plan create = False break if plan_to_save is None: plan_to_save = {} plan_to_save['interval'] = 'month' plan_to_save['nickname'] = plan_nickname plan_to_save['currency'] = DEFAULT_CURRENCY plan_to_save['amount_decimal'] = plan_data[PlanDefinitionKey.PRICE] if create: plan_to_save['product'] = product['id'] return stripe.Plan.create(api_key=djstripe_settings.STRIPE_SECRET_KEY, **plan_to_save) for k in ('amount', 'object', 'created', 'livemode', 'amount_decimal', 'billing_scheme', 'currency', 'interval', 'interval_count', 'usage_type'): if k in plan_to_save: # These keys might come back from server but can't be written del plan_to_save[k] sid = plan_to_save['id'] del plan_to_save['id'] if plan_to_save['product'] != product['id']: stripe.Plan.modify(sid, api_key=djstripe_settings.STRIPE_SECRET_KEY, product=product['id']) del plan_to_save['product'] return stripe.Plan.modify(sid, api_key=djstripe_settings.STRIPE_SECRET_KEY, **plan_to_save)