card_crash.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. public function __construct()
  15. {
  16. }
  17. public function load($cfgs)
  18. {
  19. $channels = $cfgs['channels'] ?? '';
  20. Log::record("card_crash load channels={$cfgs['channels']} succ_interval={$cfgs['succ_interval']}.", Log::DEBUG);
  21. $channels = explode(',',$channels);
  22. $this->mChannels = [];
  23. foreach ($channels as $item) {
  24. $this->mChannels[] = trim($item);
  25. }
  26. $this->mSuccInterval = intval($cfgs['succ_interval'] ?? 900);
  27. }
  28. public function onBeforeSubmit(order $order) : bool
  29. {
  30. if(!$order->is_phone()) {
  31. return true;
  32. }
  33. $card_no = $order->card_no();
  34. $ret = $this->can_submit($card_no);
  35. if(!$ret)
  36. {
  37. $mchid = $order->mchid();
  38. $mch_order = $order->mch_order();
  39. $mod_refill = Model('refill_order');
  40. $val = $mod_refill->get_detail($mchid,$mch_order);
  41. if(!empty($val)) {
  42. $params = json_decode($val['params'],true);
  43. util::async_add($params, 60);
  44. Log::record("card_crash mchid=$mchid,mch_order $mch_order sleep one minute.",Log::DEBUG);
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. public function onSubmit(order $order)
  51. {
  52. if(!$order->is_phone()) {
  53. return;
  54. }
  55. $card_no = $order->card_no();
  56. $oid = $order->unique_id();
  57. $this->submit($card_no,$oid);
  58. }
  59. public function onBeforeCommit(order $order, $ch_name): bool
  60. {
  61. if(!$order->is_phone()) {
  62. return true;
  63. }
  64. $card_no = $order->card_no();
  65. $oid = $order->unique_id();
  66. return $this->can_commit($card_no, $oid, $ch_name);
  67. }
  68. public function onCommit(order $order, $ch_name)
  69. {
  70. if(!$order->is_phone()) {
  71. return;
  72. }
  73. $card_no = $order->card_no();
  74. $oid = $order->unique_id();
  75. $this->commit($card_no, $oid, $ch_name);
  76. }
  77. public function onNeterror(order $order, $ch_name)
  78. {
  79. if(!$order->is_phone()) {
  80. return;
  81. }
  82. $card_no = $order->card_no();
  83. $oid = $order->unique_id();
  84. $this->submit($card_no,$oid);
  85. }
  86. public function onNotify($refill_info, $order_info, $success)
  87. {
  88. $card_type = intval($refill_info['card_type']);
  89. if (!in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  90. return;
  91. }
  92. $oid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  93. $ch_name = $refill_info['channel_name'];
  94. $card_no = $refill_info['card_no'];
  95. $this->notify($card_no, $oid, $ch_name, $success);
  96. }
  97. public function onComplete($refill_info, $order_info, $success)
  98. {
  99. $card_type = intval($refill_info['card_type']);
  100. if (!in_array($card_type, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard])) {
  101. return;
  102. }
  103. $oid = "{$refill_info['mchid']}-{$refill_info['mch_order']}";
  104. $ch_name = $refill_info['channel_name'];
  105. $card_no = $refill_info['card_no'];
  106. $this->complete($card_no, $oid, $ch_name, $success);
  107. }
  108. }