order.php 18 KB

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