-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling_api.go
More file actions
160 lines (141 loc) · 6.06 KB
/
billing_api.go
File metadata and controls
160 lines (141 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package inouesdk
import (
"context"
"encoding/json"
"fmt"
)
// BillingAPI provides access to the billing and subscription endpoints.
type BillingAPI struct {
client *InoueClient
}
// ---------------------------------------------------------------------------
// Request types
// ---------------------------------------------------------------------------
// BillingCheckoutCreateRequest is the body for checkout endpoints.
type BillingCheckoutCreateRequest struct {
PriceID string `json:"price_id"`
Quantity int `json:"quantity,omitempty"`
SuccessURL string `json:"success_url,omitempty"`
CancelURL string `json:"cancel_url,omitempty"`
}
// BillingPortalCreateRequest is the body for POST /v1/billing/portal.
type BillingPortalCreateRequest struct {
ReturnURL string `json:"return_url,omitempty"`
}
// BillingSubscriptionChangeRequest is the body for POST /v1/billing/subscription/change.
type BillingSubscriptionChangeRequest struct {
PriceID string `json:"price_id"`
Quantity int `json:"quantity,omitempty"`
}
// ---------------------------------------------------------------------------
// Response types
// ---------------------------------------------------------------------------
// BillingProduct represents a product available for purchase.
type BillingProduct struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Type string `json:"type"`
Prices []map[string]interface{} `json:"prices"`
Metadata map[string]interface{} `json:"metadata"`
}
// BillingSummary represents the user's billing summary.
type BillingSummary struct {
CustomerID string `json:"customer_id"`
Subscription map[string]interface{} `json:"subscription"`
CurrentPlan string `json:"current_plan"`
BillingEmail string `json:"billing_email"`
PaymentMethod map[string]interface{} `json:"payment_method"`
NextInvoice map[string]interface{} `json:"next_invoice"`
CreditBalance float64 `json:"credit_balance"`
}
// BillingCheckoutCreateResult is the response from a checkout creation.
type BillingCheckoutCreateResult struct {
CheckoutURL string `json:"checkout_url"`
SessionID string `json:"session_id"`
}
// BillingPortalCreateResult is the response from a portal session creation.
type BillingPortalCreateResult struct {
PortalURL string `json:"portal_url"`
}
// BillingSubscriptionChangeResult is the response from a subscription change.
type BillingSubscriptionChangeResult struct {
SubscriptionID string `json:"subscription_id"`
Status string `json:"status"`
PriceID string `json:"price_id"`
}
// ---------------------------------------------------------------------------
// Methods
// ---------------------------------------------------------------------------
// Products returns the list of available billing products.
func (a *BillingAPI) Products(ctx context.Context) ([]BillingProduct, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "GET", "/v1/billing/products", nil, &apiResp, nil); err != nil {
return nil, fmt.Errorf("billing products: %w", err)
}
var result []BillingProduct
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("billing products decode: %w", err)
}
return result, nil
}
// Summary returns the current user's billing summary.
func (a *BillingAPI) Summary(ctx context.Context) (*BillingSummary, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "GET", "/v1/billing/summary", nil, &apiResp, nil); err != nil {
return nil, fmt.Errorf("billing summary: %w", err)
}
var result BillingSummary
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("billing summary decode: %w", err)
}
return &result, nil
}
// CheckoutTopup creates a top-up checkout session.
func (a *BillingAPI) CheckoutTopup(ctx context.Context, req BillingCheckoutCreateRequest) (*BillingCheckoutCreateResult, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "POST", "/v1/billing/checkout/topup", req, &apiResp, nil); err != nil {
return nil, fmt.Errorf("checkout topup: %w", err)
}
var result BillingCheckoutCreateResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("checkout topup decode: %w", err)
}
return &result, nil
}
// CheckoutSubscription creates a subscription checkout session.
func (a *BillingAPI) CheckoutSubscription(ctx context.Context, req BillingCheckoutCreateRequest) (*BillingCheckoutCreateResult, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "POST", "/v1/billing/checkout/subscription", req, &apiResp, nil); err != nil {
return nil, fmt.Errorf("checkout subscription: %w", err)
}
var result BillingCheckoutCreateResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("checkout subscription decode: %w", err)
}
return &result, nil
}
// Portal creates a billing portal session.
func (a *BillingAPI) Portal(ctx context.Context, req BillingPortalCreateRequest) (*BillingPortalCreateResult, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "POST", "/v1/billing/portal", req, &apiResp, nil); err != nil {
return nil, fmt.Errorf("billing portal: %w", err)
}
var result BillingPortalCreateResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("billing portal decode: %w", err)
}
return &result, nil
}
// ChangeSubscription changes the user's active subscription.
func (a *BillingAPI) ChangeSubscription(ctx context.Context, req BillingSubscriptionChangeRequest) (*BillingSubscriptionChangeResult, error) {
var apiResp ApiResponse
if err := a.client.request(ctx, "POST", "/v1/billing/subscription/change", req, &apiResp, nil); err != nil {
return nil, fmt.Errorf("change subscription: %w", err)
}
var result BillingSubscriptionChangeResult
if err := json.Unmarshal(apiResp.Data, &result); err != nil {
return nil, fmt.Errorf("change subscription decode: %w", err)
}
return &result, nil
}