feat(iap/apple): 实现苹果IAP非续期订阅功能
Build docker and publish / build (20.15.1) (push) Successful in 6m37s
Build docker and publish / build (20.15.1) (push) Successful in 6m37s
新增苹果IAP相关接口与逻辑,包括产品列表查询、交易绑定、状态查询和恢复购买功能。移除旧的IAP验证逻辑,重构订阅系统以支持苹果IAP交易记录存储和权益计算。 - 新增/pkg/iap/apple包处理JWS解析和产品映射 - 实现GET /products、POST /attach、POST /restore和GET /status接口 - 新增apple_iap_transactions表存储交易记录 - 更新文档说明配置方式和接口规范 - 移除旧的AppleIAP验证和通知处理逻辑
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package apple
|
||||
|
||||
import "time"
|
||||
|
||||
type Transaction struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"`
|
||||
OriginalTransactionId string `gorm:"type:varchar(255);uniqueIndex:uni_original;not null;comment:Original Transaction ID"`
|
||||
TransactionId string `gorm:"type:varchar(255);not null;comment:Transaction ID"`
|
||||
ProductId string `gorm:"type:varchar(255);not null;comment:Product ID"`
|
||||
PurchaseAt time.Time `gorm:"not null;comment:Purchase Time"`
|
||||
RevocationAt *time.Time `gorm:"comment:Revocation Time"`
|
||||
JWSHash string `gorm:"type:varchar(255);not null;comment:JWS Hash"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (Transaction) TableName() string {
|
||||
return "apple_iap_transactions"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user