order.php 16 KB

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