merchant_refill.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $card_type = mtopcard\simple_card_type($no);
  79. [$can_refill, $period] = refill\util::can_commit($no, $card_type);
  80. if ($can_refill === false) {
  81. $ret = refill\util::async_add($params, $period);
  82. } else {
  83. $ret = refill\util::push_add($params);
  84. }
  85. if($ret) {
  86. $arr['state'] = $ret;
  87. $arr['err'] = '';
  88. $success_no++;
  89. }
  90. else {
  91. $arr['state'] = 202;
  92. $arr['err'] = '提交失败';
  93. $error_no++;
  94. }
  95. }
  96. $arr['card_no'] = $no;
  97. $arr['amount'] = $amount;
  98. $data[] = $arr;
  99. }
  100. $result['success_no'] = $success_no;
  101. $result['error_no'] = $error_no;
  102. $result['all_no'] = $all_no;
  103. $result['data'] = $data;
  104. return self::outsuccess($result);
  105. }
  106. private function make_sn()
  107. {
  108. return mt_rand(1000, 9999)
  109. . sprintf('%010d', time())
  110. . sprintf('%06d', (float)microtime() * 1000000);
  111. }
  112. private function check_pcode($pcode)
  113. {
  114. $mod_third = Model('thrid_refill');
  115. $product = $mod_third->getProduct(['system_code' => $pcode]);
  116. return !empty($product);
  117. }
  118. public function add_thirdOp()
  119. {
  120. $params = $_POST;
  121. $product_code = $params['product_code'];
  122. if($this->check_pcode($product_code) == false) {
  123. return self::outerr(errcode::ErrParamter, "对应的产品编码{$product_code}不存在");
  124. }
  125. $card_no = $params['card_no'];
  126. $mchid = $this->mchid();
  127. $model_merchant = Model('merchant');
  128. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  129. if (empty($merchant_info)) {
  130. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  131. }
  132. if($merchant_info['manual_recharge'] != 1) {
  133. return self::outerr(errcode::ErrParamter, "手动充值业务已被关闭,请联系管理员。");
  134. }
  135. $mch_order = $this->make_sn();
  136. $input = [
  137. 'mchid' => $mchid,
  138. 'buyer_id' => intval($merchant_info['admin_id']),
  139. 'amount' => refill\util::ThirdRefillAmount,
  140. 'mch_order' => $mch_order,
  141. 'order_time' => time(),
  142. 'notify_url' => "",
  143. 'org_quality' => 1,
  144. 'card_type' => mtopcard\ThirdRefillCard,
  145. 'card_no' => $card_no,
  146. 'product_code' => $product_code,
  147. 'quantity' => 1,
  148. 'third_card_type' => 1,
  149. 'third_product_type' => mtopcard\ThirdOnlineProduct,
  150. ];
  151. $state = refill\util::push_addthird($input);
  152. if ($state) {
  153. return self::outsuccess([]);
  154. } else {
  155. return self::outerr(errcode::ErrOperation, "系统错误.");
  156. }
  157. }
  158. }