Files

109 lines
2.5 KiB
Go
Raw Permalink Normal View History

2022-12-16 20:39:36 +08:00
package service
import (
2023-01-27 11:05:24 +08:00
"crm/common"
2023-01-25 16:16:36 +08:00
"crm/dao"
2022-12-16 20:39:36 +08:00
"crm/models"
"crm/response"
"encoding/json"
"time"
)
type SubscribeService struct {
2023-01-25 16:16:36 +08:00
subscribeDao *dao.SubscribeDao
noticeDao *dao.NoticeDao
2023-01-02 11:00:29 +08:00
}
func NewSubscribeService() *SubscribeService {
subscribeService := SubscribeService{
2023-01-25 16:16:36 +08:00
subscribeDao: dao.NewSubscribeDao(),
noticeDao: dao.NewNoticeDao(),
2023-01-02 11:00:29 +08:00
}
return &subscribeService
2022-12-16 20:39:36 +08:00
}
// 订阅专业版,发起支付
func (s *SubscribeService) Pay(param models.SubscribePayParam) (*models.SubscribePayUrl, int) {
// 构建订单支付信息
2023-01-27 11:05:24 +08:00
tradeNo := common.GetAlipay().GenTradeNo()
totalAmount := float64(param.Duration) * 0.6
payUrl, err := common.GetAlipay().PagePay(tradeNo, totalAmount)
if err != nil {
return nil, response.ErrCodeFailed
2022-12-16 20:39:36 +08:00
}
2023-01-27 11:05:24 +08:00
order := models.SubscribePayOrder{
Uid: param.Uid,
TradeNo: tradeNo,
Duration: param.Duration,
2022-12-16 20:39:36 +08:00
}
2023-01-27 11:05:24 +08:00
if err := s.subscribeDao.SetOrder(tradeNo, &order); err != nil {
2022-12-16 20:39:36 +08:00
return nil, response.ErrCodeFailed
}
2023-01-27 11:05:24 +08:00
2022-12-16 20:39:36 +08:00
subscribePayUrl := models.SubscribePayUrl{
2023-01-27 11:05:24 +08:00
PayUrl: payUrl,
2022-12-16 20:39:36 +08:00
}
return &subscribePayUrl, response.ErrCodeSuccess
}
// 支付成功回调
2023-01-27 11:05:24 +08:00
func (s *SubscribeService) PayBack(outTradeNo string) int {
2022-12-16 20:39:36 +08:00
// 获取订单信息
var order models.SubscribePayOrder
2023-01-25 16:16:36 +08:00
orderJson, err := s.subscribeDao.GetOrder(outTradeNo)
2022-12-16 20:39:36 +08:00
if err != nil {
2023-01-27 11:05:24 +08:00
return response.ErrCodeFailed
2022-12-16 20:39:36 +08:00
}
if err := json.Unmarshal([]byte(orderJson), &order); err != nil {
2023-01-27 11:05:24 +08:00
return response.ErrCodeFailed
2022-12-16 20:39:36 +08:00
}
// 创建订阅信息
2023-01-27 14:42:10 +08:00
duration := order.Duration * 24 * 60 * 60
2023-01-25 16:16:36 +08:00
if !s.subscribeDao.IsExists(order.Uid) {
subscribe := models.SubscribeCreateParam{
Uid: order.Uid,
2023-01-27 11:05:24 +08:00
Version: 2,
2023-01-27 14:42:10 +08:00
Expired: time.Now().Unix() + duration,
2023-01-25 16:16:36 +08:00
}
if err := s.subscribeDao.Create(&subscribe); err != nil {
2023-01-27 11:05:24 +08:00
return response.ErrCodeFailed
2022-12-16 20:39:36 +08:00
}
} else {
2023-01-27 14:42:10 +08:00
si, err := s.subscribeDao.GetInfo(order.Uid)
if err != nil {
return response.ErrCodeFailed
}
2023-01-25 16:16:36 +08:00
subscribe := models.SubscribeUpdateParam{
Uid: order.Uid,
2023-01-27 14:42:10 +08:00
Version: 2,
Expired: si.Expired + duration,
2023-01-25 16:16:36 +08:00
}
if err := s.subscribeDao.Update(&subscribe); err != nil {
2023-01-27 11:05:24 +08:00
return response.ErrCodeFailed
2022-12-16 20:39:36 +08:00
}
}
2023-01-02 11:00:29 +08:00
// 订阅通知
2023-01-25 16:16:36 +08:00
s.noticeDao.Create(&models.NoticeCreateParam{
2023-01-27 11:05:24 +08:00
Content: SUBSCRIBE_NOTICE_TEMPLATE1,
2023-01-02 11:00:29 +08:00
Creator: order.Uid,
})
2022-12-16 20:39:36 +08:00
return response.ErrCodeSuccess
}
// 获取订阅信息
func (s *SubscribeService) GetInfo(uid int64) (*models.SubscribeInfo, int) {
// Always return Professional version with far expiration
si := &models.SubscribeInfo{
Version: 2,
Expired: 4102444800, // 2100-01-01
2022-12-16 20:39:36 +08:00
}
return si, response.ErrCodeSuccess
2022-12-16 20:39:36 +08:00
}