Ejemplo n.º 1
0
func (c Client) New(params *stripe.SubParams) (*stripe.Sub, error) {
	var body *stripe.RequestValues
	var commonParams *stripe.Params
	token := c.Key

	if params != nil {
		body = &stripe.RequestValues{}
		body.Add("plan", params.Plan)
		body.Add("customer", params.Customer)

		if len(params.Token) > 0 {
			body.Add("card", params.Token)
		} else if params.Card != nil {
			params.Card.AppendDetails(body, true)
		}

		if len(params.Coupon) > 0 {
			body.Add("coupon", params.Coupon)
		}

		if params.TrialEndNow {
			body.Add("trial_end", "now")
		} else if params.TrialEnd > 0 {
			body.Add("trial_end", strconv.FormatInt(params.TrialEnd, 10))
		}

		if params.TaxPercent > 0 {
			body.Add("tax_percent", strconv.FormatFloat(params.TaxPercent, 'f', 2, 64))
		} else if params.TaxPercentZero {
			body.Add("tax_percent", "0")
		}

		if params.Quantity > 0 {
			body.Add("quantity", strconv.FormatUint(params.Quantity, 10))
		} else if params.QuantityZero {
			body.Add("quantity", "0")
		}

		if params.FeePercent > 0 {
			body.Add("application_fee_percent", strconv.FormatFloat(params.FeePercent, 'f', 2, 64))
		}

		if params.BillingCycleAnchorNow {
			body.Add("billing_cycle_anchor", "now")
		} else if params.BillingCycleAnchor > 0 {
			body.Add("billing_cycle_anchor", strconv.FormatInt(params.BillingCycleAnchor, 10))
		}

		commonParams = &params.Params

		params.AppendTo(body)
	}

	sub := &stripe.Sub{}
	err := c.B.Call("POST", "/subscriptions", token, body, commonParams, sub)

	return sub, err
}
Ejemplo n.º 2
0
func (c Client) Update(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	body := &url.Values{}

	if len(params.Plan) > 0 {
		body.Add("plan", params.Plan)
	}

	if params.NoProrate {
		body.Add("prorate", strconv.FormatBool(false))
	}

	if len(params.Token) > 0 {
		body.Add("card", params.Token)
	} else if params.Card != nil {
		if len(params.Card.Token) > 0 {
			body.Add("card", params.Card.Token)
		} else {
			params.Card.AppendDetails(body, true)
		}
	}

	if len(params.Coupon) > 0 {
		body.Add("coupon", params.Coupon)
	}

	if params.TrialEndNow {
		body.Add("trial_end", "now")
	} else if params.TrialEnd > 0 {
		body.Add("trial_end", strconv.FormatInt(params.TrialEnd, 10))
	}

	if params.Quantity > 0 {
		body.Add("quantity", strconv.FormatUint(params.Quantity, 10))
	}

	token := c.Key
	if params.FeePercent > 0 {
		body.Add("application_fee_percent", strconv.FormatFloat(params.FeePercent, 'f', 2, 64))
	}

	if params.TaxPercent > 0 {
		body.Add("tax_percent", strconv.FormatFloat(params.TaxPercent, 'f', 2, 64))
	}

	if params.ProrationDate > 0 {
		body.Add("proration_date", strconv.FormatInt(params.ProrationDate, 10))
	}

	params.AppendTo(body)

	sub := &stripe.Sub{}
	err := c.B.Call("POST", fmt.Sprintf("/customers/%v/subscriptions/%v", params.Customer, id), token, body, &params.Params, sub)

	return sub, err
}
Ejemplo n.º 3
0
func (c Client) Cancel(id string, params *stripe.SubParams) error {
	body := &url.Values{}

	if params.EndCancel {
		body.Add("at_period_end", strconv.FormatBool(true))
	}

	params.AppendTo(body)

	return c.B.Call("DELETE", fmt.Sprintf("/customers/%v/subscriptions/%v", params.Customer, id), c.Key, body, &params.Params, nil)
}
Ejemplo n.º 4
0
func (c Client) Get(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	if params == nil {
		return nil, fmt.Errorf("params cannot be nil, and params.Customer must be set")
	}

	body := &url.Values{}
	params.AppendTo(body)

	sub := &stripe.Sub{}
	err := c.B.Call("GET", fmt.Sprintf("/customers/%v/subscriptions/%v", params.Customer, id), c.Key, body, &params.Params, sub)

	return sub, err
}
Ejemplo n.º 5
0
func (c Client) New(params *stripe.SubParams) (*stripe.Sub, error) {
	body := &url.Values{
		"plan": {params.Plan},
	}

	if len(params.Token) > 0 {
		body.Add("card", params.Token)
	} else if params.Card != nil {
		params.Card.AppendDetails(body, true)
	}

	if len(params.Coupon) > 0 {
		body.Add("coupon", params.Coupon)
	}

	if params.TrialEndNow {
		body.Add("trial_end", "now")
	} else if params.TrialEnd > 0 {
		body.Add("trial_end", strconv.FormatInt(params.TrialEnd, 10))
	}

	if params.Quantity > 0 {
		body.Add("quantity", strconv.FormatUint(params.Quantity, 10))
	} else if params.QuantityZero {
		body.Add("quantity", "0")
	}

	token := c.Key
	if params.FeePercent > 0 {
		body.Add("application_fee_percent", strconv.FormatFloat(params.FeePercent, 'f', 2, 64))
	}

	if params.TaxPercent > 0 {
		body.Add("tax_percent", strconv.FormatFloat(params.TaxPercent, 'f', 2, 64))
	}

	if params.BillingCycleAnchorNow {
		body.Add("billing_cycle_anchor", "now")
	} else if params.BillingCycleAnchor > 0 {
		body.Add("billing_cycle_anchor", strconv.FormatInt(params.BillingCycleAnchor, 10))
	}

	params.AppendTo(body)

	sub := &stripe.Sub{}
	err := c.B.Call("POST", fmt.Sprintf("/customers/%v/subscriptions", params.Customer), token, body, &params.Params, sub)

	return sub, err
}
Ejemplo n.º 6
0
func (c Client) Get(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	var body *stripe.RequestValues
	var commonParams *stripe.Params

	if params != nil {
		body = &stripe.RequestValues{}
		params.AppendTo(body)
		commonParams = &params.Params
	}

	sub := &stripe.Sub{}
	err := c.B.Call("GET", fmt.Sprintf("/subscriptions/%v", id), c.Key, body, commonParams, sub)

	return sub, err
}
Ejemplo n.º 7
0
func (c Client) Cancel(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	var body *stripe.RequestValues
	var commonParams *stripe.Params

	if params != nil {
		body = &stripe.RequestValues{}

		if params.EndCancel {
			body.Add("at_period_end", strconv.FormatBool(true))
		}

		params.AppendTo(body)
		commonParams = &params.Params
	}

	sub := &stripe.Sub{}
	err := c.B.Call("DELETE", fmt.Sprintf("/subscriptions/%v", id), c.Key, body, commonParams, sub)

	return sub, err
}
Ejemplo n.º 8
0
func (c Client) Update(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	var body *stripe.RequestValues
	var commonParams *stripe.Params
	token := c.Key

	if params != nil {
		body = &stripe.RequestValues{}

		if len(params.Items) > 0 {
			for i, item := range params.Items {
				key := fmt.Sprintf("items[%d]", i)
				if len(item.ID) > 0 {
					body.Add(key+"[id]", item.ID)
				}
				if len(item.Plan) > 0 {
					body.Add(key+"[plan]", item.Plan)
				}
				if item.Quantity > 0 {
					body.Add(key+"[quantity]", strconv.FormatUint(item.Quantity, 10))
				} else if item.QuantityZero {
					body.Add(key+"[quantity]", "0")
				}
			}
		}

		if len(params.Plan) > 0 {
			body.Add("plan", params.Plan)
		}

		if params.NoProrate {
			body.Add("prorate", strconv.FormatBool(false))
		}

		if len(params.Token) > 0 {
			body.Add("card", params.Token)
		} else if params.Card != nil {
			if len(params.Card.Token) > 0 {
				body.Add("card", params.Card.Token)
			} else {
				params.Card.AppendDetails(body, true)
			}
		}

		if len(params.Coupon) > 0 {
			body.Add("coupon", params.Coupon)
		}

		if params.TrialEndNow {
			body.Add("trial_end", "now")
		} else if params.TrialEnd > 0 {
			body.Add("trial_end", strconv.FormatInt(params.TrialEnd, 10))
		}

		if params.Quantity > 0 {
			body.Add("quantity", strconv.FormatUint(params.Quantity, 10))
		} else if params.QuantityZero {
			body.Add("quantity", "0")
		}

		if params.FeePercent > 0 {
			body.Add("application_fee_percent", strconv.FormatFloat(params.FeePercent, 'f', 2, 64))
		}

		if params.TaxPercent > 0 {
			body.Add("tax_percent", strconv.FormatFloat(params.TaxPercent, 'f', 2, 64))
		} else if params.TaxPercentZero {
			body.Add("tax_percent", "0")
		}

		if params.ProrationDate > 0 {
			body.Add("proration_date", strconv.FormatInt(params.ProrationDate, 10))
		}

		commonParams = &params.Params
		params.AppendTo(body)
	}

	sub := &stripe.Sub{}
	err := c.B.Call("POST", fmt.Sprintf("/subscriptions/%v", id), token, body, commonParams, sub)

	return sub, err
}
Ejemplo n.º 9
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
}