| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- package liu
- import (
- "bet24.com/log"
- "bet24.com/public"
- notification "bet24.com/servers/micros/notification/proto"
- "bet24.com/servers/payment/config"
- "bet24.com/servers/payment/db"
- "encoding/json"
- "github.com/gin-gonic/gin"
- uuid "github.com/satori/go.uuid"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- )
- // 提现请求
- func WithdrawOrder(c *gin.Context) {
- obj := db.NewLiuWithdrawReq()
- if err := c.ShouldBind(&obj.In); err != nil {
- log.Debug("%s query params err %v", "liu.WithdrawOrder", err)
- c.String(http.StatusOK, "fail")
- return
- }
- obj.In.IPAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
- obj.DoAction()
- if obj.Out.OrderID == "" {
- log.Debug("%s GenOrder OrderId is empty fail obj.In=%+v obj.Out=%+v", "liu.WithdrawOrder", obj.In, obj.Out)
- c.String(http.StatusOK, "fail")
- return
- }
- // 下单失败
- if obj.Out.RetCode != 1 {
- log.Debug("%s GenOrder fail obj.In=%+v obj.Out=%+v", "liu.WithdrawOrder", obj.In, obj.Out)
- c.String(http.StatusOK, "fail")
- return
- }
- // 下单成功,待审核
- if obj.Out.GetStatus != 0 {
- log.Debug("%s GenOrder success obj.In=%+v obj.Out=%+v", "liu.WithdrawOrder", obj.In, obj.Out)
- c.String(http.StatusOK, "success")
- return
- }
- // 请求时,没有传手机号
- if obj.In.Mobile == "" {
- obj.In.Mobile = obj.Out.Tel
- }
- // 请求withdrawOrder的代码
- req := withdraw_req{
- MerchantCode: config.Server.LiuPay.MerchantCode,
- OrderNo: obj.Out.OrderID,
- Type: 1,
- AccNo: obj.In.BankCard,
- AccName: obj.In.RealName,
- Amount: obj.In.Amount,
- BankName: obj.In.BankName,
- BankBranch: "1",
- Province: "2",
- City: "3",
- Ccy_no: "IDR",
- }
- params := url.Values{}
- params.Set("merchantCode", req.MerchantCode)
- params.Set("orderNo", req.OrderNo)
- params.Set("type", strconv.Itoa(req.Type))
- params.Set("accNo", req.AccNo)
- params.Set("accName", req.AccName)
- params.Set("amount", strconv.Itoa(req.Amount))
- params.Set("bankName", req.BankName)
- params.Set("bankBranch", req.BankBranch)
- params.Set("province", req.Province)
- params.Set("city", req.City)
- params.Set("ccy_no", req.Ccy_no)
- // 生成签名
- checkContent := createEncryptStr(params) + "&key=" + config.Server.LiuPay.MD5Key
- // log.Debug("liu.WithdrawOrder.checkContent ==> %s", checkContent)
- signature := strings.ToUpper(public.GetMd5String(checkContent))
- req.Signature = signature
- // POST请求
- buf, _ := json.Marshal(req)
- respBody := public.HttpPostByJson(config.Server.LiuPay.Url_withdraw_order, string(buf))
- log.Debug("liu.WithdrawOrder req ==> %+v resp ==> %+v", string(buf), respBody)
- var resp withdraw_resp
- if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
- log.Error("liu.WithdrawOrder json unmarshal req ==> %+v resp ==> %+v fail %v", string(buf), respBody, err)
- c.String(http.StatusOK, "fail")
- return
- }
- log.Debug("liu.WithdrawOrder resp ==> %+v", resp)
- // 请求响应码
- if resp.Err_code != "000002" {
- log.Error("liu.withdrawRequest post return resp fail ==> %+v", resp)
- c.String(http.StatusOK, "fail")
- return
- }
- // 请求响应码
- if resp.Status != "SUCCESS" {
- log.Error("liu.withdrawRequest post return resp fail ==> %+v", resp)
- c.String(http.StatusOK, "fail")
- return
- }
- go notification.AddNotification(obj.In.UserID, notification.Notification_Gold, "")
- // 更新银行信息
- objBank := db.NewBankInfoUp()
- objBank.In.UserID = obj.In.UserID
- objBank.In.RealName = req.AccName
- objBank.In.BankCard = req.AccNo
- objBank.In.BankName = req.BankName
- objBank.In.Mobile = obj.In.Mobile
- objBank.In.EMail = obj.In.Email
- objBank.In.Address = obj.In.Address
- objBank.DoAction()
- log.Debug("liu.WithdrawNotify need save bankInfo")
- // 加到队列
- go withdrawQueues.push(req.OrderNo)
- c.String(http.StatusOK, "success")
- }
- // 提现审核
- func WithdrawAudit(c *gin.Context) {
- obj := db.NewLiuWithdrawAudit()
- if err := c.ShouldBind(&obj.In); err != nil {
- log.Debug("%s shouldBind err %v", "WithdrawAudit", err)
- return
- }
- obj.DoAction(nil)
- // 审核失败
- if obj.Out.RetCode != 1 {
- c.JSON(http.StatusOK, obj.Out)
- return
- }
- log.Debug("liu.WithdrawAudit obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
- // 审核成功
- // 状态 2=同意 11=拒绝
- if obj.In.Status != 2 {
- go notification.AddNotification(obj.Out.UserID, notification.Notification_Gold, "")
- c.JSON(http.StatusOK, obj.Out)
- return
- }
- // 请求withdrawOrder的代码
- req := withdraw_req{
- MerchantCode: config.Server.LiuPay.MerchantCode,
- OrderNo: obj.Out.OrderID,
- Type: 1,
- AccNo: obj.Out.BankCard,
- AccName: obj.Out.RealName,
- Amount: obj.Out.Amount,
- BankName: obj.Out.BankName,
- BankBranch: "1",
- Province: "2",
- City: "3",
- Ccy_no: "IDR",
- }
- params := url.Values{}
- params.Set("merchantCode", req.MerchantCode)
- params.Set("orderNo", req.OrderNo)
- params.Set("type", strconv.Itoa(req.Type))
- params.Set("accNo", req.AccNo)
- params.Set("accName", req.AccName)
- params.Set("amount", strconv.Itoa(req.Amount))
- params.Set("bankName", req.BankName)
- params.Set("bankBranch", req.BankBranch)
- params.Set("province", req.Province)
- params.Set("city", req.City)
- params.Set("ccy_no", req.Ccy_no)
- // 生成签名
- checkContent := createEncryptStr(params) + "&key=" + config.Server.LiuPay.MD5Key
- // log.Debug("liu.WithdrawAudit.checkContent ==> %s", checkContent)
- signature := strings.ToUpper(public.GetMd5String(checkContent))
- req.Signature = signature
- // POST请求
- buf, _ := json.Marshal(req)
- respBody := public.HttpPostByJson(config.Server.LiuPay.Url_withdraw_order, string(buf))
- log.Debug("liu.WithdrawAudit req ==> %+v resp ==> %+v", string(buf), respBody)
- var resp withdraw_resp
- if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
- log.Error("liu.WithdrawAudit json unmarshal req ==> %+v resp ==> %+v fail %v", string(buf), respBody, err)
- c.String(http.StatusOK, "fail")
- return
- }
- log.Debug("liu.WithdrawAudit resp ==> %+v", resp)
- // 请求响应码
- if resp.Err_code != "000000" {
- log.Error("liu.WithdrawAudit post return resp fail ==> %+v", resp)
- c.String(http.StatusOK, "fail")
- return
- }
- // 请求响应码
- if resp.Status != "SUCCESS" {
- log.Error("liu.WithdrawAudit post return resp fail ==> %+v", resp)
- c.String(http.StatusOK, "fail")
- return
- }
- // 加到队列
- go withdrawQueues.push(req.OrderNo)
- c.JSON(http.StatusOK, obj.Out)
- return
- }
- // 提现查询
- func withdrawSearch(orderId string) bool {
- id, _ := uuid.NewV4()
- // 请求withdrawOrder的代码
- req := withdrawSearch_req{
- RequestNo: id.String(),
- RequestTime: time.Now().Add(1 * time.Hour).Format("20060102150405"), // 北京时间,所以这里需要+1小时
- MerchantCode: config.Server.LiuPay.MerchantCode,
- OrderNo: orderId,
- TerraceNo: "",
- }
- params := url.Values{}
- params.Set("requestNo", req.RequestNo)
- params.Set("requestTime", req.RequestTime)
- params.Set("merchantCode", req.MerchantCode)
- params.Set("orderNo", req.OrderNo)
- // 生成签名
- checkContent := createEncryptStr(params) + "&key=" + config.Server.LiuPay.MD5Key
- // log.Debug("liu.withdrawSearch.checkContent ==> %s", checkContent)
- signature := strings.ToUpper(public.GetMd5String(checkContent))
- req.Signature = signature
- // POST请求
- buf, _ := json.Marshal(req)
- respBody := public.HttpPostByJson(config.Server.LiuPay.Url_withdraw_search, string(buf))
- log.Debug("liu.withdrawSearch req ==> %+v resp ==> %+v", string(buf), respBody)
- var resp withdrawSearch_resp
- if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
- log.Error("liu.withdrawSearch json unmarshal req ==> %+v resp ==> %+v fail %v", string(buf), respBody, err)
- return false
- }
- log.Debug("liu.withdrawSearch resp ==> %+v", resp)
- // 请求响应码
- if resp.Query_Status != "SUCCESS" {
- log.Error("liu.withdrawSearch post return resp fail ==> %+v", resp)
- return false
- }
- // 既没有成功,也没有失败,继续查询
- if resp.Status != "SUCCESS" && resp.Status != "FAIL" {
- return false
- }
- status := 0
- switch resp.Status {
- case "FAIL":
- status = Withdraw_Status_Failure
- case "SUCCESS":
- status = Withdraw_Status_Success
- default:
- status = Withdraw_Status_Pending_Success
- }
- obj := db.NewLiuWithdrawNotify()
- obj.In.OrderID = req.OrderNo
- obj.In.DfTransactionId = resp.TerraceNo
- obj.In.Status = status
- obj.In.DfDesc = resp.Status
- obj.In.Balance = 0
- obj.DoAction()
- log.Debug("lets.withdrawSearch obj.In=%+v obj.Out=%+v", obj.In, obj.Out)
- // 提现失败,退款
- if resp.Status == "FAIL" {
- go notification.AddNotification(obj.Out.UserID, notification.Notification_Gold, "")
- }
- return true
- }
|