Initial 007Pay Go SDK

This commit is contained in:
shiran
2026-07-10 17:18:58 +08:00
commit c9d007497b
10 changed files with 1075 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
package pay007
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestSubmitPostsSignedForm(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/pay/submit" {
t.Fatalf("path = %s", r.URL.Path)
}
if r.Method != http.MethodPost {
t.Fatalf("method = %s", r.Method)
}
if err := r.ParseForm(); err != nil {
t.Fatal(err)
}
params := map[string]string{}
for k, v := range r.PostForm {
if len(v) > 0 {
params[k] = v[0]
}
}
if params["pid"] != "pid-1" {
t.Fatalf("pid = %s", params["pid"])
}
if params["sign_type"] != "MD5" {
t.Fatalf("sign_type = %s", params["sign_type"])
}
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_no": "P123",
"out_trade_no": params["out_trade_no"],
"amount": 100,
"cashier_url": "http://pay.test/cashier/?orderNo=P123",
},
})
}))
defer server.Close()
client, err := NewClient(server.URL, "pid-1", "secret")
if err != nil {
t.Fatal(err)
}
resp, err := client.Submit(context.Background(), SubmitRequest{
OutTradeNo: "A100",
Name: "test",
Money: "1.00",
Type: "alipay",
})
if err != nil {
t.Fatal(err)
}
if resp.OrderNo != "P123" || resp.Amount != 100 {
t.Fatalf("unexpected response: %+v", resp)
}
}