fix: snapshot merchant identity for alipay and easypay
This commit is contained in:
@@ -96,47 +96,7 @@ func (s *PaymentService) confirmPayment(ctx context.Context, oid int64, tradeNo
|
||||
}
|
||||
|
||||
func validateProviderNotificationMetadata(order *dbent.PaymentOrder, providerKey string, metadata map[string]string) error {
|
||||
if order == nil || len(metadata) == 0 || !strings.EqualFold(strings.TrimSpace(providerKey), payment.TypeWxpay) {
|
||||
return nil
|
||||
}
|
||||
|
||||
snapshot := psOrderProviderSnapshot(order)
|
||||
if snapshot == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if expected := strings.TrimSpace(snapshot.MerchantAppID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["appid"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing appid")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay appid mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if expected := strings.TrimSpace(snapshot.MerchantID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["mchid"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing mchid")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay mchid mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if expected := strings.TrimSpace(snapshot.Currency); expected != "" {
|
||||
actual := strings.ToUpper(strings.TrimSpace(metadata["currency"]))
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing currency")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay currency mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if actual := strings.TrimSpace(metadata["trade_state"]); actual != "" && !strings.EqualFold(actual, "SUCCESS") {
|
||||
return fmt.Errorf("wxpay trade_state mismatch: expected SUCCESS, got %s", actual)
|
||||
}
|
||||
|
||||
return nil
|
||||
return validateProviderSnapshotMetadata(order, providerKey, metadata)
|
||||
}
|
||||
|
||||
func expectedNotificationProviderKey(registry *payment.Registry, orderPaymentType string, orderProviderKey string, instanceProviderKey string) string {
|
||||
|
||||
@@ -321,3 +321,37 @@ func TestParseLegacyPaymentOrderID(t *testing.T) {
|
||||
_, ok = parseLegacyPaymentOrderID("sub2_42", errors.New("db down"))
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestValidateProviderNotificationMetadataRejectsAlipaySnapshotMismatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
order := &dbent.PaymentOrder{
|
||||
PaymentType: payment.TypeAlipay,
|
||||
ProviderSnapshot: map[string]any{
|
||||
"schema_version": 2,
|
||||
"merchant_app_id": "alipay-app-expected",
|
||||
},
|
||||
}
|
||||
|
||||
err := validateProviderNotificationMetadata(order, payment.TypeAlipay, map[string]string{
|
||||
"app_id": "alipay-app-other",
|
||||
})
|
||||
assert.ErrorContains(t, err, "alipay app_id mismatch")
|
||||
}
|
||||
|
||||
func TestValidateProviderNotificationMetadataRejectsEasyPaySnapshotMismatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
order := &dbent.PaymentOrder{
|
||||
PaymentType: payment.TypeAlipay,
|
||||
ProviderSnapshot: map[string]any{
|
||||
"schema_version": 2,
|
||||
"merchant_id": "pid-expected",
|
||||
},
|
||||
}
|
||||
|
||||
err := validateProviderNotificationMetadata(order, payment.TypeEasyPay, map[string]string{
|
||||
"pid": "pid-other",
|
||||
})
|
||||
assert.ErrorContains(t, err, "easypay pid mismatch")
|
||||
}
|
||||
|
||||
@@ -240,6 +240,16 @@ func buildPaymentOrderProviderSnapshot(sel *payment.InstanceSelection, req Creat
|
||||
}
|
||||
snapshot["currency"] = "CNY"
|
||||
}
|
||||
if providerKey == payment.TypeAlipay {
|
||||
if merchantAppID := strings.TrimSpace(sel.Config["appId"]); merchantAppID != "" {
|
||||
snapshot["merchant_app_id"] = merchantAppID
|
||||
}
|
||||
}
|
||||
if providerKey == payment.TypeEasyPay {
|
||||
if merchantID := strings.TrimSpace(sel.Config["pid"]); merchantID != "" {
|
||||
snapshot["merchant_id"] = merchantID
|
||||
}
|
||||
}
|
||||
|
||||
if len(snapshot) == 1 {
|
||||
return nil
|
||||
|
||||
@@ -125,3 +125,81 @@ func expectedNotificationProviderKeyForOrder(registry *payment.Registry, order *
|
||||
|
||||
return expectedNotificationProviderKey(registry, order.PaymentType, orderProviderKey, instanceProviderKey)
|
||||
}
|
||||
|
||||
func validateProviderSnapshotMetadata(order *dbent.PaymentOrder, providerKey string, metadata map[string]string) error {
|
||||
if order == nil || len(metadata) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
snapshot := psOrderProviderSnapshot(order)
|
||||
if snapshot == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch strings.TrimSpace(providerKey) {
|
||||
case payment.TypeWxpay:
|
||||
if expected := strings.TrimSpace(snapshot.MerchantAppID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["appid"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing appid")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay appid mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if expected := strings.TrimSpace(snapshot.MerchantID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["mchid"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing mchid")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay mchid mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if expected := strings.TrimSpace(snapshot.Currency); expected != "" {
|
||||
actual := strings.ToUpper(strings.TrimSpace(metadata["currency"]))
|
||||
if actual == "" {
|
||||
return fmt.Errorf("wxpay notification missing currency")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("wxpay currency mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
if actual := strings.TrimSpace(metadata["trade_state"]); actual != "" && !strings.EqualFold(actual, "SUCCESS") {
|
||||
return fmt.Errorf("wxpay trade_state mismatch: expected SUCCESS, got %s", actual)
|
||||
}
|
||||
case payment.TypeAlipay:
|
||||
if expected := strings.TrimSpace(snapshot.MerchantAppID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["app_id"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("alipay app_id missing")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("alipay app_id mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
case payment.TypeEasyPay:
|
||||
if expected := strings.TrimSpace(snapshot.MerchantID); expected != "" {
|
||||
actual := strings.TrimSpace(metadata["pid"])
|
||||
if actual == "" {
|
||||
return fmt.Errorf("easypay pid missing")
|
||||
}
|
||||
if !strings.EqualFold(expected, actual) {
|
||||
return fmt.Errorf("easypay pid mismatch: expected %s, got %s", expected, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func providerMerchantIdentityMetadata(prov payment.Provider) map[string]string {
|
||||
if prov == nil {
|
||||
return nil
|
||||
}
|
||||
reporter, ok := prov.(payment.MerchantIdentityProvider)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return reporter.MerchantIdentityMetadata()
|
||||
}
|
||||
|
||||
@@ -130,6 +130,40 @@ func TestBuildPaymentOrderProviderSnapshot_UsesWxpayJSAPIAppIDForOpenIDOrders(t
|
||||
require.Equal(t, "CNY", snapshot["currency"])
|
||||
}
|
||||
|
||||
func TestBuildPaymentOrderProviderSnapshot_IncludesAlipayMerchantIdentity(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
snapshot := buildPaymentOrderProviderSnapshot(&payment.InstanceSelection{
|
||||
InstanceID: "21",
|
||||
ProviderKey: payment.TypeAlipay,
|
||||
Config: map[string]string{
|
||||
"appId": "alipay-app-21",
|
||||
"privateKey": "secret",
|
||||
},
|
||||
PaymentMode: "redirect",
|
||||
}, CreateOrderRequest{})
|
||||
|
||||
require.Equal(t, "alipay-app-21", snapshot["merchant_app_id"])
|
||||
require.NotContains(t, snapshot, "privateKey")
|
||||
}
|
||||
|
||||
func TestBuildPaymentOrderProviderSnapshot_IncludesEasyPayMerchantIdentity(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
snapshot := buildPaymentOrderProviderSnapshot(&payment.InstanceSelection{
|
||||
InstanceID: "66",
|
||||
ProviderKey: payment.TypeEasyPay,
|
||||
Config: map[string]string{
|
||||
"pid": "easypay-merchant-66",
|
||||
"pkey": "secret",
|
||||
},
|
||||
PaymentMode: "popup",
|
||||
}, CreateOrderRequest{PaymentType: payment.TypeAlipay})
|
||||
|
||||
require.Equal(t, "easypay-merchant-66", snapshot["merchant_id"])
|
||||
require.NotContains(t, snapshot, "pkey")
|
||||
}
|
||||
|
||||
func valueOrEmpty(v *string) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
|
||||
@@ -333,6 +333,12 @@ func (s *PaymentService) gwRefund(ctx context.Context, p *RefundPlan) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("get refund provider: %w", err)
|
||||
}
|
||||
if err := validateProviderSnapshotMetadata(p.Order, prov.ProviderKey(), providerMerchantIdentityMetadata(prov)); err != nil {
|
||||
s.writeAuditLog(ctx, p.Order.ID, "REFUND_PROVIDER_METADATA_MISMATCH", "admin", map[string]any{
|
||||
"detail": err.Error(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
_, err = prov.Refund(ctx, payment.RefundRequest{
|
||||
TradeNo: p.Order.PaymentTradeNo,
|
||||
OrderID: p.Order.OutTradeNo,
|
||||
|
||||
@@ -4,6 +4,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -115,3 +116,71 @@ func TestPrepareRefundRejectsLegacyGuessedProviderInstance(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
require.Equal(t, "REFUND_DISABLED", infraerrors.Reason(err))
|
||||
}
|
||||
|
||||
func TestGwRefundRejectsAlipayMerchantIdentitySnapshotMismatch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client := newPaymentConfigServiceTestClient(t)
|
||||
|
||||
user, err := client.User.Create().
|
||||
SetEmail("refund-snapshot-mismatch@example.com").
|
||||
SetPasswordHash("hash").
|
||||
SetUsername("refund-snapshot-mismatch-user").
|
||||
Save(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
inst, err := client.PaymentProviderInstance.Create().
|
||||
SetProviderKey(payment.TypeAlipay).
|
||||
SetName("alipay-refund-mismatch-instance").
|
||||
SetConfig(encryptWebhookProviderConfig(t, map[string]string{
|
||||
"appId": "runtime-alipay-app",
|
||||
"privateKey": "runtime-private-key",
|
||||
})).
|
||||
SetSupportedTypes("alipay").
|
||||
SetEnabled(true).
|
||||
SetRefundEnabled(true).
|
||||
Save(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
instID := strconv.FormatInt(inst.ID, 10)
|
||||
order, err := client.PaymentOrder.Create().
|
||||
SetUserID(user.ID).
|
||||
SetUserEmail(user.Email).
|
||||
SetUserName(user.Username).
|
||||
SetAmount(88).
|
||||
SetPayAmount(88).
|
||||
SetFeeRate(0).
|
||||
SetRechargeCode("REFUND-SNAPSHOT-MISMATCH-ORDER").
|
||||
SetOutTradeNo("sub2_refund_snapshot_mismatch_order").
|
||||
SetPaymentType(payment.TypeAlipay).
|
||||
SetPaymentTradeNo("trade-refund-snapshot-mismatch").
|
||||
SetOrderType(payment.OrderTypeBalance).
|
||||
SetStatus(OrderStatusCompleted).
|
||||
SetExpiresAt(time.Now().Add(time.Hour)).
|
||||
SetPaidAt(time.Now()).
|
||||
SetClientIP("127.0.0.1").
|
||||
SetSrcHost("api.example.com").
|
||||
SetProviderInstanceID(instID).
|
||||
SetProviderKey(payment.TypeAlipay).
|
||||
SetProviderSnapshot(map[string]any{
|
||||
"schema_version": 2,
|
||||
"provider_instance_id": instID,
|
||||
"provider_key": payment.TypeAlipay,
|
||||
"merchant_app_id": "expected-alipay-app",
|
||||
}).
|
||||
Save(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
svc := &PaymentService{
|
||||
entClient: client,
|
||||
loadBalancer: newWebhookProviderTestLoadBalancer(client),
|
||||
}
|
||||
|
||||
err = svc.gwRefund(ctx, &RefundPlan{
|
||||
OrderID: order.ID,
|
||||
Order: order,
|
||||
RefundAmount: order.Amount,
|
||||
GatewayAmount: order.Amount,
|
||||
Reason: "snapshot mismatch",
|
||||
})
|
||||
require.ErrorContains(t, err, "alipay app_id mismatch")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user