RefillPhone.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $params = $this->req_params($card_no, $params['order_sn']);
  52. $goods_id = intval($params['goods_id']);
  53. $channel_code = $this->getProductCode($goods_id, $params['product_code']);
  54. if(empty($channel_code)) {
  55. return [false, '产品有误', false];
  56. }
  57. $params['ProductNumber'] = $channel_code;
  58. $sign = $this->sign($params);
  59. $params['Sign'] = $sign;
  60. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  61. if (empty($resp)) {
  62. return [false, '系统错误', true];
  63. }
  64. else
  65. {
  66. Log::record($resp, Log::DEBUG);
  67. $resp = json_decode($resp, true);
  68. if (empty($resp)) {
  69. return [false, '系统错误', true];
  70. } elseif ($resp['Code'] === 0) {
  71. return [true, $resp['OutOrderID'], false];
  72. } elseif ($resp['Code'] === -170 || $resp['Code'] === -180){
  73. $net_errno = "HTTP-{$resp['Code']}";
  74. return [false, $net_errno, true];
  75. } else {
  76. return [false, $resp['Msg'], false];
  77. }
  78. }
  79. }
  80. public function query($refill_info)
  81. {
  82. $params['UserId'] = config::USER_ID;
  83. $params['TimesTamp'] = $this->getMillisecond();
  84. $params['UserOrderId'] = $refill_info['order_sn'];
  85. $content = "{$params['UserId']}{$params['TimesTamp']}{$params['UserOrderId']}".config::Key;
  86. $params['Sign'] = strtoupper(md5($content));
  87. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  88. if (empty($resp)) {
  89. return [false, '系统错误'];
  90. }
  91. else
  92. {
  93. Log::record($resp, Log::DEBUG);
  94. $resp = json_decode($resp, true);
  95. if (empty($resp)) {
  96. return [false, '系统错误'];
  97. }
  98. elseif ($resp['Code'] === 0)
  99. {
  100. $status = $resp['Data']['OrderStatus'];
  101. if ($status === 2) {
  102. $order_state = ORDER_STATE_SUCCESS;
  103. } elseif ($status === 3) {
  104. $order_state = ORDER_STATE_CANCEL;
  105. } elseif ($status === 1) {
  106. $order_state = ORDER_STATE_SEND;
  107. } else {
  108. return [false, $status];
  109. }
  110. return [true, $order_state];
  111. }
  112. elseif ($resp['Code'] === -240 && (time() - $refill_info['commit_time'] >= 600))
  113. {
  114. return [true, ORDER_STATE_NOEXIST];
  115. }
  116. else {
  117. return [false, $resp['Msg']];
  118. }
  119. }
  120. }
  121. public function balance()
  122. {
  123. $params['UserId'] = config::USER_ID;
  124. $params['TimesTamp'] = $this->getMillisecond();
  125. $content = "{$params['UserId']}{$params['TimesTamp']}".config::Key;
  126. $params['Sign'] = strtoupper(md5($content));
  127. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  128. if (empty($resp)) {
  129. return [false, '系统错误'];
  130. }
  131. else
  132. {
  133. Log::record($resp, Log::DEBUG);
  134. $resp = json_decode($resp, true);
  135. if (empty($resp)) {
  136. return [false, '系统错误'];
  137. }
  138. elseif ($resp['Code'] === 0)
  139. {
  140. return [true, $resp['Data'][0]['Balance']];
  141. }
  142. else {
  143. return [false, $resp['Msg']];
  144. }
  145. }
  146. }
  147. private function sign($params)
  148. {
  149. $key = config::Key;
  150. $content = "{$params['UserId']}{$params['Count']}{$params['NotifyUrl']}{$params['Account']}{$params['ClientIP']}{$params['UserOrderId']}{$params['ProductNumber']}";
  151. $content .= "{$params['TimesTamp']}{$key}";
  152. return strtoupper(md5($content));
  153. }
  154. private function getMillisecond()
  155. {
  156. $cur = microtime (true);
  157. $cur = intval($cur * 1000);
  158. return $cur;
  159. }
  160. }