pay.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package fawry
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strings"
  8. "bet24.com/log"
  9. "bet24.com/public"
  10. coreClient "bet24.com/servers/coreservice/client"
  11. "bet24.com/servers/payment/config"
  12. "bet24.com/servers/payment/db"
  13. "bet24.com/servers/payment/shop"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // 下单
  17. func PayOrder(c *gin.Context) {
  18. obj := db.NewOrder(db.SP_fawry_ORDER)
  19. if err := c.ShouldBind(&obj.In); err != nil {
  20. log.Debug("%s query params err %v", "fawry.PayOrder", err)
  21. c.String(http.StatusOK, "")
  22. return
  23. }
  24. obj.In.IpAddress = strings.Split(c.Request.RemoteAddr, ":")[0]
  25. //// 币种为空,根据ip获取
  26. //if obj.In.Currency == "" {
  27. // currency := shop.GetCurrencyRateByIp(obj.In.UserID, obj.In.IpAddress)
  28. // obj.In.Currency = currency
  29. //}
  30. // 获取产品信息
  31. item := shop.GetProduct(obj.In.ProductID)
  32. if item == nil {
  33. log.Error("%s query GetProduct productId=%s currency=%s is nil", "fawry.PayOrder", obj.In.ProductID, obj.In.Currency)
  34. c.String(http.StatusOK, "")
  35. return
  36. }
  37. //// 获取当前汇率信息
  38. //info := shop.GetExchangeRate(obj.In.Currency)
  39. //if info == nil {
  40. // log.Error("%s query GetExchangeRate obj.In.Currency=%s is nil", "fawry.PayOrder", obj.In.Currency)
  41. // c.String(http.StatusOK, "")
  42. // return
  43. //}
  44. //
  45. //// 计算价格
  46. //calPrice := info.Rate * item.Price
  47. //
  48. //// 检查价格是否篡改
  49. //if calPrice != obj.In.Price {
  50. // log.Error("%s obj.In.Price=%v info.Rate=%v calPrice=%v is invalid", "fawry.PayOrder", obj.In.Price, info.Rate, calPrice)
  51. // c.String(http.StatusOK, "")
  52. // return
  53. //}
  54. // 检查价格是否篡改
  55. if item.Price != obj.In.Price {
  56. log.Error("%s obj.In.Price=%v calPrice=%v is invalid", "fawry.PayOrder", obj.In.Price, item.Price)
  57. c.String(http.StatusOK, "")
  58. return
  59. }
  60. obj.DoAction(nil)
  61. if obj.Out.OrderID == "" {
  62. log.Debug("%s GenOrder fail obj.In=%+v", "fawry.PayOrder", obj.In)
  63. c.String(http.StatusOK, "")
  64. return
  65. }
  66. var items []chargeItems
  67. items = append(items, chargeItems{
  68. ItemId: obj.In.ProductID,
  69. Description: obj.In.ProductID,
  70. Price: fmt.Sprintf("%.2f", obj.In.Price),
  71. Quantity: 1,
  72. })
  73. // 请求payOrder的代码
  74. req := pay_req{
  75. MerchantCode: config.Server.Fawry.MerchantCode,
  76. MerchantRefNum: obj.Out.OrderID,
  77. PaymentMethod: "MWALLET",
  78. CustomerMobile: obj.In.Tel,
  79. CustomerEmail: obj.In.Email,
  80. Amount: fmt.Sprintf("%.2f", obj.In.Price),
  81. CurrencyCode: obj.In.Currency,
  82. Description: obj.In.ProductID,
  83. Language: "en-gb",
  84. ChargeItems: items,
  85. // CardNumber: "4543474002259998",
  86. // CardExpiryYear: "25",
  87. // CardExpiryMonth: "05",
  88. // Cvv: "123",
  89. // ReturnUrl: "https://meweb.metagame.cyou:8088/fawry/payNotify",
  90. // Enable3DS: true,
  91. }
  92. // The SHA-256 digested for the following concatenated string
  93. var data string
  94. if req.PaymentMethod == "PAYATFAWRY" || req.PaymentMethod == "MWALLET" {
  95. data = fmt.Sprintf("%s%s%s%s%s", req.MerchantCode, req.MerchantRefNum, req.PaymentMethod, req.Amount, config.Server.Fawry.SecureKey)
  96. } else if req.PaymentMethod == "CARD" {
  97. data = fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s",
  98. req.MerchantCode, req.MerchantRefNum, req.PaymentMethod, req.Amount,
  99. req.CardNumber, req.CardExpiryYear, req.CardExpiryMonth, req.Cvv, req.ReturnUrl, config.Server.Fawry.SecureKey)
  100. }
  101. // log.Debug("fawry.payOrder data ==> %v", data)
  102. req.Signature = public.SHA256(data)
  103. // 生成签名
  104. checkContent, err := json.Marshal(req)
  105. if err != nil {
  106. log.Error("fawry.PayOrder json marshal fail %v", err)
  107. return
  108. }
  109. // POST请求
  110. respBody := public.HttpPostByJson(config.Server.Fawry.Url_pay_order, string(checkContent))
  111. log.Debug("fawry.payOrder req ==> %+v resp ==> %+v", string(checkContent), respBody)
  112. var resp pay_resp
  113. if err := json.Unmarshal([]byte(respBody), &resp); err != nil {
  114. log.Error("fawry.PayOrder respBody json unmarshal fail %v", err)
  115. return
  116. }
  117. if resp.StatusCode != 200 {
  118. log.Error("fawry.payOrder 失败 ==> %+v", resp)
  119. return
  120. }
  121. c.HTML(http.StatusOK, "fawryQr.html", gin.H{
  122. "title": "E-Wallet QRCode",
  123. "walletQr": strings.ReplaceAll(resp.WalletQr, "data:image/PNG;base64,", ""),
  124. // "walletQr": resp.WalletQr,
  125. })
  126. // 显示二维码
  127. // html := fmt.Sprintf("<img src=\"%s\" alt=\"QR_Code\"/>", resp.WalletQr)
  128. // c.Header("content-Type", "text/html; charset=utf-8")
  129. // c.String(http.StatusOK, html)
  130. // 跳转到支付页面,以便持卡人完成支付过程
  131. // c.Redirect(http.StatusMovedPermanently, resp.Redirect_url)
  132. return
  133. }
  134. // 回调通知
  135. func PayNotify(c *gin.Context) {
  136. bodyData, _ := io.ReadAll(c.Request.Body)
  137. log.Debug("fawry.PayNotify ==> ctx.Request.body: %v", string(bodyData))
  138. var resp payNotify
  139. if err := json.Unmarshal(bodyData, &resp); err != nil {
  140. log.Debug("%s query params err %v", "fawry.PayNotify", err)
  141. c.String(http.StatusOK, "")
  142. return
  143. }
  144. log.Debug("fawry.PayNotify resp ==> %+v", resp)
  145. data := fmt.Sprintf("%s%s%.2f%.2f%s%s%s%s",
  146. resp.FawryRefNumber, resp.MerchantRefNumber, resp.PaymentAmount, resp.OrderAmount,
  147. resp.OrderStatus, resp.PaymentMethod, resp.PaymentRefrenceNumber,
  148. config.Server.Fawry.SecureKey)
  149. hmac := public.SHA256(data)
  150. // 验证签名
  151. if hmac != resp.MessageSignature {
  152. log.Error("fawry.PayNotify 签名失败 ==> %+v", resp)
  153. return
  154. }
  155. log.Debug("fawry.PayNotify 签名成功")
  156. // New, PAID, CANCELED
  157. // DELIVERED, REFUNDED, EXPIRED
  158. // PARTIAL_REFUNDED, FAILED
  159. if resp.OrderStatus != "PAID" {
  160. log.Debug("fawry.PayNotify resp ==> %+v 接收响应", resp)
  161. c.JSON(http.StatusOK, ret{
  162. Code: "SUCCESS",
  163. Msg: "Success",
  164. })
  165. return
  166. }
  167. // 数据库操作
  168. obj := db.NewNotify(db.SP_fawry_NOTIFY)
  169. obj.In.OrderID = resp.MerchantRefNumber
  170. obj.In.TradeID = resp.FawryRefNumber
  171. obj.DoAction(nil)
  172. // 操作成功,给道具
  173. if obj.Out.RetCode == 1 {
  174. // 充值
  175. resp := coreClient.Recharge(obj.Out.UserID, obj.Out.ProductID)
  176. log.Debug("%s 充值成功 %+v", "fawry.PayNotify", resp)
  177. }
  178. c.JSON(http.StatusOK, ret{
  179. Code: "SUCCESS",
  180. Msg: "Success",
  181. })
  182. return
  183. }