-
Notifications
You must be signed in to change notification settings - Fork 32
Add support for Billing endpoints #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mauricioabreu
merged 19 commits into
v2
from
mauricio-antunes/bill-1226-update-go-sdk-with-billing-endpoints
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
54f5fbc
Add support for Billing endpoints
mauricioabreu 3fa7665
refactor: Rename commerce -> billing
mauricioabreu 6821d99
fix: make PlanID nullable to support planless subs
mauricioabreu 4dc28c4
refactor: remove unused struct
mauricioabreu e87db23
fix: we will be using PaymentMethod from now on
mauricioabreu eedc390
refactor: remove deprecated fields
mauricioabreu 68c5802
refactor: remove deprecated invoice field
mauricioabreu 83d5893
refactor: omitempty is useless when unmarshalling
mauricioabreu 56788a8
refactor: query strings don't need omitempty
mauricioabreu 2e3178a
fix: several fix related to JSON tags
mauricioabreu f397d46
test: fix after struct changes
mauricioabreu 51eaa24
fix: Remove PayerType array
mauricioabreu c789911
fix: we are not returning null features
mauricioabreu ab8a6e7
fix: we are not returning null plans
mauricioabreu 7f7ebc5
fix: we are not returning null subscription items
mauricioabreu 0a5bb0d
fix: we are not returning null plans
mauricioabreu 725d325
fix: remove currency deprecated fields
mauricioabreu e834012
test: fix
mauricioabreu 1d64f3f
fix: period_start cannot be null
mauricioabreu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package clerk | ||
|
|
||
| // Feature represents a feature associated with a plan. | ||
| type Feature struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| Name string `json:"name"` | ||
| Description string `json:"description"` | ||
| Slug string `json:"slug"` | ||
| AvatarURL string `json:"avatar_url"` | ||
| } | ||
|
|
||
| // BillingMoney represents money amounts with formatting. | ||
| type BillingMoney struct { | ||
| APIResource | ||
|
|
||
| Amount int64 `json:"amount"` | ||
| AmountFormatted string `json:"amount_formatted"` | ||
| Currency string `json:"currency"` | ||
| CurrencySymbol string `json:"currency_symbol"` | ||
| } | ||
|
|
||
| // BillingProduct represents a product. | ||
| type BillingProduct struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| Slug string `json:"slug"` | ||
| Currency string `json:"currency"` | ||
| Name string `json:"name"` | ||
| IsDefault bool `json:"is_default"` | ||
| Plans []Plan `json:"plans"` | ||
| } | ||
|
|
||
| // Plan represents a billing plan. | ||
| type Plan struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| Name string `json:"name"` | ||
| Fee *BillingMoney `json:"fee"` | ||
| AnnualMonthlyFee *BillingMoney `json:"annual_monthly_fee"` | ||
| AnnualFee *BillingMoney `json:"annual_fee"` | ||
| Description string `json:"description"` | ||
| ProductID string `json:"product_id"` | ||
| Product *BillingProduct `json:"product"` | ||
| IsDefault bool `json:"is_default"` | ||
| IsRecurring bool `json:"is_recurring"` | ||
| PubliclyVisible bool `json:"publicly_visible"` | ||
| HasBaseFee bool `json:"has_base_fee"` | ||
| ForPayerType string `json:"for_payer_type"` | ||
| Slug string `json:"slug"` | ||
| AvatarURL string `json:"avatar_url"` | ||
| Features []Feature `json:"features"` | ||
| FreeTrialEnabled bool `json:"free_trial_enabled"` | ||
| FreeTrialDays *int `json:"free_trial_days"` | ||
| } | ||
|
|
||
| // PlanList contains a list of plans. | ||
| type PlanList struct { | ||
| APIResource | ||
|
|
||
| Data []Plan `json:"data"` | ||
| TotalCount int64 `json:"total_count"` | ||
| } | ||
|
|
||
| // BillingPaymentMethod represents a payment method. | ||
| type BillingPaymentMethod struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| PayerID string `json:"payer_id"` | ||
| PaymentMethod string `json:"payment_method"` | ||
| IsDefault *bool `json:"is_default"` | ||
| Gateway string `json:"gateway"` | ||
| GatewayExternalID string `json:"gateway_external_id"` | ||
| GatewayExternalAccountID *string `json:"gateway_external_account_id"` | ||
| Last4 string `json:"last4"` | ||
| Status string `json:"status"` | ||
| WalletType string `json:"wallet_type"` | ||
| CardType string `json:"card_type"` | ||
| ExpiryYear int `json:"expiry_year"` | ||
| ExpiryMonth int `json:"expiry_month"` | ||
| CreatedAt int64 `json:"created_at"` | ||
| UpdatedAt int64 `json:"updated_at"` | ||
|
paddycarver marked this conversation as resolved.
|
||
| IsRemovable *bool `json:"is_removable"` | ||
| } | ||
|
|
||
| // BillingSubscriptionItemNextPayment represents next payment info. | ||
| type BillingSubscriptionItemNextPayment struct { | ||
| APIResource | ||
|
|
||
| Amount *BillingMoney `json:"amount"` | ||
| Date *int64 `json:"date"` | ||
|
Comment on lines
+98
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will these items ever not be set when the outer struct is present? |
||
| } | ||
|
|
||
| // Payer represents a billing payer (user or organization). | ||
| type Payer struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| InstanceID string `json:"instance_id"` | ||
|
|
||
| // User payer only | ||
| UserID *string `json:"user_id"` | ||
| FirstName *string `json:"first_name"` | ||
| LastName *string `json:"last_name"` | ||
| Email *string `json:"email"` | ||
|
|
||
| // Org payer only | ||
| OrganizationID *string `json:"organization_id"` | ||
| OrganizationName *string `json:"organization_name"` | ||
|
|
||
| // Used for both org and user payers | ||
| ImageURL string `json:"image_url"` | ||
|
|
||
| CreatedAt int64 `json:"created_at"` | ||
| UpdatedAt int64 `json:"updated_at"` | ||
| } | ||
|
|
||
| // SubscriptionItem represents a billing subscription item. | ||
| type SubscriptionItem struct { | ||
| APIResource | ||
|
|
||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| InstanceID string `json:"instance_id"` | ||
| Status string `json:"status"` | ||
| PlanID *string `json:"plan_id"` | ||
| Plan *Plan `json:"plan"` | ||
| PlanPeriod string `json:"plan_period"` | ||
| PaymentMethodID string `json:"payment_method_id"` | ||
| PaymentMethod *BillingPaymentMethod `json:"payment_method"` | ||
| LifetimePaid *BillingMoney `json:"lifetime_paid"` | ||
| Amount *BillingMoney `json:"amount"` | ||
| NextPayment *BillingSubscriptionItemNextPayment `json:"next_payment"` | ||
| PayerID string `json:"payer_id"` | ||
| Payer *Payer `json:"payer"` | ||
| IsFreeTrial bool `json:"is_free_trial"` | ||
| PeriodStart int64 `json:"period_start"` | ||
| PeriodEnd *int64 `json:"period_end"` | ||
| ProrationDate string `json:"proration_date"` | ||
| CanceledAt *int64 `json:"canceled_at"` | ||
| PastDueAt *int64 `json:"past_due_at"` | ||
| EndedAt *int64 `json:"ended_at"` | ||
| CreatedAt int64 `json:"created_at"` | ||
| UpdatedAt int64 `json:"updated_at"` | ||
| } | ||
|
|
||
| // SubscriptionItemList contains a list of subscription items. | ||
| type SubscriptionItemList struct { | ||
| APIResource | ||
|
|
||
| Data []SubscriptionItem `json:"data"` | ||
| TotalCount int64 `json:"total_count"` | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Package billing provides the Billing API. | ||
| package billing | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| "github.com/clerk/clerk-sdk-go/v2" | ||
| ) | ||
|
|
||
| //go:generate go run ../cmd/gen/main.go | ||
|
|
||
| const path = "/billing" | ||
|
|
||
| // Client is used to invoke the Billing API. | ||
| type Client struct { | ||
| Backend clerk.Backend | ||
| } | ||
|
|
||
| func NewClient(config *clerk.ClientConfig) *Client { | ||
| return &Client{ | ||
| Backend: clerk.NewBackend(&config.BackendConfig), | ||
| } | ||
| } | ||
|
|
||
| type ListPlansParams struct { | ||
| clerk.APIParams | ||
| clerk.ListParams | ||
| PayerType *string | ||
| } | ||
|
|
||
| func (params *ListPlansParams) ToQuery() url.Values { | ||
| q := params.ListParams.ToQuery() | ||
|
mauricioabreu marked this conversation as resolved.
|
||
| if params.PayerType != nil { | ||
| q.Set("payer_type", *params.PayerType) | ||
| } | ||
| return q | ||
| } | ||
|
|
||
| // ListPlans returns a list of billing plans. | ||
| func (c *Client) ListPlans(ctx context.Context, params *ListPlansParams) (*clerk.PlanList, error) { | ||
| plansPath, err := clerk.JoinPath(path, "/plans") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req := clerk.NewAPIRequest(http.MethodGet, plansPath) | ||
| req.SetParams(params) | ||
| planList := &clerk.PlanList{} | ||
| err = c.Backend.Call(ctx, req, planList) | ||
| return planList, err | ||
| } | ||
|
|
||
| type ListSubscriptionItemsParams struct { | ||
| clerk.APIParams | ||
| clerk.ListParams | ||
| Status *string | ||
| PayerType *string | ||
| PlanID *string | ||
| IncludeFree *bool | ||
| Query *string | ||
| UserID *string | ||
| OrganizationID *string | ||
| } | ||
|
|
||
| func (params *ListSubscriptionItemsParams) ToQuery() url.Values { | ||
| q := params.ListParams.ToQuery() | ||
|
mauricioabreu marked this conversation as resolved.
|
||
| if params.Status != nil { | ||
| q.Set("status", *params.Status) | ||
| } | ||
| if params.PayerType != nil { | ||
| q.Set("payer_type", *params.PayerType) | ||
| } | ||
| if params.PlanID != nil { | ||
| q.Set("plan_id", *params.PlanID) | ||
| } | ||
| if params.IncludeFree != nil { | ||
| if *params.IncludeFree { | ||
| q.Set("include_free", "true") | ||
| } else { | ||
| q.Set("include_free", "false") | ||
| } | ||
| } | ||
| if params.Query != nil { | ||
| q.Set("query", *params.Query) | ||
| } | ||
| if params.UserID != nil { | ||
| q.Set("user_id", *params.UserID) | ||
| } | ||
| if params.OrganizationID != nil { | ||
| q.Set("organization_id", *params.OrganizationID) | ||
| } | ||
| return q | ||
| } | ||
|
|
||
| // ListSubscriptionItems returns a list of subscription items. | ||
| func (c *Client) ListSubscriptionItems(ctx context.Context, params *ListSubscriptionItemsParams) (*clerk.SubscriptionItemList, error) { | ||
| subscriptionItemsPath, err := clerk.JoinPath(path, "/subscription_items") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req := clerk.NewAPIRequest(http.MethodGet, subscriptionItemsPath) | ||
| req.SetParams(params) | ||
| subscriptionItemList := &clerk.SubscriptionItemList{} | ||
| err = c.Backend.Call(ctx, req, subscriptionItemList) | ||
| return subscriptionItemList, err | ||
| } | ||
|
|
||
| type CancelSubscriptionItemParams struct { | ||
| clerk.APIParams | ||
| EndNow *bool | ||
| } | ||
|
|
||
| func (params *CancelSubscriptionItemParams) ToQuery() url.Values { | ||
| q := url.Values{} | ||
|
mauricioabreu marked this conversation as resolved.
|
||
| if params.EndNow != nil { | ||
| if *params.EndNow { | ||
| q.Set("end_now", "true") | ||
| } else { | ||
| q.Set("end_now", "false") | ||
| } | ||
| } | ||
| return q | ||
| } | ||
|
|
||
| // CancelSubscriptionItem cancels a subscription item. | ||
| func (c *Client) CancelSubscriptionItem(ctx context.Context, subscriptionItemID string, params *CancelSubscriptionItemParams) (*clerk.SubscriptionItem, error) { | ||
| cancelPath, err := clerk.JoinPath(path, "/subscription_items", subscriptionItemID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if params != nil { | ||
| query := params.ToQuery() | ||
| if len(query) > 0 { | ||
| cancelPath += "?" + query.Encode() | ||
| } | ||
| } | ||
|
|
||
| req := clerk.NewAPIRequest(http.MethodDelete, cancelPath) | ||
| subscriptionItem := &clerk.SubscriptionItem{} | ||
| err = c.Backend.Call(ctx, req, subscriptionItem) | ||
| return subscriptionItem, err | ||
| } | ||
|
|
||
| type ExtendFreeTrialParams struct { | ||
| clerk.APIParams | ||
| ExtendTo string `json:"extend_to"` | ||
| } | ||
|
mauricioabreu marked this conversation as resolved.
|
||
|
|
||
| // ExtendFreeTrial extends the free trial period of a subscription item. | ||
| func (c *Client) ExtendFreeTrial(ctx context.Context, subscriptionItemID string, params *ExtendFreeTrialParams) (*clerk.SubscriptionItem, error) { | ||
| extendPath, err := clerk.JoinPath(path, "/subscription_items", subscriptionItemID, "/extend_free_trial") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req := clerk.NewAPIRequest(http.MethodPost, extendPath) | ||
| req.SetParams(params) | ||
| subscriptionItem := &clerk.SubscriptionItem{} | ||
| err = c.Backend.Call(ctx, req, subscriptionItem) | ||
| return subscriptionItem, err | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we always have a Fee and only sometimes have an annual fee? Or are there times where we may not have a fee, like the default free plan? 🤔