附近小店
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.
 
 
 

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