RefillPhone.php 5.4 KB

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