proxy.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Log::record("proxy::add mch_order={$mch_order} card_no = {$card_no}",Log::DEBUG);
  27. $need_check = false;
  28. if($card_type == 0)
  29. {
  30. $card_type = mtopcard\card_type($card_no);
  31. $params['card_type'] = $card_type;
  32. global $config;
  33. if($config['phone_life_check'] == true && $card_type != mtopcard\PetroChinaCard && $card_type != mtopcard\SinopecCard) {
  34. $need_check = true;
  35. }
  36. }
  37. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  38. if ($need_check)
  39. {
  40. $valided = mtopcard\valid_phone($card_no);
  41. if (!$valided) {
  42. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  43. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times,
  44. "无效的手机号");
  45. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id, 'manual' => false]);
  46. return;
  47. }
  48. }
  49. [$errcode, $errmsg, $order_id, $neterr] = refill\RefillFactory::instance()->add($mchid, $buyer_id, $amount, $card_no,
  50. $mch_order, $idcard, $card_name, $notify_url, $quality,$org_quality,
  51. $order_time, $commit_times, $last_order_id,$card_type);
  52. $params['commit_times'] += 1;
  53. $commit_times += 1;
  54. if($errcode !== true)
  55. {
  56. $fNotify = true;
  57. if(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr) || $errcode == refill\errcode::PROVIDER_OVERLOAD || $errcode == errcode::MERCHANT_PRICE_UNSETTING)
  58. {
  59. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  60. if ($quality > 0) {
  61. $fNotify = false;
  62. $params['order_id'] = $order_id;
  63. if($order_id > 0) {
  64. $refill_order->edit($order_id, ['is_retrying' => 1]);
  65. }
  66. if(defined('USE_COROUTINE') && USE_COROUTINE && $errcode == refill\errcode::PROVIDER_OVERLOAD) {
  67. \Swoole\Coroutine::sleep(1);
  68. }
  69. refill\util::push_add($params);
  70. }
  71. }
  72. if($fNotify)
  73. {
  74. if ($order_id === 0)
  75. {
  76. $order_info = $this->latest_order($refill_order, $mchid, $mch_order);
  77. if (empty($order_info)) {
  78. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  79. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times, $errmsg);
  80. } else {
  81. $order_id = $order_info['order_id'];
  82. }
  83. }
  84. else {
  85. $refill_order->edit($order_id, ['notify_time' => time(), 'notify_state' => 1,'is_retrying' => 0]);
  86. }
  87. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  88. }
  89. }
  90. }
  91. private function latest_order($refill_order,$mchid,$mch_order)
  92. {
  93. $orders = $refill_order->getMerchantOrderList(['mchid' => $mchid,'mch_order' => $mch_order]);
  94. if(empty($orders)) {
  95. return [];
  96. }
  97. else {
  98. $orders[0];
  99. }
  100. }
  101. public function notify($channel,$input)
  102. {
  103. return refill\RefillFactory::instance()->notify($channel,$input);
  104. }
  105. public function notify_merchant($order_id,$manual)
  106. {
  107. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  108. }
  109. public function query($order_id)
  110. {
  111. return refill\RefillFactory::instance()->query($order_id);
  112. }
  113. public function manual_success($order_id)
  114. {
  115. refill\RefillFactory::instance()->manual_success($order_id);
  116. }
  117. public function manual_cancel($order_id)
  118. {
  119. refill\RefillFactory::instance()->manual_cancel($order_id);
  120. }
  121. }