card_crash.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace refill\event;
  3. use refill\order;
  4. use refill\util;
  5. use mtopcard;
  6. use Log;
  7. # 规避相同手机卡号撞单问题解
  8. class card_crash extends IEventRefill
  9. {
  10. use crash;
  11. public const cache_name = 'card_crash';
  12. protected $mChannels = [];
  13. protected $mSuccInterval = 900;
  14. protected $mExMchids = [];
  15. public function __construct()
  16. {
  17. }
  18. public function load($cfgs)
  19. {
  20. $channels = $cfgs['channels'] ?? '';
  21. Log::record("card_crash load channels={$cfgs['channels']} succ_interval={$cfgs['succ_interval']}.", Log::DEBUG);
  22. $channels = explode(',',$channels);
  23. $this->mChannels = [];
  24. foreach ($channels as $item) {
  25. $this->mChannels[] = trim($item);
  26. }
  27. $this->mSuccInterval = intval($cfgs['succ_interval'] ?? 900);
  28. //读取撞单排除机构
  29. $mchids = $cfgs['mchids'] ?? '';
  30. $mchids = explode(',',$mchids);
  31. $this->mExMchids = [];
  32. foreach ($mchids as $item)
  33. {
  34. $mchid = intval($item);
  35. if($mchid > 0) {
  36. $this->mExMchids[] = $mchid;
  37. }
  38. }
  39. }
  40. public function onBeforeSubmit(order $order) : bool
  41. {
  42. if(!$order->is_phone()) {
  43. return true;
  44. }
  45. $card_no = $order->card_no();
  46. $ret = $this->can_submit($card_no);
  47. if(!$ret)
  48. {
  49. $mchid = $order->mchid();
  50. $mch_order = $order->mch_order();
  51. $mod_refill = Model('refill_order');
  52. $val = $mod_refill->get_detail($mchid, $mch_order);
  53. if(!empty($val))
  54. {
  55. $params = json_decode($val['params'],true);
  56. //在排除队列,这时可以提单,后面需要尽快失败返回.
  57. if (in_array($mchid, $this->mExMchids)) {
  58. // util::async_add_zero($params, 5);
  59. // Log::record("card_crash mchid=$mchid,mch_order $mch_order add zero order.", Log::DEBUG);
  60. return true;
  61. } else {
  62. util::async_add($params, 60);
  63. Log::record("card_crash mchid=$mchid,mch_order $mch_order sleep one minute.", Log::DEBUG);
  64. }
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. public function onSubmit(order $order)
  71. {
  72. if(!$order->is_phone()) {
  73. return;
  74. }
  75. $card_no = $order->card_no();
  76. $oid = $order->unique_id();
  77. $this->submit($card_no,$oid);
  78. }
  79. public function onBeforeCommit(order $order, $ch_name): bool
  80. {
  81. if(!$order->is_phone()) {
  82. return true;
  83. }
  84. $mchid = $order->mchid();
  85. $card_no = $order->card_no();
  86. $oid = $order->unique_id();
  87. return $this->can_commit($card_no, $oid, $ch_name);
  88. }
  89. public function onCommit(order $order, $ch_name)
  90. {
  91. if(!$order->is_phone()) {
  92. return;
  93. }
  94. $card_no = $order->card_no();
  95. $oid = $order->unique_id();
  96. $this->commit($card_no, $oid, $ch_name);
  97. }
  98. public function onNeterror(order $order, $ch_name)
  99. {
  100. if(!$order->is_phone()) {
  101. return;
  102. }
  103. $card_no = $order->card_no();
  104. $oid = $order->unique_id();
  105. $this->submit($card_no,$oid);
  106. }
  107. public function onNotify($refill_info, $order_info, $success)
  108. {
  109. $card_type = intval($refill_info['card_type']);
  110. if (!in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  111. return;
  112. }
  113. $oid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  114. $ch_name = $refill_info['channel_name'];
  115. $card_no = $refill_info['card_no'];
  116. $this->notify($card_no, $oid, $ch_name, $success);
  117. }
  118. public function onComplete($refill_info, $order_info, $success)
  119. {
  120. $card_type = intval($refill_info['card_type']);
  121. if (!in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  122. return;
  123. }
  124. $oid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  125. $ch_name = $refill_info['channel_name'];
  126. $card_no = $refill_info['card_no'];
  127. $this->complete($card_no, $oid, $ch_name, $success);
  128. }
  129. }