notification.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "encoding/json"
  19. const (
  20. INITIAL_BUY = 0
  21. CANCEL = 1
  22. RENEWAL = 2
  23. INTERACTIVE_RENEWAL = 3
  24. NEW_RENEWAL_PREF = 4
  25. RENEWAL_STOPPED = 5
  26. RENEWAL_RESTORED = 6
  27. RENEWAL_RECURRING = 7
  28. ON_HOLD = 9
  29. PAUSED = 10
  30. PAUSE_PLAN_CHANGED = 11
  31. PRICE_CHANGE_CONFIRMED = 12
  32. DEFERRED = 13
  33. )
  34. type NotificationServer struct {
  35. }
  36. var NotificationDemo = &NotificationServer{}
  37. type NotificationRequest struct {
  38. StatusUpdateNotification string `json:"statusUpdateNotification"`
  39. NotificationSignature string `json:"notifycationSignature"`
  40. }
  41. type NotificationResponse struct {
  42. ErrorCode string `json:"errorCode"`
  43. ErrorMsg string `json:"errorMsg"`
  44. }
  45. type StatusUpdateNotification struct {
  46. Environment string `json:"environment"`
  47. NotificationType int `json:"notificationType"`
  48. SubscriptionID string `json:"subscriptionId"`
  49. CancellationDate int64 `json:"cancellationDate"`
  50. OrderID string `json:"orderId"`
  51. LatestReceipt string `json:"latestReceipt"`
  52. LatestReceiptInfo string `json:"latestReceiptInfo"`
  53. LatestReceiptInfoSignature string `json:"latestReceiptInfoSignature"`
  54. LatestExpiredReceipt string `json:"latestExpiredReceipt"`
  55. LatestExpiredReceiptInfo string `json:"latestExpiredReceiptInfo"`
  56. LatestExpiredReceiptInfoSignature string `json:"latestExpiredReceiptInfoSignature"`
  57. AutoRenewStatus int `json:"autoRenewStatus"`
  58. RefundPayOrderId string `json:"refundPayOrderId"`
  59. ProductID string `json:"productId"`
  60. ApplicationID string `json:"applicationId"`
  61. ExpirationIntent int `json:"expirationIntent"`
  62. }
  63. func (eventServer *NotificationServer) DealNotification(information string) (*NotificationResponse, error) {
  64. var request NotificationRequest
  65. err := json.Unmarshal([]byte(information), &request)
  66. if err != nil {
  67. return nil, err
  68. }
  69. err = VerifyRsaSign(request.StatusUpdateNotification, request.NotificationSignature, DefaultConfig.ApplicationPublicKey)
  70. if err != nil {
  71. return nil, err
  72. }
  73. var info StatusUpdateNotification
  74. json.Unmarshal([]byte(request.StatusUpdateNotification), &info)
  75. switch notificationType := info.NotificationType; notificationType {
  76. case INITIAL_BUY:
  77. case CANCEL:
  78. case RENEWAL:
  79. case INTERACTIVE_RENEWAL:
  80. case NEW_RENEWAL_PREF:
  81. case RENEWAL_STOPPED:
  82. case RENEWAL_RESTORED:
  83. case RENEWAL_RECURRING:
  84. case ON_HOLD:
  85. case PAUSED:
  86. case PAUSE_PLAN_CHANGED:
  87. case PRICE_CHANGE_CONFIRMED:
  88. case DEFERRED:
  89. default:
  90. }
  91. response := NotificationResponse{ErrorCode: "0"}
  92. return &response, nil
  93. }