refill.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. require_once(BASE_ROOT_PATH . '/mobile/control/merchant.php');
  3. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  4. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  5. require_once(BASE_HELPER_PATH . '/model/member_info.php');
  6. //商家充值接口中心
  7. class refillControl extends merchantControl
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function goodsOp()
  14. {
  15. $goods_list = refill\RefillFactory::instance()->goods();
  16. $sorter = function (array $items) {
  17. $ret = [];
  18. foreach ($items as $key => $val) {
  19. sort($val);
  20. $ret[$key] = $val;
  21. }
  22. return $ret;
  23. };
  24. $goods_list = $sorter($goods_list);
  25. $result = [];
  26. foreach ($goods_list as $card_type => $value) {
  27. $key = mtopcard\scard_type($card_type);
  28. $result[$key] = $value;
  29. }
  30. return self::outsuccess($result);
  31. }
  32. private function check_params($params)
  33. {
  34. if(empty($params['cardno'])) {
  35. return [false,'参数没有包含cardno'];
  36. }
  37. if(empty($params['amount'])) {
  38. return [false,'参数没有包含充值金额:amount'];
  39. }
  40. $card_no = $_GET['cardno'];
  41. $card_type = mtopcard\simple_card_type($card_no);
  42. if($card_type == mtopcard\UnknownCard) {
  43. return [false,'卡类型无法识别'];
  44. }
  45. if(empty($params['notifyurl'])) {
  46. return [false,'参数没有包含notifyurl'];
  47. }
  48. if(empty($params['order_sn'])) {
  49. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  50. }
  51. return [true,""];
  52. }
  53. public function addOp()
  54. {
  55. [$success,$error] = $this->check_params($_GET);
  56. if($success === false) {
  57. return self::outerr(201,$error);
  58. }
  59. $amount = intval($_GET['amount']);
  60. $card_no = $_GET['cardno'];
  61. $notify_url = $_GET['notifyurl'];
  62. $mch_order = $_GET['order_sn']; //对方的order编号
  63. $quality = $_GET['quality'] ?? 0;
  64. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  65. return self::outerr(205,"客户订单号重复或者为空.");
  66. }
  67. $card_type = mtopcard\simple_card_type($card_no);
  68. if($card_type === mtopcard\SinopecCard || $card_type === mtopcard\PetroChinaCard)
  69. {
  70. if(!$this->can_refill($card_no)) {
  71. return self::outerr(206,"平台不支持该卡充值.");
  72. }
  73. $allow = refill\RefillFactory::instance()->allow($this->mchid(),$card_type,$amount,$quality);
  74. if(!$allow)
  75. {
  76. if ($card_type === mtopcard\SinopecCard) {
  77. $text = "中石化";
  78. }
  79. else {
  80. $text = "中石油";
  81. }
  82. return self::outerr(207,"今日贵司{$amount}{$text}充值库存已经用完.");
  83. }
  84. }
  85. $params = ['mchid' => $this->mchid(),
  86. 'buyer_id' => $this->adminid(),
  87. 'amount' => $amount,
  88. 'card_no' => $card_no,
  89. 'mch_order' => $mch_order,
  90. 'notify_url' => $notify_url,
  91. 'org_quality' => $quality];
  92. [$can_refill, $period] = refill\util::can_commit($card_no, $card_type);
  93. if ($can_refill === false) {
  94. refill\util::push_queue($this->mchid(),$mch_order,time() + $period);
  95. $state = refill\util::async_add($params, $period);
  96. } else {
  97. refill\util::push_queue($this->mchid(),$mch_order,time());
  98. $state = refill\util::push_add($params);
  99. }
  100. if ($state === true) {
  101. return self::outsuccess(['state' => true]);
  102. } else {
  103. $code = $state;
  104. return self::outerr($code, '提交失败');
  105. }
  106. }
  107. private function check_third($params)
  108. {
  109. if(empty($params['product_code'])) {
  110. return [false,'参数没有包含product_code'];
  111. }
  112. $pcode = $params['product_code'];
  113. if($this->check_pcode($pcode)) {
  114. return [false,"对应的产品编码{$pcode}"];
  115. }
  116. if(empty($params['quantity']) || intval($params['quantity']) < 1) {
  117. return [false,"购买数量参数不存在,或者小于0"];
  118. }
  119. if(empty($params['notifyurl'])) {
  120. return [false,'参数没有包含notifyurl'];
  121. }
  122. if(empty($params['order_sn'])) {
  123. return [false,'参数没有包含贵方唯一订单号:order_sn'];
  124. }
  125. return [true,""];
  126. }
  127. private function check_pcode($pcode)
  128. {
  129. $mod_third = Model('thrid_refill');
  130. $product = $mod_third->getProduct(['system_code' => $pcode]);
  131. return !empty($product);
  132. }
  133. public function add_thirdOp()
  134. {
  135. [$success,$error] = $this->check_third($_GET);
  136. if($success === false) {
  137. return self::outerr(201,$error);
  138. }
  139. $amount = refill\util::ThirdRefillAmount;
  140. $notify_url = $_GET['notifyurl'];
  141. $mch_order = $_GET['order_sn']; //对方的order编号
  142. $product_code = $_GET['product_code'];
  143. $card_no = $_GET['cardno'] ?? '';
  144. $quantity = $_GET['quantity'];
  145. $third_card_type = $_GET['card_type'] ?? 1;
  146. //三方充值没有质量
  147. $quality = 1;
  148. $card_type = mtopcard\ThirdRefillCard;
  149. if(!$this->check_mchorder($this->mchid(),$mch_order)) {
  150. return self::outerr(205,"客户订单号重复或者为空.");
  151. }
  152. $params = ['mchid' => $this->mchid(),
  153. 'buyer_id' => $this->adminid(),
  154. 'amount' => $amount,
  155. 'mch_order' => $mch_order,
  156. 'notify_url' => $notify_url,
  157. 'org_quality' => $quality,
  158. 'card_type' => $card_type,
  159. 'card_no' => $card_no,
  160. 'product_code' => $product_code,
  161. 'quantity' => $quantity,
  162. 'third_card_type' => $third_card_type
  163. ];
  164. $state = refill\util::push_addthird($params);
  165. if ($state === true) {
  166. return self::outsuccess(['state' => true]);
  167. } else {
  168. $code = $state;
  169. return self::outerr($code, '提交失败');
  170. }
  171. }
  172. private function can_refill($cardno)
  173. {
  174. $card_info = refill\util::read_card($cardno);
  175. if(empty($card_info)) return false;
  176. return intval($card_info['black_card']) === 0;
  177. }
  178. private function check_mchorder($mchid,$mch_order)
  179. {
  180. if(empty($mch_order)) {
  181. return false;
  182. }
  183. else {
  184. $refill_order = Model('refill_order');
  185. $ret = $refill_order->getOrderInfo(['mchid' => $mchid,'mch_order' => $mch_order]);
  186. return empty($ret);
  187. }
  188. }
  189. public function balanceOp()
  190. {
  191. $uid = $this->adminid();
  192. $minfo = new member_info($uid);
  193. $available = $minfo->available_predeposit();
  194. return self::outsuccess(['balance' => $available]);
  195. }
  196. public function query_tradeOp()
  197. {
  198. $mchid = $this->mchid();
  199. $trade_no = $_GET['trade_no'];
  200. if(empty($trade_no)) {
  201. return self::outerr(201,"交易号trade_no错误");
  202. }
  203. $mod_refill = Model('refill_order');
  204. $refill_info = $mod_refill->getOrderInfo(['order_sn' => $trade_no,'mchid' => $mchid]);
  205. $vr_order = Model('vr_order');
  206. $order_info = $vr_order->getOrderInfo(['order_sn' => $trade_no]);
  207. if(empty($refill_info) || empty($order_info)) {
  208. return self::outerr(201,"无此交易号");
  209. }
  210. $result = $this->format($order_info,$refill_info);
  211. return self::outsuccess($result);
  212. }
  213. private function format($order_info,$refill_info)
  214. {
  215. $result = [];
  216. $result['mchid'] = $refill_info['mchid'];
  217. $result['trade_no'] = $refill_info['order_sn'];
  218. $result['order_sn'] = $refill_info['mch_order'];
  219. $result['card_no'] = $refill_info['card_no'];
  220. $result['card_type'] = $refill_info['card_type'];
  221. $result['refill_amount'] = $refill_info['refill_amount'];
  222. $result['order_amount'] = $refill_info['mch_amount'];
  223. $result['order_time'] = $refill_info['order_time'];
  224. $result['success_time'] = $refill_info['notify_time'];
  225. $result['official_sn'] = $refill_info['official_sn'] ?? "";
  226. if($refill_info['is_retrying']) {
  227. $result['order_state'] = ORDER_STATE_NEW;
  228. }
  229. else {
  230. $result['order_state'] = $order_info['order_state'];
  231. }
  232. return $result;
  233. }
  234. public function queryOp()
  235. {
  236. $mchid = $this->mchid();
  237. $order_sn = $_GET['order_sn']; //用户方的订单号,对应数据库中的mch_order
  238. if(empty($order_sn)) {
  239. return self::outerr(201,"客户订单号order_sn错误");
  240. }
  241. $mod_refill = Model('refill_order');
  242. $refill_info = $mod_refill->getOrderInfo(['mch_order' => $order_sn,'mchid' => $mchid,'inner_status' => 0]);
  243. if(empty($refill_info))
  244. {
  245. $ret = refill\util::query_queue($mchid,$order_sn);
  246. if ($ret > 0) {
  247. $result['mchid'] = $mchid;
  248. $result['order_sn'] = $order_sn;
  249. $result['order_state'] = ORDER_STATE_QUEUE;
  250. return self::outsuccess($result);
  251. } else {
  252. return self::outerr(202, "无此订单");
  253. }
  254. }
  255. else
  256. {
  257. $vr_order = Model('vr_order');
  258. $order_info = $vr_order->getOrderInfo(['order_sn' => $refill_info['order_sn']]);
  259. if(empty($order_info)) {
  260. return self::outerr(203,"无此交易号");
  261. }
  262. if($order_info['order_state'] == ORDER_STATE_SEND) {
  263. QueueClient::push("QueryRefillState",['order_id' => $refill_info['order_id']]);
  264. }
  265. $result = $this->format($order_info,$refill_info);
  266. return self::outsuccess($result);
  267. }
  268. }
  269. }