merchant_refill.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  3. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  4. class merchant_refillControl extends mbMerchantControl
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. public function goodsOp()
  11. {
  12. $oil_amount = [50, 100, 200, 500, 1000, 2000];
  13. $phone_amount = [10, 20, 30, 50, 100, 200, 300, 500];
  14. $phone_small_amount = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  15. $phone_amount = array_merge($phone_amount, $phone_small_amount);
  16. sort($phone_amount);
  17. return self::outsuccess(['oil_amount' => $oil_amount,'phone_amount' => $phone_amount]);
  18. }
  19. private function check_params($params)
  20. {
  21. if(empty($params['cardno'])) {
  22. return [false,'参数没有包含cardno'];
  23. }
  24. if(empty($params['amount'])) {
  25. return [false,'参数没有包含充值金额:amount'];
  26. }
  27. $card_no = $params['cardno'];
  28. $card_type = mtopcard\card_type($card_no,$regin_no);
  29. if($card_type == mtopcard\UnknownCard) {
  30. return [false,'卡类型无法识别'];
  31. }
  32. return [true,""];
  33. }
  34. public function addOp()
  35. {
  36. $params = $_POST;
  37. $mchid = $this->mchid();
  38. $model_merchant = Model('merchant');
  39. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  40. if (empty($merchant_info)) {
  41. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  42. }
  43. if($merchant_info['manual_recharge'] != 1) {
  44. return self::outerr(errcode::ErrParamter, "手动充值业务已被关闭,请联系管理员。");
  45. }
  46. $amount = intval($params['amount']);
  47. $card_nos = trim($params['cardno']);
  48. $card_nos = explode(',' , $card_nos);
  49. if(empty($card_nos)){
  50. return self::outerr(errcode::ErrParamter, "卡号格式错误或未上传");
  51. }
  52. $quality = $params['quality'] ?? 0;
  53. //成功个数、失败个数
  54. $success_no = $error_no = 0;
  55. $data = [];
  56. $all_no = count($card_nos);
  57. foreach ($card_nos as $no)
  58. {
  59. $params['cardno'] = $no;
  60. [$success,$error] = $this->check_params($params);
  61. if($success === false) {
  62. $error_no++;
  63. $arr['state'] = 201;
  64. $arr['err'] = $error;
  65. }
  66. else
  67. {
  68. Log::record("no = {$no}",Log::DEBUG);
  69. $mch_order = $this->make_sn();
  70. $params = [ 'mchid' => $this->mchid(),
  71. 'buyer_id' => intval($merchant_info['admin_id']),
  72. 'amount' => $amount,
  73. 'card_no' => $no,
  74. 'mch_order' => $mch_order,
  75. 'order_time' => time(),
  76. 'notify_url' => "",
  77. 'org_quality' => $quality];
  78. refill\util::push_queue_order($this->mchid(),$mch_order,ORDER_STATE_QUEUE);
  79. Model('refill_order')->add_detail($this->mchid(),$mch_order,time(),$params,ORDER_STATE_QUEUE);
  80. $card_type = mtopcard\simple_card_type($no);
  81. [$can_refill, $period] = refill\util::can_commit($no, $card_type);
  82. if ($can_refill === false) {
  83. $ret = refill\util::async_add($params, $period);
  84. } else {
  85. $ret = refill\util::push_add($params);
  86. }
  87. if($ret) {
  88. $arr['state'] = $ret;
  89. $arr['err'] = '';
  90. $success_no++;
  91. }
  92. else {
  93. $arr['state'] = 202;
  94. $arr['err'] = '提交失败';
  95. $error_no++;
  96. }
  97. }
  98. $arr['card_no'] = $no;
  99. $arr['amount'] = $amount;
  100. $data[] = $arr;
  101. }
  102. $result['success_no'] = $success_no;
  103. $result['error_no'] = $error_no;
  104. $result['all_no'] = $all_no;
  105. $result['data'] = $data;
  106. return self::outsuccess($result);
  107. }
  108. private function make_sn()
  109. {
  110. return mt_rand(1000, 9999)
  111. . sprintf('%010d', time())
  112. . sprintf('%06d', (float)microtime() * 1000000);
  113. }
  114. private function check_pcode($pcode)
  115. {
  116. $mod_third = Model('thrid_refill');
  117. $product = $mod_third->getProduct(['system_code' => $pcode]);
  118. return !empty($product);
  119. }
  120. public function add_thirdOp()
  121. {
  122. $params = $_POST;
  123. $product_code = $params['product_code'];
  124. if($this->check_pcode($product_code) == false) {
  125. return self::outerr(errcode::ErrParamter, "对应的产品编码{$product_code}不存在");
  126. }
  127. $card_no = $params['card_no'];
  128. $mchid = $this->mchid();
  129. $model_merchant = Model('merchant');
  130. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  131. if (empty($merchant_info)) {
  132. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  133. }
  134. if($merchant_info['manual_recharge'] != 1) {
  135. return self::outerr(errcode::ErrParamter, "手动充值业务已被关闭,请联系管理员。");
  136. }
  137. $mch_order = $this->make_sn();
  138. $input = [
  139. 'mchid' => $mchid,
  140. 'buyer_id' => intval($merchant_info['admin_id']),
  141. 'amount' => refill\util::ThirdRefillAmount,
  142. 'mch_order' => $mch_order,
  143. 'order_time' => time(),
  144. 'notify_url' => "",
  145. 'org_quality' => 1,
  146. 'card_type' => mtopcard\ThirdRefillCard,
  147. 'card_no' => $card_no,
  148. 'product_code' => $product_code,
  149. 'quantity' => 1,
  150. 'third_card_type' => 1,
  151. 'third_product_type' => mtopcard\ThirdOnlineProduct,
  152. ];
  153. $state = refill\util::push_addthird($input);
  154. if ($state) {
  155. return self::outsuccess([]);
  156. } else {
  157. return self::outerr(errcode::ErrOperation, "系统错误.");
  158. }
  159. }
  160. }