app_server.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "rsa_sha256.php";
  19. header("Content-Type: application/json; charset=utf-8");
  20. $raw_post_data = file_get_contents('php://input');
  21. $server = new AppServer();
  22. $server->deal_notification($raw_post_data);
  23. class AppServer{
  24. var $publicKey = "publicKey"; //your app's public Key
  25. function deal_notification($raw_post_data){
  26. $request = json_decode($raw_post_data,false);
  27. $response = new StatusUpdateNotificationResponse();
  28. if(empty( $request->statusUpdateNotification)|| empty($request->notifycationSignature))
  29. {
  30. $response->errorCode = 1; //failure
  31. $response->errorMsg = "the notification message is empty";
  32. echo $response->buildResponse();
  33. return;
  34. }
  35. // verify the notification
  36. $ok=RSA::doCheck($request->statusUpdateNotification,$request->notifycationSignature,$this->publicKey);
  37. if(!$ok) {
  38. $response->errorCode = 2; //failure
  39. $response->errorMsg = "verify the sign failure";
  40. echo $response->buildResponse();
  41. return;
  42. }
  43. $statusUpdateNotification = json_decode($request->statusUpdateNotification,false);
  44. //TODO replace with your logic
  45. switch ($statusUpdateNotification->notificationType) {
  46. case NOTIFICATION_TYPE::INITIAL_BUY:
  47. break;
  48. case NOTIFICATION_TYPE::CANCEL:
  49. break;
  50. case NOTIFICATION_TYPE::RENEWAL:
  51. break;
  52. case NOTIFICATION_TYPE::INTERACTIVE_RENEWAL:
  53. break;
  54. case NOTIFICATION_TYPE::NEW_RENEWAL_PREF:
  55. break;
  56. case NOTIFICATION_TYPE::RENEWAL_STOPPED:
  57. break;
  58. case NOTIFICATION_TYPE::RENEWAL_RESTORED:
  59. break;
  60. case NOTIFICATION_TYPE::RENEWAL_RECURRING:
  61. break;
  62. case NOTIFICATION_TYPE::ON_HOLD:
  63. break;
  64. case NOTIFICATION_TYPE::PAUSED:
  65. break;
  66. case NOTIFICATION_TYPE::PAUSE_PLAN_CHANGED:
  67. break;
  68. case NOTIFICATION_TYPE::PRICE_CHANGE_CONFIRMED:
  69. break;
  70. case NOTIFICATION_TYPE::DEFERRED:
  71. break;
  72. }
  73. }
  74. }
  75. interface NOTIFICATION_TYPE {
  76. const INITIAL_BUY = 0;
  77. const CANCEL = 1;
  78. const RENEWAL = 2;
  79. const INTERACTIVE_RENEWAL = 3;
  80. const NEW_RENEWAL_PREF = 4;
  81. const RENEWAL_STOPPED =5;
  82. const RENEWAL_RESTORED = 6;
  83. const RENEWAL_RECURRING = 7;
  84. const ON_HOLD = 9;
  85. const PAUSED = 10;
  86. const PAUSE_PLAN_CHANGED = 11;
  87. const PRICE_CHANGE_CONFIRMED = 12;
  88. const DEFERRED = 13;
  89. }
  90. class StatusUpdateNotificationResponse {
  91. var $errorCode;
  92. var $errorMsg;
  93. function buildResponse(){
  94. return "{\"errorCode\":$this->errorCode,\"errorMsg\":\"$this->errorMsg\"}";
  95. }
  96. }
  97. class StatusUpdateNotificationRequest{
  98. var $statusUpdateNotification;
  99. var $notifycationSignature;
  100. }
  101. class StatusUpdateNotification{
  102. var $environment;
  103. var $notificationType;
  104. var $subscriptionId;
  105. var $cancellationDate;
  106. var $orderId;
  107. var $latestReceipt;
  108. var $latestReceiptInfo;
  109. var $latestReceiptInfoSignature;
  110. var $latestExpiredReceipt;
  111. var $latestExpiredReceiptInfo;
  112. var $latestExpiredReceiptInfoSignature;
  113. var $autoRenewStatus;
  114. var $refundPayOrderId;
  115. var $productId;
  116. var $expirationIntent;
  117. }