shanshanzhong 62186ca672
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m37s
feat(iap/apple): 实现苹果IAP非续期订阅功能
新增苹果IAP相关接口与逻辑,包括产品列表查询、交易绑定、状态查询和恢复购买功能。移除旧的IAP验证逻辑,重构订阅系统以支持苹果IAP交易记录存储和权益计算。

- 新增/pkg/iap/apple包处理JWS解析和产品映射
- 实现GET /products、POST /attach、POST /restore和GET /status接口
- 新增apple_iap_transactions表存储交易记录
- 更新文档说明配置方式和接口规范
- 移除旧的AppleIAP验证和通知处理逻辑
2025-12-13 20:54:50 -08:00

69 lines
1.9 KiB
Go

package apple
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/perfect-panel/server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type Model interface {
Insert(ctx context.Context, data *Transaction, tx ...*gorm.DB) error
FindByOriginalId(ctx context.Context, originalId string) (*Transaction, error)
FindByUserAndProduct(ctx context.Context, userId int64, productId string) (*Transaction, error)
}
type defaultModel struct {
cache.CachedConn
table string
}
type customModel struct {
*defaultModel
}
func NewModel(db *gorm.DB, c *redis.Client) Model {
return &customModel{
defaultModel: &defaultModel{
CachedConn: cache.NewConn(db, c),
table: "`apple_iap_transactions`",
},
}
}
func (m *defaultModel) jwsKey(jws string) string {
sum := sha256.Sum256([]byte(jws))
return fmt.Sprintf("cache:iap:jws:%s", hex.EncodeToString(sum[:]))
}
func (m *customModel) Insert(ctx context.Context, data *Transaction, tx ...*gorm.DB) error {
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
if len(tx) > 0 {
conn = tx[0]
}
return conn.Model(&Transaction{}).Create(data).Error
}, m.jwsKey(data.JWSHash))
}
func (m *customModel) FindByOriginalId(ctx context.Context, originalId string) (*Transaction, error) {
var data Transaction
key := fmt.Sprintf("cache:iap:original:%s", originalId)
err := m.QueryCtx(ctx, &data, key, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Transaction{}).Where("original_transaction_id = ?", originalId).First(&data).Error
})
return &data, err
}
func (m *customModel) FindByUserAndProduct(ctx context.Context, userId int64, productId string) (*Transaction, error) {
var data Transaction
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Transaction{}).Where("user_id = ? AND product_id = ?", userId, productId).Order("purchase_at DESC").First(&data).Error
})
return &data, err
}