Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
实现Apple应用内购支付功能,包括: 1. 新增AppleIAP和ApplePay支付平台枚举 2. 添加IAP验证接口/v1/public/iap/verify处理初购验证 3. 实现Apple服务器通知处理逻辑/v1/iap/notifications 4. 新增JWS验签和JWKS公钥缓存功能 5. 复用现有订单系统处理IAP支付订单 相关文档已更新,包含接入方案和实现细节
30 lines
963 B
Go
30 lines
963 B
Go
package appleiap
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
func verifyWithEnv(env, token string) (jwt.MapClaims, error) {
|
|
parsed, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
|
|
h, ok := t.Header["kid"].(string)
|
|
if !ok { return nil, errors.New("kid missing") }
|
|
return GetKey(env, h)
|
|
})
|
|
if err != nil { return nil, err }
|
|
if !parsed.Valid { return nil, errors.New("invalid jws") }
|
|
c, ok := parsed.Claims.(jwt.MapClaims)
|
|
if !ok { return nil, errors.New("claims invalid") }
|
|
return c, nil
|
|
}
|
|
|
|
func VerifyWithEnv(env, token string) (jwt.MapClaims, error) { return verifyWithEnv(env, token) }
|
|
|
|
func VerifyAutoEnv(token string) (jwt.MapClaims, string, error) {
|
|
c, err := verifyWithEnv("production", token)
|
|
if err == nil { return c, "production", nil }
|
|
c2, err2 := verifyWithEnv("sandbox", token)
|
|
if err2 == nil { return c2, "sandbox", nil }
|
|
return nil, "", err
|
|
}
|