Exemplo n.º 1
0
// EnsurePlan makes sure plan is in stripe
func EnsurePlan(planParams *stripe.PlanParams) error {
	p, err := stripeplan.Get(planParams.ID, nil)
	if err == nil && p != nil && p.ID == planParams.ID {
		return nil
	}

	stripeErr, ok := err.(*stripe.Error)
	if !ok {
		return err
	}

	if stripeErr.Type != stripe.ErrorTypeInvalidRequest {
		return err
	}

	_, err = stripeplan.New(planParams)
	return err
}
Exemplo n.º 2
0
func getPlan(subscription *stripe.Sub, totalCount int) (*stripe.Plan, error) {
	plan := subscription.Plan

	expectedPlanID := GetPlanID(totalCount)
	// in the cases where the active subscription and the have-to-be
	// subscription is different, fetch the real plan from system. This can only
	// happen if the team got more members than the previous subscription's user
	// count in the current month. The subscription will be automatically fixed
	// on the next billing date. We do not change the subscription on each user
	// addition or deletion becasue Stripe charges the user whenever a
	// subscription change happens, so we only change the subscription on the
	// billing date with cancelling the previous subscription & invoice and
	// creating a new subscription with new requirement
	if plan.ID == expectedPlanID {
		return plan, nil
	}

	return stripeplan.Get(expectedPlanID, nil)
}
Exemplo n.º 3
0
func TestCreateDefaultPlans(t *testing.T) {
	tests.WithConfiguration(t, func(c *config.Config) {
		stripe.Key = c.Stripe.SecretToken

		Convey("Given default plans object", t, func() {
			So(CreateDefaultPlans(), ShouldBeNil)

			Convey("Plans should be in Stripe", func() {
				for _, plan := range Plans {
					_, err := stripePlan.Get(plan.ID, nil)
					So(err, ShouldBeNil)
				}
			})

			Convey("Trying to create them again should not return error", func() {
				So(CreateDefaultPlans(), ShouldBeNil)
			})
		})
	})
}