RefillPhone.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace refill\dixin_normal;
  3. require_once(BASE_HELPER_RAPI_PATH . '/dixin_normal/config.php');
  4. use mtopcard;
  5. use refill;
  6. use Log;
  7. class RefillPhone extends refill\IRefillPhone
  8. {
  9. public function __construct($cfgs)
  10. {
  11. parent::__construct($cfgs);
  12. }
  13. private function add_params(int $phone, int $amount, string $order_sn, int $card_type): array
  14. {
  15. $oper_getter = function ($card_type)
  16. {
  17. if ($card_type == mtopcard\ChinaMobileCard) {
  18. return 'YD';
  19. } elseif ($card_type == mtopcard\ChinaUnicomCard) {
  20. return 'LT';
  21. } elseif ($card_type == mtopcard\ChinaTelecomCard) {
  22. return 'DX';
  23. } else {
  24. return false;
  25. }
  26. };
  27. $operator = $oper_getter($card_type);
  28. $sku_code = config::sku_code($card_type,$amount);
  29. if($operator === false || empty($sku_code)) {
  30. return [];
  31. }
  32. $params = [
  33. 'merchant_no' => $order_sn,
  34. 'sku_code' => $sku_code,
  35. 'format' => ['account' => $phone],
  36. "format_type" => "common",
  37. 'notice_url' => config::NOTIFY_URL,
  38. "uid" => "123456"
  39. ];
  40. $result = $this->method('create.order',$params);
  41. return $result;
  42. }
  43. private function method($method,$data = [],$attach = '')
  44. {
  45. $param = [
  46. 'access_token' => config::ACCESS_TOKEN,
  47. 'once' => uniqid(),
  48. 'timestamp' => $this->getUnixTimestamp(),
  49. 'attach' => $attach,
  50. 'format' => 'JSON',
  51. 'sign_type' => 'MD5',
  52. 'version' => '1.0.0',
  53. 'method' => $method,
  54. 'data' => json_encode($data, 256),
  55. ];
  56. $param = array_merge($param, ['sign' => config::sign($param)]);
  57. return $param;
  58. }
  59. private function getUnixTimestamp ()
  60. {
  61. list($s1, $s2) = explode(' ', microtime());
  62. return (float)sprintf('%.0f',(floatval($s1) + floatval($s2)) * 1000);
  63. }
  64. //[$state, $errmsg, $neterr]
  65. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  66. {
  67. $params = $this->add_params($card_no, $amount, $params['order_sn'],$card_type);
  68. if(empty($params)) {
  69. return [false, '提单参数不符合', false];
  70. }
  71. $resp = http_request(config::ORDER_URL, $params , 'POST' , false , [] , $net_errno);
  72. if (empty($resp)) {
  73. return [false, '系统错误', true];
  74. }
  75. else
  76. {
  77. Log::record($resp, Log::DEBUG);
  78. $resp = json_decode($resp ,true);
  79. if (empty($resp)) {
  80. return [false, '系统错误', true];
  81. } elseif ($resp['code'] === 200) {
  82. return [true, '', false];
  83. } else {
  84. return [false, $resp['message'], false];
  85. }
  86. }
  87. }
  88. public function query($refill_info): array
  89. {
  90. $params['merchant_no'] = $refill_info['order_sn']; //sn码
  91. $params = $this->method('query.order',$params);//查数据库情况,查询全部数据结构包括orderid
  92. $resp = http_request(config::ORDER_URL, $params , 'POST');
  93. if (empty($resp)) {
  94. return [false, '系统错误', ''];
  95. }
  96. else
  97. {
  98. Log::record($resp, Log::DEBUG);
  99. $resp = json_decode($resp, true);
  100. if (empty($resp)) {
  101. return [false, '系统错误', ''];
  102. }
  103. elseif ($resp['code'] === 200)
  104. {
  105. $val = $resp['data'][0];
  106. $status = $val['recharge_status'];
  107. $official_sn = '';//strtolower($val['official_sn']) == 'null' ? '' : $val['official_sn'];
  108. //充值状态:1=待充值,2=充值中,3=充值完成,4=充值失败,5=运营商维护,8=部分到账
  109. if ($status == '3') {
  110. $save['ch_trade_no'] = $val['order_no'];//平台的订单号
  111. $save['official_sn'] = $official_sn;
  112. Model('refill_order')->edit($refill_info['order_id'], $save);
  113. $order_state = ORDER_STATE_SUCCESS;
  114. }
  115. elseif (in_array($status,[4,5])) {
  116. Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['order_no']]);
  117. $order_state = ORDER_STATE_CANCEL;
  118. }
  119. else {
  120. $order_state = ORDER_STATE_SEND;
  121. }
  122. return [true, $order_state, $official_sn];
  123. }
  124. else
  125. {
  126. return [false, $resp['message'], ''];
  127. }
  128. }
  129. }
  130. public function balance(): array
  131. {
  132. $params = $this->method('query.balance',[]);
  133. $resp = http_request(config::ORDER_URL, $params , 'POST');
  134. if (empty($resp)) {
  135. return [false, '系统错误'];
  136. }
  137. else
  138. {
  139. Log::record($resp, Log::DEBUG);
  140. $resp = json_decode($resp, true);
  141. if (empty($resp)) {
  142. return [false, '系统错误'];
  143. } elseif ($resp['code'] === 200) {
  144. return [true, ncPriceFormat($resp['data']['predict_money'])];
  145. } else {
  146. return [false, $resp['message']];
  147. }
  148. }
  149. }
  150. }