RefillPhone.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace refill\yanhao;
  3. require_once(BASE_HELPER_RAPI_PATH . '/yanhao/config.php');
  4. use refill;
  5. use Log;
  6. class RefillPhone extends refill\IRefillPhone
  7. {
  8. public function __construct($cfgs)
  9. {
  10. parent::__construct($cfgs);
  11. }
  12. public function goods($quality, int $amount, int $card_type, $regin_no, $other)
  13. {
  14. [$goods_id, $price] = parent::goods($quality, $amount, $card_type, $regin_no, $other);
  15. if ($goods_id <= 0) return [0, 0];
  16. $store_id = $this->mStoreID;
  17. $pcode = $other['product_code'];
  18. $thrid_refill = Model('thrid_refill');
  19. $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $pcode);
  20. if (empty($product)) {
  21. Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}", Log::ERR);
  22. return [0, 0];
  23. } else {
  24. return [$goods_id, ncPriceFormat($product['channel_amount'])];
  25. }
  26. }
  27. private function getProductCode($goods_id, $sys_pcode)
  28. {
  29. $thrid_refill = Model('thrid_refill');
  30. $store_id = $this->mStoreID;
  31. $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $sys_pcode);
  32. if (empty($product)) {
  33. return false;
  34. } else {
  35. return $product['channel_code'];
  36. }
  37. }
  38. private function req_params(int $phone, string $order_sn)
  39. {
  40. $params['UserId'] = config::USER_ID;
  41. $params['TimesTamp'] = $this->getMillisecond();
  42. $params['Count'] = 1;
  43. $params['UserOrderId'] = $order_sn;
  44. $params['Account'] = $phone;
  45. $params['ClientIP'] = config::API_IP;
  46. $params['NotifyUrl'] = config::NOTIFY_URL;
  47. return $params;
  48. }
  49. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  50. {
  51. return [true, '', false];
  52. // $params = $this->req_params($card_no, $params['order_sn']);
  53. // $goods_id = intval($params['goods_id']);
  54. // $channel_code = $this->getProductCode($goods_id, $params['product_code']);
  55. // if(empty($channel_code)) {
  56. // return [false, '产品有误', false];
  57. // }
  58. // $params['ProductNumber'] = $channel_code;
  59. // $sign = $this->sign($params);
  60. // $params['Sign'] = $sign;
  61. //
  62. // $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  63. //
  64. // if (empty($resp)) {
  65. // return [false, '系统错误', true];
  66. // }
  67. // else
  68. // {
  69. // Log::record($resp, Log::DEBUG);
  70. // $resp = json_decode($resp, true);
  71. // if (empty($resp)) {
  72. // return [false, '系统错误', true];
  73. // } elseif ($resp['Code'] === 0) {
  74. // return [true, $resp['OutOrderID'], false];
  75. // } elseif ($resp['Code'] === -170 || $resp['Code'] === -180){
  76. // $net_errno = "HTTP-{$resp['Code']}";
  77. // return [false, $net_errno, true];
  78. // } else {
  79. // return [false, $resp['Msg'], false];
  80. // }
  81. // }
  82. }
  83. public function query($refill_info)
  84. {
  85. $params['UserId'] = config::USER_ID;
  86. $params['TimesTamp'] = $this->getMillisecond();
  87. $params['UserOrderId'] = $refill_info['order_sn'];
  88. $content = "{$params['UserId']}{$params['TimesTamp']}{$params['UserOrderId']}".config::Key;
  89. $params['Sign'] = strtoupper(md5($content));
  90. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  91. if (empty($resp)) {
  92. return [false, '系统错误'];
  93. }
  94. else
  95. {
  96. Log::record($resp, Log::DEBUG);
  97. $resp = json_decode($resp, true);
  98. if (empty($resp)) {
  99. return [false, '系统错误'];
  100. }
  101. elseif ($resp['Code'] === 0)
  102. {
  103. $status = $resp['Data']['OrderStatus'];
  104. if ($status === 2) {
  105. $order_state = ORDER_STATE_SUCCESS;
  106. } elseif ($status === 3) {
  107. $order_state = ORDER_STATE_CANCEL;
  108. } elseif ($status === 1) {
  109. $order_state = ORDER_STATE_SEND;
  110. } else {
  111. return [false, $status];
  112. }
  113. return [true, $order_state];
  114. }
  115. elseif ($resp['Code'] === -240 && (time() - $refill_info['commit_time'] >= 600))
  116. {
  117. return [true, ORDER_STATE_NOEXIST];
  118. }
  119. else {
  120. return [false, $resp['Msg']];
  121. }
  122. }
  123. }
  124. public function balance()
  125. {
  126. $params['UserId'] = config::USER_ID;
  127. $params['TimesTamp'] = $this->getMillisecond();
  128. $content = "{$params['UserId']}{$params['TimesTamp']}".config::Key;
  129. $params['Sign'] = strtoupper(md5($content));
  130. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  131. if (empty($resp)) {
  132. return [false, '系统错误'];
  133. }
  134. else
  135. {
  136. Log::record($resp, Log::DEBUG);
  137. $resp = json_decode($resp, true);
  138. if (empty($resp)) {
  139. return [false, '系统错误'];
  140. }
  141. elseif ($resp['Code'] === 0)
  142. {
  143. return [true, $resp['Data'][0]['Balance'] / 10000];
  144. }
  145. else {
  146. return [false, $resp['Msg']];
  147. }
  148. }
  149. }
  150. private function sign($params)
  151. {
  152. $key = config::Key;
  153. $content = "{$params['UserId']}{$params['Count']}{$params['NotifyUrl']}{$params['Account']}{$params['ClientIP']}{$params['UserOrderId']}{$params['ProductNumber']}";
  154. $content .= "{$params['TimesTamp']}{$key}";
  155. return strtoupper(md5($content));
  156. }
  157. private function getMillisecond()
  158. {
  159. $cur = microtime (true);
  160. $cur = intval($cur * 1000);
  161. return $cur;
  162. }
  163. }