fix(upgrade): close payment and oidc compatibility gaps
This commit is contained in:
@@ -200,14 +200,7 @@ func (w *Wxpay) CreatePayment(ctx context.Context, req payment.CreatePaymentRequ
|
||||
case wxpayModeJSAPI:
|
||||
return w.prepayJSAPI(ctx, client, req, notifyURL, totalFen)
|
||||
case wxpayModeH5:
|
||||
resp, err := w.prepayH5(ctx, client, req, notifyURL, totalFen)
|
||||
if err == nil {
|
||||
return resp, nil
|
||||
}
|
||||
if wxpayShouldFallbackToNative(err) {
|
||||
return w.prepayNativeFallback(ctx, client, req, notifyURL, totalFen)
|
||||
}
|
||||
return nil, err
|
||||
return w.prepayH5(ctx, client, req, notifyURL, totalFen)
|
||||
case wxpayModeNative:
|
||||
return w.prepayNative(ctx, client, req, notifyURL, totalFen)
|
||||
default:
|
||||
@@ -292,23 +285,6 @@ func (w *Wxpay) prepayH5(ctx context.Context, c *core.Client, req payment.Create
|
||||
return &payment.CreatePaymentResponse{TradeNo: req.OrderID, PayURL: h5URL}, nil
|
||||
}
|
||||
|
||||
func (w *Wxpay) prepayNativeFallback(ctx context.Context, c *core.Client, req payment.CreatePaymentRequest, notifyURL string, totalFen int64) (*payment.CreatePaymentResponse, error) {
|
||||
resp, err := w.prepayNative(ctx, c, req, notifyURL, totalFen)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("wxpay native fallback after NO_AUTH: %w", err)
|
||||
}
|
||||
nativeURL := strings.TrimSpace(resp.PayURL)
|
||||
if nativeURL == "" {
|
||||
nativeURL = strings.TrimSpace(resp.QRCode)
|
||||
}
|
||||
if nativeURL == "" {
|
||||
return resp, nil
|
||||
}
|
||||
resp.PayURL = nativeURL
|
||||
resp.QRCode = nativeURL
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func buildWxpayH5Info(config map[string]string) *h5.H5Info {
|
||||
tp := wxpayH5Type
|
||||
info := &h5.H5Info{Type: &tp}
|
||||
@@ -321,10 +297,6 @@ func buildWxpayH5Info(config map[string]string) *h5.H5Info {
|
||||
return info
|
||||
}
|
||||
|
||||
func wxpayShouldFallbackToNative(err error) bool {
|
||||
return err != nil && strings.Contains(err.Error(), wxpayErrNoAuth)
|
||||
}
|
||||
|
||||
func resolveWxpayCreateMode(req payment.CreatePaymentRequest) (string, error) {
|
||||
if strings.TrimSpace(req.OpenID) != "" {
|
||||
return wxpayModeJSAPI, nil
|
||||
|
||||
@@ -643,7 +643,7 @@ func TestCreatePaymentMobileH5IncludesConfiguredSceneInfo(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePaymentMobileH5FallsBackToNativeOnNoAuth(t *testing.T) {
|
||||
func TestCreatePaymentMobileH5ReturnsNoAuthErrorWithoutNativeFallback(t *testing.T) {
|
||||
origJSAPIPrepay := wxpayJSAPIPrepayWithRequestPayment
|
||||
origNativePrepay := wxpayNativePrepay
|
||||
origH5Prepay := wxpayH5Prepay
|
||||
@@ -688,8 +688,8 @@ func TestCreatePaymentMobileH5FallsBackToNativeOnNoAuth(t *testing.T) {
|
||||
ClientIP: "203.0.113.10",
|
||||
IsMobile: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
if err == nil {
|
||||
t.Fatal("expected no-auth error, got nil")
|
||||
}
|
||||
if jsapiCalls != 0 {
|
||||
t.Fatalf("jsapi prepay calls = %d, want 0", jsapiCalls)
|
||||
@@ -697,13 +697,13 @@ func TestCreatePaymentMobileH5FallsBackToNativeOnNoAuth(t *testing.T) {
|
||||
if h5Calls != 1 {
|
||||
t.Fatalf("h5 prepay calls = %d, want 1", h5Calls)
|
||||
}
|
||||
if nativeCalls != 1 {
|
||||
t.Fatalf("native prepay calls = %d, want 1", nativeCalls)
|
||||
if nativeCalls != 0 {
|
||||
t.Fatalf("native prepay calls = %d, want 0", nativeCalls)
|
||||
}
|
||||
if resp.PayURL != "weixin://wxpay/bizpayurl?pr=fallback-native" {
|
||||
t.Fatalf("pay_url = %q, want native fallback url", resp.PayURL)
|
||||
if resp != nil {
|
||||
t.Fatalf("expected nil response, got %+v", resp)
|
||||
}
|
||||
if resp.QRCode != "weixin://wxpay/bizpayurl?pr=fallback-native" {
|
||||
t.Fatalf("qr_code = %q, want native fallback url", resp.QRCode)
|
||||
if !strings.Contains(err.Error(), "NO_AUTH") {
|
||||
t.Fatalf("error = %v, want NO_AUTH", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user