35 lines
1.1 KiB
Go
35 lines
1.1 KiB
Go
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
|
||
}
|