Skip to content

Support free plan creation #134

@thisiskseniab

Description

@thisiskseniab

Currently, this API does not support free plan creation due to UnitAmount having omitempty set on every currency and UnitAmount.MarshalXML method explicitly checking values to be greater than 0.

In order to create a free plan one (or all) currency values have to be set to 0. Unfortunately, solving this problem isn't as simple as just unsetting omitempty on every currency in UnitAmount struct and not checking for values being greater than 0 because this will mean that by default all plans will cost 0 in currencies where the value isn't set which might have unintended consequences. Besides, removing omitempty on every currency will also cause issues with sites where multi-currency support is not enabled. UnitAmount is also used to define adjustments, coupons, and subscriptions.

We're not entirely sure what is the best approach to solving this is, but hopefully, maintainers of this project have some suggestions! 🙏

Reference code.

// UnitAmount can read or write amounts in various currencies.
type UnitAmount struct {
	USD int `xml:"USD,omitempty"`
	EUR int `xml:"EUR,omitempty"`
	GBP int `xml:"GBP,omitempty"`
	CAD int `xml:"CAD,omitempty"`
	AUD int `xml:"AUD,omitempty"`
}

// MarshalXML ensures UnitAmount is not marshaled unless one or more currencies
// has a value greater than zero.
func (u UnitAmount) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	if u.USD > 0 || u.EUR > 0 || u.CAD > 0 || u.GBP > 0 || u.AUD > 0 {
		type uaAlias UnitAmount
		e.EncodeElement(uaAlias(u), start)
	}
	return nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions