RefillPhone.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace refill\zhiren_doubi;
  3. require_once(BASE_HELPER_RAPI_PATH . '/zhiren_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(int $phone, int $amount, int $card_type, string $order_sn)
  38. {
  39. $params['merchantId'] = config::merchantId;
  40. $params['outTradeNo'] = $order_sn;
  41. $params['productId'] = config::PRODUCT[$card_type][$amount];
  42. $params['account'] = $phone;
  43. $params['num'] = 1;
  44. $params['amount'] = $amount;
  45. $params['callbackUrl'] = config::NOTIFY_URL;
  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. $order_sn = $params['order_sn'];
  55. $params = $this->req_params($card_no, $amount, $card_type, $order_sn);
  56. if(empty($params['skuCode'])) {
  57. return [false, '商品编号错误', false];
  58. }
  59. $sign = $this->sign($params);
  60. $params['verifyString'] = $sign;
  61. $resp = http_request(config::ORDER_URL, $params, 'POST', false, config::ExtHeaders, $net_errno);
  62. if (empty($resp)) {
  63. return [false, '网络错误', true];
  64. }
  65. else
  66. {
  67. Log::record($resp, Log::DEBUG);
  68. $resp = json_decode($resp, true);
  69. if (empty($resp)) {
  70. return [false, '网络错误', true];
  71. } elseif ($resp['code'] === 200) {
  72. return [true, $resp['data']['sysOrderNo'], false];
  73. } else {
  74. return [false, $resp['msg'], false];
  75. }
  76. }
  77. }
  78. public function query($refill_info)
  79. {
  80. $params['clientId'] = config::clientId;
  81. $params['clientOrderNo'] = $refill_info['order_sn'];
  82. $params['verifyString'] = $this->sign($params);
  83. $resp = http_request(config::QUERY_URL, $params, 'POST', false, config::ExtHeaders);
  84. if (empty($resp)) {
  85. return [false, '网络错误'];
  86. }
  87. else
  88. {
  89. Log::record($resp, Log::DEBUG);
  90. $resp = json_decode($resp, true);
  91. if (empty($resp)) {
  92. return [false, '网络错误'];
  93. }
  94. elseif ($resp['code'] === 200)
  95. {
  96. $data = $resp['data'];
  97. $status = $data['status'];
  98. if ($status === '4') {
  99. $updata['official_sn'] = $data['officialOrderNo'];
  100. Model('refill_order')->edit($refill_info['order_id'], $updata);
  101. $order_state = ORDER_STATE_SUCCESS;
  102. } elseif ($status === '3') {
  103. $order_state = ORDER_STATE_CANCEL;
  104. } elseif ($status === '0' || $status === '2') {
  105. $order_state = ORDER_STATE_SEND;
  106. } else {
  107. return [false, $resp['msg']];
  108. }
  109. return [true, $order_state];
  110. }
  111. elseif ($resp['code'] === -9 && (time() - $refill_info['commit_time'] >= 600))
  112. {
  113. return [true, ORDER_STATE_NOEXIST];
  114. }
  115. else
  116. {
  117. return [false, $resp['msg']];
  118. }
  119. }
  120. }
  121. public function balance()
  122. {
  123. $params['clientId'] = config::clientId;
  124. $params['verifyString'] = $this->sign($params);
  125. $resp = http_request(config::BALANCE_URL, $params, 'POST', false, config::ExtHeaders);
  126. if (empty($resp)) {
  127. return [false, '网络错误'];
  128. }
  129. else
  130. {
  131. Log::record($resp, Log::DEBUG);
  132. $resp = json_decode($resp, true);
  133. if (empty($resp)) {
  134. return [false, '网络错误'];
  135. } elseif ($resp['code'] === 200) {
  136. return [true, $resp['data']['balance']];
  137. } else {
  138. return [false, $resp['msg']];
  139. }
  140. }
  141. }
  142. private function sign($params)
  143. {
  144. ksort($params);
  145. $str = urldecode(http_build_query($params));
  146. $str .= '&key=' . config::Key;
  147. return md5($str);
  148. }
  149. }