app.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package app
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/internal/utils"
  6. "go-nc/model"
  7. "go-nc/model/common"
  8. "go-nc/pkg/stripe"
  9. "strconv"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // 通过iccid 获取资费
  13. func GetSelfPayByIccid(c *gin.Context) {
  14. iccid := c.Query("iccid")
  15. if iccid == "" {
  16. c.Error(errors.New("缺少参数:iccid"))
  17. return
  18. }
  19. var data model.Iot_sim_map
  20. if err := global.App.DB.Select("tariff_id").Where("iccid = ?", iccid).First(&data).Error; err != nil {
  21. c.Error(err)
  22. return
  23. }
  24. var tariff model.Sim_traffic
  25. if err := global.App.DB.Where("id = ?", data.TariffId).Find(&tariff).Error; err != nil {
  26. c.Error(err)
  27. return
  28. }
  29. type records struct {
  30. UserName string `json:"userName"`
  31. model.Sim_traffic
  32. }
  33. var user model.Sys_user
  34. if err := global.App.DB.Select("id", "name").Where("id = ?", tariff.UserId).First(&user).Error; err != nil {
  35. c.Error(err)
  36. return
  37. }
  38. recordsData := records{
  39. UserName: user.Name,
  40. Sim_traffic: tariff,
  41. }
  42. c.Set("res_data", recordsData)
  43. }
  44. // 支付订单
  45. func PayOrder(c *gin.Context) {
  46. type Param struct {
  47. Iccid string `json:"iccid" validate:"required"`
  48. TariffId string `json:"tariffId" validate:"required"`
  49. ProductId string `json:"productId" validate:"required"`
  50. }
  51. var param Param
  52. if err := c.ShouldBindJSON(&param); err != nil {
  53. c.Error(errors.New("缺少参数:" + err.Error()))
  54. return
  55. }
  56. err := utils.ValidateStruct(param)
  57. if err != nil {
  58. c.Error(err)
  59. return
  60. }
  61. var product model.Sim_traffic_product
  62. if err := global.App.DB.Where("id = ?", param.ProductId).First(&product).Error; err != nil {
  63. c.Error(err)
  64. return
  65. }
  66. var tariff model.Sim_traffic
  67. if err := global.App.DB.Select("id", "Pricing", "label", "currency", "user_id").Where("id = ?", param.TariffId).First(&tariff).Error; err != nil {
  68. c.Error(err)
  69. return
  70. }
  71. //生成订单uid
  72. orderId := model.GenerateUUIDWithoutHyphens()
  73. // 创建支付订单
  74. order := model.Pay_order{
  75. UserId: tariff.UserId,
  76. Iccid: param.Iccid,
  77. ProductId: param.ProductId,
  78. TariffId: tariff.Id,
  79. OrderId: orderId,
  80. Source: tariff.Source,
  81. PayStatus: "wait",
  82. PayChannel: "Stripe",
  83. FlowingWaterId: model.GenerateUUIDWithoutHyphens(),
  84. OrderChannel: "H5",
  85. PayAmount: tariff.Pricing,
  86. Currency: tariff.Currency,
  87. Remark: "",
  88. }
  89. if err := global.App.DB.Create(&order).Error; err != nil {
  90. c.Error(err)
  91. return
  92. }
  93. price, err := strconv.ParseInt(product.Price, 10, 64)
  94. if err != nil {
  95. c.Error(err)
  96. return
  97. }
  98. // 金额必须至少转换为 400 美分
  99. url, err := stripe.Pay(price, "usd", orderId, tariff.Label)
  100. if err != nil {
  101. c.Error(err)
  102. return
  103. }
  104. c.Set("res_data", url)
  105. }
  106. // 订单列表
  107. func OrderList(c *gin.Context) {
  108. type Param struct {
  109. Iccid string `json:"iccid" validate:"required"`
  110. common.Pagination
  111. }
  112. var param Param
  113. param.Pagination = common.NewPagination()
  114. if err := c.ShouldBindJSON(&param); err != nil {
  115. c.Error(errors.New("缺少参数:" + err.Error()))
  116. return
  117. }
  118. err := utils.ValidateStruct(param)
  119. if err != nil {
  120. c.Error(err)
  121. return
  122. }
  123. var total int64
  124. if err := global.App.DB.Model(&model.Pay_order{}).Where("iccid = ?", param.Iccid).Count(&total).Error; err != nil {
  125. c.Error(err)
  126. return
  127. }
  128. var order []model.Pay_order
  129. if err := global.App.DB.Where("iccid = ?", param.Iccid).Find(&order).Error; err != nil {
  130. c.Error(err)
  131. return
  132. }
  133. c.Set("res_data", order)
  134. }