RefillPhone.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace refill\yushang_normal;
  3. require_once(BASE_HELPER_RAPI_PATH . '/yushang_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. }
  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. $input = [
  36. 'BuyCount' => 1,
  37. 'CallBackUrl' => config::NOTIFY_URL,
  38. 'ChargeAccount' => $phone,
  39. 'CustomerIP' => '127.0.0.1',
  40. 'MOrderID' => $order_sn,
  41. 'ProductCode' => $sku_code,
  42. 'TimesTamp' => config::time_stamp()
  43. ];
  44. return config::gen_params($input,config::add_keys);
  45. }
  46. //[$state, $errmsg, $neterr]
  47. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0): array
  48. {
  49. $params = $this->add_params($card_no, $amount, $params['order_sn'],$card_type);
  50. if(empty($params)) {
  51. return [false, '提单参数不符合', false];
  52. }
  53. $resp = http_request(config::ORDER_URL, $params, 'POST', false, [], $net_errno);
  54. if (empty($resp)) {
  55. return [false, '系统错误', true];
  56. }
  57. else
  58. {
  59. Log::record($resp, Log::DEBUG);
  60. $resp = json_decode($resp, true);
  61. if (empty($resp)) {
  62. return [false, '系统错误', true];
  63. } elseif ($resp['Code'] === 999) { /// 999 收单成功
  64. return [true, $resp['OrderID'], false];
  65. } else {
  66. return [false, $resp['Msg'], false];
  67. }
  68. }
  69. }
  70. public function query($refill_info): array
  71. {
  72. $input = [
  73. 'TimesTamp' => config::time_stamp(),
  74. 'MOrderID' =>$refill_info['order_sn']
  75. ];
  76. $params = config::gen_params($input,config::query_keys);
  77. $resp = http_request(config::QUERY_URL, $params , 'POST');
  78. if (empty($resp)) {
  79. return [false, '系统错误', ''];
  80. }
  81. else
  82. {
  83. Log::record($resp, Log::DEBUG);
  84. $resp = json_decode($resp, true);
  85. if (empty($resp)) {
  86. return [false, '系统错误', ''];
  87. }
  88. $code = intval($resp['Code']);
  89. if ($code === 999)
  90. {
  91. $val = $resp['Data'];
  92. $status = intval($val['OrderState']);
  93. $official_sn = $val['ExtendParam']['OfficialOrderID'] ?? '';
  94. //充值状态:1=订单正在处理中,2=订单成功,3=订单失败,4=订单状态异常或位置
  95. if ($status == 2) {
  96. Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['OrderID'],'official_sn' => $official_sn]);
  97. $order_state = ORDER_STATE_SUCCESS;
  98. }
  99. elseif ($status == 3) {
  100. Model('refill_order')->edit($refill_info['order_id'], ['ch_trade_no' => $val['OrderID']]);
  101. $order_state = ORDER_STATE_CANCEL;
  102. }
  103. else {
  104. $order_state = ORDER_STATE_SEND;
  105. }
  106. return [true, $order_state, $official_sn];
  107. }
  108. elseif($code === 10 and (time() - $refill_info['commit_time']) >= 300) {
  109. return [true, ORDER_STATE_NOEXIST, ''];
  110. }
  111. else
  112. {
  113. return [false, $resp['Msg']];
  114. }
  115. }
  116. }
  117. public function balance(): array
  118. {
  119. $input = [
  120. 'TimesTamp' => config::time_stamp()
  121. ];
  122. $params = config::gen_params($input,config::balance_keys);
  123. $resp = http_request(config::BALANCE_URL, $params , 'POST');
  124. if (empty($resp)) {
  125. return [false, '系统错误'];
  126. }
  127. else
  128. {
  129. Log::record($resp, Log::DEBUG);
  130. $resp = json_decode($resp, true);
  131. if (empty($resp)) {
  132. return [false, '系统错误'];
  133. } elseif ($resp['Code'] === 999) {
  134. return [true, ncPriceFormat($resp['Data'][0]['Balance'])];
  135. } else {
  136. return [false, $resp['Msg']];
  137. }
  138. }
  139. }
  140. }