Skip to content

Commit

Permalink
feat(commerce): add payers
Browse files Browse the repository at this point in the history
  • Loading branch information
benhowdle89 committed Dec 16, 2024
1 parent f4eedc2 commit d553f55
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 62 deletions.
120 changes: 59 additions & 61 deletions commerce.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ type CreateProductParams struct {
Name string `json:"name"`
Slug string `json:"slug"`
Currency string `json:"currency"`
SubscriberType []string `json:"subscriber_type"`
PayerType []string `json:"subscriber_type"`
OwnerEntityType string `json:"owner_entity_type"`
}

type UpdateProductParams struct {
APIParams
Name *string `json:"name,omitempty"`
Slug *string `json:"slug,omitempty"`
Currency *string `json:"currency,omitempty"`
SubscriberType *[]string `json:"subscriber_type,omitempty"`
Name *string `json:"name,omitempty"`
Slug *string `json:"slug,omitempty"`
Currency *string `json:"currency,omitempty"`
PayerType *[]string `json:"subscriber_type,omitempty"`
}

type GetProductByIDParams struct {
Expand All @@ -36,7 +36,7 @@ type CommerceProduct struct {
Slug string `json:"slug"`
Currency string `json:"currency"`
Plans []*CommercePlan `json:"plans"`
SubscriberType []string `json:"subscriber_type"`
PayerType []string `json:"subscriber_type"`
OwnerEntityType string `json:"owner_entity_type"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Expand Down Expand Up @@ -207,23 +207,23 @@ type GetPlanByIDParams struct {

type CommercePlan struct {
APIResource
ID string `json:"id"`
Name string `json:"name"`
Product *CommerceProduct `json:"product,omitempty"`
Amount int64 `json:"amount"`
IsRecurring bool `json:"is_recurring"`
IsProrated bool `json:"is_prorated"`
Period string `json:"period"`
Interval int `json:"interval"`
AvatarURL string `json:"avatar_url"`
ProductID string `json:"product_id"`
Description string `json:"description"`
Slug string `json:"slug"`
BillingCycles *int `json:"billing_cycles,omitempty"`
SubscriberCount int64 `json:"subscriber_count"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Features []CommerceFeature `json:"features"`
ID string `json:"id"`
Name string `json:"name"`
Product *CommerceProduct `json:"product,omitempty"`
Amount int64 `json:"amount"`
IsRecurring bool `json:"is_recurring"`
IsProrated bool `json:"is_prorated"`
Period string `json:"period"`
Interval int `json:"interval"`
AvatarURL string `json:"avatar_url"`
ProductID string `json:"product_id"`
Description string `json:"description"`
Slug string `json:"slug"`
BillingCycles *int `json:"billing_cycles,omitempty"`
PayerCount int64 `json:"subscriber_count"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Features []CommerceFeature `json:"features"`
}

type CommercePlanWithNoProduct struct {
Expand All @@ -241,7 +241,7 @@ type ListPlansByInstanceIDParams struct {

type CreateSubscriptionParams struct {
APIParams
CustomerID string `json:"customer_id"`
PayerID string `json:"payer_id"`
PlanID string `json:"plan_id"`
PaymentSourceID string `json:"payment_source_id"`
}
Expand All @@ -258,24 +258,24 @@ type GetSubscriptionByIDParams struct {

type ListSubscriptionsByUserIDParams struct {
APIParams
ID string `json:"id"`
SubscriberType string `json:"subscriber_type"`
ID string `json:"id"`
PayerType string `json:"payer_type"`
}

type CommerceSubscription struct {
APIResource
ID string `json:"id"`
AppID string `json:"app_id"`
Customer *CommerceCustomer `json:"customer,omitempty"`
InstanceID string `json:"instance_id"`
PaymentSourceID string `json:"payment_source_id"`
PlanID string `json:"plan_id"`
Plan *CommercePlan `json:"plan,omitempty"`
Status string `json:"status"`
LastInvoice *CommerceInvoice `json:"last_invoice,omitempty"`
NextInvoice *CommerceInvoice `json:"next_invoice,omitempty"`
CreatedAt string `json:"created_at"` // ISO 8601 format
UpdatedAt string `json:"updated_at"` // ISO 8601 format
ID string `json:"id"`
AppID string `json:"app_id"`
Payer *CommercePayer `json:"payer,omitempty"`
InstanceID string `json:"instance_id"`
PaymentSourceID string `json:"payment_source_id"`
PlanID string `json:"plan_id"`
Plan *CommercePlan `json:"plan,omitempty"`
Status string `json:"status"`
LastInvoice *CommerceInvoice `json:"last_invoice,omitempty"`
NextInvoice *CommerceInvoice `json:"next_invoice,omitempty"`
CreatedAt string `json:"created_at"` // ISO 8601 format
UpdatedAt string `json:"updated_at"` // ISO 8601 format
}

type ListCommerceSubscriptionsResponse struct {
Expand All @@ -290,20 +290,30 @@ type GetSubscriptionParams struct {
ID string `json:"id"`
}

type ListSubscribersParams struct {
type ListPayersParams struct {
APIParams
InstanceID string `json:"instance_id"`
}

type CommerceSubscriber struct {
type CommercePayer struct {
APIResource
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}

type ListCommerceSubscribersResponse struct {
type CommercePayerList PaginatedList[CommercePayer]

type CreatePayerParams struct {
APIParams
InstanceID string `json:"instance_id"`
Name string `json:"name"`
Email string `json:"email"`
}

type ListCommercePayersResponse struct {
APIResource
PaginatedList[CommerceSubscriber]
PaginatedList[CommercePayer]
}

// Supporting structs for emails
Expand Down Expand Up @@ -340,30 +350,18 @@ type ListCommercePaymentAttemptsResponse struct {
PaginatedList[CommercePaymentAttempt]
}

// --- Customer Types ---

type CommerceCustomer struct {
ID string `json:"id"`
AppID string `json:"app_id"`
Entity *struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"entity"`
}

// --- Payee Types ---

type CommercePayee struct {
APIResource

ID string `json:"id"`
GatewayStatus string `json:"gateway_status"`
GatewayType string `json:"gateway_type"`
StripeURL string `json:"stripe_url"`
StripeID string `json:"stripe_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID string `json:"id"`
GatewayStatus string `json:"gateway_status"`
GatewayType string `json:"gateway_type"`
StripeURL string `json:"stripe_url"`
StripeID string `json:"stripe_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}


type CommercePayeeList PaginatedList[CommercePayee]
31 changes: 31 additions & 0 deletions commerce/payers/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions commerce/payers/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package payers

import (
"context"
"net/http"

"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../../cmd/gen/main.go
const (
rootPath = "/commerce"
path = "/payers"
)

type CreateParams struct {
clerk.APIParams
Name string `json:"name" form:"name"`
Email string `json:"email" form:"email"`
}

type UpdateParams struct {
clerk.APIParams
}

type ListParams struct {
clerk.APIParams
}

type Client struct {
Backend clerk.Backend
}

func NewClient(config *clerk.ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.CommercePayer, error) {
reqPath, err := clerk.JoinPath(rootPath, path)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, reqPath)
req.SetParams(params)
resource := &clerk.CommercePayer{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.CommercePayerList, error) {
reqPath, err := clerk.JoinPath(rootPath, path)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, reqPath)
resource := &clerk.CommercePayerList{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

func (c *Client) Get(ctx context.Context, id string) (*clerk.CommercePayer, error) {
reqPath, err := clerk.JoinPath(rootPath, path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodGet, reqPath)
resource := &clerk.CommercePayer{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}

func (c *Client) Update(ctx context.Context, id string, params *UpdateParams) (*clerk.CommercePayer, error) {
reqPath, err := clerk.JoinPath(rootPath, path, id)
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPut, reqPath)
req.SetParams(params)
resource := &clerk.CommercePayer{}
err = c.Backend.Call(ctx, req, resource)
return resource, err
}
2 changes: 1 addition & 1 deletion commerce/subscriptions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *Client) ListInvoices(ctx context.Context, subscriptionID string) (*cler
}

func (c *Client) ListByUserID(ctx context.Context, params *clerk.ListSubscriptionsByUserIDParams) (*clerk.ListCommerceSubscriptionsResponse, error) {
reqPath, err := clerk.JoinPath(rootPath, "subscribers", params.SubscriberType, params.ID, "subscriptions")
reqPath, err := clerk.JoinPath(rootPath, "subscribers", params.PayerType, params.ID, "subscriptions")
if err != nil {
return nil, err
}
Expand Down

0 comments on commit d553f55

Please sign in to comment.