def subscribe(customer, plan, flow): cu = customer.stripe_customer cu.update_subscription(plan=plan.stripe_id, prorate=False) try: current_subscription = customer.current_subscription current_subscription.plan = plan.stripe_id current_subscription.amount = plan.amount current_subscription.save() except CurrentSubscription.DoesNotExist: sub = cu.subscription cs = CurrentSubscription.objects.create( customer=customer, plan=plan.stripe_id, current_period_start=convert_tstamp(sub.current_period_start), current_period_end=convert_tstamp(sub.current_period_end), amount=sub.plan.amount, status=sub.status, cancel_at_period_end=sub.cancel_at_period_end, canceled_at=convert_tstamp(sub, 'canceled_at'), start=convert_tstamp(sub.start), quantity=sub.quantity) return cs
def create_subscription(customer, plan="basic"): return CurrentSubscription.objects.create( customer=customer, plan=plan, quantity=1, start=convert_tstamp(1395527780), amount=decimal.Decimal("100.00" if plan == "basic" else "1000.00"), status="trialing")
def test_conversion_with_field_name_no_tz(self): if settings.DJSTRIPE_TESTS_SKIP_UTC: raise SkipTest("UTC test skipped via command-line arg.") stamp = convert_tstamp({"my_date": 1365567407}, "my_date") self.assertEquals( stamp, datetime.datetime(2013, 4, 10, 4, 16, 47), "Is your system clock timezone in UTC? Change it, or run tests with '--skip-utc'." )
def create_subscription(customer, plan="basic"): return CurrentSubscription.objects.create( customer=customer, plan=plan, quantity=1, start=convert_tstamp(1395527780), amount=decimal.Decimal("100.00" if plan == "basic" else "1000.00"), status="trialing" )
def test_cancel_with_stripe_sub(self, StripeCustomerMock, CancelSubscriptionMock): StripeCustomerMock.return_value = convert_to_fake_stripe_object(DUMMY_CUSTOMER_WITH_SUB_BASIC) CancelSubscriptionMock.return_value = convert_to_fake_stripe_object(DUMMY_SUB_BASIC_CANCELED) create_subscription(self.customer) self.assertEqual(self.customer.current_subscription.status, "trialing") self.customer.cancel_subscription(at_period_end=False) self.assertEqual(self.customer.has_active_subscription(), False) self.assertEqual(self.customer.current_subscription.status, "canceled") self.assertEqual(self.customer.current_subscription.ended_at, None) self.assertEqual(self.customer.current_subscription.canceled_at, convert_tstamp(CANCELED_TIME))
def test_cancel_with_stripe_sub(self, StripeCustomerMock, CancelSubscriptionMock): StripeCustomerMock.return_value = convert_to_fake_stripe_object( DUMMY_CUSTOMER_WITH_SUB_BASIC) CancelSubscriptionMock.return_value = convert_to_fake_stripe_object( DUMMY_SUB_BASIC_CANCELED) create_subscription(self.customer) self.assertEqual(self.customer.current_subscription.status, "trialing") self.customer.cancel_subscription(at_period_end=False) self.assertEqual(self.customer.has_active_subscription(), False) self.assertEqual(self.customer.current_subscription.status, "canceled") self.assertEqual(self.customer.current_subscription.ended_at, None) self.assertEqual(self.customer.current_subscription.canceled_at, convert_tstamp(CANCELED_TIME))
def test_cancel_with_both_subs(self, StripeCustomerMock, SubscriptionDeleteMock): StripeCustomerMock.side_effect = [convert_to_fake_stripe_object(DUMMY_CUSTOMER_WITH_BOTH_SUBS), convert_to_fake_stripe_object(DUMMY_CUSTOMER_WITH_SUB_GOLD)] SubscriptionDeleteMock.return_value = convert_to_fake_stripe_object(DUMMY_SUB_BASIC_CANCELED) create_subscription(self.customer) create_subscription(self.customer, "gold") sub_basic = self.customer.subscriptions.get(plan="basic") self.assertEqual(sub_basic.status, "trialing") self.assertEqual(sub_basic.plan, "basic") self.customer.cancel_subscription(at_period_end=False, subscription=sub_basic) self.assertEqual(self.customer.subscriptions.count(), 2) self.assertEqual(sub_basic.status, "canceled") self.assertEqual(sub_basic.canceled_at, convert_tstamp(CANCELED_TIME)) sub_gold = self.customer.subscriptions.get(plan="gold") self.assertEqual(sub_gold.status, "trialing") # Now, after a synchronise, canceled subs will be removed. self.customer.sync_subscriptions() self.assertEqual(self.customer.subscriptions.count(), 1) with self.assertRaises(Subscription.DoesNotExist): self.customer.subscriptions.get(plan="basic")
def test_conversion_with_invalid_field_name_no_tz(self): stamp = convert_tstamp({"my_date": 1365567407}, "foo") self.assertEquals( stamp, None, )
def test_conversion_with_field_name(self): stamp = convert_tstamp({"my_date": 1365567407}, "my_date") self.assertEquals( stamp, datetime.datetime(2013, 4, 10, 4, 16, 47, tzinfo=timezone.utc))
def test_conversion_with_invalid_field_name_no_tz(self): stamp = convert_tstamp({"my_date": 1365567407}, "foo") self.assertEquals( stamp, None )
def test_conversion_with_field_name_no_tz(self): stamp = convert_tstamp({"my_date": 1365567407}, "my_date") self.assertEquals( stamp, datetime.datetime(2013, 4, 10, 4, 16, 47) )
def test_conversion_without_field_name_no_tz(self): stamp = convert_tstamp(1365567407) self.assertEquals( stamp, datetime.datetime(2013, 4, 10, 4, 16, 47) )
def test_conversion_with_field_name(self): stamp = convert_tstamp({"my_date": 1365567407}, "my_date") self.assertEquals( stamp, datetime.datetime(2013, 4, 10, 4, 16, 47, tzinfo=timezone.utc) )
def test_conversion_with_field_name_no_tz(self): stamp = convert_tstamp({"my_date": 1365567407}, "my_date") self.assertEquals(stamp, datetime.datetime(2013, 4, 10, 4, 16, 47))
def test_conversion_without_field_name_no_tz(self): stamp = convert_tstamp(1365567407) self.assertEquals(stamp, datetime.datetime(2013, 4, 10, 4, 16, 47))