buy_virtual.logic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * 虚拟商品购买行为
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. require_once(BASE_HELPER_PATH . '/account_helper.php');
  8. class buy_virtualLogic
  9. {
  10. /**
  11. * 虚拟商品购买第一步,得到购买数据(商品、店铺、会员)
  12. * @param int $goods_id 商品ID
  13. * @param int $quantity 购买数量
  14. * @param int $member_id 会员ID
  15. * @return array
  16. */
  17. public function getBuyStep1Data($goods_id, $quantity, $member_id) {
  18. return $this->getBuyStepData($goods_id, $quantity, $member_id);
  19. }
  20. /**
  21. * 虚拟商品购买第二步,得到购买数据(商品、店铺、会员)
  22. * @param int $goods_id 商品ID
  23. * @param int $quantity 购买数量
  24. * @param int $member_id 会员ID
  25. * @return array
  26. */
  27. public function getBuyStep2Data($goods_id, $quantity, $member_id) {
  28. return $this->getBuyStepData($goods_id, $quantity, $member_id);
  29. }
  30. /**
  31. * 得到虚拟商品购买数据(商品、店铺、会员)
  32. * @param int $goods_id 商品ID
  33. * @param int $quantity 购买数量
  34. * @param int $member_id 会员ID
  35. * @return array
  36. */
  37. public function getBuyStepData($goods_id, $quantity, $member_id,$fRefill = false)
  38. {
  39. $goods_info = Model('goods')->getVirtualGoodsOnlineInfoByID($goods_id);
  40. if (empty($goods_info)) {
  41. return callback(false, '该商品不符合购买条件,可能的原因有:下架、不存在、过期等');
  42. }
  43. if ($goods_info['goods_storage'] <= 0) {
  44. return callback(false, '该商品库存不足');
  45. }
  46. if ($goods_info['virtual_limit'] > $goods_info['goods_storage']) {
  47. $goods_info['virtual_limit'] = $goods_info['goods_storage'];
  48. }
  49. if(!$fRefill) {
  50. //取得抢购信息
  51. $goods_info = $this->_getGroupbuyInfo($goods_info);
  52. }
  53. $quantity = abs(intval($quantity));
  54. $quantity = $quantity == 0 ? 1 : $quantity;
  55. $quantity = $quantity > $goods_info['virtual_limit'] ? $goods_info['virtual_limit'] : $quantity;
  56. if ($quantity > $goods_info['goods_storage']) {
  57. return callback(false, '该商品库存不足');
  58. }
  59. $goods_info['quantity'] = $quantity;
  60. $goods_info['goods_total'] = ncPriceFormat($goods_info['goods_price'] * $goods_info['quantity']);
  61. if(!$fRefill) {
  62. $goods_info['goods_image_url'] = cthumb($goods_info['goods_image'], 240, $goods_info['store_id']);
  63. }
  64. $return = [];
  65. $return['goods_info'] = $goods_info;
  66. $return['store_info'] = Model('store')->getStoreOnlineInfoByID($goods_info['store_id']);
  67. $return['member_info'] = Model('member')->getMemberInfoByID($member_id);
  68. return callback(true,'',$return);
  69. }
  70. /**
  71. * 虚拟商品购买第三步
  72. * @param array $post 接收POST数据,必须传入goods_id:商品ID,quantity:购买数量,buyer_phone:接收手机,buyer_msg:买家留言
  73. * @param int $member_id
  74. * @return array
  75. */
  76. public function buyStep3($post, $member_id,$calc_vorder_amount = null,$merchant = false,$use_trans = true)
  77. {
  78. $fRefill = $merchant;
  79. $result = $this->getBuyStepData($post['goods_id'], $post['quantity'], $member_id, $fRefill);
  80. if (!$result['state']) return $result;
  81. $goods_info = $result['data']['goods_info'];
  82. //应付总金额计算
  83. if(empty($calc_vorder_amount)) {
  84. $pay_total = $goods_info['goods_price'] * $goods_info['quantity'];
  85. }
  86. else {
  87. $pay_total = call_user_func($calc_vorder_amount,$goods_info);
  88. }
  89. $store_id = $goods_info['store_id'];
  90. $store_goods_total_list = [$store_id => $pay_total];
  91. $pay_total = $store_goods_total_list[$store_id];
  92. //整理数据
  93. $input = [];
  94. $input['quantity'] = $goods_info['quantity'];
  95. $input['buyer_phone'] = $post['buyer_phone'];
  96. $input['buyer_msg'] = $post['buyer_msg'];
  97. $input['pay_total'] = $pay_total;
  98. $input['order_from'] = $post['order_from'];
  99. try
  100. {
  101. $model_goods = Model('goods');
  102. //开始事务
  103. if($use_trans) {
  104. $trans = new trans_wapper($model_goods,__METHOD__);
  105. }
  106. $member_info = Model('member')->getMemberInfo(['member_id' => $member_id]);
  107. $order_info = $this->_createOrder($input,$goods_info,$member_info,$fRefill);
  108. if (!empty($post['password']))
  109. {
  110. if ($member_info['member_paypwd'] != '' && $member_info['member_paypwd'] == md5($post['password']))
  111. {
  112. //充值卡支付
  113. if (!empty($post['rcb_pay'])) {
  114. $order_info = $this->_rcbPay($order_info, $post, $member_info);
  115. }
  116. //预存款支付
  117. if (!empty($post['pd_pay'])) {
  118. $this->_pdPay($order_info, $post, $member_info,$fRefill);
  119. }
  120. }
  121. }
  122. elseif($merchant)
  123. {
  124. //预存款支付
  125. if (!empty($post['pd_pay'])) {
  126. $this->_pdPay($order_info, $post, $member_info,$fRefill);
  127. }
  128. }
  129. else {
  130. Log::record('',Log::DEBUG);
  131. }
  132. if($use_trans) {
  133. $trans->commit();
  134. }
  135. }
  136. catch (Exception $e)
  137. {
  138. if($use_trans) {
  139. $trans->rollback();
  140. }
  141. return callback(false, $e->getMessage());
  142. }
  143. if(!$fRefill)
  144. {
  145. //变更库存和销量
  146. QueueClient::push('createOrderUpdateStorage', [$goods_info['goods_id'] => $goods_info['quantity']]);
  147. //更新抢购信息
  148. $this->_updateGroupBuy($goods_info);
  149. //发送兑换码到手机
  150. $param = ['order_id'=>$order_info['order_id'],'buyer_id'=>$member_id,'buyer_phone'=>$order_info['buyer_phone']];
  151. QueueClient::push('sendVrCode', $param);
  152. }
  153. return callback(true,'', ['order_id' => $order_info['order_id'],'order_sn' => $order_info['order_sn']]);
  154. }
  155. /**
  156. * 生成订单
  157. * @param array $input 表单数据
  158. * @param unknown $goods_info 商品数据
  159. * @param unknown $member_info 会员数据
  160. * @throws Exception
  161. * @return array
  162. */
  163. private function _createOrder($input, $goods_info, $member_info,$fRefill)
  164. {
  165. extract($input);
  166. $model_vr_order = Model('vr_order');
  167. //存储生成的订单,函数会返回该数组
  168. $order = [];
  169. $order['order_sn'] = $this->_makeOrderSn();
  170. $order['store_id'] = $goods_info['store_id'];
  171. $order['store_name'] = $goods_info['store_name'];
  172. $order['buyer_id'] = $member_info['member_id'];
  173. $order['buyer_name'] = $member_info['member_name'];
  174. $order['buyer_phone'] = $input['buyer_phone'];
  175. $order['buyer_msg'] = $input['buyer_msg'];
  176. $order['add_time'] = time();
  177. $order['order_state'] = ORDER_STATE_NEW;
  178. $order['order_amount'] = $pay_total;
  179. $order['goods_id'] = $goods_info['goods_id'];
  180. $order['goods_name'] = $goods_info['goods_name'];
  181. $order['goods_price'] = $goods_info['goods_price'];
  182. $order['goods_num'] = $input['quantity'];
  183. $order['goods_image'] = $goods_info['goods_image'];
  184. $order['commis_rate'] = 200;
  185. $order['gc_id'] = $goods_info['gc_id'];
  186. $order['vr_indate'] = $goods_info['virtual_indate'];
  187. $order['vr_invalid_refund'] = $goods_info['virtual_invalid_refund'];
  188. $order['order_from'] = $input['order_from'];
  189. if ($goods_info['ifgroupbuy'] == 1) {
  190. $order['order_promotion_type'] = 1;
  191. $order['promotions_id'] = $goods_info['groupbuy_id'];
  192. }
  193. $order_id = $model_vr_order->addOrder($order);
  194. if (!$order_id) {
  195. throw new Exception('订单保存失败');
  196. }
  197. $order['order_id'] = $order_id;
  198. if ($fRefill) return $order;
  199. // 提醒[库存报警]
  200. if ($goods_info['goods_storage_alarm'] >= ($goods_info['goods_storage'] - $input['quantity'])) {
  201. $param = [];
  202. $param['common_id'] = $goods_info['goods_commonid'];
  203. $param['sku_id'] = $goods_info['goods_id'];
  204. QueueClient::push('sendStoreMsg', ['code' => 'goods_storage_alarm', 'store_id' => $goods_info['store_id'], 'param' => $param]);
  205. }
  206. return $order;
  207. }
  208. /**
  209. * 生成支付单编号(两位随机 + 从2000-01-01 00:00:00 到现在的秒数+微秒+会员ID%1000),该值会传给第三方支付接口
  210. * 长度 =2位 + 10位 + 3位 + 3位 = 18位
  211. * 1000个会员同一微秒提订单,重复机率为1/100
  212. * @return string
  213. */
  214. private function _makeOrderSn()
  215. {
  216. static $prime_num = [11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];
  217. $pos = mt_rand(0,count($prime_num) - 1);
  218. $factor = $prime_num[$pos];
  219. $prefix = (getmypid() * $factor) % 1000;
  220. return sprintf('%03d', $prefix)
  221. . sprintf('%03d', mt_rand(100,999))
  222. . sprintf('%010d', time() - 946656000)
  223. . sprintf('%06d', (float)microtime() * 1000000);
  224. }
  225. /**
  226. * 更新抢购购买人数和数量
  227. */
  228. private function _updateGroupBuy($goods_info) {
  229. if ($goods_info['ifgroupbuy'] && $goods_info['groupbuy_id']) {
  230. $groupbuy_info = array();
  231. $groupbuy_info['groupbuy_id'] = $goods_info['groupbuy_id'];
  232. $groupbuy_info['quantity'] = $goods_info['quantity'];
  233. QueueClient::push('editGroupbuySaleCount', $groupbuy_info);
  234. }
  235. }
  236. /**
  237. * 充值卡支付
  238. * 如果充值卡足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  239. */
  240. private function _rcbPay($order_info, $input, $buyer_info)
  241. {
  242. $available_rcb_amount = floatval($buyer_info['available_rc_balance']);
  243. if ($available_rcb_amount <= 0) return false;
  244. $model_vr_order = Model('vr_order');
  245. $model_pd = Model('predeposit');
  246. $order_amount = floatval($order_info['order_amount']);
  247. $data_pd = [];
  248. $data_pd['member_id'] = $buyer_info['member_id'];
  249. $data_pd['member_name'] = $buyer_info['member_name'];
  250. $data_pd['amount'] = $order_amount;
  251. $data_pd['order_sn'] = $order_info['order_sn'];
  252. if ($available_rcb_amount >= $order_amount)
  253. {
  254. // 预存款立即支付,订单支付完成
  255. $model_pd->changeRcb('order_pay',$data_pd);
  256. $available_rcb_amount -= $order_amount;
  257. // 订单状态 置为已支付
  258. $data_order = [];
  259. $order_info['order_state'] = $data_order['order_state'] = ORDER_STATE_PAY;
  260. $data_order['payment_time'] = time();
  261. $data_order['payment_code'] = 'predeposit';
  262. $data_order['rcb_amount'] = $order_info['order_amount'];
  263. $result = $model_vr_order->editOrder($data_order, ['order_id'=>$order_info['order_id']]);
  264. if (!$result) {
  265. throw new Exception('订单更新失败');
  266. }
  267. //发放兑换码
  268. $insert = $model_vr_order->addOrderCode($order_info);
  269. if (!$insert) {
  270. throw new Exception('兑换码发送失败');
  271. }
  272. }
  273. else {
  274. //暂冻结预存款,后面还需要 API彻底完成支付
  275. $data_pd['amount'] = $available_rcb_amount;
  276. $model_pd->changeRcb('order_freeze',$data_pd);
  277. //预存款支付金额保存到订单
  278. $data_order = [];
  279. $order_info['rcb_amount'] = $data_order['rcb_amount'] = $available_rcb_amount;
  280. $result = $model_vr_order->editOrder($data_order, ['order_id'=>$order_info['order_id']]);
  281. if (!$result) {
  282. throw new Exception('订单更新失败');
  283. }
  284. }
  285. return $order_info;
  286. }
  287. /**
  288. * 预存款支付
  289. * 如果预存款足够就单独支付了该订单,如果不足就暂时冻结,等API支付成功了再彻底扣除
  290. */
  291. private function _pdPay($order_info, $input, $buyer_info,$fRefill=false)
  292. {
  293. if ($order_info['order_state'] == ORDER_STATE_PAY) return false;
  294. $available_pd_amount = floatval($buyer_info['available_predeposit']);
  295. if ($available_pd_amount <= 0) return false;
  296. $model_vr_order = Model('vr_order');
  297. $model_pd = Model('predeposit');
  298. $order_amount = floatval($order_info['order_amount'])-floatval($order_info['rcb_amount']);
  299. $data_pd = [];
  300. $data_pd['member_id'] = $buyer_info['member_id'];
  301. $data_pd['member_name'] = $buyer_info['member_name'];
  302. $data_pd['amount'] = $order_amount;
  303. $data_pd['order_sn'] = $order_info['order_sn'];
  304. if ($available_pd_amount >= $order_amount)
  305. {
  306. //预存款立即支付,订单支付完成
  307. $model_pd->changePd('order_pay',$data_pd,$fRefill);
  308. //下单,支付被冻结的充值卡
  309. $pd_amount = floatval($order_info['rcb_amount']);
  310. if ($pd_amount > 0) {
  311. $data_pd = [];
  312. $data_pd['member_id'] = $buyer_info['member_id'];
  313. $data_pd['member_name'] = $buyer_info['member_name'];
  314. $data_pd['amount'] = $pd_amount;
  315. $data_pd['order_sn'] = $order_info['order_sn'];
  316. $model_pd->changeRcb('order_comb_pay',$data_pd);
  317. }
  318. // 订单状态 置为已支付
  319. $data_order = [];
  320. $data_order['order_state'] = ORDER_STATE_PAY;
  321. $data_order['payment_time'] = time();
  322. $data_order['payment_code'] = 'predeposit';
  323. $data_order['pd_amount'] = $order_amount;
  324. $result = $model_vr_order->editOrder($data_order, ['order_id'=>$order_info['order_id']]);
  325. if (!$result) {
  326. throw new Exception('订单更新失败');
  327. }
  328. //发放兑换码
  329. if(!$fRefill) {
  330. $model_vr_order->addOrderCode($order_info);
  331. }
  332. }
  333. else {
  334. //暂冻结预存款,后面还需要API彻底完成支付
  335. $data_pd['amount'] = $available_pd_amount;
  336. $model_pd->changePd('order_freeze',$data_pd,$fRefill);
  337. //预存款支付金额保存到订单
  338. $data_order = [];
  339. $data_order['pd_amount'] = $available_pd_amount;
  340. $result = $model_vr_order->editOrder($data_order, ['order_id'=>$order_info['order_id']]);
  341. if (!$result) {
  342. throw new Exception('订单更新失败');
  343. }
  344. }
  345. return true;
  346. }
  347. /**
  348. * 取得抢购信息
  349. * @param array $goods_info
  350. * @return array
  351. */
  352. private function _getGroupbuyInfo($goods_info = []) {
  353. if (!C('groupbuy_allow') || empty($goods_info) || !is_array($goods_info)) return $goods_info;
  354. $groupbuy_info = Model('groupbuy')->getGroupbuyInfoByGoodsCommonID($goods_info['goods_commonid']);
  355. if (empty($groupbuy_info)) return $goods_info;
  356. // 虚拟抢购数量限制
  357. if ($groupbuy_info['upper_limit'] > 0 && $groupbuy_info['upper_limit'] < $goods_info['virtual_limit']) {
  358. $goods_info['virtual_limit'] = $groupbuy_info['upper_limit'];
  359. }
  360. $goods_info['goods_price'] = $groupbuy_info['groupbuy_price'];
  361. $goods_info['groupbuy_id'] = $groupbuy_info['groupbuy_id'];
  362. $goods_info['ifgroupbuy'] = true;
  363. return $goods_info;
  364. }
  365. }