RefillBase.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. namespace refill;
  3. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  4. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  5. require_once(BASE_HELPER_PATH . '/refill/IRefill.php');
  6. require_once(BASE_HELPER_PATH . '/refill/IRefillOil.php');
  7. require_once(BASE_HELPER_PATH . '/refill/IRefillPhone.php');
  8. require_once(BASE_HELPER_PATH . '/refill/IRefillCallBack.php');
  9. require_once(BASE_HELPER_PATH . '/refill/ProviderManager.php');
  10. require_once(BASE_HELPER_PATH . '/refill/CalcMerchantPrice.php');
  11. require_once(BASE_HELPER_PATH . '/refill/util.php');
  12. require_once(BASE_HELPER_PATH . '/refill/errcode.php');
  13. use Log;
  14. use mtopcard;
  15. use QueueClient;
  16. use member_info;
  17. use Exception;
  18. use trans_wapper;
  19. class RefillBase
  20. {
  21. protected $mPolicy;
  22. protected $mLimits = [];
  23. protected function __construct()
  24. {
  25. }
  26. public function allow($mchid,$card_type,$amount,$quality)
  27. {
  28. return $this->mPolicy->allow($mchid,$card_type,$amount,$quality);
  29. }
  30. public function goods()
  31. {
  32. return $this->mPolicy->goods();
  33. }
  34. public function load()
  35. {
  36. $this->mPolicy->load();
  37. }
  38. public function first_quality($mchid) {
  39. return $this->mPolicy->first_quality($mchid);
  40. }
  41. public function notify($chname, $input)
  42. {
  43. $caller = $this->mPolicy->getCaller($chname);
  44. if($caller === false) {
  45. return false;
  46. }
  47. elseif ($caller->verify($input))
  48. {
  49. [$order_id, $success, $can_try, $need_handle] = $caller->notify($input);
  50. if (!$need_handle) {
  51. return true;
  52. }
  53. if ($order_id !== false) {
  54. return $this->proc_notify($order_id, $success, $can_try, $chname);
  55. } else {
  56. Log::record("系统无此订单ID:{$order_id}", Log::ERR);
  57. }
  58. }
  59. else {
  60. Log::record("{$chname} 签名失败.");
  61. }
  62. return true;
  63. }
  64. private function proc_notify($order_id,$success, $can_try,$chname)
  65. {
  66. $mod_order = Model('vr_order');
  67. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  68. $order_state = intval($order_info['order_state']);
  69. if ($order_state != ORDER_STATE_SEND) {
  70. return false;
  71. }
  72. $logic_vr_order = Logic("vr_order");
  73. $mod_refill = Model('refill_order');
  74. try
  75. {
  76. $tran = new trans_wapper($mod_order,'notify change order state trans');
  77. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  78. $order_state = intval($order_info['order_state']);
  79. if ($order_state != ORDER_STATE_SEND) {
  80. $tran->commit();
  81. return false;
  82. }
  83. if ($success) {
  84. $logic_vr_order->changeOrderStateSuccess($order_id);
  85. }
  86. elseif ($can_try)
  87. {
  88. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  89. if(!empty($refill_info))
  90. {
  91. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,正在重试");
  92. if ($this->retry($refill_info, $order_info)) {
  93. $mod_refill->edit($order_id, ['is_retrying' => 1]);
  94. $tran->commit();
  95. return true;
  96. }
  97. }
  98. }
  99. else {
  100. $logic_vr_order->changeOrderStateCancel($order_info, '', "{$chname}接口回调通知失败,不可重试.");
  101. }
  102. $tran->commit();
  103. }
  104. catch (Exception $ex) {
  105. $tran->rollback();
  106. Log::record("Error:" . $ex->getMessage(), Log::ERR);
  107. }
  108. $mod_refill->edit($order_id, ['notify_time' => time(), 'notify_state' => 1]);
  109. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  110. return true;
  111. }
  112. private function retry(array $refill_info, array $order_info)
  113. {
  114. [$cantry,$quality] = $this->mPolicy->can_retry($refill_info,$order_info);
  115. if(!$cantry) return false;
  116. $params = [ 'mchid' => $refill_info['mchid'],
  117. 'buyer_id' => $order_info['buyer_id'],
  118. 'amount' => $refill_info['refill_amount'],
  119. 'card_no' => $refill_info['card_no'],
  120. 'quality' => $quality,
  121. 'mch_order' => $refill_info['mch_order'],
  122. 'notify_url' => $refill_info['notify_url'],
  123. 'idcard' => $refill_info['idcard'] ?? '',
  124. 'card_name' => $refill_info['card_name'] ?? '',
  125. 'order_time' => $refill_info['order_time'],
  126. 'commit_times' => $refill_info['commit_times'] + 1,
  127. 'order_id' => $refill_info['order_id']
  128. ];
  129. return util::push_add($params);
  130. }
  131. public function zero_order($mchid, $buyer_id, $amount, $card_no,
  132. $mch_order, $idcard, $card_name, $notify_url,$quality,
  133. $order_time = 0, $commit_times = 0,$errmsg='')
  134. {
  135. $card_type = mtopcard\card_type($card_no);
  136. $minfo = new member_info($buyer_id);
  137. $calc = new ZeroMerchantPrice($mchid, $amount, $card_type,$quality);
  138. $mch_amount = $calc->calc_vgoods_price([]);
  139. $input['goods_id'] = ZERO_GOODS_ID;
  140. $input['quantity'] = 1; //数量
  141. $input['buyer_phone'] = $minfo->mobile();
  142. $input['buyer_name'] = $minfo->truename();
  143. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  144. $input['order_from'] = 1;
  145. $input['pd_pay'] = true;
  146. $logic_buy_virtual = Logic('buy_virtual');
  147. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true);
  148. $mod_refill = Model('refill_order');
  149. if ($result['state'] === true)
  150. {
  151. $order_sn = $result['data']['order_sn'];
  152. $order_id = $result['data']['order_id'];
  153. $logic_vr_order = Logic("vr_order");
  154. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  155. $logic_vr_order->changeOrderStateCancel($order_info, '', '无法下单创建0元订单');
  156. if (empty($mch_order)) {
  157. $mch_order = $order_sn;
  158. }
  159. //虚拟订单表信息扩展
  160. $orderext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $mchid,
  161. 'refill_amount' => $amount, 'mch_order' => $mch_order,
  162. 'idcard' => $idcard, 'card_name' => $card_name,
  163. 'notify_url' => $notify_url, 'channel_name' => '',
  164. 'mch_amount' => $mch_amount, 'channel_amount' => 0,
  165. 'order_time' => $order_time, 'commit_times' => $commit_times,
  166. 'commit_time' => time(),'notify_state' => 1,'notify_time' => time(),
  167. 'card_type' => $card_type, 'card_no' => $card_no,'quality' => $quality,'err_msg' => $errmsg];
  168. $mod_refill->add_refill($orderext);
  169. return $order_id;
  170. }
  171. else {
  172. return 0;
  173. }
  174. }
  175. public function can_nettry($quality,$order_time,$commit_times) : bool
  176. {
  177. return $this->mPolicy->can_nettry($quality,$order_time,$commit_times);
  178. }
  179. //$quality,质量
  180. //返回值:[ 错误码,错误信息,订单ID,是否是网络错误]
  181. //说明:错误码为true 表示成功
  182. // 其它情况,则需要判断订单ID
  183. public function add($mchid, $buyer_id, $amount, $card_no,
  184. $mch_order, $idcard, $card_name, $notify_url,$quality,
  185. $order_time, $commit_times,$last_orderid = 0)
  186. {
  187. $card_type = mtopcard\card_type($card_no);
  188. [$providers,$overload] = $this->mPolicy->find_providers($amount,$card_type,$quality);
  189. if (empty($providers))
  190. {
  191. if($overload) {
  192. return [errcode::PROVIDER_OVERLOAD, "匹配不到合适的充值通道",0,false];
  193. }
  194. else {
  195. return [errcode::CANNOT_MATCH_PROVIDER, "匹配不到合适的充值通道",0,false];
  196. }
  197. }
  198. if (empty($notify_url)) {
  199. $notify_url = "";
  200. }
  201. $minfo = new member_info($buyer_id);
  202. $calc = new CalcMerchantPrice($mchid, $amount, $card_type,$quality);
  203. $mch_amount = $calc->calc_vgoods_price([]);
  204. $available = $minfo->available_predeposit();
  205. if ($mch_amount > $available) {
  206. Log::record("下单时机构余额不足,可用余额为:{$available}", Log::DEBUG);
  207. return [errcode::MERCHANT_SHORT_MONEY, "余额不足支付订单",0,false];
  208. }
  209. $refill_state = false;
  210. $order_success = false;
  211. foreach ($providers as $provider)
  212. {
  213. $channel_name = $provider->name();
  214. Log::record("select channel name = {$channel_name}",Log::DEBUG);
  215. //通道价格大于客户价格,换通道.
  216. [$goods_id, $price] = $provider->goods($quality,$amount,$card_type);
  217. if ($price > $mch_amount) continue;
  218. Log::record("start create order",Log::DEBUG);
  219. $input['goods_id'] = $goods_id;
  220. $input['quantity'] = 1; //数量
  221. $input['buyer_phone'] = $minfo->mobile();
  222. $input['buyer_name'] = $minfo->truename();
  223. $input['buyer_msg'] = $_POST['buyer_msg'] ?? '';
  224. $input['order_from'] = 1;
  225. $input['pd_pay'] = true;
  226. $logic_buy_virtual = Logic('buy_virtual');
  227. $result = $logic_buy_virtual->buyStep3($input, $buyer_id, [$calc, 'calc_vorder_amount'], true);
  228. $mod_refill = Model('refill_order');
  229. if ($result['state'] === true)
  230. {
  231. $order_sn = $result['data']['order_sn'];
  232. $order_id = $result['data']['order_id'];
  233. if($last_orderid > 0) {
  234. $mod_refill->edit($last_orderid, ['inner_status' => 1]);
  235. }
  236. $last_orderid = $order_id;
  237. if(!$this->pay_completed($order_sn)) {
  238. $logic_vr_order = Logic("vr_order");
  239. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  240. $logic_vr_order->changeOrderStateCancel($order_info, '', "预存款不足以支付该订单");
  241. continue;
  242. }
  243. if (empty($mch_order)) {
  244. $mch_order = $order_sn;
  245. }
  246. //虚拟订单表信息扩展
  247. $orderext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $mchid,
  248. 'refill_amount' => $amount, 'mch_order' => $mch_order,
  249. 'idcard' => $idcard, 'card_name' => $card_name,
  250. 'notify_url' => $notify_url, 'channel_name' => $channel_name,
  251. 'mch_amount' => $mch_amount, 'channel_amount' => $price,
  252. 'order_time' => $order_time, 'commit_times' => $commit_times,
  253. 'card_type' => $card_type, 'card_no' => $card_no,'quality' => $quality];
  254. $mod_refill->add_refill($orderext);
  255. } else {
  256. Log::record("{$result['msg']}",Log::ERR);
  257. continue;
  258. }
  259. $order_success = true;
  260. util::incr_order($channel_name,$card_type,$amount,$quality);
  261. $params = ['order_sn' => $order_sn, 'idcard' => $idcard, 'card_name' => $card_name];
  262. [$state, $errmsg,$neterr] = $provider->add($card_no, $card_type, $amount, $params);
  263. if ($state)
  264. {
  265. //提交成功
  266. $trade_no = $errmsg;
  267. if ($provider->refill_type() == 'api') {
  268. $logic_vr_order = Logic("vr_order");
  269. $logic_vr_order->changeOrderStateSend($order_id);
  270. }
  271. $data = ['commit_time' => time(), 'ch_trade_no' => $trade_no];
  272. $mod_refill->edit($order_id, $data);
  273. $refill_state = true;
  274. //如果对方没有回调能力,则启动主动查询.
  275. if($provider->callback() === false) {
  276. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],180);
  277. }
  278. break;
  279. }
  280. else {
  281. //提交失败
  282. Log::record("channel:{$channel_name} err:{$errmsg}");
  283. $logic_vr_order = Logic("vr_order");
  284. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  285. $logic_vr_order->changeOrderStateCancel($order_info, '', "调用{$channel_name}接口失败");
  286. $mod_refill->edit($order_id, ['commit_time' => time()]);
  287. }
  288. }
  289. if ($refill_state) {
  290. return [true, '', $order_id,false];
  291. }
  292. elseif($order_success) {
  293. return [errcode::MERCHANT_REFILL_ERROR, "充值失败",$order_id,$neterr];
  294. }
  295. else {
  296. return [errcode::MERCHANT_REFILL_ERROR, "充值失败",0,false];
  297. }
  298. }
  299. private function pay_completed($order_sn)
  300. {
  301. $logic_payment = Logic('payment');
  302. $order = $logic_payment->getVrOrderInfo($order_sn);
  303. $api_pay_amount = $order['data']['api_pay_amount'];
  304. return ($api_pay_amount == ncPriceFormat(0.00));
  305. }
  306. public function notify_merchant($order_id,$manual)
  307. {
  308. if ($order_id <= 0) {
  309. return [false, "订单ID小于0"];
  310. }
  311. $vr_order = Model('vr_order');
  312. $refill_order = Model('refill_order');
  313. $order_info = $vr_order->getOrderInfo(['order_id' => $order_id]);
  314. $refill_info = $refill_order->getOrderInfo(['order_id' => $order_id]);
  315. if (empty($order_info) || empty($refill_info)) {
  316. return [false, "无此订单"];
  317. }
  318. //手动通知,之所以不做尝试,是担心客户方状态处理不当
  319. if (!$manual && $refill_info['mch_notify_state'] != 0) {
  320. return [false, "已经通知客户方"];
  321. }
  322. $notify_url = $refill_info['notify_url'];
  323. if (empty($notify_url)) {
  324. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => 0]);
  325. return [false, "回调地址为空"];
  326. }
  327. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  328. $order_state = intval($order_info['order_state']);
  329. if ($order_state !== ORDER_STATE_CANCEL && $order_state !== ORDER_STATE_SUCCESS) {
  330. return [false, "错误的订单状态,不能通知."];
  331. }
  332. $resp = $this->mPolicy->notify($order_info,$refill_info);
  333. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  334. if ($resp) {
  335. $refill_order->edit($order_id, ['mch_notify_state' => 1, 'mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  336. return [true, ""];
  337. }
  338. else {
  339. $refill_order->edit($order_id, ['mch_notify_times' => ['exp', 'mch_notify_times+1']]);
  340. $times = $refill_info['mch_notify_times'] + 1;
  341. if ($times > 80) {
  342. $refill_order->edit($order_id, ['mch_notify_state' => 2]);
  343. } else {
  344. $N = intval($times / 5);
  345. $period = intval(pow(2, $N));
  346. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false], $period);
  347. }
  348. return [false, "通知{$times}次,失败."];
  349. }
  350. }
  351. public function query($order_id)
  352. {
  353. $mod_order = Model('vr_order');
  354. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id]);
  355. if(empty($order_info)) return false;
  356. $mod_refill = Model('refill_order');
  357. $refill_info = $mod_refill->getOrderInfo(['order_id' => $order_id,'inner_status' => 0]);
  358. $chname = $refill_info['channel_name'];
  359. if($refill_info['notify_state'] == 1) {
  360. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  361. return true;
  362. }
  363. try
  364. {
  365. $tran = new trans_wapper($mod_order,'query order state trans');
  366. $order_info = $mod_order->getOrderInfo(['order_id' => $order_id],'*',true,true);
  367. if($refill_info['is_retrying'] == 1) {
  368. $query_able = false;
  369. }
  370. elseif($order_info['order_state'] == ORDER_STATE_SEND) {
  371. $query_able = true;
  372. }
  373. else {
  374. $query_able = false;
  375. }
  376. $tran->commit();
  377. }
  378. catch (Exception $ex) {
  379. $tran->rollback();
  380. Log::record($ex->getMessage(),Log::ERR);
  381. }
  382. if($query_able)
  383. {
  384. if(empty($chname)) return false;
  385. $provider = $this->mPolicy->provider($chname);
  386. if(empty($provider)) return false;
  387. [$state, $order_state] = $provider->query($refill_info);
  388. if(!$state) {
  389. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],180);
  390. return false;
  391. }
  392. elseif($order_state == ORDER_STATE_SUCCESS) {
  393. $this->proc_notify($order_id,true,false,$chname);
  394. }
  395. elseif($order_state == ORDER_STATE_CANCEL) {
  396. $this->proc_notify($order_id,false,true,$chname);
  397. }
  398. else {
  399. QueueClient::async_push("QueryRefillState",['order_id' => $order_id],180);
  400. }
  401. }
  402. return true;
  403. }
  404. }