RefillPhone.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace refill\guochuang;
  3. require_once(BASE_HELPER_RAPI_PATH . '/guochuang/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. $key = "{$card_type}-{$amount}-{$regin_no}";
  17. $price = config::Price[$key];
  18. if(empty($price)) {
  19. Log::record("channel cannot find price where name={$this->mName}, goods_id = {$goods_id} card_type={$card_type} amount={$amount} regin_no={$regin_no}",Log::ERR);
  20. return [0,0];
  21. } else {
  22. return [$goods_id,ncPriceFormat($price)];
  23. }
  24. }
  25. private function req_params(int $phone, int $amount, int $card_type, string $order_sn)
  26. {
  27. $params['phone'] = $phone;
  28. $params['phoneType'] = config::operator[$card_type];
  29. $params['money'] = $amount;
  30. $params['outerId'] = $order_sn;
  31. $params['callBackUrl'] = config::NOTIFY_URL;
  32. $params['speed'] = 0;
  33. return $params;
  34. }
  35. //统一请求
  36. private function url_request($params,$service)
  37. {
  38. $get_params['service'] = $service;
  39. $get_params['userId'] = config::UserId;
  40. $get_params['ts'] = $this->getMillisecond();
  41. $sign = $this->sign($params,$get_params);
  42. $get_params['sign'] = $sign;
  43. $url = config::API_URL;
  44. $url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($get_params) ? http_build_query($get_params) : $get_params);
  45. return http_request($url, $params, 'POST', false, [], $net_errno);
  46. }
  47. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  48. {
  49. refill\util::send_quick($params['order_sn']);
  50. return [true , '',false];
  51. }
  52. public function query($refill_info)
  53. {
  54. //上游单号,可以为空,但需参与签名
  55. $params['id'] = '';
  56. $params['outerId'] = $refill_info['order_sn'];
  57. $resp = $this->url_request($params,'order.status.query');
  58. if (empty($resp)) {
  59. return [false, '系统错误'];
  60. }
  61. else
  62. {
  63. Log::record($resp, Log::DEBUG);
  64. $resp = json_decode($resp, true);
  65. $code = $resp['code'];
  66. if (empty($resp))
  67. {
  68. return [false, '系统错误'];
  69. }
  70. elseif ($code == 'SUCCESS')
  71. {
  72. $status = $resp['status'];
  73. if ($status === 'SUCCESS') {
  74. $order_state = ORDER_STATE_SUCCESS;
  75. $save['official_sn'] = strtolower($resp['evidence']) == 'null' ? '' : $resp['evidence'];
  76. Model('refill_order')->edit($refill_info['order_id'], $save);
  77. } elseif ($status === 'FAIL') {
  78. $order_state = ORDER_STATE_CANCEL;
  79. } elseif ($status === 'PROCESSING') {
  80. $order_state = ORDER_STATE_SEND;
  81. } elseif ($status === 'ORDER_NOT_EXIST' && (time() - $refill_info['commit_time'] >= 600)) {
  82. $order_state = ORDER_STATE_NOEXIST;
  83. } else {
  84. return [false, $code];
  85. }
  86. return [true, $order_state];
  87. }
  88. else
  89. {
  90. return [false, "code={$code}"];
  91. }
  92. }
  93. }
  94. public function balance()
  95. {
  96. $resp = $this->url_request([],'user.balance.query');
  97. if (empty($resp)) {
  98. return [false, '系统错误'];
  99. }
  100. else
  101. {
  102. Log::record($resp, Log::DEBUG);
  103. $resp = json_decode($resp, true);
  104. if (empty($resp))
  105. {
  106. return [false, '系统错误'];
  107. }
  108. elseif ($resp['code'] == 'SUCCESS')
  109. {
  110. return [true, $resp['balance']];
  111. }
  112. else
  113. {
  114. return [false, $resp['code']];
  115. }
  116. }
  117. }
  118. private function sign($params,$get_params)
  119. {
  120. $userId = config::UserId;
  121. $api_key = config::ApiKey;
  122. $content = "service={$get_params['service']}&userId={$userId}&ts={$get_params['ts']}&";
  123. if(!empty($params)) {
  124. foreach ($params as $key => $value){
  125. $content .= "{$key}={$value}&";
  126. }
  127. $content = rtrim($content, '&');
  128. }
  129. $content .= "&key={$api_key}";
  130. return strtoupper(md5($content));
  131. }
  132. /**
  133. * 获取毫秒级别的时间戳
  134. */
  135. private function getMillisecond()
  136. {
  137. $cur = microtime (true);
  138. $cur = intval($cur * 1000);
  139. return $cur;
  140. }
  141. }