task: 完成支付宝共享基础(迁移/WechatConfig/PaymentStore/pkg/alipay)
This commit is contained in:
34
pkg/alipay/notify.go
Normal file
34
pkg/alipay/notify.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package alipay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
)
|
||||
|
||||
// DecodeNotification 对支付宝异步回调进行验签并解析通知内容。
|
||||
// values 为原始 form body 构建的 url.Values,调用方负责解析请求体。
|
||||
// 验签使用创建支付单时的 WechatConfig 中的 ali_public_key。
|
||||
// 验签失败时返回 error,不推进业务。
|
||||
func DecodeNotification(ctx context.Context, cfg *model.WechatConfig, values url.Values) (*alipay.Notification, error) {
|
||||
client, err := NewClientFromConfig(cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("构建支付宝客户端失败: %w", err)
|
||||
}
|
||||
|
||||
notification, err := client.DecodeNotification(ctx, values)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("支付宝回调验签失败: %w", err)
|
||||
}
|
||||
|
||||
return notification, nil
|
||||
}
|
||||
|
||||
// TradeStatusSuccess 判断交易状态是否为支付成功(TRADE_SUCCESS 或 TRADE_FINISHED)
|
||||
func TradeStatusSuccess(status alipay.TradeStatus) bool {
|
||||
return status == alipay.TradeStatusSuccess || status == alipay.TradeStatusFinished
|
||||
}
|
||||
Reference in New Issue
Block a user