附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
3.6 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/shopspring/decimal"
  11. "math"
  12. "math/rand"
  13. "time"
  14. )
  15. func BalanceCommunityTeam(c *gin.Context) (interface{}, error) {
  16. ord, err := CheckCommunityTeam(c)
  17. if err != nil || ord == nil {
  18. return nil, err
  19. }
  20. err = BalancePay(c, ord.Amount, utils.Int64ToStr(ord.Oid), md.CommunityTeam)
  21. if err != nil {
  22. return nil, err
  23. }
  24. // 回调
  25. CommonCallbackCommunityTeam(c, utils.AnyToString(ord.Oid), md.BALANCE_PAY)
  26. return nil, nil
  27. }
  28. func AlipayCommunityTeam(c *gin.Context) (interface{}, error) {
  29. ord, err := CheckCommunityTeam(c)
  30. if err != nil {
  31. return nil, err
  32. }
  33. payParams := &md.AliPayPayParams{
  34. Subject: "小店下单",
  35. Amount: ord.Amount,
  36. OrdId: utils.AnyToString(ord.Oid),
  37. OrderType: md.CommunityTeam,
  38. Uid: utils.IntToStr(ord.Uid),
  39. }
  40. r, err := PrepareAlipayCode(c, payParams)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return r, nil
  45. }
  46. func WxPayCommunityTeam(c *gin.Context) (interface{}, error) {
  47. var r interface{}
  48. var err error
  49. ord, err := CheckCommunityTeam(c)
  50. if err != nil {
  51. return nil, err
  52. }
  53. params := map[string]string{
  54. "subject": md.NeedPayPart[md.AggregationRecharge],
  55. "amount": wxMoneyMulHundred(ord.Amount),
  56. "order_type": md.AggregationRecharge,
  57. "ord_id": utils.AnyToString(ord.Oid),
  58. "pay_wx_mch_id": SysCfgGet(c, "pay_wx_mch_id"),
  59. "pay_wx_api_key": SysCfgGet(c, "pay_wx_api_key"),
  60. "uid": utils.IntToStr(ord.Uid),
  61. }
  62. params["notify_url"] = fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params["order_type"], md.WX_PAY)
  63. r, err = CommPayData(c, params)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return r, nil
  68. }
  69. func AlipayCallbackCommunityTeam(c *gin.Context) {
  70. orderId, err := AlipayCallback(c)
  71. if err != nil || orderId == "" {
  72. return
  73. }
  74. CommonCallbackCommunityTeam(c, orderId, md.ALIPAY)
  75. }
  76. func WxPayCallbackCommunityTeam(c *gin.Context) {
  77. orderId, err := wxPayCallback(c)
  78. if err != nil || orderId == "" {
  79. return
  80. }
  81. CommonCallbackCommunityTeam(c, orderId, md.WX_PAY)
  82. }
  83. // 微信金额乘100
  84. func wxMoneyMulHundred(m string) string {
  85. amount, _ := decimal.NewFromString(m)
  86. newM := amount.Mul(decimal.NewFromInt(100))
  87. return newM.String()
  88. }
  89. func CommonCallbackCommunityTeam(c *gin.Context, orderId string, payMethod string) {
  90. ord := db.GetOrderEg(db.DBs[c.GetString("mid")], orderId)
  91. if ord == nil {
  92. return
  93. }
  94. // 判断是否失效
  95. if ord.State != 0 {
  96. return
  97. }
  98. ord.State = 1
  99. ord.UpdateAt = time.Now()
  100. ord.Code = Code()
  101. ord.PayAt = time.Now()
  102. ord.PayMethod = md.PayMethodIDs[payMethod]
  103. MasterDb(c).Where("id=?", ord.Id).Cols("state,update_at,code,pay_at,pay_method").Update(ord)
  104. return
  105. }
  106. func Code() string {
  107. rand.Seed(time.Now().UnixNano())
  108. var digits = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
  109. b := make([]rune, 6)
  110. for i := range b {
  111. if fl := float64(rand.Intn(10)); fl > math.Log10(float64(i+1)) {
  112. b[i] = digits[rand.Intn(len(digits))]
  113. } else {
  114. b[i] = digits[rand.Intn(10)]
  115. }
  116. }
  117. fmt.Println(string(b))
  118. return string(b)
  119. }
  120. func CheckCommunityTeam(c *gin.Context) (*model.CommunityTeamOrder, error) {
  121. var args struct {
  122. MainOrdId string `json:"main_ord_id"`
  123. }
  124. if err := c.ShouldBindJSON(&args); err != nil || args.MainOrdId == "" {
  125. return nil, e.NewErrCode(e.ERR_INVALID_ARGS)
  126. }
  127. // 查询订单
  128. ord := db.GetOrderEg(db.DBs[c.GetString("mid")], args.MainOrdId)
  129. if ord == nil {
  130. return nil, e.NewErr(403000, "不存在该订单")
  131. }
  132. // 判断是否失效
  133. if ord.State != 0 {
  134. return nil, e.NewErr(403000, "订单已处理")
  135. }
  136. return ord, nil
  137. }