fix: snapshot merchant identity for alipay and easypay
This commit is contained in:
@@ -91,6 +91,17 @@ func (a *Alipay) SupportedTypes() []payment.PaymentType {
|
||||
return []payment.PaymentType{payment.TypeAlipay}
|
||||
}
|
||||
|
||||
func (a *Alipay) MerchantIdentityMetadata() map[string]string {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
appID := strings.TrimSpace(a.config["appId"])
|
||||
if appID == "" {
|
||||
return nil
|
||||
}
|
||||
return map[string]string{"app_id": appID}
|
||||
}
|
||||
|
||||
// CreatePayment creates an Alipay payment page URL.
|
||||
func (a *Alipay) CreatePayment(ctx context.Context, req payment.CreatePaymentRequest) (*payment.CreatePaymentResponse, error) {
|
||||
client, err := a.getClient()
|
||||
@@ -181,10 +192,11 @@ func (a *Alipay) QueryOrder(ctx context.Context, tradeNo string) (*payment.Query
|
||||
}
|
||||
|
||||
return &payment.QueryOrderResponse{
|
||||
TradeNo: result.TradeNo,
|
||||
Status: status,
|
||||
Amount: amount,
|
||||
PaidAt: result.SendPayDate,
|
||||
TradeNo: result.TradeNo,
|
||||
Status: status,
|
||||
Amount: amount,
|
||||
PaidAt: result.SendPayDate,
|
||||
Metadata: a.MerchantIdentityMetadata(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -215,12 +227,21 @@ func (a *Alipay) VerifyNotification(ctx context.Context, rawBody string, _ map[s
|
||||
return nil, fmt.Errorf("alipay parse notification amount %q: %w", notification.TotalAmount, err)
|
||||
}
|
||||
|
||||
metadata := a.MerchantIdentityMetadata()
|
||||
if appID := strings.TrimSpace(notification.AppId); appID != "" {
|
||||
if metadata == nil {
|
||||
metadata = map[string]string{}
|
||||
}
|
||||
metadata["app_id"] = appID
|
||||
}
|
||||
|
||||
return &payment.PaymentNotification{
|
||||
TradeNo: notification.TradeNo,
|
||||
OrderID: notification.OutTradeNo,
|
||||
Amount: amount,
|
||||
Status: status,
|
||||
RawData: rawBody,
|
||||
TradeNo: notification.TradeNo,
|
||||
OrderID: notification.OutTradeNo,
|
||||
Amount: amount,
|
||||
Status: status,
|
||||
RawData: rawBody,
|
||||
Metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -283,6 +304,7 @@ func isTradeNotExist(err error) bool {
|
||||
|
||||
// Ensure interface compliance.
|
||||
var (
|
||||
_ payment.Provider = (*Alipay)(nil)
|
||||
_ payment.CancelableProvider = (*Alipay)(nil)
|
||||
_ payment.Provider = (*Alipay)(nil)
|
||||
_ payment.CancelableProvider = (*Alipay)(nil)
|
||||
_ payment.MerchantIdentityProvider = (*Alipay)(nil)
|
||||
)
|
||||
|
||||
@@ -243,3 +243,18 @@ func TestCreateTradeUsesWapPayForMobile(t *testing.T) {
|
||||
t.Fatalf("qr_code = %q, want empty", resp.QRCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlipayMerchantIdentityMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
provider := &Alipay{
|
||||
config: map[string]string{
|
||||
"appId": "2021001234567890",
|
||||
},
|
||||
}
|
||||
|
||||
metadata := provider.MerchantIdentityMetadata()
|
||||
if metadata["app_id"] != "2021001234567890" {
|
||||
t.Fatalf("app_id = %q, want %q", metadata["app_id"], "2021001234567890")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,17 @@ func (e *EasyPay) SupportedTypes() []payment.PaymentType {
|
||||
return []payment.PaymentType{payment.TypeAlipay, payment.TypeWxpay}
|
||||
}
|
||||
|
||||
func (e *EasyPay) MerchantIdentityMetadata() map[string]string {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
pid := strings.TrimSpace(e.config["pid"])
|
||||
if pid == "" {
|
||||
return nil
|
||||
}
|
||||
return map[string]string{"pid": pid}
|
||||
}
|
||||
|
||||
func (e *EasyPay) CreatePayment(ctx context.Context, req payment.CreatePaymentRequest) (*payment.CreatePaymentResponse, error) {
|
||||
// Payment mode determined by instance config, not payment type.
|
||||
// "popup" → hosted page (submit.php); "qrcode"/default → API call (mapi.php).
|
||||
@@ -178,7 +189,12 @@ func (e *EasyPay) QueryOrder(ctx context.Context, tradeNo string) (*payment.Quer
|
||||
status = payment.ProviderStatusPaid
|
||||
}
|
||||
amount, _ := strconv.ParseFloat(resp.Money, 64)
|
||||
return &payment.QueryOrderResponse{TradeNo: tradeNo, Status: status, Amount: amount}, nil
|
||||
return &payment.QueryOrderResponse{
|
||||
TradeNo: tradeNo,
|
||||
Status: status,
|
||||
Amount: amount,
|
||||
Metadata: e.MerchantIdentityMetadata(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (e *EasyPay) VerifyNotification(_ context.Context, rawBody string, _ map[string]string) (*payment.PaymentNotification, error) {
|
||||
@@ -203,9 +219,17 @@ func (e *EasyPay) VerifyNotification(_ context.Context, rawBody string, _ map[st
|
||||
status = payment.ProviderStatusSuccess
|
||||
}
|
||||
amount, _ := strconv.ParseFloat(params["money"], 64)
|
||||
|
||||
metadata := e.MerchantIdentityMetadata()
|
||||
if pid := strings.TrimSpace(params["pid"]); pid != "" {
|
||||
if metadata == nil {
|
||||
metadata = map[string]string{}
|
||||
}
|
||||
metadata["pid"] = pid
|
||||
}
|
||||
return &payment.PaymentNotification{
|
||||
TradeNo: params["trade_no"], OrderID: params["out_trade_no"],
|
||||
Amount: amount, Status: status, RawData: rawBody,
|
||||
Amount: amount, Status: status, RawData: rawBody, Metadata: metadata,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -178,3 +178,18 @@ func TestEasyPayVerifySignWrongSignValue(t *testing.T) {
|
||||
t.Fatal("easyPayVerifySign should return false for an incorrect sign value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEasyPayMerchantIdentityMetadata(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
provider := &EasyPay{
|
||||
config: map[string]string{
|
||||
"pid": "1001",
|
||||
},
|
||||
}
|
||||
|
||||
metadata := provider.MerchantIdentityMetadata()
|
||||
if metadata["pid"] != "1001" {
|
||||
t.Fatalf("pid = %q, want %q", metadata["pid"], "1001")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ func TestBuildWxpayTransactionMetadata(t *testing.T) {
|
||||
Appid: strPtr("wx-app-id"),
|
||||
Mchid: strPtr("mch-id"),
|
||||
TradeState: strPtr(wxpayTradeStateSuccess),
|
||||
Amount: &payments.Amount{
|
||||
Amount: &payments.TransactionAmount{
|
||||
Currency: strPtr(wxpayCurrency),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -214,3 +214,9 @@ type CancelableProvider interface {
|
||||
// CancelPayment cancels/expires a pending payment on the upstream platform.
|
||||
CancelPayment(ctx context.Context, tradeNo string) error
|
||||
}
|
||||
|
||||
// MerchantIdentityProvider exposes the current non-sensitive merchant identity
|
||||
// derived from provider configuration for snapshot consistency checks.
|
||||
type MerchantIdentityProvider interface {
|
||||
MerchantIdentityMetadata() map[string]string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user