subscription.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "fmt"
  20. "log"
  21. )
  22. type SubscriptionClient struct {
  23. }
  24. var SubscriptionDemo = &SubscriptionClient{}
  25. func getSubUrl(accountFlag int) string {
  26. if accountFlag == 1 {
  27. // site for telecom carrier
  28. return "https://subscr-at-dre.iap.dbankcloud.com"
  29. } else
  30. {
  31. // TODO: replace the (ip:port) to the real one
  32. return "http://exampleserver/_mockserver_"
  33. }
  34. }
  35. func (subscriptionDemo *SubscriptionClient) GetSubscription(subscriptionId, purchaseToken string,accountFlag int) {
  36. bodyMap := map[string]string{
  37. "subscriptionId": subscriptionId,
  38. "purchaseToken": purchaseToken,
  39. }
  40. url := getSubUrl(accountFlag) + "/sub/applications/v2/purchases/get"
  41. bodyBytes, err := SendRequest(url, bodyMap)
  42. if err != nil {
  43. log.Printf("err is %s", err)
  44. }
  45. // TODO: display the response as string in console, you can replace it with your business logic.
  46. log.Printf("%s", bodyBytes)
  47. }
  48. func (subscriptionDemo *SubscriptionClient) StopSubscription(subscriptionId, purchaseToken string,accountFlag int) {
  49. bodyMap := map[string]string{
  50. "subscriptionId": subscriptionId,
  51. "purchaseToken": purchaseToken,
  52. }
  53. url := getSubUrl(accountFlag) + "/sub/applications/v2/purchases/stop"
  54. bodyBytes, err := SendRequest(url, bodyMap)
  55. if err != nil {
  56. log.Printf("err is %s", err)
  57. }
  58. // TODO: display the response as string in console, you can replace it with your business logic.
  59. log.Printf("%s", bodyBytes)
  60. }
  61. func (subscriptionDemo *SubscriptionClient) DelaySubscription(subscriptionId, purchaseToken string, currentExpirationTime, desiredExpirationTime int64,accountFlag int) {
  62. bodyMap := map[string]string{
  63. "subscriptionId": subscriptionId,
  64. "purchaseToken": purchaseToken,
  65. "currentExpirationTime": fmt.Sprintf("%v", currentExpirationTime),
  66. "desiredExpirationTime": fmt.Sprintf("%v", desiredExpirationTime),
  67. }
  68. url := getSubUrl(accountFlag) + "/sub/applications/v2/purchases/delay"
  69. bodyBytes, err := SendRequest(url, bodyMap)
  70. if err != nil {
  71. log.Printf("err is %s", err)
  72. }
  73. // TODO: display the response as string in console, you can replace it with your business logic.
  74. log.Printf("%s", bodyBytes)
  75. }
  76. func (subscriptionDemo *SubscriptionClient) ReturnFeeSubscription(subscriptionId, purchaseToken string,accountFlag int) {
  77. bodyMap := map[string]string{
  78. "subscriptionId": subscriptionId,
  79. "purchaseToken": purchaseToken,
  80. }
  81. url := getSubUrl(accountFlag) + "/sub/applications/v2/purchases/returnFee"
  82. bodyBytes, err := SendRequest(url, bodyMap)
  83. if err != nil {
  84. log.Printf("err is %s", err)
  85. }
  86. // TODO: display the response as string in console, you can replace it with your business logic.
  87. log.Printf("%s", bodyBytes)
  88. }
  89. func (subscriptionDemo *SubscriptionClient) WithdrawalSubscription(subscriptionId, purchaseToken string,accountFlag int) {
  90. bodyMap := map[string]string{
  91. "subscriptionId": subscriptionId,
  92. "purchaseToken": purchaseToken,
  93. }
  94. url := getSubUrl(accountFlag) + "/sub/applications/v2/purchases/withdrawal"
  95. bodyBytes, err := SendRequest(url, bodyMap)
  96. if err != nil {
  97. log.Printf("err is %s", err)
  98. }
  99. // TODO: display the response as string in console, you can replace it with your business logic.
  100. log.Printf("%s", bodyBytes)
  101. }