order_service.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. require "at_demo.php";
  19. class OrderService {
  20. // TODO: replace the (ip:port) to the real one, and if the protocol is https, you should deal with the license yourself.
  21. // gwang: 地址如何选择请参考: https://developer.huawei.com/consumer/cn/doc/development/HMSCore-References/api-common-statement-0000001050986127
  22. //yourself 以前: "https://orders-drcn.iap.hicloud.com";
  23. const TOC_SITE_URL = "https://orders-drcn.iap.cloud.huawei.com.cn"; # 国内,选用这个
  24. // const TOC_SITE_URL = "https://orders-dra.iap.cloud.huawei.asia"; # 新加坡,选用这个
  25. //site fore telecom carrier
  26. const TOBTOC_SITE_URL = "https://subscr-at-dre.iap.dbankcloud.com";
  27. const VERIFY_TOKEN_URL = "/applications/purchases/tokens/verify";
  28. const CANCELLED_LIST_PURCHASE_URL = "/applications/v2/purchases/cancelledList";
  29. const CONFIRM_PURCHASE_URL = "/applications/v2/purchases/confirm";
  30. public function getRootUrl($accountFlag) {
  31. if ($accountFlag != null && $accountFlag == 1) {
  32. return self::TOBTOC_SITE_URL;
  33. }
  34. return self::TOC_SITE_URL;
  35. }
  36. public function verifyToken($purchaseToken, $productId, $accountFlag = null) {
  37. // fetch the App Level AccessToken
  38. $appAT = AtDemo::getAppAT();
  39. if ($appAT == null) {
  40. return;
  41. }
  42. // construct the Authorization in Header
  43. $headers = AtDemo::buildAuthorization($appAT);
  44. // pack the request body
  45. $body = ["purchaseToken" => $purchaseToken, "productId" => $productId];
  46. $msgBody = json_encode($body);
  47. $response = AtDemo::httpPost(self::getRootUrl($accountFlag) . self::VERIFY_TOKEN_URL, $msgBody, 5, 5, $headers);
  48. // TODO: display the response as string in console, you can replace it with your business logic.
  49. // echo $response; # gwang 将echo 改为return (2022.10.19)
  50. $respData = json_decode($response);
  51. return $respData;
  52. }
  53. public function cancelledListPurchase($endTime, $startTime, $maxRows, $type, $continuationToken, $accountFlag) {
  54. // fetch the App Level AccessToken
  55. $appAT = AtDemo::getAppAT();
  56. if ($appAT == null) {
  57. return;
  58. }
  59. // construct the Authorization in Header
  60. $headers = AtDemo::buildAuthorization($appAT);
  61. // pack the request body
  62. $body = ["endAt" => $endTime, "startAt" => $startTime, "maxRows" => $maxRows, "type" => $type, "continuationToken" => $continuationToken];
  63. $msgBody = json_encode($body);
  64. $response = AtDemo::httpPost(self::getRootUrl($accountFlag) . self::CANCELLED_LIST_PURCHASE_URL, $msgBody, 5, 5, $headers);
  65. // TODO: display the response as string in console, you can replace it with your business logic.
  66. echo $response;
  67. }
  68. public function confirmPurchase($purchaseToken, $productId, $accountFlag) {
  69. // fetch the App Level AccessToken
  70. $appAT = AtDemo::getAppAT();
  71. if ($appAT == null) {
  72. return;
  73. }
  74. // construct the Authorization in Header
  75. $headers = AtDemo::buildAuthorization($appAT);
  76. // pack the request body
  77. $body = ["purchaseToken" => $purchaseToken, "productId" => $productId];
  78. $msgBody = json_encode($body);
  79. $response = AtDemo::httpPost(self::getRootUrl($accountFlag) . self::CONFIRM_PURCHASE_URL, $msgBody, 5, 5, $headers);
  80. // TODO: display the response as string in console, you can replace it with your business logic.
  81. echo $response;
  82. }
  83. }