Skip to content

Commit

Permalink
Merge pull request #12 from webtaken/main
Browse files Browse the repository at this point in the history
Subscription Item object
  • Loading branch information
AchoArnold authored Nov 15, 2023
2 parents e3a708e + f224f1c commit d66aeba
Show file tree
Hide file tree
Showing 10 changed files with 628 additions and 63 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/lemonsqueezy-go?color=brightgreen)](https://github.com/NdoleStudio/lemonsqueezy-go/blob/master/LICENSE)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/lemonsqueezy-go)](https://pkg.go.dev/github.com/NdoleStudio/lemonsqueezy-go)


This package provides a go API client for the lemonsqueezy API

## Installation
Expand Down Expand Up @@ -58,6 +57,11 @@ import "github.com/NdoleStudio/lemonsqueezy-go"
- **Subscription Invoices**
- `GET /v1/subscription-invoices/:id`: Retrieve a subscription invoice
- `GET /v1/subscription-invoices`: List all subscription invoices
- **Subscription Items**
- `GET /v1/subscription-items/:id`: Retrieve a subscription item
- `PATCH /v1/subscription-items/:id`: Update a subscription item
- `GET /v1/subscription-items`: List all subscription items
- `GET /v1/subscription-items/:id/current-usage`: Retrieve a subscription item's current usage
- **Discounts**
- `POST /v1/discounts`: Create a discount
- `GET /v1/discounts/:id`: Retrieve a discount
Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Client struct {
Orders *OrdersService
OrderItems *OrderItemsService
SubscriptionInvoices *SubscriptionInvoicesService
SubscriptionItems *SubscriptionItemsService
DiscountRedemptions *DiscountRedemptionsService
Discounts *DiscountsService
Checkouts *CheckoutsService
Expand Down Expand Up @@ -66,6 +67,7 @@ func New(options ...Option) *Client {
client.Orders = (*OrdersService)(&client.common)
client.OrderItems = (*OrderItemsService)(&client.common)
client.SubscriptionInvoices = (*SubscriptionInvoicesService)(&client.common)
client.SubscriptionItems = (*SubscriptionItemsService)(&client.common)
client.DiscountRedemptions = (*DiscountRedemptionsService)(&client.common)
client.Discounts = (*DiscountsService)(&client.common)
client.Checkouts = (*CheckoutsService)(&client.common)
Expand Down
179 changes: 179 additions & 0 deletions internal/stubs/subscription_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package stubs

// SubscriptionItemGetResponse returns a dummy response to GET /v1/subscription-items/:id endpoint
func SubscriptionItemGetResponse() []byte {
return []byte(`
{
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "https://api.lemonsqueezy.com/v1/subscription-item/1"
},
"data": {
"type": "subscription-items",
"id": "1",
"attributes": {
"subscription_id": 1,
"price_id": 1,
"quantity": 1,
"is_usage_based": false,
"created_at": "2023-07-18T12:16:24.000000Z",
"updated_at": "2023-07-18T12:16:24.000000Z"
},
"relationships": {
"subscription": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/subscription",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/subscription"
}
},
"price": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/price",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/price"
}
},
"usage-records": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/usage-records",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/usage-records"
}
}
},
"links": {
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1"
}
}
}
`)
}

// SubscriptionItemUpdateResponse is a dummy response to the PATCH /v1/subscription-items/:id endpoint
func SubscriptionItemUpdateResponse() []byte {
return []byte(`
{
"jsonapi": {
"version": "1.0"
},
"links": {
"self": "https://api.lemonsqueezy.com/v1/subscription-item/1"
},
"data": {
"type": "subscription-items",
"id": "1",
"attributes": {
"subscription_id": 1,
"price_id": 1,
"quantity": 10,
"is_usage_based": false,
"created_at": "2023-07-18T12:16:24.000000Z",
"updated_at": "2023-07-18T12:23:18.000000Z"
},
"relationships": {
"subscription": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/subscription",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/subscription"
}
},
"price": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/price",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/price"
}
},
"usage-records": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/usage-records",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/usage-records"
}
}
},
"links": {
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1"
}
}
}
`)
}

// SubscriptionItemsListResponse returns a dummy response to GET /v1/subscription-items endpoint
func SubscriptionItemsListResponse() []byte {
return []byte(`
{
"meta": {
"page": {
"currentPage": 1,
"from": 1,
"lastPage": 1,
"perPage": 10,
"to": 10,
"total": 10
}
},
"jsonapi": {
"version": "1.0"
},
"links": {
"first": "https://api.lemonsqueezy.com/v1/subscription-items?page%5Bnumber%5D=1&page%5Bsize%5D=10&sort=-created_at",
"last": "https://api.lemonsqueezy.com/v1/subscription-items?page%5Bnumber%5D=1&page%5Bsize%5D=10&sort=-created_at"
},
"data": [
{
"type": "subscription-items",
"id": "1",
"attributes": {
"subscription_id": 1,
"price_id": 1,
"quantity": 1,
"is_usage_based": false,
"created_at": "2023-07-18T12:16:24.000000Z",
"updated_at": "2023-07-18T12:16:24.000000Z"
},
"relationships": {
"subscription": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/subscription",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/subscription"
}
},
"price": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/price",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/price"
}
},
"usage-records": {
"links": {
"related": "https://api.lemonsqueezy.com/v1/subscription-items/1/usage-records",
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1/relationships/usage-records"
}
}
},
"links": {
"self": "https://api.lemonsqueezy.com/v1/subscription-items/1"
}
}
]
}
`)
}

// SubscriptionItemCurrentUsageResponse returns a dummy response to GET /v1/subscription-items/:id/current-usage endpoint
func SubscriptionItemCurrentUsageResponse() []byte {
return []byte(`
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"period_start": "2023-08-10T13:08:16.000000Z",
"period_end": "2023-09-10T13:03:16.000000Z",
"quantity": 5,
"interval_unit": "month",
"interval_quantity": 1
}
}
`)
}
15 changes: 10 additions & 5 deletions internal/stubs/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func SubscriptionGetResponse() []byte {
"updated_at": "2021-08-11T13:47:28.000000Z"
},
"urls": {
"update_payment_method": "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
"update_payment_method": "https://my-store.lemonsqueezy.com/subscription/1/payment-details?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413",
"customer_portal": "https://my-store.lemonsqueezy.com/billing?expires=1666869343&signature=82ae290ceac8edd4190c82825dd73a8743346d894a8ddbc4898b97eb96d105a5"
},
"renews_at": "2022-11-12T00:00:00.000000Z",
"ends_at": null,
Expand Down Expand Up @@ -146,7 +147,8 @@ func SubscriptionUpdateResponse() []byte {
"updated_at": "2021-08-11T13:47:28.000000Z"
},
"urls": {
"update_payment_method": "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
"update_payment_method": "https://my-store.lemonsqueezy.com/subscription/1/payment-details?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413",
"customer_portal": "https://my-store.lemonsqueezy.com/billing?expires=1666869343&signature=82ae290ceac8edd4190c82825dd73a8743346d894a8ddbc4898b97eb96d105a5"
},
"renews_at": "2022-11-12T00:00:00.000000Z",
"ends_at": null,
Expand Down Expand Up @@ -210,7 +212,8 @@ func SubscriptionsListResponse() []byte {
"updated_at": "2021-08-11T13:47:28.000000Z"
},
"urls":{
"update_payment_method":"https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
"update_payment_method": "https://my-store.lemonsqueezy.com/subscription/1/payment-details?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413",
"customer_portal": "https://my-store.lemonsqueezy.com/billing?expires=1666869343&signature=82ae290ceac8edd4190c82825dd73a8743346d894a8ddbc4898b97eb96d105a5"
},
"renews_at":"2022-11-12T00:00:00.000000Z",
"ends_at":null,
Expand Down Expand Up @@ -301,7 +304,8 @@ func SubscriptionsListResponse() []byte {
"updated_at": "2021-08-11T13:47:28.000000Z"
},
"urls":{
"update_payment_method":"https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
"update_payment_method": "https://my-store.lemonsqueezy.com/subscription/2/payment-details?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413",
"customer_portal": "https://my-store.lemonsqueezy.com/billing?expires=1666869343&signature=82ae290ceac8edd4190c82825dd73a8743346d894a8ddbc4898b97eb96d105a5"
},
"renews_at":"2022-11-12T00:00:00.000000Z",
"ends_at":null,
Expand Down Expand Up @@ -407,7 +411,8 @@ func SubscriptionCancelResponse() []byte {
"updated_at": "2021-08-11T13:47:28.000000Z"
},
"urls": {
"update_payment_method": "https://app.lemonsqueezy.com/my-orders/2ba92a4e-a00a-45d2-a128-16856ffa8cdf/subscription/8/update-payment-method?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413"
"update_payment_method": "https://my-store.lemonsqueezy.com/subscription/1/payment-details?expires=1666869343&signature=9985e3bf9007840aeb3951412be475abc17439c449c1af3e56e08e45e1345413",
"customer_portal": "https://my-store.lemonsqueezy.com/billing?expires=1666869343&signature=82ae290ceac8edd4190c82825dd73a8743346d894a8ddbc4898b97eb96d105a5"
},
"renews_at": "2022-11-12T00:00:00.000000Z",
"ends_at": "2022-11-12T00:00:00.000000Z",
Expand Down
23 changes: 21 additions & 2 deletions subscription_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "time"
// In Lemon Squeezy A subscription item is an object that links a price to a subscription and also contains quantity information.
// https://docs.lemonsqueezy.com/api/subscription-items#the-subscription-item-object
type SubscriptionItem struct {
ID int `json:"id"`
SubscriptionID int `json:"subscription_id"`
PriceID int `json:"price_id"`
Quantity int `json:"quantity"`
Expand All @@ -20,12 +19,26 @@ type SubscriptionItemUpdateParams struct {
Attributes SubscriptionItemUpdateParamsAttributes `json:"attributes"`
}

// SubscriptionItemListParams are parameters for filtering list responses
type SubscriptionItemListParams struct {
SubscriptionID string
PriceID string
}

// SubscriptionUpdateParamsAttributes are subscription update attributes
type SubscriptionItemUpdateParamsAttributes struct {
Quantity int `json:"quantity,omitempty"`
}

// ApiResponseRelationshipsSubscription relationships of a subscription object
type ApiResponseMetaSubscriptionItemCurrentUsage struct {
PeriodStart time.Time `json:"period_start"`
PeriodEnd time.Time `json:"period_end"`
Quantity int `json:"quantity"`
IntervalUnit string `json:"interval_unit"`
IntervalQuantity int `json:"interval_quantity"`
}

// ApiResponseRelationshipsSubscription relationships of a subscription item object
type ApiResponseRelationshipsSubscriptionItem struct {
Subscription ApiResponseLinks `json:"subscription"`
Price ApiResponseLinks `json:"price"`
Expand All @@ -37,3 +50,9 @@ type SubscriptionItemApiResponse = ApiResponse[SubscriptionItem, ApiResponseRela

// SubscriptionItemsApiResponse represents a list of subscription items api responses
type SubscriptionItemsApiResponse = ApiResponseList[SubscriptionItem, ApiResponseRelationshipsSubscriptionItem]

// SubscriptionItemsCurrentUsageApiResponse represents the subscription item's current usage api response
type SubscriptionItemCurrentUsageApiResponse struct {
Jsonapi ApiResponseJSONAPI `json:"jsonapi"`
Meta ApiResponseMetaSubscriptionItemCurrentUsage `json:"meta"`
}
Loading

0 comments on commit d66aeba

Please sign in to comment.