feat: 互动处理
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package radio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/radio"
|
||||
"sundynix-go/model/radio/request"
|
||||
"sundynix-go/model/system"
|
||||
"sundynix-go/utils/uniqueid"
|
||||
"sundynix-go/utils/wechat"
|
||||
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
||||
)
|
||||
|
||||
type VipService struct{}
|
||||
|
||||
func (s *VipService) UpdateVipConfig(req request.UpdateVipConfig) error {
|
||||
updateData := map[string]interface{}{
|
||||
"price": req.Price,
|
||||
"discountedPrice": req.DiscountedPrice,
|
||||
"remark": req.Remark,
|
||||
}
|
||||
err := global.DB.Model(&radio.Vip{}).Where("id = ?", req.Id).Updates(updateData).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *VipService) VipConfigDetail() (radio.Vip, error) {
|
||||
var vip radio.Vip
|
||||
err := global.DB.Model(&radio.Vip{}).First(&vip).Error
|
||||
return vip, err
|
||||
}
|
||||
|
||||
// VipVip 开通vip
|
||||
func (s *VipService) VipVip(userId string) (*jsapi.PrepayWithRequestPaymentResponse, string, error) {
|
||||
//1.查询vip配置
|
||||
var config radio.Vip
|
||||
err := global.DB.Model(&radio.Vip{}).First(&config).Error
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
//2.创建订单
|
||||
order := radio.Order{
|
||||
Type: 2, //很重要 1订阅 2开通vip
|
||||
UserId: userId,
|
||||
OutTradeNo: uniqueid.GenOrderNo(),
|
||||
SubscriptionType: "vip sub",
|
||||
Amount: config.DiscountedPrice,
|
||||
Name: "vip sub",
|
||||
}
|
||||
err = global.DB.Create(&order).Error
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
//3.调用微信api 拉起支付
|
||||
var user system.User
|
||||
err = global.DB.Where("id = ?", userId).First(&user).Error
|
||||
if err != nil || user.OpenId == "" {
|
||||
return nil, "", err
|
||||
}
|
||||
payClient, err := wechat.GetWxPayClient()
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
svc := jsapi.JsapiApiService{Client: payClient}
|
||||
result, _, err := svc.PrepayWithRequestPayment(context.Background(),
|
||||
jsapi.PrepayRequest{
|
||||
Appid: core.String(global.Config.MiniProgram.AppId),
|
||||
Mchid: core.String(global.Config.WechatPay.MchId),
|
||||
Description: core.String(order.Name),
|
||||
OutTradeNo: core.String(order.OutTradeNo),
|
||||
//TimeExpire: core.Time(time.Now()), //选填
|
||||
//Attach: core.String("自定义数据说明"), //选填
|
||||
NotifyUrl: core.String(global.Config.WechatPay.NotifyUrl),
|
||||
//GoodsTag: core.String("WXG"), //选填
|
||||
//SupportFapiao: core.Bool(false), //选填
|
||||
Amount: &jsapi.Amount{
|
||||
Currency: core.String("CNY"),
|
||||
Total: core.Int64(int64(config.DiscountedPrice)),
|
||||
},
|
||||
Payer: &jsapi.Payer{
|
||||
Openid: core.String(user.OpenId),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return result, order.OutTradeNo, nil
|
||||
}
|
||||
Reference in New Issue
Block a user