<commit_before>// Package sub provides the /subscriptions APIs
package sub

import (
	"fmt"
	"strconv"

	stripe "github.com/stripe/stripe-go"
)

const (
	Trialing stripe.SubStatus = "trialing"
	Active   stripe.SubStatus = "active"
	PastDue  stripe.SubStatus = "past_due"
	Canceled stripe.SubStatus = "canceled"
	Unpaid   stripe.SubStatus = "unpaid"
	All      stripe.SubStatus = "all"
)

// Client is used to invoke /subscriptions APIs.
type Client struct {
	B   stripe.Backend
	Key string
}

// New POSTS a new subscription for a customer.
// For more details see https://stripe.com/docs/api#create_subscription.
func New(params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().New(params)
}

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("customer", params.Customer)

		if len(params.Items) > 0 {
			for i, item := range params.Items {
				key := fmt.Sprintf("items[%d]", i)
				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 len(params.Billing) > 0 {
			body.Add("billing", string(params.Billing))
		}

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

		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.TrialPeriod > 0 {
			body.Add("trial_period_days", strconv.FormatInt(params.TrialPeriod, 10))
		}

		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', 4, 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
}

// Get returns the details of a subscription.
// For more details see https://stripe.com/docs/api#retrieve_subscription.
func Get(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().Get(id, params)
}

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
}

// Update updates a subscription's properties.
// For more details see https://stripe.com/docs/api#update_subscription.
func Update(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().Update(id, params)
}

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)<commit_msg>Handling subscription item's deletion when updating subscription (#418)<commit_after>// Package sub provides the /subscriptions APIs
package sub

import (
	"fmt"
	"strconv"

	stripe "github.com/stripe/stripe-go"
)

const (
	Trialing stripe.SubStatus = "trialing"
	Active   stripe.SubStatus = "active"
	PastDue  stripe.SubStatus = "past_due"
	Canceled stripe.SubStatus = "canceled"
	Unpaid   stripe.SubStatus = "unpaid"
	All      stripe.SubStatus = "all"
)

// Client is used to invoke /subscriptions APIs.
type Client struct {
	B   stripe.Backend
	Key string
}

// New POSTS a new subscription for a customer.
// For more details see https://stripe.com/docs/api#create_subscription.
func New(params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().New(params)
}

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("customer", params.Customer)

		if len(params.Items) > 0 {
			for i, item := range params.Items {
				key := fmt.Sprintf("items[%d]", i)
				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 len(params.Billing) > 0 {
			body.Add("billing", string(params.Billing))
		}

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

		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.TrialPeriod > 0 {
			body.Add("trial_period_days", strconv.FormatInt(params.TrialPeriod, 10))
		}

		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', 4, 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
}

// Get returns the details of a subscription.
// For more details see https://stripe.com/docs/api#retrieve_subscription.
func Get(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().Get(id, params)
}

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
}

// Update updates a subscription's properties.
// For more details see https://stripe.com/docs/api#update_subscription.
func Update(id string, params *stripe.SubParams) (*stripe.Sub, error) {
	return getC().Update(id, params)
}

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 item.Deleted {
					body.Add(key+"[deleted]", "true")
				}
			}
		}

		if len(params.Plan) > 0 {
			body.Add("plan", params.Plan)
{'fraction_non_alphanumeric': 0.153575744260134, 'fraction_numerical': 0.009776996594529276, 'mean_word_length': 3.604957005563986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7168997', 'n_tokens_mistral': 3719, 'n_tokens_neox': 3215, 'n_words': 730}