order.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package huawei
  18. import (
  19. "bet24.com/log"
  20. coreClient "bet24.com/servers/coreservice/client"
  21. "bet24.com/servers/payment/config"
  22. "bet24.com/servers/payment/db"
  23. "encoding/json"
  24. "fmt"
  25. )
  26. type OrderClient struct {
  27. }
  28. var Client = &OrderClient{}
  29. func getOrderUrl(accountFlag int) string {
  30. if accountFlag == 1 {
  31. // site for telecom carrier
  32. return "https://orders-at-dre.iap.dbankcloud.com"
  33. } else {
  34. // TODO: replace the (ip:port) to the real one
  35. return "https://orders-drcn.iap.hicloud.com"
  36. }
  37. }
  38. func (oc *OrderClient) VerifyToken(purchaseToken, productId string, accountFlag int) (int, error) {
  39. bodyMap := map[string]string{"purchaseToken": purchaseToken, "productId": productId}
  40. url := getOrderUrl(accountFlag) + "/applications/purchases/tokens/verify"
  41. bodyBytes, err := SendRequest(url, bodyMap)
  42. if err != nil {
  43. log.Error("huawei.VerifyToken.SendRequest err is %s", err)
  44. return 0, err
  45. }
  46. // TODO: display the response as string in console, you can replace it with your business logic.
  47. log.Debug("order.VerifyToken ==>%s", bodyBytes)
  48. resp := &data{}
  49. err = json.Unmarshal([]byte(bodyBytes), resp)
  50. if err != nil {
  51. log.Error("huawei.VerifyToken.Unmarshal err is %v", err)
  52. return 0, err
  53. }
  54. //验签
  55. err = VerifyRsaSign(resp.PurchaseTokenData, resp.DataSignature, config.Server.Huawei.ApplicationPublicKey)
  56. if err != nil {
  57. log.Error("huawei.VerifyToken.VerifyRsaSign err is %v", err)
  58. return 0, err
  59. }
  60. log.Debug("huawei.VerifyToken Verify Success!")
  61. purchase := purchaseData{}
  62. err = json.Unmarshal([]byte(resp.PurchaseTokenData), &purchase)
  63. if err != nil {
  64. log.Error("huawei.VerifyToken.VerifyRsaSign.Unmarshal err is %v", err)
  65. return 0, err
  66. }
  67. notify := db.NewNotify(db.SP_HuaweiPay_NOTIFY)
  68. notify.In.OrderID = purchase.DeveloperPayload
  69. notify.In.Price = float64(purchase.Price)
  70. notify.In.TradeID = purchase.OrderId
  71. notify.DoAction(nil)
  72. // 操作成功,给道具
  73. if notify.Out.RetCode == 1 {
  74. // 充值
  75. resp := coreClient.Recharge(notify.Out.UserID, notify.Out.ProductID)
  76. log.Debug("%s 充值成功 %+v", "huawei.VerifyToken.Notify", resp)
  77. }
  78. return notify.Out.RetCode, nil
  79. }
  80. func (oc *OrderClient) CancelledListPurchase(endAt int64, startAt int64, maxRows int, productType int, continuationToken string, accountFlag int) {
  81. bodyMap := map[string]string{
  82. "endAt": fmt.Sprintf("%v", endAt),
  83. "startAt": fmt.Sprintf("%v", startAt),
  84. "maxRows": fmt.Sprintf("%v", maxRows),
  85. "type": fmt.Sprintf("%v", productType),
  86. "continuationToken": continuationToken,
  87. }
  88. url := getOrderUrl(accountFlag) + "/applications/v2/purchases/cancelledList"
  89. bodyBytes, err := SendRequest(url, bodyMap)
  90. if err != nil {
  91. log.Error("err is %s", err)
  92. }
  93. // TODO: display the response as string in console, you can replace it with your business logic.
  94. log.Debug("%s", bodyBytes)
  95. }
  96. func (oc *OrderClient) confirmPurchase(productId, purchaseToken string, accountFlag int) {
  97. bodyMap := map[string]string{
  98. "purchaseToken": purchaseToken,
  99. "productId": productId,
  100. }
  101. url := getOrderUrl(accountFlag) + "/applications/v2/purchases/confirm"
  102. bodyBytes, err := SendRequest(url, bodyMap)
  103. if err != nil {
  104. log.Error("err is %s", err)
  105. }
  106. // TODO: display the response as string in console, you can replace it with your business logic.
  107. log.Debug("%s", bodyBytes)
  108. }