proxy.php 5.2 KB

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