| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package crush
- import (
- "encoding/hex"
- "encoding/json"
- "fmt"
- "net/http"
- "strconv"
- "strings"
- "bet24.com/servers/payment/shop"
- "bet24.com/log"
- "bet24.com/public"
- coreClient "bet24.com/servers/coreservice/client"
- "bet24.com/servers/payment/config"
- "bet24.com/servers/payment/db"
- "github.com/gin-gonic/gin"
- )
- // 下单
- func PayOrder(c *gin.Context) {
- obj := db.NewOrder(db.SP_Crush_ORDER)
- if err := c.ShouldBind(&obj.In); err != nil {
- log.Debug("%s query params err %v", "crush.PayOrder", err)
- c.String(http.StatusOK, "")
- return
- }
- obj.In.IpAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
- //// 币种为空,根据ip获取
- //if obj.In.Currency == "" {
- // currency := shop.GetCurrencyRateByIp(obj.In.UserID, obj.In.IpAddress)
- // obj.In.Currency = currency
- //}
- // 获取产品信息
- item := shop.GetProduct(obj.In.ProductID)
- if item == nil {
- log.Error("%s query GetProduct productId=%s currency=%s is nil", "crush.PayOrder", obj.In.ProductID, obj.In.Currency)
- c.String(http.StatusOK, "")
- return
- }
- //// 获取当前汇率信息
- //info := shop.GetExchangeRate(obj.In.Currency)
- //if info == nil {
- // log.Error("%s query GetExchangeRate obj.In.Currency=%s is nil", "crush.PayOrder", obj.In.Currency)
- // c.String(http.StatusOK, "")
- // return
- //}
- //
- //// 计算价格
- //calPrice := info.Rate * item.Price
- //
- //// 检查价格是否篡改
- //if calPrice != obj.In.Price {
- // log.Error("%s obj.In.Price=%v info.Rate=%v calPrice=%v is invalid", "crush.PayOrder", obj.In.Price, info.Rate, calPrice)
- // c.String(http.StatusOK, "")
- // return
- //}
- // 检查价格是否篡改
- if item.Price != obj.In.Price {
- log.Error("%s obj.In.Price=%v calPrice=%v is invalid", "crush.PayOrder", obj.In.Price, item.Price)
- c.String(http.StatusOK, "")
- return
- }
- obj.DoAction(nil)
- if obj.Out.OrderID == "" {
- log.Debug("%s GenOrder fail obj.In=%+v", "crush.PayOrder", obj.In)
- c.String(http.StatusOK, "")
- return
- }
- // 请求payOrder的代码
- req := pay_req{
- MerchantOrderId: obj.Out.OrderID,
- OrderAmount: fmt.Sprintf("%f", obj.In.Price),
- NotifyUrl: config.Server.CrushPay.Url_pay_Notify,
- }
- // 生成签名
- checkContent, err := json.Marshal(req)
- if err != nil {
- log.Error("crush.PayOrder json marshal fail %v", err)
- return
- }
- buf, err := public.AesEncrypt(checkContent, []byte(config.Server.CrushPay.MerchantKey))
- if err != nil {
- log.Error("crush.PayOrder AESEncrypt err %v", err)
- return
- }
- sign := strings.ToUpper(hex.EncodeToString(buf))
- // 生成请求的 body
- bodyBuf, _ := json.Marshal(struct {
- Data string `json:"data"`
- }{
- Data: sign,
- })
- body := string(bodyBuf)
- // POST请求
- respBody := httpPostByJson(config.Server.CrushPay.Url_pay_order, body, config.Server.CrushPay.MerchantAppId)
- log.Debug("crush.payOrder req ==> %+v resp ==> %+v", string(checkContent), respBody)
- var resp pay_resp
- if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
- log.Error("crush.payOrder json unmarshal req ==> %+v resp ==> %+v fail %v", body, respBody, err)
- return
- }
- log.Debug("crush.payOrder resp ==> %+v", resp)
- // 返回码,’APPLY_SUCCESS’代表成功
- if !resp.Success {
- log.Error("crush.payOrder post return resp fail ==> %+v", resp)
- return
- }
- // 跳转到支付页面,以便持卡人完成支付过程
- c.Redirect(http.StatusMovedPermanently, resp.Data.PayUrl)
- // c.String(http.StatusOK, "Success")
- return
- }
- // 回调通知
- func PayNotify(c *gin.Context) {
- var Req struct {
- Data string `json:"data" form:"data"`
- }
- if err := c.ShouldBind(&Req); err != nil {
- log.Debug("%s query params err %v", "crush.PayNotify", err)
- c.String(http.StatusOK, "")
- return
- }
- log.Debug("crush.PayNotify resp ==> %+v", Req)
- data, err := hex.DecodeString(Req.Data)
- if err != nil {
- log.Debug("crush.PayNotify hex.DecodeString %s ==> err %v", Req.Data, err)
- c.String(http.StatusOK, "")
- return
- }
- buf, err := public.AesDecrypt(data, []byte(config.Server.CrushPay.MerchantKey))
- if err != nil {
- log.Debug("crush.PayNotify AESDecrypt err %v", err)
- c.String(http.StatusOK, "")
- return
- }
- log.Debug("crush.PayNotify 解析成功 ==> %s", string(buf))
- var resp payNotify
- if err := json.Unmarshal(buf, &resp); err != nil {
- log.Debug("%s query params err %v", "crush.PayNotify", err)
- c.String(http.StatusOK, "")
- return
- }
- log.Debug("crush.PayNotify resp ==> %+v", resp)
- // 返回码,’APPLY_SUCCESS’代表成功
- if resp.Status != "SUCCESS" {
- log.Error("crush.PayNotify resp ==> %+v 失败", resp)
- c.String(http.StatusOK, "SUCCESS")
- return
- }
- payAmount, err := strconv.Atoi(strings.ReplaceAll(resp.PayAmount, ".", ""))
- if err != nil {
- log.Error("crush.WithdrawNotify balance err %v", err)
- return
- }
- payAmount = payAmount / 100
- // 数据库操作
- obj := db.NewNotify(db.SP_Crush_NOTIFY)
- obj.In.OrderID = resp.MerchantOrderId
- obj.In.TradeID = resp.OrderId
- // obj.In.Price = payAmount
- obj.DoAction(nil)
- // 操作成功,给道具
- if obj.Out.RetCode == 1 {
- // 充值
- resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
- log.Debug("%s 充值成功 %+v", "crush.PayNotify", resp)
- }
- c.String(http.StatusOK, "SUCCESS")
- return
- }
- // 支付完成跳转处理
- func PayCallback(c *gin.Context) {
- c.String(http.StatusOK, "SUCCESS")
- log.Debug("crush.payCallback")
- }
|