拉卡拉增加不限制支付方式
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package lakala
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -70,6 +74,22 @@ func TestParsePayloadNormalizesAliases(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePaymentOmitsCounterParamWhenPayModeBlank(t *testing.T) {
|
||||
body := captureCreatePaymentBody(t, "")
|
||||
reqData := body["req_data"].(map[string]any)
|
||||
if _, ok := reqData["counter_param"]; ok {
|
||||
t.Fatalf("counter_param exists when pay_mode is blank: %#v", reqData["counter_param"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePaymentSetsCounterParamWhenPayModeConfigured(t *testing.T) {
|
||||
body := captureCreatePaymentBody(t, "WECHAT")
|
||||
reqData := body["req_data"].(map[string]any)
|
||||
if reqData["counter_param"] != `{"pay_mode":"WECHAT"}` {
|
||||
t.Fatalf("counter_param = %#v, want WECHAT", reqData["counter_param"])
|
||||
}
|
||||
}
|
||||
|
||||
func testKeyPairPEM(t *testing.T, key *rsa.PrivateKey) (string, string) {
|
||||
t.Helper()
|
||||
privateDER := x509.MarshalPKCS1PrivateKey(key)
|
||||
@@ -88,3 +108,44 @@ func testKeyPairPEM(t *testing.T, key *rsa.PrivateKey) (string, string) {
|
||||
}))
|
||||
return privatePEM, certPEM
|
||||
}
|
||||
|
||||
func captureCreatePaymentBody(t *testing.T, payMode string) map[string]any {
|
||||
t.Helper()
|
||||
var captured map[string]any
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != endpointCounterCreate {
|
||||
t.Fatalf("path = %s, want %s", r.URL.Path, endpointCounterCreate)
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
|
||||
t.Fatalf("decode request: %v", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"resp_code":"0","result_code":"0","pay_order_no":"LK1"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
t.Fatalf("generate key: %v", err)
|
||||
}
|
||||
privatePEM, certPEM := testKeyPairPEM(t, key)
|
||||
client := NewClient(Config{
|
||||
GatewayURL: server.URL,
|
||||
AppID: "app-1",
|
||||
SerialNo: "serial-1",
|
||||
MerchantID: "merchant-1",
|
||||
PrivateKey: privatePEM,
|
||||
NotifyCert: certPEM,
|
||||
PayMode: payMode,
|
||||
})
|
||||
_, err = client.CreatePayment(context.Background(), CreatePaymentRequest{
|
||||
ThirdOrderID: "ORDER1",
|
||||
AmountCent: 100,
|
||||
NotifyURL: "https://example.com/notify",
|
||||
Body: "测试订单",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreatePayment error = %v", err)
|
||||
}
|
||||
return captured
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user