merchant_refill.php 5.9 KB

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