buy_virtual.logic.php 16 KB

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