merchant_refill.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $goods = refill\RefillFactory::instance()->goods();
  14. $result['oil_amount'] = [];
  15. $result['phone_amount'] = [];
  16. foreach ($goods as $type => $specs)
  17. {
  18. if($type == mtopcard\SinopecCard || $type == mtopcard\PetroChinaCard) {
  19. foreach ($specs as $spec) {
  20. $result['oil_amount'][] = $spec;
  21. }
  22. }
  23. else {
  24. foreach ($specs as $spec) {
  25. $result['phone_amount'][] = $spec;
  26. }
  27. }
  28. }
  29. $oil_amount = array_unique($result['oil_amount']);
  30. sort($oil_amount);
  31. $phone_amount = array_unique($result['phone_amount']);
  32. sort($phone_amount);
  33. return self::outsuccess(['oil_amount' => $oil_amount,'phone_amount' => $phone_amount]);
  34. }
  35. private function check_params($params)
  36. {
  37. if(empty($params['cardno'])) {
  38. return [false,'参数没有包含cardno'];
  39. }
  40. if(empty($params['amount'])) {
  41. return [false,'参数没有包含充值金额:amount'];
  42. }
  43. $card_no = $params['cardno'];
  44. $card_type = mtopcard\card_type($card_no,$regin_no);
  45. if($card_type == mtopcard\UnknownCard) {
  46. return [false,'卡类型无法识别'];
  47. }
  48. return [true,""];
  49. }
  50. public function addOp()
  51. {
  52. $params = $_POST;
  53. $mchid = $this->mchid();
  54. $model_merchant = Model('merchant');
  55. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  56. if (empty($merchant_info)) {
  57. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  58. }
  59. $amount = intval($params['amount']);
  60. $card_nos = trim($params['cardno']);
  61. $card_nos = explode(',' , $card_nos);
  62. if(empty($card_nos)){
  63. return self::outerr(errcode::ErrParamter, "卡号格式错误或未上传");
  64. }
  65. //成功个数、失败个数
  66. $success_no = $error_no = 0;
  67. $data = [];
  68. $all_no = count($card_nos);
  69. foreach ($card_nos as $no)
  70. {
  71. $params['cardno'] = $no;
  72. [$success,$error] = $this->check_params($params);
  73. if($success === false) {
  74. $error_no++;
  75. $arr['state'] = 201;
  76. $arr['err'] = $error;
  77. }
  78. else
  79. {
  80. Log::record("no = {$no}",Log::DEBUG);
  81. $params = [ 'mchid' => $this->mchid(),
  82. 'buyer_id' => intval($merchant_info['admin_id']),
  83. 'amount' => $amount,
  84. 'card_no' => $no,
  85. 'order_time' => time(),
  86. 'mch_order' => "",
  87. 'notify_url' => ""];
  88. $card_type = mtopcard\simple_card_type($no);
  89. [$can_refill, $period] = refill\util::can_commit($no, $card_type);
  90. if ($can_refill === false) {
  91. $ret = refill\util::async_add($params, $period);
  92. } else {
  93. $ret = refill\util::push_add($params);
  94. }
  95. if($ret) {
  96. $arr['state'] = $ret;
  97. $arr['err'] = '';
  98. $success_no++;
  99. }
  100. else {
  101. $arr['state'] = 202;
  102. $arr['err'] = '提交失败';
  103. $error_no++;
  104. }
  105. }
  106. $arr['card_no'] = $no;
  107. $arr['amount'] = $amount;
  108. $data[] = $arr;
  109. }
  110. $result['success_no'] = $success_no;
  111. $result['error_no'] = $error_no;
  112. $result['all_no'] = $all_no;
  113. $result['data'] = $data;
  114. return self::outsuccess($result);
  115. }
  116. }