RefillPhone.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace refill\feimingyu_fs;
  3. require_once(BASE_HELPER_RAPI_PATH . '/feimingyu_fs/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, $regin_no)
  26. {
  27. $params['appId'] = config::APP_ID;
  28. $params['timestamp'] = $this->get_millisecond();
  29. $params['applyNo'] = $order_sn;
  30. $params['rechargeNo'] = $phone;
  31. $params['agentProductId'] = config::PRODUCT[$card_type][$regin_no][$amount];
  32. $params['denomination'] = $amount;
  33. $params['callbackUrl'] = config::NOTIFY_URL;
  34. return $params;
  35. }
  36. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  37. {
  38. $regin_no = $params['regin_no'] ?? -1;
  39. if($regin_no <= 0) {
  40. return [false, '省份获取错误', false];
  41. }
  42. $order_sn = $params['order_sn'];
  43. $params = $this->req_params($card_no, $amount, $card_type, $order_sn, $regin_no);
  44. if(empty($params['agentProductId'])) {
  45. return [false, '商品编号错误', false];
  46. }
  47. $sign = config::sign($params);
  48. $params['sign'] = $sign;
  49. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  50. if (empty($resp)) {
  51. return [false, '网络错误', true];
  52. }
  53. else
  54. {
  55. Log::record($resp, Log::DEBUG);
  56. $resp = json_decode($resp, true);
  57. if (empty($resp)) {
  58. return [false, '网络错误', true];
  59. } elseif ($resp['code'] === '00000') {
  60. return [true, $resp['data']['orderNo'], false];
  61. } elseif ($resp['code'] === 'A0503') {
  62. $net_errno = "HTTP-{$resp['code']}";
  63. return [false, $resp['msg'], true];
  64. } else {
  65. return [false, $resp['msg'], false];
  66. }
  67. }
  68. }
  69. public function query($refill_info)
  70. {
  71. $params['appId'] = config::APP_ID;
  72. $params['timestamp'] = $this->get_millisecond();
  73. $params['applyNo'] = $refill_info['order_sn'];
  74. $params['sign'] = config::sign($params);
  75. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  76. if (empty($resp)) {
  77. return [false, '网络错误', ''];
  78. }
  79. else
  80. {
  81. Log::record($resp, Log::DEBUG);
  82. $resp = json_decode($resp, true);
  83. if (empty($resp)) {
  84. return [false, '网络错误', ''];
  85. }
  86. elseif ($resp['code'] === '00000')
  87. {
  88. $offical_sn = '';
  89. $status = $resp['data']['orderStatus'];
  90. if ($status === 'SUCCESS') {
  91. $offical_sn = $resp['data']['ext1'];
  92. $updata['official_sn'] = $offical_sn;
  93. Model('refill_order')->edit($refill_info['order_id'], $updata);
  94. $order_state = ORDER_STATE_SUCCESS;
  95. } elseif ($status === 'FAILED') {
  96. $order_state = ORDER_STATE_CANCEL;
  97. } elseif ($status === 'PROCESSING' || $status === 'INIT') {
  98. $order_state = ORDER_STATE_SEND;
  99. } else {
  100. return [false, $resp['msg'], $offical_sn];
  101. }
  102. return [true, $order_state, $offical_sn];
  103. }
  104. else
  105. {
  106. return [false, $resp['msg'], ''];
  107. }
  108. }
  109. }
  110. public function balance()
  111. {
  112. $params['appId'] = config::APP_ID;
  113. $params['timestamp'] = $this->get_millisecond();
  114. $params['sign'] = config::sign($params);
  115. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  116. if (empty($resp)) {
  117. return [false, '网络错误'];
  118. }
  119. else
  120. {
  121. Log::record($resp, Log::DEBUG);
  122. $resp = json_decode($resp, true);
  123. if (empty($resp)) {
  124. return [false, '网络错误'];
  125. } elseif ($resp['code'] === '00000') {
  126. return [true, $resp['data']['balance']];
  127. } else {
  128. return [false, $resp['msg']];
  129. }
  130. }
  131. }
  132. /**
  133. * 获取毫秒级别的时间戳
  134. */
  135. private function get_millisecond()
  136. {
  137. list($msec, $sec) = explode(' ', microtime());
  138. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  139. return date('YmdHis').substr($msectime, -3);
  140. }
  141. }