RefillPhone.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace refill\cangxin;
  3. require_once(BASE_HELPER_RAPI_PATH . '/cangxin/config.php');
  4. use refill;
  5. use Log;
  6. use mtopcard;
  7. class RefillPhone extends refill\IRefillPhone
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function req_params(int $phone, int $amount, int $card_type, string $order_sn, $regin_no)
  14. {
  15. $params['clientId'] = config::clientId;
  16. $params['clientOrderNo'] = $order_sn;
  17. $params['account'] = $phone;
  18. if ($card_type === mtopcard\ChinaMobileCard) {
  19. $params['skuCode'] = config::CMCC_product_code[$regin_no][$amount];
  20. } elseif ($card_type === mtopcard\ChinaTelecomCard){
  21. $params['skuCode'] = config::CTCC_product_code[$regin_no][$amount];
  22. } else {
  23. $params['skuCode'] = config::PRODUCT[$card_type][$amount];
  24. }
  25. $params['amount'] = $amount;
  26. $params['callbackUrl'] = config::NOTIFY_URL;
  27. return $params;
  28. }
  29. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  30. {
  31. $regin_no = $params['regin_no'] ?? -1;
  32. if ($regin_no <= 0) {
  33. return [false, '省份获取错误', false];
  34. }
  35. $order_sn = $params['order_sn'];
  36. $params = $this->req_params($card_no, $amount, $card_type, $order_sn, $regin_no);
  37. if(empty($params['skuCode'])) {
  38. return [false, '商品编号错误', false];
  39. }
  40. $sign = $this->sign($params);
  41. $params['verifyString'] = $sign;
  42. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  43. if (empty($resp)) {
  44. return [false, '网络错误', true];
  45. }
  46. else
  47. {
  48. Log::record($resp, Log::DEBUG);
  49. $resp = json_decode($resp, true);
  50. if (empty($resp)) {
  51. return [false, '网络错误', true];
  52. } elseif ($resp['code'] === 200) {
  53. return [true, $resp['data']['sysOrderNo'], false];
  54. } else {
  55. return [false, $resp['msg'], false];
  56. }
  57. }
  58. }
  59. public function query($refill_info)
  60. {
  61. $params['clientId'] = config::clientId;
  62. $params['clientOrderNo'] = $refill_info['order_sn'];
  63. $params['verifyString'] = $this->sign($params);
  64. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  65. if (empty($resp)) {
  66. return [false, '网络错误'];
  67. }
  68. else
  69. {
  70. Log::record($resp, Log::DEBUG);
  71. $resp = json_decode($resp, true);
  72. if (empty($resp)) {
  73. return [false, '网络错误'];
  74. }
  75. elseif ($resp['code'] === 200)
  76. {
  77. $data = $resp['data'];
  78. $status = $data['status'];
  79. if ($status === '4') {
  80. $updata['official_sn'] = $data['officialOrderNo'];
  81. Model('refill_order')->edit($refill_info['order_id'], $updata);
  82. $order_state = ORDER_STATE_SUCCESS;
  83. } elseif ($status === '3') {
  84. $order_state = ORDER_STATE_CANCEL;
  85. } elseif ($status === '0' || $status === '2') {
  86. $order_state = ORDER_STATE_SEND;
  87. } else {
  88. return [false, $resp['msg']];
  89. }
  90. return [true, $order_state];
  91. }
  92. elseif ($resp['code'] === -9 && (time() - $refill_info['commit_time'] >= 600))
  93. {
  94. return [true, ORDER_STATE_NOEXIST];
  95. }
  96. else
  97. {
  98. return [false, $resp['msg']];
  99. }
  100. }
  101. }
  102. public function balance()
  103. {
  104. $params['clientId'] = config::clientId;
  105. $params['verifyString'] = $this->sign($params);
  106. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  107. if (empty($resp)) {
  108. return [false, '网络错误'];
  109. }
  110. else
  111. {
  112. Log::record($resp, Log::DEBUG);
  113. $resp = json_decode($resp, true);
  114. if (empty($resp)) {
  115. return [false, '网络错误'];
  116. } elseif ($resp['code'] === 200) {
  117. return [true, $resp['data']['balance']];
  118. } else {
  119. return [false, $resp['msg']];
  120. }
  121. }
  122. }
  123. private function sign($params)
  124. {
  125. ksort($params);
  126. $str = urldecode(http_build_query($params));
  127. $str .= '&key=' . config::Key;
  128. return md5($str);
  129. }
  130. }