order.php 19 KB

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