buy_virtual.logic.php 16 KB

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