proxy.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. use refill\util;
  3. use refill;
  4. require_once (BASE_ROOT_PATH . '/helper/model_helper.php');
  5. class proxy
  6. {
  7. const max_retry = 5;
  8. const max_time_out = 120;
  9. public function add($params)
  10. {
  11. $refill_order = Model('refill_order');
  12. $mchid = $params['mchid'];
  13. $buyer_id = $params['buyer_id'];
  14. $amount = intval($params['amount']);
  15. $card_no = $params['card_no'];
  16. $mch_order = $params['mch_order'];
  17. $notify_url = $params['notify_url'];
  18. $idcard = $params['idcard'] ?? '';
  19. $card_name = $params['card_name'] ?? '';
  20. $order_time = $params['order_time'] ?? time();
  21. $commit_times = $params['commit_times'] ?? 0;
  22. $last_order_id = $params['order_id'] ?? 0;
  23. $org_quality = intval($params['org_quality']) ?? 0;
  24. $card_type = intval($params['card_type']) ?? 0;
  25. if($card_type == 0) {
  26. $card_type = mtopcard\card_type($card_no);
  27. }
  28. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  29. [$errcode, $errmsg, $order_id, $neterr] = refill\RefillFactory::instance()->add($mchid, $buyer_id, $amount, $card_no,
  30. $mch_order, $idcard, $card_name, $notify_url, $quality,$org_quality,
  31. $order_time, $commit_times, $last_order_id,$card_type);
  32. $params['commit_times'] += 1;
  33. $commit_times += 1;
  34. if($errcode !== true)
  35. {
  36. $fNotify = true;
  37. if(($errcode === refill\errcode::MERCHANT_REFILL_ERROR && $neterr) || $errcode == refill\errcode::PROVIDER_OVERLOAD)
  38. {
  39. [$org_quality,$quality] = refill\RefillFactory::instance()->find_quality($mchid,$amount,$card_type,$org_quality,$commit_times,time() - $order_time);
  40. if ($quality > 0) {
  41. $fNotify = false;
  42. $params['order_id'] = $order_id;
  43. refill\util::push_add($params);
  44. if($order_id > 0) {
  45. $refill_order->edit($order_id, ['is_retrying' => 1]);
  46. }
  47. }
  48. }
  49. if($fNotify)
  50. {
  51. if ($order_id === 0)
  52. {
  53. $order_info = $this->latest_order($refill_order, $mchid, $mch_order);
  54. if (empty($order_info)) {
  55. $order_id = refill\RefillFactory::instance()->zero_order($mchid, $buyer_id, $amount, $card_no,
  56. $mch_order, $idcard, $card_name, $notify_url, $quality, $org_quality, $order_time, $commit_times, $errmsg);
  57. } else {
  58. $order_id = $order_info['order_id'];
  59. }
  60. }
  61. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => false]);
  62. }
  63. }
  64. }
  65. private function latest_order($refill_order,$mchid,$mch_order)
  66. {
  67. $orders = $refill_order->getMerchantOrderList(['mchid' => $mchid,'mch_order' => $mch_order]);
  68. if(empty($orders)) {
  69. return [];
  70. }
  71. else {
  72. $orders[0];
  73. }
  74. }
  75. public function notify($channel,$input)
  76. {
  77. return refill\RefillFactory::instance()->notify($channel,$input);
  78. }
  79. public function notify_merchant($order_id,$manual)
  80. {
  81. return refill\RefillFactory::instance()->notify_merchant($order_id,$manual);
  82. }
  83. public function query($order_id)
  84. {
  85. return refill\RefillFactory::instance()->query($order_id);
  86. }
  87. }