fix wxpay config contract and h5 scene info
This commit is contained in:
@@ -250,13 +250,12 @@ func (w *Wxpay) prepayNative(ctx context.Context, c *core.Client, req payment.Cr
|
||||
func (w *Wxpay) prepayH5(ctx context.Context, c *core.Client, req payment.CreatePaymentRequest, notifyURL string, totalFen int64) (*payment.CreatePaymentResponse, error) {
|
||||
svc := h5.H5ApiService{Client: c}
|
||||
cur := wxpayCurrency
|
||||
tp := wxpayH5Type
|
||||
resp, _, err := wxpayH5Prepay(ctx, svc, h5.PrepayRequest{
|
||||
Appid: core.String(w.config["appId"]), Mchid: core.String(w.config["mchId"]),
|
||||
Description: core.String(req.Subject), OutTradeNo: core.String(req.OrderID),
|
||||
NotifyUrl: core.String(notifyURL),
|
||||
Amount: &h5.Amount{Total: core.Int64(totalFen), Currency: &cur},
|
||||
SceneInfo: &h5.SceneInfo{PayerClientIp: core.String(req.ClientIP), H5Info: &h5.H5Info{Type: &tp}},
|
||||
SceneInfo: &h5.SceneInfo{PayerClientIp: core.String(req.ClientIP), H5Info: buildWxpayH5Info(w.config)},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("wxpay h5 prepay: %w", err)
|
||||
@@ -272,6 +271,18 @@ func (w *Wxpay) prepayH5(ctx context.Context, c *core.Client, req payment.Create
|
||||
return &payment.CreatePaymentResponse{TradeNo: req.OrderID, PayURL: h5URL}, nil
|
||||
}
|
||||
|
||||
func buildWxpayH5Info(config map[string]string) *h5.H5Info {
|
||||
tp := wxpayH5Type
|
||||
info := &h5.H5Info{Type: &tp}
|
||||
if appName := strings.TrimSpace(config["h5AppName"]); appName != "" {
|
||||
info.AppName = core.String(appName)
|
||||
}
|
||||
if appURL := strings.TrimSpace(config["h5AppUrl"]); appURL != "" {
|
||||
info.AppUrl = core.String(appURL)
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func resolveWxpayCreateMode(req payment.CreatePaymentRequest) (string, error) {
|
||||
if strings.TrimSpace(req.OpenID) != "" {
|
||||
return wxpayModeJSAPI, nil
|
||||
|
||||
@@ -487,3 +487,86 @@ func TestCreatePaymentWithOpenIDReturnsJSAPIResult(t *testing.T) {
|
||||
t.Fatalf("jsapi paySign = %q, want %q", resp.JSAPI.PaySign, "signed-payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePaymentMobileH5IncludesConfiguredSceneInfo(t *testing.T) {
|
||||
origJSAPIPrepay := wxpayJSAPIPrepayWithRequestPayment
|
||||
origNativePrepay := wxpayNativePrepay
|
||||
origH5Prepay := wxpayH5Prepay
|
||||
t.Cleanup(func() {
|
||||
wxpayJSAPIPrepayWithRequestPayment = origJSAPIPrepay
|
||||
wxpayNativePrepay = origNativePrepay
|
||||
wxpayH5Prepay = origH5Prepay
|
||||
})
|
||||
|
||||
jsapiCalls := 0
|
||||
nativeCalls := 0
|
||||
h5Calls := 0
|
||||
wxpayJSAPIPrepayWithRequestPayment = func(ctx context.Context, svc jsapi.JsapiApiService, req jsapi.PrepayRequest) (*jsapi.PrepayWithRequestPaymentResponse, *core.APIResult, error) {
|
||||
jsapiCalls++
|
||||
return &jsapi.PrepayWithRequestPaymentResponse{}, nil, nil
|
||||
}
|
||||
wxpayNativePrepay = func(ctx context.Context, svc native.NativeApiService, req native.PrepayRequest) (*native.PrepayResponse, *core.APIResult, error) {
|
||||
nativeCalls++
|
||||
return &native.PrepayResponse{}, nil, nil
|
||||
}
|
||||
wxpayH5Prepay = func(ctx context.Context, svc h5.H5ApiService, req h5.PrepayRequest) (*h5.PrepayResponse, *core.APIResult, error) {
|
||||
h5Calls++
|
||||
if req.SceneInfo == nil {
|
||||
t.Fatal("expected scene_info, got nil")
|
||||
}
|
||||
if got := wxSV(req.SceneInfo.PayerClientIp); got != "203.0.113.10" {
|
||||
t.Fatalf("scene_info payer_client_ip = %q, want %q", got, "203.0.113.10")
|
||||
}
|
||||
if req.SceneInfo.H5Info == nil {
|
||||
t.Fatal("expected scene_info.h5_info, got nil")
|
||||
}
|
||||
if got := wxSV(req.SceneInfo.H5Info.Type); got != wxpayH5Type {
|
||||
t.Fatalf("scene_info.h5_info.type = %q, want %q", got, wxpayH5Type)
|
||||
}
|
||||
if got := wxSV(req.SceneInfo.H5Info.AppName); got != "Sub2API" {
|
||||
t.Fatalf("scene_info.h5_info.app_name = %q, want %q", got, "Sub2API")
|
||||
}
|
||||
if got := wxSV(req.SceneInfo.H5Info.AppUrl); got != "https://app.example.com" {
|
||||
t.Fatalf("scene_info.h5_info.app_url = %q, want %q", got, "https://app.example.com")
|
||||
}
|
||||
return &h5.PrepayResponse{
|
||||
H5Url: core.String("https://wx.tenpay.example/h5pay?prepay_id=1"),
|
||||
}, nil, nil
|
||||
}
|
||||
|
||||
provider := &Wxpay{
|
||||
config: map[string]string{
|
||||
"appId": "wx123",
|
||||
"mchId": "mch123",
|
||||
"h5AppName": "Sub2API",
|
||||
"h5AppUrl": "https://app.example.com",
|
||||
},
|
||||
coreClient: &core.Client{},
|
||||
}
|
||||
|
||||
resp, err := provider.CreatePayment(context.Background(), payment.CreatePaymentRequest{
|
||||
OrderID: "sub2_99",
|
||||
Amount: "66.88",
|
||||
PaymentType: payment.TypeWxpay,
|
||||
Subject: "Balance Recharge",
|
||||
NotifyURL: "https://merchant.example/payment/notify",
|
||||
ReturnURL: "https://merchant.example/payment/result?resume_token=resume-99",
|
||||
ClientIP: "203.0.113.10",
|
||||
IsMobile: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if jsapiCalls != 0 {
|
||||
t.Fatalf("jsapi prepay calls = %d, want 0", jsapiCalls)
|
||||
}
|
||||
if nativeCalls != 0 {
|
||||
t.Fatalf("native prepay calls = %d, want 0", nativeCalls)
|
||||
}
|
||||
if h5Calls != 1 {
|
||||
t.Fatalf("h5 prepay calls = %d, want 1", h5Calls)
|
||||
}
|
||||
if !strings.Contains(resp.PayURL, "redirect_url=") {
|
||||
t.Fatalf("pay_url = %q, want redirect_url query appended", resp.PayURL)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user