|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "applet/app/md"
- "applet/app/utils"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/shopspring/decimal"
- "math"
- "math/rand"
- "time"
- )
-
- func BalanceCommunityTeam(c *gin.Context) (interface{}, error) {
-
- ord, err := CheckCommunityTeam(c)
- if err != nil || ord == nil {
- return nil, err
- }
- err = BalancePay(c, ord.Amount, utils.Int64ToStr(ord.Oid), md.CommunityTeam)
- if err != nil {
- return nil, err
- }
- // 回调
- CommonCallbackCommunityTeam(c, utils.AnyToString(ord.Oid), md.BALANCE_PAY)
- return nil, nil
- }
- func AlipayCommunityTeam(c *gin.Context) (interface{}, error) {
- ord, err := CheckCommunityTeam(c)
- if err != nil {
- return nil, err
- }
- payParams := &md.AliPayPayParams{
- Subject: "小店下单",
- Amount: ord.Amount,
- OrdId: utils.AnyToString(ord.Oid),
- OrderType: md.CommunityTeam,
- Uid: utils.IntToStr(ord.Uid),
- }
- r, err := PrepareAlipayCode(c, payParams)
- if err != nil {
- return nil, err
- }
- return r, nil
- }
- func WxPayCommunityTeam(c *gin.Context) (interface{}, error) {
- var r interface{}
- var err error
- ord, err := CheckCommunityTeam(c)
- if err != nil {
- return nil, err
- }
- params := map[string]string{
- "subject": md.NeedPayPart[md.AggregationRecharge],
- "amount": wxMoneyMulHundred(ord.Amount),
- "order_type": md.AggregationRecharge,
- "ord_id": utils.AnyToString(ord.Oid),
- "pay_wx_mch_id": SysCfgGet(c, "pay_wx_mch_id"),
- "pay_wx_api_key": SysCfgGet(c, "pay_wx_api_key"),
- "uid": utils.IntToStr(ord.Uid),
- }
- params["notify_url"] = fmt.Sprintf(md.CALLBACK_URL, c.Request.Host, c.GetString("mid"), params["order_type"], md.WX_PAY)
- r, err = CommPayData(c, params)
- if err != nil {
- return nil, err
- }
-
- return r, nil
- }
- func AlipayCallbackCommunityTeam(c *gin.Context) {
- orderId, err := AlipayCallback(c)
- if err != nil || orderId == "" {
- return
- }
- CommonCallbackCommunityTeam(c, orderId, md.ALIPAY)
- }
- func WxPayCallbackCommunityTeam(c *gin.Context) {
- orderId, err := wxPayCallback(c)
- if err != nil || orderId == "" {
- return
- }
- CommonCallbackCommunityTeam(c, orderId, md.WX_PAY)
- }
-
- // 微信金额乘100
- func wxMoneyMulHundred(m string) string {
- amount, _ := decimal.NewFromString(m)
- newM := amount.Mul(decimal.NewFromInt(100))
- return newM.String()
- }
- func CommonCallbackCommunityTeam(c *gin.Context, orderId string, payMethod string) {
- ord := db.GetOrderEg(db.DBs[c.GetString("mid")], orderId)
- if ord == nil {
- return
- }
- // 判断是否失效
- if ord.State != 0 {
- return
- }
- ord.State = 1
- ord.UpdateAt = time.Now()
- ord.Code = Code()
- ord.PayAt = time.Now()
- ord.PayMethod = md.PayMethodIDs[payMethod]
- MasterDb(c).Where("id=?", ord.Id).Cols("state,update_at,code,pay_at,pay_method").Update(ord)
- return
- }
- func Code() string {
- rand.Seed(time.Now().UnixNano())
- var digits = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
- b := make([]rune, 6)
- for i := range b {
- if fl := float64(rand.Intn(10)); fl > math.Log10(float64(i+1)) {
- b[i] = digits[rand.Intn(len(digits))]
- } else {
- b[i] = digits[rand.Intn(10)]
- }
- }
- fmt.Println(string(b))
- return string(b)
- }
- func CheckCommunityTeam(c *gin.Context) (*model.CommunityTeamOrder, error) {
- var args struct {
- MainOrdId string `json:"main_ord_id"`
- }
- if err := c.ShouldBindJSON(&args); err != nil || args.MainOrdId == "" {
- return nil, e.NewErrCode(e.ERR_INVALID_ARGS)
- }
- // 查询订单
- ord := db.GetOrderEg(db.DBs[c.GetString("mid")], args.MainOrdId)
- if ord == nil {
- return nil, e.NewErr(403000, "不存在该订单")
- }
- // 判断是否失效
- if ord.State != 0 {
- return nil, e.NewErr(403000, "订单已处理")
- }
- return ord, nil
- }
|