本文整理汇总了Golang中github.com/stripe/stripe-go.SubParams.Plan方法的典型用法代码示例。如果您正苦于以下问题:Golang SubParams.Plan方法的具体用法?Golang SubParams.Plan怎么用?Golang SubParams.Plan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/stripe/stripe-go.SubParams
的用法示例。
在下文中一共展示了SubParams.Plan方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: EnsureSubscriptionForGroup
// 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
}