merchant_refill.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. require_once(BASE_ROOT_PATH . '/mobile/control/merchantweb.php');
  3. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  4. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  5. class merchant_refillControl extends mbMerchantControl
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function goodsOp()
  12. {
  13. global $config;
  14. $oil_amount = $config['refill_oil_specs'];
  15. $phone_amount = $config['refill_phone_specs'];
  16. $phone_small_amount = $config['refill_phone_small_specs'];
  17. $phone_amount = array_merge($phone_amount, $phone_small_amount);
  18. sort($phone_amount);
  19. return self::outsuccess(['oil_amount' => $oil_amount,'phone_amount' => $phone_amount]);
  20. }
  21. private function check_params($params)
  22. {
  23. if(empty($params['cardno'])) {
  24. return [false,'参数没有包含cardno'];
  25. }
  26. if(empty($params['amount'])) {
  27. return [false,'参数没有包含充值金额:amount'];
  28. }
  29. $card_no = $params['cardno'];
  30. $card_type = mtopcard\card_type($card_no,$regin_no);
  31. if($card_type == mtopcard\UnknownCard) {
  32. return [false,'卡类型无法识别'];
  33. }
  34. return [true,""];
  35. }
  36. public function addOp()
  37. {
  38. $params = $_POST;
  39. $mchid = $this->mchid();
  40. $model_merchant = Model('merchant');
  41. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  42. if (empty($merchant_info)) {
  43. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  44. }
  45. $amount = intval($params['amount']);
  46. $card_nos = trim($params['cardno']);
  47. $card_nos = explode(',' , $card_nos);
  48. if(empty($card_nos)){
  49. return self::outerr(errcode::ErrParamter, "卡号格式错误或未上传");
  50. }
  51. $quality = $params['quality'] ?? 0;
  52. //成功个数、失败个数
  53. $success_no = $error_no = 0;
  54. $data = [];
  55. $all_no = count($card_nos);
  56. foreach ($card_nos as $no)
  57. {
  58. $params['cardno'] = $no;
  59. [$success,$error] = $this->check_params($params);
  60. if($success === false) {
  61. $error_no++;
  62. $arr['state'] = 201;
  63. $arr['err'] = $error;
  64. }
  65. else
  66. {
  67. Log::record("no = {$no}",Log::DEBUG);
  68. $mch_order = $this->make_sn();
  69. $params = [ 'mchid' => $this->mchid(),
  70. 'buyer_id' => intval($merchant_info['admin_id']),
  71. 'amount' => $amount,
  72. 'card_no' => $no,
  73. 'mch_order' => $mch_order,
  74. 'order_time' => time(),
  75. 'notify_url' => "",
  76. 'org_quality' => $quality];
  77. $card_type = mtopcard\simple_card_type($no);
  78. [$can_refill, $period] = refill\util::can_commit($no, $card_type);
  79. if ($can_refill === false) {
  80. $ret = refill\util::async_add($params, $period);
  81. } else {
  82. $ret = refill\util::push_add($params);
  83. }
  84. if($ret) {
  85. $arr['state'] = $ret;
  86. $arr['err'] = '';
  87. $success_no++;
  88. }
  89. else {
  90. $arr['state'] = 202;
  91. $arr['err'] = '提交失败';
  92. $error_no++;
  93. }
  94. }
  95. $arr['card_no'] = $no;
  96. $arr['amount'] = $amount;
  97. $data[] = $arr;
  98. }
  99. $result['success_no'] = $success_no;
  100. $result['error_no'] = $error_no;
  101. $result['all_no'] = $all_no;
  102. $result['data'] = $data;
  103. return self::outsuccess($result);
  104. }
  105. private function make_sn()
  106. {
  107. return mt_rand(1000, 9999)
  108. . sprintf('%010d', time())
  109. . sprintf('%06d', (float)microtime() * 1000000);
  110. }
  111. }