merchant_refill.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $quality = $params['quality'] ?? 0;
  66. //成功个数、失败个数
  67. $success_no = $error_no = 0;
  68. $data = [];
  69. $all_no = count($card_nos);
  70. foreach ($card_nos as $no)
  71. {
  72. $params['cardno'] = $no;
  73. [$success,$error] = $this->check_params($params);
  74. if($success === false) {
  75. $error_no++;
  76. $arr['state'] = 201;
  77. $arr['err'] = $error;
  78. }
  79. else
  80. {
  81. Log::record("no = {$no}",Log::DEBUG);
  82. $mch_order = $this->make_sn();
  83. $params = [ 'mchid' => $this->mchid(),
  84. 'buyer_id' => intval($merchant_info['admin_id']),
  85. 'amount' => $amount,
  86. 'card_no' => $no,
  87. 'mch_order' => $mch_order,
  88. 'notify_url' => "",
  89. 'org_quality' => $quality];
  90. $card_type = mtopcard\simple_card_type($no);
  91. [$can_refill, $period] = refill\util::can_commit($no, $card_type);
  92. if ($can_refill === false) {
  93. $ret = refill\util::async_add($params, $period);
  94. } else {
  95. $ret = refill\util::push_add($params);
  96. }
  97. if($ret) {
  98. $arr['state'] = $ret;
  99. $arr['err'] = '';
  100. $success_no++;
  101. }
  102. else {
  103. $arr['state'] = 202;
  104. $arr['err'] = '提交失败';
  105. $error_no++;
  106. }
  107. }
  108. $arr['card_no'] = $no;
  109. $arr['amount'] = $amount;
  110. $data[] = $arr;
  111. }
  112. $result['success_no'] = $success_no;
  113. $result['error_no'] = $error_no;
  114. $result['all_no'] = $all_no;
  115. $result['data'] = $data;
  116. return self::outsuccess($result);
  117. }
  118. private function make_sn()
  119. {
  120. return mt_rand(1000, 9999)
  121. . sprintf('%010d', time())
  122. . sprintf('%06d', (float)microtime() * 1000000);
  123. }
  124. }