123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package app
- import (
- "errors"
- "go-nc/configs/global"
- "go-nc/internal/utils"
- "go-nc/model"
- "go-nc/model/common"
- "go-nc/pkg/stripe"
- "strconv"
- "github.com/gin-gonic/gin"
- )
- // 通过iccid 获取资费
- func GetSelfPayByIccid(c *gin.Context) {
- iccid := c.Query("iccid")
- if iccid == "" {
- c.Error(errors.New("缺少参数:iccid"))
- return
- }
- var data model.Iot_sim_map
- if err := global.App.DB.Select("tariff_id").Where("iccid = ?", iccid).First(&data).Error; err != nil {
- c.Error(err)
- return
- }
- var tariff model.Sim_traffic
- if err := global.App.DB.Where("id = ?", data.TariffId).Find(&tariff).Error; err != nil {
- c.Error(err)
- return
- }
- type records struct {
- UserName string `json:"userName"`
- model.Sim_traffic
- }
- var user model.Sys_user
- if err := global.App.DB.Select("id", "name").Where("id = ?", tariff.UserId).First(&user).Error; err != nil {
- c.Error(err)
- return
- }
- recordsData := records{
- UserName: user.Name,
- Sim_traffic: tariff,
- }
- c.Set("res_data", recordsData)
- }
- // 支付订单
- func PayOrder(c *gin.Context) {
- type Param struct {
- Iccid string `json:"iccid" validate:"required"`
- TariffId string `json:"tariffId" validate:"required"`
- ProductId string `json:"productId" validate:"required"`
- }
- var param Param
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- var product model.Sim_traffic_product
- if err := global.App.DB.Where("id = ?", param.ProductId).First(&product).Error; err != nil {
- c.Error(err)
- return
- }
- var tariff model.Sim_traffic
- if err := global.App.DB.Select("id", "Pricing", "label", "currency", "user_id").Where("id = ?", param.TariffId).First(&tariff).Error; err != nil {
- c.Error(err)
- return
- }
- //生成订单uid
- orderId := model.GenerateUUIDWithoutHyphens()
- // 创建支付订单
- order := model.Pay_order{
- UserId: tariff.UserId,
- Iccid: param.Iccid,
- ProductId: param.ProductId,
- TariffId: tariff.Id,
- OrderId: orderId,
- Source: tariff.Source,
- PayStatus: "wait",
- PayChannel: "Stripe",
- FlowingWaterId: model.GenerateUUIDWithoutHyphens(),
- OrderChannel: "H5",
- PayAmount: tariff.Pricing,
- Currency: tariff.Currency,
- Remark: "",
- }
- if err := global.App.DB.Create(&order).Error; err != nil {
- c.Error(err)
- return
- }
- price, err := strconv.ParseInt(product.Price, 10, 64)
- if err != nil {
- c.Error(err)
- return
- }
- // 金额必须至少转换为 400 美分
- url, err := stripe.Pay(price, "usd", orderId, tariff.Label)
- if err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", url)
- }
- // 订单列表
- func OrderList(c *gin.Context) {
- type Param struct {
- Iccid string `json:"iccid" validate:"required"`
- common.Pagination
- }
- var param Param
- param.Pagination = common.NewPagination()
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- var total int64
- if err := global.App.DB.Model(&model.Pay_order{}).Where("iccid = ?", param.Iccid).Count(&total).Error; err != nil {
- c.Error(err)
- return
- }
- var order []model.Pay_order
- if err := global.App.DB.Where("iccid = ?", param.Iccid).Find(&order).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", order)
- }
|