def sync_current_subscription(self, cu=None): cu = cu or self.stripe_customer sub = cu.subscription if sub: try: sub_obj = self.current_subscription sub_obj.plan = plan_from_stripe_id(sub.plan.id) sub_obj.current_period_start = convert_tstamp( sub.current_period_start) sub_obj.current_period_end = convert_tstamp( sub.current_period_end) sub_obj.amount = (sub.plan.amount / decimal.Decimal("100.0")) sub_obj.status = sub.status sub_obj.start = convert_tstamp(sub.start) sub_obj.quantity = sub.quantity sub_obj.save() except CurrentSubscription.DoesNotExist: sub_obj = CurrentSubscription.objects.create( customer=self, plan=plan_from_stripe_id(sub.plan.id), current_period_start=convert_tstamp( sub.current_period_start), current_period_end=convert_tstamp(sub.current_period_end), amount=(sub.plan.amount / decimal.Decimal("100.0")), status=sub.status, start=convert_tstamp(sub.start), quantity=sub.quantity) if sub.trial_start and sub.trial_end: sub_obj.trial_start = convert_tstamp(sub.trial_start) sub_obj.trial_end = convert_tstamp(sub.trial_end) sub_obj.save() return sub_obj
def sync_current_subscription(self, cu=None): cu = cu or self.stripe_customer sub = cu.subscription if sub: try: sub_obj = self.current_subscription sub_obj.plan = plan_from_stripe_id(sub.plan.id) sub_obj.current_period_start = convert_tstamp(sub.current_period_start) sub_obj.current_period_end = convert_tstamp(sub.current_period_end) sub_obj.amount = (sub.plan.amount / 100.0) sub_obj.status = sub.status sub_obj.start = convert_tstamp(sub.start) sub_obj.quantity = sub.quantity sub_obj.save() except CurrentSubscription.DoesNotExist: sub_obj = CurrentSubscription.objects.create( customer=self, plan=plan_from_stripe_id(sub.plan.id), current_period_start=convert_tstamp(sub.current_period_start), current_period_end=convert_tstamp(sub.current_period_end), amount=(sub.plan.amount / 100.0), status=sub.status, start=convert_tstamp(sub.start), quantity=sub.quantity ) if sub.trial_start and sub.trial_end: sub_obj.trial_start = convert_tstamp(sub.trial_start) sub_obj.trial_end = convert_tstamp(sub.trial_end) sub_obj.save() return sub_obj
def update_plan_quantity(self, quantity, charge_immediately=False): self.subscribe( plan=plan_from_stripe_id( self.stripe_customer.subscription.plan.id ), quantity=quantity, charge_immediately=charge_immediately )
def create_from_stripe_data(cls, stripe_invoice): if not cls.objects.filter(stripe_id=stripe_invoice["id"]).exists(): c = Customer.objects.get(stripe_id=stripe_invoice["customer"]) period_end = convert_tstamp(stripe_invoice, "period_end") period_start = convert_tstamp(stripe_invoice, "period_start") date = convert_tstamp(stripe_invoice, "date") invoice = c.invoices.create( attempted=stripe_invoice["attempted"], closed=stripe_invoice["closed"], paid=stripe_invoice["paid"], period_end=period_end, period_start=period_start, subtotal=stripe_invoice["subtotal"] / 100.0, total=stripe_invoice["total"] / 100.0, date=date, charge=stripe_invoice.get("charge") or "", stripe_id=stripe_invoice["id"] ) for item in stripe_invoice["lines"].get("invoiceitems", []): invoice.items.get_or_create( stripe_id=item["id"], amount=(item["amount"] / 100.0), date=convert_tstamp(item, "date"), description=item["description"] ) for sub in stripe_invoice["lines"].get("subscriptions", []): period_end = convert_tstamp(sub["period"], "end") period_start = convert_tstamp(sub["period"], "start") subscription, created = Subscription.objects.get_or_create( plan=plan_from_stripe_id(sub["plan"]["id"]), customer=c, period_start=period_start, period_end=period_end, defaults={ "amount": (sub["amount"] / 100.0) } ) subscription.status = c.stripe_customer.subscription.status subscription.amount = (sub["amount"] / 100.0) subscription.save() # @@@ what happens if the same sub.pk gets added twice? invoice.subscriptions.add(subscription) if stripe_invoice.get("charge"): charge = stripe.Charge.retrieve(stripe_invoice["charge"]) desc = stripe_invoice["lines"]["subscriptions"][0]["plan"]["name"] obj = c.record_charge(charge) obj.invoice = invoice obj.description = desc obj.save() obj.send_receipt() return invoice
def sync_current_subscription(self, cu=None): cu = cu or self.stripe_customer sub = getattr(cu, "subscription", None) if sub is None: try: self.current_subscription.delete() except CurrentSubscription.DoesNotExist: pass else: try: sub_obj = self.current_subscription sub_obj.plan = plan_from_stripe_id(sub.plan.id) sub_obj.current_period_start = convert_tstamp(sub.current_period_start) sub_obj.current_period_end = convert_tstamp(sub.current_period_end) sub_obj.amount = sub.plan.amount / decimal.Decimal("100") sub_obj.status = sub.status sub_obj.cancel_at_period_end = sub.cancel_at_period_end sub_obj.start = convert_tstamp(sub.start) sub_obj.quantity = sub.quantity sub_obj.save() except CurrentSubscription.DoesNotExist: sub_obj = CurrentSubscription.objects.create( customer=self, plan=plan_from_stripe_id(sub.plan.id), current_period_start=convert_tstamp(sub.current_period_start), current_period_end=convert_tstamp(sub.current_period_end), amount=(sub.plan.amount / decimal.Decimal("100")), status=sub.status, cancel_at_period_end=sub.cancel_at_period_end, start=convert_tstamp(sub.start), quantity=sub.quantity, ) if sub.trial_start and sub.trial_end: sub_obj.trial_start = convert_tstamp(sub.trial_start) sub_obj.trial_end = convert_tstamp(sub.trial_end) sub_obj.save() return sub_obj
def create_from_stripe_data(cls, stripe_invoice, send_receipt=True): if not cls.objects.filter(stripe_id=stripe_invoice["id"]).exists(): c = Customer.objects.get(stripe_id=stripe_invoice["customer"]) period_end = convert_tstamp(stripe_invoice, "period_end") period_start = convert_tstamp(stripe_invoice, "period_start") date = convert_tstamp(stripe_invoice, "date") invoice = c.invoices.create( attempted=stripe_invoice["attempted"], closed=stripe_invoice["closed"], paid=stripe_invoice["paid"], period_end=period_end, period_start=period_start, subtotal=stripe_invoice["subtotal"] / 100.0, total=stripe_invoice["total"] / 100.0, date=date, charge=stripe_invoice.get("charge") or "", stripe_id=stripe_invoice["id"] ) for item in stripe_invoice["lines"].get("data", []): period_end = convert_tstamp(item["period"], "end") period_start = convert_tstamp(item["period"], "start") if item.get("plan"): plan = plan_from_stripe_id(item["plan"]["id"]) else: plan = "" invoice.items.create( stripe_id=item["id"], amount=(item["amount"] / 100.0), currency=item["currency"], proration=item["proration"], description=item.get("description") or "", line_type=item["type"], plan=plan, period_start=period_start, period_end=period_end, quantity=item.get("quantity") ) if stripe_invoice.get("charge"): obj = c.record_charge(stripe_invoice["charge"]) obj.invoice = invoice obj.save() if send_receipt: obj.send_receipt() return invoice
def purchase(self, plan, trial_days=None): cu = self.stripe_customer if settings.PAYMENTS_PLANS[plan].get("stripe_plan_id"): if trial_days: resp = cu.update_subscription( plan=PAYMENTS_PLANS[plan]["stripe_plan_id"], trial_end=timezone.now() + datetime.timedelta(days=trial_days) ) else: resp = cu.update_subscription( plan=PAYMENTS_PLANS[plan]["stripe_plan_id"] ) sub_obj, created = self.subscriptions.get_or_create( plan=plan_from_stripe_id(resp["plan"]["id"]), period_start=convert_tstamp(resp, "current_period_start"), period_end=convert_tstamp(resp, "current_period_end"), defaults={ "amount": (resp["plan"]["amount"] / 100.0), "status": resp["status"] } ) if resp.get("trial_start") and resp.get("trial_end"): sub_obj.trial_period_start = convert_tstamp(resp, "trial_start") sub_obj.trial_period_end = convert_tstamp(resp, "trial_end") sub_obj.save() if not created: sub_obj.amount = (resp["plan"]["amount"] / 100.0) sub_obj.status = resp["status"] sub_obj.save() else: # It's just a single transaction resp = stripe.Charge.create( amount=PAYMENTS_PLANS[plan]["price"] * 100, currency="usd", customer=self.stripe_id, description=PAYMENTS_PLANS[plan]["name"] ) obj = self.record_charge(resp["id"]) obj.description = resp["description"] obj.save() obj.send_receipt() self.plan = plan self.save() purchase_made.send(sender=self, plan=plan, stripe_response=resp)
def purchase(self, plan, trial_days=None): cu = self.stripe_customer if settings.PAYMENTS_PLANS[plan].get("stripe_plan_id"): if trial_days: resp = cu.update_subscription( plan=PAYMENTS_PLANS[plan]["stripe_plan_id"], trial_end=timezone.now() + datetime.timedelta(days=trial_days) ) else: resp = cu.update_subscription( plan=PAYMENTS_PLANS[plan]["stripe_plan_id"] ) sub_obj, created = self.subscriptions.get_or_create( plan=plan_from_stripe_id(resp["plan"]["id"]), period_start=convert_tstamp(resp, "current_period_start"), period_end=convert_tstamp(resp, "current_period_end"), defaults={ "amount": (resp["plan"]["amount"] / 100.0), "status": resp["status"] } ) if resp.get("trial_start") and resp.get("trial_end"): sub_obj.trial_period_start = convert_tstamp(resp, "trial_start") sub_obj.trial_period_end = convert_tstamp(resp, "trial_end") sub_obj.save() if not created: sub_obj.amount = (resp["plan"]["amount"] / 100.0) sub_obj.status = resp["status"] sub_obj.save() else: # It's just a single transaction resp = stripe.Charge.create( amount=PAYMENTS_PLANS[plan]["price"] * 100, currency="usd", customer=self.stripe_id, description=PAYMENTS_PLANS[plan]["name"] ) obj = self.record_charge(resp) obj.description = resp["description"] obj.save() obj.send_receipt() self.plan = plan self.save() purchase_made.send(sender=self, plan=plan, stripe_response=resp)
def create_from_stripe_data(cls, stripe_invoice, send_receipt=True): if not cls.objects.filter(stripe_id=stripe_invoice["id"]).exists(): c = Customer.objects.get(stripe_id=stripe_invoice["customer"]) period_end = convert_tstamp(stripe_invoice, "period_end") period_start = convert_tstamp(stripe_invoice, "period_start") date = convert_tstamp(stripe_invoice, "date") invoice = c.invoices.create( attempted=stripe_invoice["attempted"], closed=stripe_invoice["closed"], paid=stripe_invoice["paid"], period_end=period_end, period_start=period_start, subtotal=stripe_invoice["subtotal"] / 100.0, total=stripe_invoice["total"] / 100.0, date=date, charge=stripe_invoice.get("charge") or "", stripe_id=stripe_invoice["id"]) for item in stripe_invoice["lines"].get("data", []): period_end = convert_tstamp(item["period"], "end") period_start = convert_tstamp(item["period"], "start") if item.get("plan"): plan = plan_from_stripe_id(item["plan"]["id"]) else: plan = "" invoice.items.create(stripe_id=item["id"], amount=(item["amount"] / 100.0), currency=item["currency"], proration=item["proration"], description=item.get("description") or "", line_type=item["type"], plan=plan, period_start=period_start, period_end=period_end, quantity=item.get("quantity")) if stripe_invoice.get("charge"): obj = c.record_charge(stripe_invoice["charge"]) obj.invoice = invoice obj.save() if send_receipt: obj.send_receipt() return invoice
def sync_from_stripe_data(cls, stripe_invoice, send_receipt=True): c = Customer.objects.get(stripe_id=stripe_invoice["customer"]) period_end = convert_tstamp(stripe_invoice, "period_end") period_start = convert_tstamp(stripe_invoice, "period_start") date = convert_tstamp(stripe_invoice, "date") invoice, created = cls.objects.get_or_create( stripe_id=stripe_invoice["id"], defaults=dict( customer=c, attempted=stripe_invoice["attempted"], closed=stripe_invoice["closed"], paid=stripe_invoice["paid"], period_end=period_end, period_start=period_start, subtotal=stripe_invoice["subtotal"] / decimal.Decimal("100"), total=stripe_invoice["total"] / decimal.Decimal("100"), date=date, charge=stripe_invoice.get("charge") or "" ) ) if not created: # pylint: disable=C0301 invoice.attempted = stripe_invoice["attempted"] invoice.closed = stripe_invoice["closed"] invoice.paid = stripe_invoice["paid"] invoice.period_end = period_end invoice.period_start = period_start invoice.subtotal = stripe_invoice["subtotal"] / decimal.Decimal("100") invoice.total = stripe_invoice["total"] / decimal.Decimal("100") invoice.date = date invoice.charge = stripe_invoice.get("charge") or "" invoice.save() for item in stripe_invoice["lines"].get("data", []): period_end = convert_tstamp(item["period"], "end") period_start = convert_tstamp(item["period"], "start") if item.get("plan"): plan = plan_from_stripe_id(item["plan"]["id"]) else: plan = "" inv_item, inv_item_created = invoice.items.get_or_create( stripe_id=item["id"], defaults=dict( amount=(item["amount"] / decimal.Decimal("100")), currency=item["currency"], proration=item["proration"], description=item.get("description") or "", line_type=item["type"], plan=plan, period_start=period_start, period_end=period_end, quantity=item.get("quantity") ) ) if not inv_item_created: inv_item.amount = (item["amount"] / decimal.Decimal("100")) inv_item.currency = item["currency"] inv_item.proration = item["proration"] inv_item.description = item.get("description") or "" inv_item.line_type = item["type"] inv_item.plan = plan inv_item.period_start = period_start inv_item.period_end = period_end inv_item.quantity = item.get("quantity") inv_item.save() if stripe_invoice.get("charge"): obj = c.record_charge(stripe_invoice["charge"]) obj.invoice = invoice obj.save() if send_receipt: obj.send_receipt() return invoice
def sync_from_stripe_data(cls, stripe_invoice, send_receipt=True): c = Customer.objects.get(stripe_id=stripe_invoice["customer"]) period_end = convert_tstamp(stripe_invoice, "period_end") period_start = convert_tstamp(stripe_invoice, "period_start") date = convert_tstamp(stripe_invoice, "date") invoice, created = cls.objects.get_or_create( stripe_id=stripe_invoice["id"], defaults=dict( customer=c, attempted=stripe_invoice["attempted"], closed=stripe_invoice["closed"], paid=stripe_invoice["paid"], period_end=period_end, period_start=period_start, subtotal=stripe_invoice["subtotal"] / decimal.Decimal("100.0"), total=stripe_invoice["total"] / decimal.Decimal("100.0"), date=date, charge=stripe_invoice.get("charge") or "")) if not created: # pylint: disable=C0301 invoice.attempted = stripe_invoice["attempted"] invoice.closed = stripe_invoice["closed"] invoice.paid = stripe_invoice["paid"] invoice.period_end = period_end invoice.period_start = period_start invoice.subtotal = stripe_invoice["subtotal"] / decimal.Decimal( "100.0") invoice.total = stripe_invoice["total"] / decimal.Decimal("100.0") invoice.date = date invoice.charge = stripe_invoice.get("charge") or "" invoice.save() for item in stripe_invoice["lines"].get("data", []): period_end = convert_tstamp(item["period"], "end") period_start = convert_tstamp(item["period"], "start") if item.get("plan"): plan = plan_from_stripe_id(item["plan"]["id"]) else: plan = "" inv_item, inv_item_created = invoice.items.get_or_create( stripe_id=item["id"], defaults=dict(amount=(item["amount"] / decimal.Decimal("100.0")), currency=item["currency"], proration=item["proration"], description=item.get("description") or "", line_type=item["type"], plan=plan, period_start=period_start, period_end=period_end, quantity=item.get("quantity"))) if not inv_item_created: inv_item.amount = (item["amount"] / decimal.Decimal("100.0")) inv_item.currency = item["currency"] inv_item.proration = item["proration"] inv_item.description = item.get("description") or "" inv_item.line_type = item["type"] inv_item.plan = plan inv_item.period_start = period_start inv_item.period_end = period_end inv_item.quantity = item.get("quantity") inv_item.save() if stripe_invoice.get("charge"): obj = c.record_charge(stripe_invoice["charge"]) obj.invoice = invoice obj.save() if send_receipt: obj.send_receipt() return invoice