RefillPhone.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace refill\xiaoniu_doubi;
  3. require_once(BASE_HELPER_RAPI_PATH . '/xiaoniu_doubi/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. $store_id = $this->mStoreID;
  17. $pcode = $other['product_code'];
  18. $thrid_refill = Model('thrid_refill');
  19. $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $pcode);
  20. if (empty($product)) {
  21. Log::record("cannot find provider's produce where name={$this->mName}, goods_id = {$goods_id} pcode={$pcode}", Log::ERR);
  22. return [0, 0];
  23. } else {
  24. return [$goods_id, ncPriceFormat($product['channel_amount'])];
  25. }
  26. }
  27. private function getProductAmount($sys_pcode)
  28. {
  29. $thrid_refill = Model('thrid_refill');
  30. $product = $thrid_refill->getProduct(['system_code' => $sys_pcode]);
  31. if (empty($product)) {
  32. return false;
  33. } else {
  34. return $product['refill_amount'];
  35. }
  36. }
  37. private function req_params($phone, int $amount, string $order_sn)
  38. {
  39. $params['mId'] = config::MCH_ID;
  40. $params['mOrderNo'] = $order_sn;
  41. $params['account'] = $phone;
  42. $params['subclassType'] = 'DB';
  43. $params['faceAmount'] = $amount;
  44. $params['notifyUrl'] = config::NOTIFY_URL;
  45. $params['timestamp'] = date("YmdHis");
  46. return $params;
  47. }
  48. public function add($card_no, $card_type, $amount, $params,&$net_errno = 0)
  49. {
  50. $amount = $this->getProductAmount($params['product_code']);
  51. if(empty($amount)) {
  52. return [false, '产品未开启', false];
  53. }
  54. $params = $this->req_params($card_no, $amount, $params['order_sn']);
  55. $sign = $this->sign($params);
  56. $params['sign'] = $sign;
  57. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  58. if (empty($resp)) {
  59. return [false, '系统错误', true];
  60. }
  61. else
  62. {
  63. Log::record($resp, Log::DEBUG);
  64. $resp = json_decode($resp, true);
  65. if (empty($resp)) {
  66. return [false, '系统错误', true];
  67. } elseif ($resp['code'] === '0') {
  68. return [true, $resp['orderNo'], false];
  69. } elseif ($resp['code'] === 'XN_1000'){
  70. $net_errno = 'HTTP-XN_1000';
  71. return [false, $net_errno, true];
  72. } else {
  73. return [false, $resp['msg'], false];
  74. }
  75. }
  76. }
  77. public function query($refill_info)
  78. {
  79. $params['mId'] = config::MCH_ID;
  80. $params['mOrderNo'] = $refill_info['order_sn'];
  81. $params['sign'] = $this->sign($params);
  82. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  83. if (empty($resp)) {
  84. return [false, '系统错误'];
  85. }
  86. else
  87. {
  88. Log::record($resp, Log::DEBUG);
  89. $resp = json_decode($resp, true);
  90. if (empty($resp)) {
  91. return [false, '系统错误'];
  92. }
  93. elseif ($resp['code'] === '0')
  94. {
  95. $status = intval($resp['status']);
  96. if ($status === 1) {
  97. $updata['official_sn'] = $resp['content'];
  98. Model('refill_order')->edit($refill_info['order_id'], $updata);
  99. $order_state = ORDER_STATE_SUCCESS;
  100. } elseif ($status === 2) {
  101. $order_state = ORDER_STATE_CANCEL;
  102. } elseif ($status === 3) {
  103. $order_state = ORDER_STATE_SEND;
  104. } else {
  105. return [false, $status];
  106. }
  107. return [true, $order_state];
  108. }
  109. else {
  110. return [false, $resp['msg']];
  111. }
  112. }
  113. }
  114. public function balance()
  115. {
  116. $params['mId'] = config::MCH_ID;
  117. $params['sign'] = $this->sign($params);
  118. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  119. if (empty($resp)) {
  120. return [false, '系统错误'];
  121. }
  122. else
  123. {
  124. Log::record($resp, Log::DEBUG);
  125. $resp = json_decode($resp, true);
  126. if (empty($resp)) {
  127. return [false, '系统错误'];
  128. }
  129. elseif ($resp['code'] === '0')
  130. {
  131. return [true, $resp['balance']];
  132. }
  133. else {
  134. return [false, $resp['msg']];
  135. }
  136. }
  137. }
  138. private function sign($params)
  139. {
  140. $content = '';
  141. ksort($params);
  142. foreach ($params as $key => $val){
  143. if(false === $this->check_empty($val)) {
  144. $content .= "{$key}={$val}&";
  145. }
  146. }
  147. $content .= "key=".config::Key;
  148. return md5($content);
  149. }
  150. }