order.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. namespace refill;
  3. use mtopcard;
  4. use Log;
  5. class order
  6. {
  7. private $mMchid;
  8. private $mBuyerId;
  9. private $mAmount;
  10. private $mMchAmount;
  11. private $mCardNo;
  12. private $mMchOrder;
  13. private $mNotifyUrl;
  14. private $mIdCard;
  15. private $mCardName;
  16. private $mOrderTime;
  17. private $mCommitTimes;
  18. private $mLastOrderID;
  19. private $mOriginQuality;
  20. private $mCurQuality;
  21. private $mMchCardTypes; //用于产品和卡类型绑定的充值机构.
  22. private $mCardType;
  23. private $mRegionNo;
  24. private $mIsTransfer;
  25. private $mCardState;
  26. private $mBlack = 0;
  27. private $mIsValidate;
  28. private $mFirstCommit;
  29. private $mMatchRatio;
  30. //mCardType等于7的时候,需要这些参数
  31. private $mProductCode;
  32. private $mQuantity; //数量
  33. private $mThirdPorductType;
  34. private $mThirdCardType; // 三方充值账号类型
  35. private $mThirdExtes = []; //三方扩展参数
  36. private $mChannelFilter = null;
  37. private $mFirstQuality;
  38. private $mOverPriceFlag;
  39. public function __construct()
  40. {
  41. $this->mProductCode = '';
  42. $this->mThirdPorductType = 0;
  43. $this->mMatchRatio = false;
  44. $this->mMchCardTypes = [];
  45. $this->mFirstQuality = 0;
  46. $this->mOverPriceFlag = 0;
  47. $this->mMchAmount = 0;
  48. }
  49. public function finish()
  50. {
  51. if($this->mChannelFilter != null) {
  52. $this->mChannelFilter->finish();
  53. }
  54. }
  55. public function mchid()
  56. {
  57. return $this->mMchid;
  58. }
  59. public function buyer_id()
  60. {
  61. return $this->mBuyerId;
  62. }
  63. public function mch_order()
  64. {
  65. return $this->mMchOrder;
  66. }
  67. public function set_mchorder($mch_order)
  68. {
  69. $this->mMchOrder = $mch_order;
  70. }
  71. public function set_last_orderid($order_id)
  72. {
  73. $this->mLastOrderID = $order_id;
  74. }
  75. public function first_commit()
  76. {
  77. return $this->mFirstCommit;
  78. }
  79. public function set_match($match)
  80. {
  81. $this->mMatchRatio = $match;
  82. }
  83. public function match_ratio()
  84. {
  85. return $this->mMatchRatio;
  86. }
  87. public function spec()
  88. {
  89. return $this->mAmount;
  90. }
  91. public function mch_amount()
  92. {
  93. return $this->mMchAmount;
  94. }
  95. public function card_no()
  96. {
  97. return $this->mCardNo;
  98. }
  99. public function card_type()
  100. {
  101. return $this->mCardType;
  102. }
  103. public function org_quality()
  104. {
  105. return $this->mOriginQuality;
  106. }
  107. public function cur_quality()
  108. {
  109. return $this->mCurQuality;
  110. }
  111. public function set_first_quality($quality)
  112. {
  113. $this->mFirstQuality = $quality;
  114. }
  115. public function set_can_overprice($match_ratio, $can_last)
  116. {
  117. if($match_ratio) //能匹配成功率
  118. {
  119. if($can_last) {
  120. $value = 11;//能匹配成功率,能匹配利润率 -> 不要溢价补充
  121. }
  122. else {
  123. $value = 12; //能匹配成功率,不能匹配利润率 -> 不要溢价补充
  124. }
  125. }
  126. elseif($can_last) {
  127. $value = 1;//不能匹配成功率,但能匹配利润率 -> 需要溢价补充
  128. }
  129. else {
  130. $value = 2;//不能匹配成功率,也不能匹配利润率 -> 不需要溢价补充
  131. }
  132. $this->mOverPriceFlag = $value;
  133. }
  134. public function can_over_price()
  135. {
  136. //尚未设置初始值,不可溢价
  137. if($this->mFirstQuality === 0) {
  138. return false;
  139. }
  140. if($this->mFirstQuality === $this->mCurQuality) {
  141. return false;
  142. }
  143. else
  144. {
  145. $value = $this->mOverPriceFlag;
  146. Log::record("can_over_price value is $value", Log::DEBUG);
  147. if ($value === 1 or $value === 0) {
  148. return true;
  149. }
  150. else {
  151. return false;
  152. }
  153. }
  154. }
  155. public function set_quality($org_quality, $quality)
  156. {
  157. $this->mOriginQuality = $org_quality;
  158. $this->mCurQuality = $quality;
  159. }
  160. public function order_time()
  161. {
  162. return $this->mOrderTime;
  163. }
  164. public function commit_times()
  165. {
  166. return $this->mCommitTimes;
  167. }
  168. public function commit_times_inc()
  169. {
  170. $this->mCommitTimes += 1;
  171. }
  172. public function elapse_secs()
  173. {
  174. return time() - $this->mOrderTime;
  175. }
  176. public function region_no()
  177. {
  178. return $this->mRegionNo;
  179. }
  180. public function pcode()
  181. {
  182. return $this->mProductCode;
  183. }
  184. public function last_order_id()
  185. {
  186. return $this->mLastOrderID;
  187. }
  188. public function validate()
  189. {
  190. return $this->mIsValidate;
  191. }
  192. public function quantity()
  193. {
  194. return $this->mQuantity;
  195. }
  196. public function card_state()
  197. {
  198. return $this->mCardState;
  199. }
  200. public function thrid_params()
  201. {
  202. $third_params = ['product_code' => $this->mProductCode,
  203. 'quantity' => $this->mQuantity,
  204. 'third_card_type' => $this->mThirdCardType];
  205. return $third_params;
  206. }
  207. public function match_card_type()
  208. {
  209. if (empty($this->mMchCardTypes)) {
  210. return true;
  211. } else {
  212. return in_array($this->card_type(), $this->mMchCardTypes);
  213. }
  214. }
  215. public function is_transfer()
  216. {
  217. return $this->mIsTransfer;
  218. }
  219. public function is_black()
  220. {
  221. return $this->mBlack == 1;
  222. }
  223. public static function from_parameters($params)
  224. {
  225. $order = new order();
  226. $order->setParams($params);
  227. return $order;
  228. }
  229. public static function from_db(array $refill_info, array $order_info, array $third_info = [])
  230. {
  231. $order = new order();
  232. $order->setDbParams($refill_info, $order_info, $third_info);
  233. return $order;
  234. }
  235. private function setDbParams(array $refill_info, array $order_info, array $third_info = [])
  236. {
  237. $this->mMchid = intval($refill_info['mchid']);
  238. $this->mBuyerId = intval($order_info['buyer_id']);
  239. $this->mAmount = intval($refill_info['refill_amount']);
  240. $this->mCardNo = $refill_info['card_no'];
  241. $this->mCardType = intval($refill_info['card_type']);
  242. $this->mRegionNo = intval($refill_info['regin_no']);
  243. $this->mOriginQuality = intval($refill_info['org_quality']);
  244. $this->mCurQuality = intval($refill_info['quality']);
  245. $this->mMchOrder = $refill_info['mch_order'] ?? '';
  246. $this->mNotifyUrl = $refill_info['notify_url'] ?? '';
  247. $this->mIdCard = $refill_info['idcard'] ?? '';
  248. $this->mCardName = $refill_info['card_name'] ?? '';
  249. $this->mOrderTime = intval($refill_info['order_time']);
  250. $this->mCommitTimes = intval($refill_info['commit_times']);
  251. $this->mLastOrderID = intval($refill_info['order_id']);
  252. $this->mCardState = intval($refill_info['cardno_state']);
  253. $this->mIsTransfer = $refill_info['is_transfer'] == 1;
  254. $this->mIsValidate = mtopcard\is_validate($this->mCardState);
  255. $this->mQuantity = intval($order_info['goods_num'] ?? 1);
  256. $mch_amount = $refill_info['mch_amount'];
  257. if (bccomp("$mch_amount", '0', 5) === 0) {
  258. $this->mMchAmount = 0;
  259. } else {
  260. $this->mMchAmount = $mch_amount;
  261. }
  262. if (!empty($third_info) && $this->is_third()) {
  263. $this->mProductCode = $third_info['product_code'];
  264. $this->mThirdPorductType = $third_info['third_product_type'];
  265. $this->mThirdCardType = $third_info['third_card_type'];
  266. if ($this->mThirdPorductType == mtopcard\ThirdElectricProduct) {
  267. $this->mThirdExtes['company_type'] = $third_info['company_type'];
  268. $this->mThirdExtes['use_type'] = $third_info['use_type'];
  269. $this->mThirdExtes['province'] = $third_info['province'];
  270. $this->mThirdExtes['city'] = $third_info['city'];
  271. $this->mThirdExtes['card_id'] = $third_info['card_id'];
  272. } elseif ($this->mThirdPorductType == mtopcard\ThirdSinopecECouponPoroduct) {
  273. $this->mThirdExtes['province'] = $third_info['province'];
  274. }
  275. }
  276. $this->mChannelFilter = new channel_filter($this->mMchid,$this->mMchOrder,$this->mCardType);
  277. }
  278. public function is_third()
  279. {
  280. return $this->mCardType == 7;
  281. }
  282. public function third_product_type()
  283. {
  284. return $this->mThirdPorductType;
  285. }
  286. public function is_oil()
  287. {
  288. return in_array($this->mCardType, [mtopcard\SinopecCard, mtopcard\PetroChinaCard]);
  289. }
  290. public function is_phone()
  291. {
  292. return in_array($this->mCardType, [mtopcard\ChinaMobileCard, mtopcard\ChinaUnicomCard, mtopcard\ChinaTelecomCard]);
  293. }
  294. public function unique_id()
  295. {
  296. return "{$this->mMchid}-{$this->mMchOrder}";
  297. }
  298. public function filter()
  299. {
  300. return $this->mChannelFilter;
  301. }
  302. private function setParams($params)
  303. {
  304. $this->mMchid = intval($params['mchid']);
  305. $this->mBuyerId = intval($params['buyer_id']);
  306. $this->mAmount = intval($params['amount']);
  307. $this->mMchAmount = $params['mch_amount'] ?? 0;
  308. $this->mCardNo = $params['card_no'];
  309. $this->mMchOrder = $params['mch_order'] ?? '';
  310. $this->mNotifyUrl = $params['notify_url'] ?? '';
  311. $this->mIdCard = $params['idcard'] ?? '';
  312. $this->mCardName = $params['card_name'] ?? '';
  313. $this->mOrderTime = $params['order_time'] ?? time();
  314. $this->mCommitTimes = $params['commit_times'] ?? 0;
  315. $this->mLastOrderID = $params['order_id'] ?? 0;
  316. $this->mQuantity = $params['quantity'] ?? 1;
  317. $this->mOriginQuality = intval($params['org_quality']) ?? 0;
  318. $this->mMatchRatio = $params['match_ratio'] ?? false;
  319. $this->mMchCardTypes = $params['mch_card_types'] ?? [];
  320. $card_type = intval($params['card_type']) ?? 0;
  321. if ($card_type == mtopcard\ThirdRefillCard) {
  322. $this->mCurQuality = $this->mOriginQuality;
  323. $this->mProductCode = $params['product_code'];
  324. $this->mThirdPorductType = $params['third_product_type'];
  325. $this->mThirdCardType = $params['third_card_type'];
  326. $this->mRegionNo = $params['province'];
  327. $this->mIsTransfer = false;
  328. $this->mCardState = 0;
  329. $this->mCardType = $card_type;
  330. $this->mIsValidate = true;
  331. $this->mFirstCommit = true;
  332. if ($this->mThirdPorductType == mtopcard\ThirdElectricProduct) {
  333. $this->mThirdExtes['company_type'] = $params['company_type'];
  334. $this->mThirdExtes['use_type'] = $params['use_type'];
  335. $this->mThirdExtes['province'] = $params['province'];
  336. $this->mThirdExtes['city'] = $params['city'];
  337. $this->mThirdExtes['card_id'] = $params['card_id'];
  338. } elseif ($this->mThirdPorductType == mtopcard\ThirdSinopecECouponPoroduct) {
  339. $this->mThirdExtes['province'] = $params['province'];
  340. }
  341. }
  342. else
  343. {
  344. $this->mProductCode = '';
  345. $this->mCurQuality = intval($params['quality']) ?? 0;
  346. $this->mThirdCardType = 1; //deafult value
  347. if ($card_type == 0) {
  348. $this->mFirstCommit = true;
  349. [$validate, $card_type, $regin_no, $isTransfer, $card_state, $black] = mtopcard\valid_phone($this->mCardNo);
  350. $this->mCardType = $card_type;
  351. $this->mRegionNo = $regin_no;
  352. $this->mIsTransfer = $isTransfer;
  353. $mchid = $this->mMchid;
  354. global $config;
  355. $no_valid_mchids = $config['neednot_check_card_mchids'] ?? [];
  356. if(in_array($mchid,$no_valid_mchids)) {
  357. $this->mIsValidate = true;
  358. $this->mCardState = 1;
  359. $this->mBlack = false;
  360. }
  361. else {
  362. $this->mIsValidate = $validate;
  363. $this->mCardState = $card_state;
  364. $this->mBlack = $black;
  365. }
  366. } else {
  367. $this->mFirstCommit = false;
  368. $this->mCardType = $params['card_type'];
  369. $this->mRegionNo = $params['regin_no'];
  370. $this->mIsTransfer = $params['is_transfer'] == 1;
  371. $this->mCardState = $params['cardno_state'];
  372. $this->mIsValidate = $params['is_validate'] == 1;
  373. }
  374. }
  375. $this->mChannelFilter = new channel_filter($this->mMchid,$this->mMchOrder,$this->mCardType);
  376. }
  377. public function ZeroRefillParams($order_id, $order_sn, $spec, $mch_amount, $channel_name, $channel_amout, $err_msg)
  378. {
  379. $refill_amount = $spec * $this->mQuantity;
  380. $params = ['order_id' => $order_id, 'order_sn' => $order_sn,
  381. 'mchid' => $this->mMchid, 'refill_amount' => $refill_amount, 'mch_order' => $this->mMchOrder,
  382. 'idcard' => $this->mIdCard, 'card_name' => $this->mCardName,
  383. 'notify_url' => $this->mNotifyUrl,
  384. 'channel_name' => $channel_name, 'mch_amount' => $mch_amount, 'channel_amount' => $channel_amout,
  385. 'order_time' => $this->mOrderTime, 'commit_times' => $this->mCommitTimes,
  386. 'commit_time' => time(), 'notify_state' => 1, 'notify_time' => time(),
  387. 'is_transfer' => $this->mIsTransfer ? 1 : 0, 'cardno_state' => $this->mCardState,
  388. 'card_type' => $this->mCardType, 'regin_no' => $this->mRegionNo,
  389. 'card_no' => $this->mCardNo, 'quality' => $this->mCurQuality, 'org_quality' => $this->mOriginQuality,
  390. 'err_msg' => $err_msg];
  391. return $params;
  392. }
  393. public function refill_params($order_id, $order_sn, $spec, $channel_name, $channel_amout, $mch_amount)
  394. {
  395. $refill_amount = $spec * $this->mQuantity;
  396. $params = ['order_id' => $order_id, 'order_sn' => $order_sn, 'mchid' => $this->mMchid,
  397. 'refill_amount' => $refill_amount, 'mch_order' => $this->mMchOrder,
  398. 'idcard' => $this->mIdCard, 'card_name' => $this->mCardName,
  399. 'notify_url' => $this->mNotifyUrl, 'channel_name' => $channel_name,
  400. 'mch_amount' => $mch_amount, 'channel_amount' => $channel_amout,
  401. 'order_time' => $this->mOrderTime, 'commit_times' => $this->mCommitTimes,
  402. 'card_type' => $this->mCardType, 'regin_no' => $this->mRegionNo,
  403. 'is_transfer' => $this->mIsTransfer ? 1 : 0, 'cardno_state' => $this->mCardState,
  404. 'card_no' => $this->mCardNo, 'quality' => $this->mCurQuality, 'org_quality' => $this->mOriginQuality];
  405. return $params;
  406. }
  407. public function third_extparams($order_id, $order_sn)
  408. {
  409. $ext = ['order_id' => $order_id, 'order_sn' => $order_sn, 'product_type' => $this->mThirdPorductType,
  410. 'account_type' => $this->mThirdCardType,
  411. 'quantity' => $this->mQuantity, 'user_account' => $this->mCardNo, 'pcode' => $this->mProductCode,
  412. 'add_time' => time()
  413. ];
  414. if ($this->is_third()) {
  415. if ($this->mThirdPorductType == mtopcard\ThirdElectricProduct) {
  416. $ext['company_type'] = $this->mThirdExtes['company_type'];
  417. $ext['use_type'] = $this->mThirdExtes['use_type'];
  418. $ext['province'] = $this->mThirdExtes['province'];
  419. $ext['city'] = $this->mThirdExtes['city'];
  420. $ext['card_id'] = $this->mThirdExtes['card_id'];
  421. } elseif ($this->mThirdPorductType == mtopcard\ThirdSinopecECouponPoroduct) {
  422. $ext['province'] = $this->mThirdExtes['province'];
  423. }
  424. }
  425. return $ext;
  426. }
  427. public function channel_params($order_id, $order_sn, $goods_id, $ch_price)
  428. {
  429. $params = ['order_sn' => $order_sn, 'idcard' => $this->mIdCard, 'card_name' => $this->mCardName,
  430. 'buyer_id' => $this->mBuyerId,
  431. 'quality' => $this->mCurQuality, 'order_id' => $order_id, 'regin_no' => $this->mRegionNo,
  432. 'mchid' => $this->mMchid,'ch_price' => $ch_price];
  433. if ($this->is_third()) {
  434. $params['quantity'] = $this->mQuantity;
  435. $params['product_code'] = $this->mProductCode;
  436. $params['third_product_type'] = $this->mThirdPorductType;
  437. $params['third_card_type'] = $this->mThirdCardType;
  438. $params['goods_id'] = $goods_id;
  439. if ($this->mThirdPorductType == mtopcard\ThirdElectricProduct) {
  440. $params['company_type'] = $this->mThirdExtes['company_type'];
  441. $params['use_type'] = $this->mThirdExtes['use_type'];
  442. $params['province'] = $this->mThirdExtes['province'];
  443. $params['city'] = $this->mThirdExtes['city'];
  444. $params['card_id'] = $this->mThirdExtes['card_id'];
  445. } elseif ($this->mThirdPorductType == mtopcard\ThirdSinopecECouponPoroduct) {
  446. $params['province'] = $this->mThirdExtes['province'];
  447. }
  448. }
  449. return $params;
  450. }
  451. public function queue_params()
  452. {
  453. $params = ['mchid' => $this->mMchid,
  454. 'buyer_id' => $this->mBuyerId,
  455. 'amount' => $this->mAmount, //spec
  456. 'mch_amount' => $this->mMchAmount,
  457. 'quantity' => $this->mQuantity,
  458. 'card_no' => $this->mCardNo,
  459. 'card_type' => $this->mCardType,
  460. 'regin_no' => $this->mRegionNo,
  461. 'org_quality' => $this->mOriginQuality,
  462. 'quality' => $this->mCurQuality,
  463. 'mch_order' => $this->mMchOrder,
  464. 'notify_url' => $this->mNotifyUrl,
  465. 'idcard' => $this->mIdCard,
  466. 'card_name' => $this->mCardName,
  467. 'order_time' => $this->mOrderTime,
  468. 'commit_times' => $this->mCommitTimes,
  469. 'is_transfer' => $this->mIsTransfer ? 1 : 0,
  470. 'cardno_state' => $this->mCardState,
  471. 'order_id' => $this->mLastOrderID,
  472. 'is_validate' => $this->mIsValidate,
  473. 'match_ratio' => $this->mMatchRatio];
  474. return $params;
  475. }
  476. public function third_requeue_params($pcode)
  477. {
  478. $params = ['mchid' => $this->mMchid,
  479. 'buyer_id' => $this->mBuyerId,
  480. 'mch_amount' => $this->mMchAmount,
  481. 'quantity' => $this->mQuantity,
  482. 'card_no' => $this->mCardNo,
  483. 'org_quality' => $this->mOriginQuality,
  484. 'quality' => $this->mCurQuality,
  485. 'mch_order' => $this->mMchOrder,
  486. 'notify_url' => $this->mNotifyUrl,
  487. 'idcard' => $this->mIdCard,
  488. 'card_name' => $this->mCardName,
  489. 'order_time' => $this->mOrderTime,
  490. 'commit_times' => $this->mCommitTimes,
  491. 'order_id' => $this->mLastOrderID,
  492. 'card_type' => $this->mCardType,
  493. 'third_product_type' => $this->mThirdPorductType,
  494. 'third_card_type' => $this->mThirdCardType
  495. ];
  496. $params['product_code'] = $pcode;
  497. if($this->is_third()) {
  498. $params['amount'] = util::ThirdRefillAmount;;
  499. } else {
  500. $params['amount'] = $this->mAmount;
  501. }
  502. if ($this->mThirdPorductType == mtopcard\ThirdElectricProduct) {
  503. $params['company_type'] = $this->mThirdExtes['company_type'];
  504. $params['use_type'] = $this->mThirdExtes['use_type'];
  505. $params['province'] = $this->mThirdExtes['province'];
  506. $params['city'] = $this->mThirdExtes['city'];
  507. $params['card_id'] = $this->mThirdExtes['card_id'];
  508. } elseif ($this->mThirdPorductType == mtopcard\ThirdSinopecECouponPoroduct) {
  509. $params['province'] = $this->mThirdExtes['province'];
  510. }
  511. return $params;
  512. }
  513. }