-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement billing charges API (#156)
This adds support for the `listCharges` API. Belongs to https://github.com/dnsimple/dnsimple-business/issues/1763
- Loading branch information
Wilson Lin
authored
Nov 3, 2023
1 parent
e0ce476
commit ccfeab5
Showing
9 changed files
with
317 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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,74 @@ | ||
package dnsimple | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/shopspring/decimal" | ||
) | ||
|
||
type ListChargesOptions struct { | ||
// Only include results after the given date. | ||
StartDate string `url:"start_date,omitempty"` | ||
|
||
// Only include results before the given date. | ||
EndDate string `url:"end_date,omitempty"` | ||
|
||
// Sort results. Default sorting is by invoiced ascending. | ||
Sort string `url:"sort,omitempty"` | ||
} | ||
|
||
type ListChargesResponse struct { | ||
Response | ||
Data []Charge `json:"data"` | ||
} | ||
|
||
type Charge struct { | ||
InvoicedAt string `json:"invoiced_at,omitempty"` | ||
TotalAmount decimal.Decimal `json:"total_amount,omitempty"` | ||
BalanceAmount decimal.Decimal `json:"balance_amount,omitempty"` | ||
Reference string `json:"reference,omitempty"` | ||
State string `json:"state,omitempty"` | ||
Items []ChargeItem `json:"items,omitempty"` | ||
} | ||
|
||
type ChargeItem struct { | ||
Description string `json:"description,omitempty"` | ||
Amount decimal.Decimal `json:"amount,omitempty"` | ||
ProductId int64 `json:"product_id,omitempty"` | ||
ProductType string `json:"product_type,omitempty"` | ||
ProductReference string `json:"product_reference,omitempty"` | ||
} | ||
|
||
type BillingService struct { | ||
client *Client | ||
} | ||
|
||
// Lists the billing charges for the account. | ||
// | ||
// See https://developer.dnsimple.com/v2/billing/#listCharges | ||
func (s *BillingService) ListCharges( | ||
ctx context.Context, | ||
account string, | ||
options ListChargesOptions, | ||
) (*ListChargesResponse, error) { | ||
res := &ListChargesResponse{} | ||
path := fmt.Sprintf("/v2/%v/billing/charges", account) | ||
|
||
path, err := addURLQueryOptions(path, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
httpRes, err := s.client.get( | ||
ctx, | ||
path, | ||
res, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res.HTTPResponse = httpRes | ||
return res, nil | ||
} |
This file contains 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,190 @@ | ||
package dnsimple | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func toDecimal(t *testing.T, s string) decimal.Decimal { | ||
d, err := decimal.NewFromString(s) | ||
if err != nil { | ||
assert.Nilf(t, err, "toDecimal() error = %v", err) | ||
} | ||
|
||
return d | ||
} | ||
|
||
func TestBillingService_ListCharges_Success(t *testing.T) { | ||
setupMockServer() | ||
defer teardownMockServer() | ||
|
||
mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) { | ||
httpResponse := httpResponseFixture(t, "/api/listCharges/success.http") | ||
|
||
testMethod(t, r, "GET") | ||
testHeaders(t, r) | ||
testQuery(t, r, url.Values{}) | ||
|
||
w.WriteHeader(httpResponse.StatusCode) | ||
_, _ = io.Copy(w, httpResponse.Body) | ||
}) | ||
|
||
response, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{}) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, response.Pagination, &Pagination{CurrentPage: 1, PerPage: 30, TotalPages: 1, TotalEntries: 3}) | ||
assert.Equal(t, response.Data, []Charge{ | ||
{ | ||
InvoicedAt: "2023-08-17T05:53:36Z", | ||
TotalAmount: toDecimal(t, "14.50"), | ||
BalanceAmount: toDecimal(t, "0.00"), | ||
Reference: "1-2", | ||
State: "collected", | ||
Items: []ChargeItem{ | ||
{ | ||
Description: "Register bubble-registered.com", | ||
Amount: toDecimal(t, "14.50"), | ||
ProductId: 1, | ||
ProductType: "domain-registration", | ||
ProductReference: "bubble-registered.com", | ||
}, | ||
}, | ||
}, | ||
{ | ||
InvoicedAt: "2023-08-17T05:57:53Z", | ||
TotalAmount: toDecimal(t, "14.50"), | ||
BalanceAmount: toDecimal(t, "0.00"), | ||
Reference: "2-2", | ||
State: "refunded", | ||
Items: []ChargeItem{ | ||
{ | ||
Description: "Register example.com", | ||
Amount: toDecimal(t, "14.50"), | ||
ProductId: 2, | ||
ProductType: "domain-registration", | ||
ProductReference: "example.com", | ||
}, | ||
}, | ||
}, | ||
{ | ||
InvoicedAt: "2023-10-24T07:49:05Z", | ||
TotalAmount: toDecimal(t, "1099999.99"), | ||
BalanceAmount: toDecimal(t, "0.00"), | ||
Reference: "4-2", | ||
State: "collected", | ||
Items: []ChargeItem{ | ||
{ | ||
Description: "Test Line Item 1", | ||
Amount: toDecimal(t, "99999.99"), | ||
ProductId: 0, | ||
ProductType: "manual", | ||
ProductReference: "", | ||
}, | ||
{ | ||
Description: "Test Line Item 2", | ||
Amount: toDecimal(t, "1000000.00"), | ||
ProductId: 0, | ||
ProductType: "manual", | ||
ProductReference: "", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestBillingService_ListCharges_Fail400BadFilter(t *testing.T) { | ||
setupMockServer() | ||
defer teardownMockServer() | ||
|
||
mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) { | ||
httpResponse := httpResponseFixture(t, "/api/listCharges/fail-400-bad-filter.http") | ||
|
||
testMethod(t, r, "GET") | ||
testHeaders(t, r) | ||
testQuery(t, r, url.Values{}) | ||
|
||
w.WriteHeader(httpResponse.StatusCode) | ||
_, _ = io.Copy(w, httpResponse.Body) | ||
}) | ||
|
||
_, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{}) | ||
|
||
assert.Equal(t, err.(*ErrorResponse).Message, "Invalid date format must be ISO8601 (YYYY-MM-DD)") | ||
} | ||
|
||
func TestBillingService_ListCharges_Fail403(t *testing.T) { | ||
setupMockServer() | ||
defer teardownMockServer() | ||
|
||
mux.HandleFunc("/v2/1010/billing/charges", func(w http.ResponseWriter, r *http.Request) { | ||
httpResponse := httpResponseFixture(t, "/api/listCharges/fail-403.http") | ||
|
||
testMethod(t, r, "GET") | ||
testHeaders(t, r) | ||
testQuery(t, r, url.Values{}) | ||
|
||
w.WriteHeader(httpResponse.StatusCode) | ||
_, _ = io.Copy(w, httpResponse.Body) | ||
}) | ||
|
||
_, err := client.Billing.ListCharges(context.Background(), "1010", ListChargesOptions{}) | ||
|
||
assert.Equal(t, err.(*ErrorResponse).Message, "Permission Denied. Required Scope: billing:*:read") | ||
} | ||
|
||
func TestUnmarshalCharge(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
jsonStr string | ||
want Charge | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid json", | ||
jsonStr: `{"total_amount": "123.45", "balance_amount": "67.89"}`, | ||
want: Charge{ | ||
TotalAmount: decimal.NewFromFloat(123.45), | ||
BalanceAmount: decimal.NewFromFloat(67.89), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "zero values", | ||
jsonStr: `{"total_amount": "0.00", "balance_amount": "0.00"}`, | ||
want: Charge{ | ||
TotalAmount: decimal.NewFromFloat(0.00), | ||
BalanceAmount: decimal.NewFromFloat(0.00), | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid amount value", | ||
jsonStr: `{"total_amount": "123.45", "balance_amount": "abc"}`, | ||
want: Charge{ | ||
TotalAmount: decimal.NewFromFloat(123.45), | ||
BalanceAmount: decimal.Decimal{}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var got Charge | ||
err := json.Unmarshal([]byte(tt.jsonStr), &got) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
assert.Truef(t, got.TotalAmount.Equals(tt.want.TotalAmount), "TotalAmount: got %v, want %v\nTesting: %s\n%s", got.TotalAmount, tt.want.TotalAmount, tt.name, tt.jsonStr) | ||
assert.Truef(t, got.BalanceAmount.Equals(tt.want.BalanceAmount), "BalanceAmount: got %v, want %v\nTesting: %s\n%s", got.BalanceAmount, tt.want.BalanceAmount, tt.name, tt.jsonStr) | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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,14 @@ | ||
HTTP/1.1 400 Bad Request | ||
Date: Tue, 24 Oct 2023 08:13:01 GMT | ||
Connection: close | ||
X-RateLimit-Limit: 2400 | ||
X-RateLimit-Remaining: 2392 | ||
X-RateLimit-Reset: 1698136677 | ||
Content-Type: application/json; charset=utf-8 | ||
X-WORK-WITH-US: Love automation? So do we! https://dnsimple.com/jobs | ||
Cache-Control: no-cache | ||
X-Request-Id: bdfbf3a7-d9dc-4018-9732-61502be989a3 | ||
X-Runtime: 0.455303 | ||
Transfer-Encoding: chunked | ||
|
||
{"message":"Invalid date format must be ISO8601 (YYYY-MM-DD)"} |
This file contains 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,14 @@ | ||
HTTP/1.1 403 Forbidden | ||
Date: Tue, 24 Oct 2023 09:49:29 GMT | ||
Connection: close | ||
X-RateLimit-Limit: 2400 | ||
X-RateLimit-Remaining: 2398 | ||
X-RateLimit-Reset: 1698143967 | ||
Content-Type: application/json; charset=utf-8 | ||
X-WORK-WITH-US: Love automation? So do we! https://dnsimple.com/jobs | ||
Cache-Control: no-cache | ||
X-Request-Id: 5554e2d3-2652-4ca7-8c5e-92b4c35f28d6 | ||
X-Runtime: 0.035309 | ||
Transfer-Encoding: chunked | ||
|
||
{"message":"Permission Denied. Required Scope: billing:*:read"} |
This file contains 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,14 @@ | ||
HTTP/1.1 200 OK | ||
Date: Tue, 24 Oct 2023 09:52:55 GMT | ||
Connection: close | ||
X-RateLimit-Limit: 2400 | ||
X-RateLimit-Remaining: 2397 | ||
X-RateLimit-Reset: 1698143967 | ||
Content-Type: application/json; charset=utf-8 | ||
X-WORK-WITH-US: Love automation? So do we! https://dnsimple.com/jobs | ||
Cache-Control: no-store, must-revalidate, private, max-age=0 | ||
X-Request-Id: a57a87c8-626a-4361-9fb8-b55ca9be8e5d | ||
X-Runtime: 0.060526 | ||
Transfer-Encoding: chunked | ||
|
||
{"data":[{"invoiced_at":"2023-08-17T05:53:36Z","total_amount":"14.50","balance_amount":"0.00","reference":"1-2","state":"collected","items":[{"description":"Register bubble-registered.com","amount":"14.50","product_id":1,"product_type":"domain-registration","product_reference":"bubble-registered.com"}]},{"invoiced_at":"2023-08-17T05:57:53Z","total_amount":"14.50","balance_amount":"0.00","reference":"2-2","state":"refunded","items":[{"description":"Register example.com","amount":"14.50","product_id":2,"product_type":"domain-registration","product_reference":"example.com"}]},{"invoiced_at":"2023-10-24T07:49:05Z","total_amount":"1099999.99","balance_amount":"0.00","reference":"4-2","state":"collected","items":[{"description":"Test Line Item 1","amount":"99999.99","product_id":null,"product_type":"manual","product_reference":null},{"description":"Test Line Item 2","amount":"1000000.00","product_id":null,"product_type":"manual","product_reference":null}]}],"pagination":{"current_page":1,"per_page":30,"total_entries":3,"total_pages":1}} |
This file contains 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
This file contains 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