feat: H5 沉默期延期微信支付改为微信内直接拉起(JSAPI)
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
- H5 下单带 app_type 且选微信、商户为微信直连时,复用套餐购买的 JSAPI 预下单,返回 pay_config - 调起参数保存在支付单,延期订单详情可继续支付;查单、回调、超时关单、作废沿用原流程 - 后台生成二维码、支付宝、富友商户保持原方式
This commit is contained in:
@@ -45,6 +45,7 @@ type ExtensionService struct {
|
||||
runtime *merchantpayment.RuntimeLoader
|
||||
payments PaymentPorts
|
||||
refunder refundchannel.Refunder
|
||||
jsapi JSAPIPayer
|
||||
logger *zap.Logger
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
64
internal/application/packagesilence/online_jsapi.go
Normal file
64
internal/application/packagesilence/online_jsapi.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package packagesilence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// jsapiContentPrefix 标记支付单 qr_content 里保存的是微信 JSAPI 调起参数(JSON),而不是二维码 / 支付链接。
|
||||
const jsapiContentPrefix = "jsapi:"
|
||||
|
||||
// JSAPIPayer 为 C 端客户按已冻结商户创建微信 JSAPI 预下单(复用套餐购买的公众号 / 小程序支付)。
|
||||
type JSAPIPayer interface {
|
||||
CreateJSAPIPayConfig(ctx context.Context, customerID uint, appType string, config *model.WechatConfig,
|
||||
paymentNo, description string, amount int64) (*dto.ClientPayConfig, error)
|
||||
}
|
||||
|
||||
// SetJSAPIPayer 注入微信 JSAPI 预下单能力;未注入时 H5 微信延期仍走 H5 支付链接。
|
||||
func (s *ExtensionService) SetJSAPIPayer(payer JSAPIPayer) { s.jsapi = payer }
|
||||
|
||||
// useJSAPI 判断本次下单是否走微信内直接支付:H5 客户、微信支付、带应用类型、商户为微信直连(v3 / v2)。
|
||||
// 富友商户仍走扫码,避免查单与下单协议不一致。
|
||||
func (s *ExtensionService) useJSAPI(source string, req dto.SilenceOnlineOrderRequest, config *model.WechatConfig) bool {
|
||||
return s.jsapi != nil && source == constants.SilenceOrderSourceClient && req.PaymentMethod == model.PaymentByWechat &&
|
||||
strings.TrimSpace(req.AppType) != "" && config != nil &&
|
||||
(config.ProviderType == model.ProviderTypeWechat || config.ProviderType == model.ProviderTypeWechatV2)
|
||||
}
|
||||
|
||||
// createJSAPIContent 预下单并把调起参数编码成支付单内容,便于"继续支付"复用(预支付有效期 2 小时,长于支付截止)。
|
||||
func (s *ExtensionService) createJSAPIContent(ctx context.Context, customerID uint, appType string, config *model.WechatConfig, payment *model.Payment) (string, error) {
|
||||
payConfig, err := s.jsapi.CreateJSAPIPayConfig(ctx, customerID, appType, config, payment.PaymentNo, "沉默期延期", payment.Amount)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if payConfig == nil || strings.TrimSpace(payConfig.PackageVal) == "" {
|
||||
return "", errors.New(errors.CodeWechatPayFailed, "微信支付未返回调起参数")
|
||||
}
|
||||
raw, err := json.Marshal(payConfig)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(errors.CodeInternalError, err, "编码微信支付参数失败")
|
||||
}
|
||||
return jsapiContentPrefix + string(raw), nil
|
||||
}
|
||||
|
||||
// ApplyPaymentContent 把支付单内容写到响应:JSAPI 参数放 pay_config,其余(二维码 / 支付链接)放 qr_content。
|
||||
func ApplyPaymentContent(resp *dto.SilenceExtensionOrderResponse, content string) {
|
||||
if resp == nil || content == "" {
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(content, jsapiContentPrefix) {
|
||||
var payConfig dto.ClientPayConfig
|
||||
if err := json.Unmarshal([]byte(strings.TrimPrefix(content, jsapiContentPrefix)), &payConfig); err == nil {
|
||||
resp.PayConfig = &payConfig
|
||||
resp.QRContent = ""
|
||||
}
|
||||
return
|
||||
}
|
||||
resp.QRContent = content
|
||||
}
|
||||
57
internal/application/packagesilence/online_jsapi_test.go
Normal file
57
internal/application/packagesilence/online_jsapi_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -101,6 +101,20 @@ func (s *ExtensionService) CreateOnlineOrder(ctx context.Context, command Online
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if s.useJSAPI(source, req, config) {
|
||||
// H5 客户在微信内用微信支付:走 JSAPI 直接拉起,不生成 H5 支付链接(H5 链接在微信内无法支付)
|
||||
content, jsapiErr := s.createJSAPIContent(ctx, command.Scope.CustomerID, req.AppType, config, payment)
|
||||
if jsapiErr != nil {
|
||||
_ = s.closeOrder(ctx, order.ID, "微信支付参数生成失败")
|
||||
return nil, jsapiErr
|
||||
}
|
||||
if err := s.db.WithContext(ctx).Model(&model.Payment{}).
|
||||
Where("id = ? AND status = ? AND (qr_content IS NULL OR qr_content = '')", payment.ID, model.PaymentRecordStatusPending).
|
||||
Update("qr_content", content).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "保存支付内容失败")
|
||||
}
|
||||
return s.orderResponse(ctx, order, true)
|
||||
}
|
||||
paymentResult, err := adapter.CreatePaymentURL(ctx, agentrechargeApp.OnlinePaymentRequest{
|
||||
PaymentID: payment.ID, PaymentNo: payment.PaymentNo, CorrelationID: payment.PaymentNo,
|
||||
Description: "沉默期延期", Amount: payment.Amount, ExpireAt: *payment.ExpireAt,
|
||||
|
||||
@@ -67,8 +67,9 @@ func (s *ExtensionService) orderResponse(ctx context.Context, order *model.Packa
|
||||
resp.ThirdPartyTradeNo = payment.ThirdPartyTradeNo
|
||||
resp.PaymentNo = payment.PaymentNo
|
||||
}
|
||||
if includeQR && order.Status == constants.SilenceOrderStatusPendingPayment && payment.Status == model.PaymentRecordStatusPending && resp.QRContent == "" {
|
||||
resp.QRContent = payment.QRContent
|
||||
if includeQR && order.Status == constants.SilenceOrderStatusPendingPayment && payment.Status == model.PaymentRecordStatusPending &&
|
||||
resp.QRContent == "" && resp.PayConfig == nil {
|
||||
ApplyPaymentContent(resp, payment.QRContent)
|
||||
resp.PaymentNo = payment.PaymentNo
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user