Пример #1
0
// GetSubscriptionForGroup gets the subscription of a group
func GetSubscriptionForGroup(groupName string) (*stripe.Sub, error) {
	group, err := modelhelper.GetGroup(groupName)
	if err != nil {
		return nil, err
	}

	if group.Payment.Subscription.ID == "" {
		return nil, ErrCustomerNotSubscribedToAnyPlans
	}

	return sub.Get(group.Payment.Subscription.ID, nil)
}
Пример #2
0
func TestAtTheEndOfTrialPeriodSubscriptionStatusIsStillTrialing(t *testing.T) {
	Convey("Given stub data", t, func() {
		withTestServer(t, func(endpoint string) {
			withStubData(endpoint, func(username, groupName, sessionID string) {
				withTestCreditCardToken(func(token string) {
					req, err := json.Marshal(&stripe.CustomerParams{
						Source: &stripe.SourceParams{
							Token: token,
						},
					})
					tests.ResultedWithNoErrorCheck(req, err)

					res, err := rest.DoRequestWithAuth(
						"POST",
						endpoint+EndpointCustomerUpdate,
						req,
						sessionID,
					)
					tests.ResultedWithNoErrorCheck(res, err)

					createURL := endpoint + EndpointSubscriptionCreate
					group, err := modelhelper.GetGroup(groupName)
					tests.ResultedWithNoErrorCheck(group, err)

					pp := &stripe.PlanParams{
						Amount:        12345,
						Interval:      stripeplan.Month,
						IntervalCount: 1,
						TrialPeriod:   1, // trial for one day
						Name:          fmt.Sprintf("plan for %s", username),
						Currency:      currency.USD,
						ID:            fmt.Sprintf("plan_for_%s", username),
						Statement:     "NAN-FREE",
					}
					plan, err := stripeplan.New(pp)
					So(err, ShouldBeNil)
					defer stripeplan.Del(plan.ID)

					req, err = json.Marshal(&stripe.SubParams{
						Customer: group.Payment.Customer.ID,
						Plan:     plan.ID,
					})
					tests.ResultedWithNoErrorCheck(req, err)

					res, err = rest.DoRequestWithAuth("POST", createURL, req, sessionID)
					tests.ResultedWithNoErrorCheck(res, err)

					sub := &stripe.Sub{}
					err = json.Unmarshal(res, sub)
					So(err, ShouldBeNil)

					subParams := &stripe.SubParams{
						Customer: group.Payment.Customer.ID,
						Plan:     plan.ID,
						TrialEnd: time.Now().UTC().Add(time.Second * 60 * 5).Unix(),
					}
					sub, err = stripesub.Update(sub.ID, subParams)
					tests.ResultedWithNoErrorCheck(sub, err)

					sub, err = stripesub.Get(sub.ID, nil)
					tests.ResultedWithNoErrorCheck(sub, err)

					So(sub.Status, ShouldEqual, "trialing")
				})
			})
		})
	})
}
Пример #3
0
// EnsureSubscriptionForGroup ensures subscription for a group
func EnsureSubscriptionForGroup(groupName string, params *stripe.SubParams) (*stripe.Sub, error) {
	if params == nil {
		params = &stripe.SubParams{
			Plan: Plans[UpTo10Users].ID,
		}
	}

	group, err := modelhelper.GetGroup(groupName)
	if err != nil {
		return nil, err
	}

	if group.Payment.Subscription.ID != "" {
		return sub.Get(group.Payment.Subscription.ID, nil)
	}

	if group.Payment.Customer.ID == "" {
		return nil, ErrCustomerNotExists
	}

	if err := CheckCustomerHasSource(group.Payment.Customer.ID); err != nil {
		return nil, err
	}

	now := time.Now().UTC()
	thirtyDaysLater := now.Add(30 * 24 * time.Hour).Unix()
	sevenDaysLater := now.Add(7 * 24 * time.Hour).Unix()

	if params.TrialEnd != 0 {
		// we only allow 0, 7 and 30 day trials
		if params.TrialEnd < sevenDaysLater {
			params.TrialEnd = sevenDaysLater
		}

		if params.TrialEnd > sevenDaysLater {
			params.TrialEnd = thirtyDaysLater
		}
	}

	// override quantity and plan in case we did not charge the user previously
	// due to failed payment and the subscription is deleted by stripe, create
	// new subscription
	quantity := uint64(1)
	activeCount, _ := (&socialapimodels.PresenceDaily{}).CountDistinctByGroupName(groupName)
	if activeCount != 0 {
		quantity = uint64(activeCount)
		params.Plan = GetPlanID(activeCount)
		params.TrialEnd = 0
	}

	// only send our whitelisted params
	req := &stripe.SubParams{
		Customer: group.Payment.Customer.ID,
		Quantity: quantity,
		Plan:     params.Plan,
		Coupon:   params.Coupon,
		Token:    params.Token,
		TrialEnd: params.TrialEnd,
		Card:     params.Card,
	}

	sub, err := sub.New(req)
	if err != nil {
		return nil, err
	}

	if err := syncGroupWithCustomerID(group.Payment.Customer.ID); err != nil {
		return nil, err
	}

	return sub, nil
}