RefillPhone.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace refill\jumithird;
  3. require_once(BASE_HELPER_RAPI_PATH . '/jumithird/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 getProductCode($goods_id, $sys_pcode)
  28. {
  29. $thrid_refill = Model('thrid_refill');
  30. $store_id = $this->mStoreID;
  31. $product = $thrid_refill->getProviderProduct($store_id, $goods_id, $sys_pcode);
  32. if (empty($product)) {
  33. return false;
  34. } else {
  35. return $product['channel_code'];
  36. }
  37. }
  38. private function getProductAmount($sys_pcode)
  39. {
  40. $thrid_refill = Model('thrid_refill');
  41. $product = $thrid_refill->getProduct(['system_code' => $sys_pcode]);
  42. if (empty($product)) {
  43. return false;
  44. } else {
  45. return $product['refill_amount'];
  46. }
  47. }
  48. private function req_params(int $phone, int $amount, string $order_sn, string $product_code)
  49. {
  50. $params['mch_no'] = config::MCH_ID;
  51. $params['order_no'] = $order_sn;
  52. $params['product_id'] = $product_code;
  53. $params['money'] = $amount;
  54. $params['account'] = $phone;
  55. $params['notify_url'] = config::NOTIFY_URL;
  56. return $params;
  57. }
  58. public function add($card_no, $card_type, $amount, $params, &$net_errno = 0)
  59. {
  60. $order_sn = $params['order_sn'];
  61. $goods_id = intval($params['goods_id']);
  62. $product_code = $this->getProductCode($goods_id, $params['product_code']);
  63. $amount = $this->getProductAmount($params['product_code']);
  64. $params = $this->req_params($card_no, $amount, $order_sn, $product_code);
  65. $sign = $this->sign($params);
  66. $params['sign'] = $sign;
  67. $params = json_encode($params);
  68. $resp = http_post_data(config::ORDER_URL, $params, config::ExtHeaders, $net_errno);
  69. if (empty($resp)) {
  70. return [false, '系统错误', true];
  71. } else {
  72. Log::record($resp, Log::DEBUG);
  73. $resp = json_decode($resp, true);
  74. if (empty($resp)) {
  75. return [false, '系统错误', true];
  76. } elseif ($resp['code'] === '0000') {
  77. return [true, $resp['data']['channel_order_no'], false];
  78. } else {
  79. return [false, $resp['msg'], false];
  80. }
  81. }
  82. }
  83. public function query($refill_info)
  84. {
  85. $params['mch_no'] = config::MCH_ID;
  86. $params['order_no'] = $refill_info['order_sn'];
  87. $params['sign'] = $this->sign($params);
  88. $params = json_encode($params);
  89. $resp = http_post_data(config::QUERY_URL, $params, config::ExtHeaders);
  90. if (empty($resp)) {
  91. return [false, '系统错误'];
  92. } else {
  93. Log::record($resp, Log::DEBUG);
  94. $resp = json_decode($resp, true);
  95. if (empty($resp)) {
  96. return [false, '系统错误'];
  97. } elseif ($resp['code'] == '0000') {
  98. $status = intval($resp['data']['status']);
  99. if ($status === 1) {
  100. $updata['official_sn'] = $resp['data']['official_sn'];
  101. Model('refill_order')->edit($refill_info['order_id'], $updata);
  102. $order_state = ORDER_STATE_SUCCESS;
  103. } elseif ($status === 3) {
  104. $order_state = ORDER_STATE_CANCEL;
  105. } elseif (in_array($status, [0, 2])) {
  106. $order_state = ORDER_STATE_SEND;
  107. } else {
  108. return [false, $resp['msg']];
  109. }
  110. return [true, $order_state];
  111. } else {
  112. return [false, $resp['msg']];
  113. }
  114. }
  115. }
  116. public function balance()
  117. {
  118. $params['mch_no'] = config::MCH_ID;
  119. $params['sign'] = $this->sign($params);
  120. $params = json_encode($params);
  121. $resp = http_post_data(config::BALANCE_URL, $params, config::ExtHeaders);
  122. if (empty($resp)) {
  123. return [false, '系统错误'];
  124. } else {
  125. Log::record($resp, Log::DEBUG);
  126. $resp = json_decode($resp, true);
  127. if (empty($resp)) {
  128. return [false, '系统错误'];
  129. } elseif ($resp['code'] == '0000') {
  130. return [true, $resp['data']['money']];
  131. } else {
  132. return [false, $resp['msg']];
  133. }
  134. }
  135. }
  136. private function sign($params)
  137. {
  138. $content = '';
  139. ksort($params);
  140. foreach ($params as $key => $val) {
  141. $content .= "{$key}={$val}&";
  142. }
  143. $content .= "token=" . config::Token;
  144. return md5($content);
  145. }
  146. }