package packagesilence import ( "context" "testing" "github.com/break/junhong_cmp_fiber/internal/model" "github.com/break/junhong_cmp_fiber/internal/model/dto" "github.com/break/junhong_cmp_fiber/pkg/constants" ) type fakeJSAPIPayer struct{} func (fakeJSAPIPayer) CreateJSAPIPayConfig(context.Context, uint, string, *model.WechatConfig, string, string, int64) (*dto.ClientPayConfig, error) { return &dto.ClientPayConfig{PackageVal: "prepay_id=wx1"}, nil } func TestUseJSAPI(t *testing.T) { s := &ExtensionService{jsapi: fakeJSAPIPayer{}} wechatReq := dto.SilenceOnlineOrderRequest{PaymentMethod: model.PaymentByWechat, AppType: "official_account"} tests := []struct { name string svc *ExtensionService source string req dto.SilenceOnlineOrderRequest config *model.WechatConfig want bool }{ {"H5 微信直连 v2", s, constants.SilenceOrderSourceClient, wechatReq, &model.WechatConfig{ProviderType: model.ProviderTypeWechatV2}, true}, {"H5 微信直连 v3", s, constants.SilenceOrderSourceClient, wechatReq, &model.WechatConfig{ProviderType: model.ProviderTypeWechat}, true}, {"富友仍走扫码", s, constants.SilenceOrderSourceClient, wechatReq, &model.WechatConfig{ProviderType: model.ProviderTypeFuiou}, false}, {"后台二维码", s, constants.SilenceOrderSourceAdmin, wechatReq, &model.WechatConfig{ProviderType: model.ProviderTypeWechatV2}, false}, {"未带应用类型", s, constants.SilenceOrderSourceClient, dto.SilenceOnlineOrderRequest{PaymentMethod: model.PaymentByWechat}, &model.WechatConfig{ProviderType: model.ProviderTypeWechatV2}, false}, {"支付宝", s, constants.SilenceOrderSourceClient, dto.SilenceOnlineOrderRequest{PaymentMethod: model.PaymentByAlipay, AppType: "official_account"}, &model.WechatConfig{ProviderType: "alipay"}, false}, {"未注入", &ExtensionService{}, constants.SilenceOrderSourceClient, wechatReq, &model.WechatConfig{ProviderType: model.ProviderTypeWechatV2}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := tt.svc.useJSAPI(tt.source, tt.req, tt.config); got != tt.want { t.Fatalf("useJSAPI() = %v, want %v", got, tt.want) } }) } } func TestApplyPaymentContent(t *testing.T) { resp := &dto.SilenceExtensionOrderResponse{} ApplyPaymentContent(resp, `jsapi:{"app_id":"wxA","package":"prepay_id=wx1","pay_sign":"S"}`) if resp.PayConfig == nil || resp.PayConfig.PackageVal != "prepay_id=wx1" || resp.PayConfig.AppID != "wxA" || resp.QRContent != "" { t.Fatalf("jsapi content not mapped: %+v", resp) } link := &dto.SilenceExtensionOrderResponse{} ApplyPaymentContent(link, "https://wx.tenpay.com/checkmweb?x=1") if link.QRContent != "https://wx.tenpay.com/checkmweb?x=1" || link.PayConfig != nil { t.Fatalf("link content not mapped: %+v", link) } }