proxy.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
  3. use mtopcard;
  4. use refill;
  5. use refill\errcode;
  6. class proxy
  7. {
  8. const max_retry = 5;
  9. const max_time_out = 120;
  10. public function add($params)
  11. {
  12. $refill_order = Model('refill_order');
  13. $mchid = $params['mchid'];
  14. $buyer_id = $params['buyer_id'];
  15. $amount = intval($params['amount']);
  16. $card_no = $params['card_no'];
  17. $mch_order = $params['mch_order'];
  18. $notify_url = $params['notify_url'];
  19. $idcard = $params['idcard'] ?? '';
  20. $card_name = $params['card_name'] ?? '';
  21. $order_time = $params['order_time'] ?? time();
  22. $commit_times = $params['commit_times'] ?? 0;
  23. $last_order_id = $params['order_id'] ?? 0;
  24. $org_quality = intval($params['org_quality']) ?? 0;
  25. $card_type = intval($params['card_type']) ?? 0;
  26. $regin_no = intval($params['regin_no']) ?? 0;
  27. $quantity = 1;
  28. Log::record("proxy::add mch_order={$mch_order} card_no = {$card_no}",Log::DEBUG);
  29. $need_check = false;
  30. if($card_type == 0)
  31. {
  32. $card_type = mtopcard\card_type($card_no,$regin_no);
  33. $params['card_type'] = $card_type;
  34. $params['regin_no'] = $regin_no;
  35. refill\util::incr_user_commit($card_type,$amount);
  36. global $config;
  37. if($config['phone_life_check'] == true && $card_type != mtopcard\PetroChinaCard && $card_type != mtopcard\SinopecCard) {
  38. $need_check = true;
  39. }
  40. }
  41. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  42. if ($need_check)
  43. {
  44. $valided = mtopcard\valid_phone($card_no);
  45. if (!$valided) {
  46. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  47. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times,
  48. "无效的手机号");
  49. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  50. return;
  51. }
  52. }
  53. [$errcode, $errmsg, $order_id, $neterr] = refill\RefillFactory::instance()->add($mchid, $buyer_id, $amount, $card_no,
  54. $mch_order, $idcard, $card_name, $notify_url, $quality,$org_quality,
  55. $order_time, $commit_times, $last_order_id,$card_type,$regin_no,$quantity);
  56. $params['commit_times'] += 1;
  57. $commit_times += 1;
  58. if(!empty($mch_order) && $last_order_id == 0 && $order_id > 0) {
  59. refill\util::pop_queue($mchid,$mch_order);
  60. }
  61. if($errcode !== true)
  62. {
  63. $fNotify = true;
  64. if(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr) || $errcode == refill\errcode::PROVIDER_OVERLOAD || $errcode == errcode::MERCHANT_PRICE_UNSETTING)
  65. {
  66. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  67. if ($quality > 0) {
  68. $fNotify = false;
  69. $params['order_id'] = $order_id;
  70. if($order_id > 0) {
  71. $refill_order->edit($order_id, ['is_retrying' => 1]);
  72. }
  73. if(defined('USE_COROUTINE') && USE_COROUTINE && $errcode == refill\errcode::PROVIDER_OVERLOAD) {
  74. \Swoole\Coroutine::sleep(1);
  75. }
  76. refill\util::push_add($params);
  77. }
  78. }
  79. if($fNotify)
  80. {
  81. if ($order_id === 0)
  82. {
  83. $order_info = $this->latest_order($refill_order, $mchid, $mch_order);
  84. if (empty($order_info)) {
  85. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  86. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times, $errmsg);
  87. refill\util::pop_queue($mchid,$mch_order);
  88. } else {
  89. $order_id = $order_info['order_id'];
  90. }
  91. }
  92. else {
  93. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  94. }
  95. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  96. }
  97. }
  98. }
  99. private function latest_order($refill_order,$mchid,$mch_order)
  100. {
  101. $orders = $refill_order->getMerchantOrderList(['mchid' => $mchid,'mch_order' => $mch_order]);
  102. if(empty($orders)) {
  103. return [];
  104. }
  105. else {
  106. $orders[0];
  107. }
  108. }
  109. public function notify($channel,$input)
  110. {
  111. return refill\RefillFactory::instance()->notify($channel,$input);
  112. }
  113. public function notify_merchant($order_id,$manual)
  114. {
  115. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  116. }
  117. public function query($order_id)
  118. {
  119. return refill\RefillFactory::instance()->query($order_id);
  120. }
  121. public function manual_success($order_id)
  122. {
  123. refill\RefillFactory::instance()->manual_success($order_id);
  124. }
  125. public function manual_cancel($order_id)
  126. {
  127. refill\RefillFactory::instance()->manual_cancel($order_id);
  128. }
  129. public function addthird($params)
  130. {
  131. $refill_order = Model('refill_order');
  132. $mchid = $params['mchid'];
  133. $buyer_id = $params['buyer_id'];
  134. $amount = intval($params['amount']);
  135. $card_no = $params['card_no'];
  136. $mch_order = $params['mch_order'];
  137. $notify_url = $params['notify_url'];
  138. $idcard = $params['idcard'] ?? '';
  139. $card_name = $params['card_name'] ?? '';
  140. $order_time = $params['order_time'] ?? time();
  141. $commit_times = $params['commit_times'] ?? 0;
  142. $last_order_id = $params['order_id'] ?? 0;
  143. $org_quality = intval($params['org_quality']) ?? 0;
  144. $card_type = intval($params['card_type']);
  145. $regin_no = 0;
  146. $product_code = $params['product_code'];
  147. $quantity = intval($params['quantity']);
  148. $third_card_type = $params['card_type'];
  149. $third_params = ['product_code' => $product_code,
  150. 'quantity' => $quantity,
  151. 'third_card_type' => $third_card_type];
  152. refill\util::incr_user_commit($card_type,$amount);
  153. [$errcode, $errmsg, $order_id, $neterr] = refill\RefillFactory::instance()->add($mchid, $buyer_id, $amount, $card_no,
  154. $mch_order, $idcard, $card_name, $notify_url, $org_quality,$org_quality,
  155. $order_time, $commit_times, $last_order_id,$card_type,$regin_no,$third_params);
  156. if($errcode !== true)
  157. {
  158. $fNotify = true;
  159. if(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr) || $errcode == refill\errcode::PROVIDER_OVERLOAD || $errcode == errcode::MERCHANT_PRICE_UNSETTING)
  160. {
  161. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  162. if ($quality > 0) {
  163. $fNotify = false;
  164. $params['order_id'] = $order_id;
  165. if($order_id > 0) {
  166. $refill_order->edit($order_id, ['is_retrying' => 1]);
  167. }
  168. if(defined('USE_COROUTINE') && USE_COROUTINE && $errcode == refill\errcode::PROVIDER_OVERLOAD) {
  169. \Swoole\Coroutine::sleep(1);
  170. }
  171. refill\util::push_add($params);
  172. }
  173. }
  174. if($fNotify)
  175. {
  176. if ($order_id === 0)
  177. {
  178. $order_info = $this->latest_order($refill_order, $mchid, $mch_order);
  179. if (empty($order_info)) {
  180. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  181. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times, $errmsg);
  182. refill\util::pop_queue($mchid,$mch_order);
  183. } else {
  184. $order_id = $order_info['order_id'];
  185. }
  186. }
  187. else {
  188. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  189. }
  190. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  191. }
  192. }
  193. }
  194. }