From 0fd4b6cf169034fe69cf242776a5439defebdaa3 Mon Sep 17 00:00:00 2001 From: shiran Date: Mon, 20 Jul 2026 11:34:42 +0800 Subject: [PATCH] feat: expand merchant revenue statistics --- README.md | 4 ++++ client_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 17 +++++++++++++---- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2dea157..5056edb 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,10 @@ export PAY007_KEY=your-secret-key 007pay stats --begin "2026-07-01 00:00:00" --end "2026-07-31 23:59:59" ``` +`StatsResponse` includes order counts by state, manual-order count, gross and +received amounts, successful refund count/amount, fee, and net amount. All +amount fields use cents. + 签名调试: ```bash diff --git a/client_test.go b/client_test.go index 75ac5ad..3d77dd2 100644 --- a/client_test.go +++ b/client_test.go @@ -67,3 +67,55 @@ func TestSubmitPostsSignedForm(t *testing.T) { t.Fatalf("unexpected response: %+v", resp) } } + +func TestStatsDecodesDetailedResponse(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/pay/stats" { + t.Fatalf("path = %s", r.URL.Path) + } + params := map[string]string{} + for key, values := range r.URL.Query() { + if len(values) > 0 { + params[key] = values[0] + } + } + if !VerifySign(params, "secret") { + t.Fatalf("invalid sign: %s", params["sign"]) + } + + _ = json.NewEncoder(w).Encode(map[string]any{ + "code": 200, + "message": "Success", + "data": map[string]any{ + "order_count": 3, + "total_order_count": 5, + "paid_order_count": 3, + "unpaid_order_count": 2, + "refunded_order_count": 1, + "closed_order_count": 1, + "manual_order_count": 1, + "total_amount": 17000, + "received_amount": 11000, + "refund_amount": 6000, + "refund_count": 2, + "total_fee": 170, + "net_amount": 10830, + }, + }) + })) + defer server.Close() + + client, err := NewClient(server.URL, "pid-1", "secret") + if err != nil { + t.Fatal(err) + } + resp, err := client.Stats(context.Background(), StatsRequest{}) + if err != nil { + t.Fatal(err) + } + if resp.TotalOrderCount != 5 || resp.PaidOrderCount != 3 || + resp.RefundedOrderCount != 1 || resp.RefundCount != 2 || + resp.ReceivedAmount != 11000 || resp.NetAmount != 10830 { + t.Fatalf("unexpected response: %+v", resp) + } +} diff --git a/types.go b/types.go index 21731de..c26ca80 100644 --- a/types.go +++ b/types.go @@ -168,8 +168,17 @@ func (r StatsRequest) params() map[string]string { } type StatsResponse struct { - OrderCount int64 `json:"order_count"` - TotalAmount int64 `json:"total_amount"` - TotalFee int64 `json:"total_fee"` - NetAmount int64 `json:"net_amount"` + OrderCount int64 `json:"order_count"` + TotalAmount int64 `json:"total_amount"` + TotalFee int64 `json:"total_fee"` + NetAmount int64 `json:"net_amount"` + TotalOrderCount int64 `json:"total_order_count"` + PaidOrderCount int64 `json:"paid_order_count"` + UnpaidOrderCount int64 `json:"unpaid_order_count"` + RefundedOrderCount int64 `json:"refunded_order_count"` + ClosedOrderCount int64 `json:"closed_order_count"` + ManualOrderCount int64 `json:"manual_order_count"` + ReceivedAmount int64 `json:"received_amount"` + RefundAmount int64 `json:"refund_amount"` + RefundCount int64 `json:"refund_count"` }