PresellOrderRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\store\order;
  12. use app\common\dao\store\order\PresellOrderDao;
  13. use app\common\model\store\order\PresellOrder;
  14. use app\common\model\user\User;
  15. use app\common\repositories\BaseRepository;
  16. use app\common\repositories\store\product\ProductPresellSkuRepository;
  17. use app\common\repositories\store\product\ProductRepository;
  18. use app\common\repositories\system\merchant\FinancialRecordRepository;
  19. use app\common\repositories\system\merchant\MerchantRepository;
  20. use app\common\repositories\user\UserBillRepository;
  21. use app\common\repositories\user\UserMerchantRepository;
  22. use app\common\repositories\user\UserRepository;
  23. use app\common\repositories\wechat\WechatUserRepository;
  24. use crmeb\services\AlipayService;
  25. use crmeb\services\MiniProgramService;
  26. use crmeb\services\WechatService;
  27. use think\exception\ValidateException;
  28. use think\facade\Cache;
  29. use think\facade\Db;
  30. /**
  31. * Class PresellOrderRepository
  32. * @package app\common\repositories\store\order
  33. * @author xaboy
  34. * @day 2020/10/27
  35. * @mixin PresellOrderDao
  36. */
  37. class PresellOrderRepository extends BaseRepository
  38. {
  39. public function __construct(PresellOrderDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * @return string
  45. * @author xaboy
  46. * @day 2020/6/9
  47. */
  48. public function getNewOrderId()
  49. {
  50. list($msec, $sec) = explode(' ', microtime());
  51. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  52. $orderId = 'ps' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  53. return $orderId;
  54. }
  55. public function createOrder($uid, $orderId, $price, $final_start_time, $final_end_time)
  56. {
  57. return $this->dao->create([
  58. 'uid' => $uid,
  59. 'order_id' => $orderId,
  60. 'final_start_time' => $final_start_time,
  61. 'final_end_time' => $final_end_time,
  62. 'pay_price' => $price,
  63. 'presell_order_sn' => $this->getNewOrderId()
  64. ]);
  65. }
  66. public function pay($type, User $user, PresellOrder $order, $return_url = '')
  67. {
  68. $method = 'pay' . ucfirst($type);
  69. if (!method_exists($this, $method))
  70. throw new ValidateException('不支持该支付方式');
  71. return $this->{$method}($user, $order, $return_url);
  72. }
  73. /**
  74. * @param User $user
  75. * @param PresellOrder $order
  76. * @return mixed
  77. * @author xaboy
  78. * @day 2020/6/9
  79. */
  80. public function payBalance(User $user, PresellOrder $order)
  81. {
  82. if (!systemConfig('yue_pay_status'))
  83. throw new ValidateException('未开启余额支付');
  84. if ($user['now_money'] < $order['pay_price'])
  85. throw new ValidateException('余额不足' . floatval($order['pay_price']));
  86. Db::transaction(function () use ($user, $order) {
  87. $user->now_money = bcsub($user->now_money, $order['pay_price'], 2);
  88. $user->save();
  89. $userBillRepository = app()->make(UserBillRepository::class);
  90. $userBillRepository->decBill($user['uid'], 'now_money', 'presell', [
  91. 'link_id' => $order['presell_order_id'],
  92. 'status' => 1,
  93. 'title' => '支付预售尾款',
  94. 'number' => $order['pay_price'],
  95. 'mark' => '余额支付支付' . floatval($order['pay_price']) . '元购买商品',
  96. 'balance' => $user->now_money
  97. ]);
  98. $this->paySuccess($order);
  99. });
  100. return app('json')->status('success', '余额支付成功', ['order_id' => $order['presell_order_id']]);
  101. }
  102. /**
  103. * @param User $user
  104. * @param PresellOrder $groupOrder
  105. * @return \think\response\Json
  106. * @author xaboy
  107. * @day 2020/10/28
  108. */
  109. public function payWeixin(User $user, PresellOrder $groupOrder)
  110. {
  111. $wechatUserRepository = app()->make(WechatUserRepository::class);
  112. $openId = $wechatUserRepository->idByOpenId($user['wechat_user_id']);
  113. if (!$openId)
  114. new ValidateException('请关联微信公众号!');
  115. $config = WechatService::create()->jsPay($openId, $groupOrder['presell_order_sn'], $groupOrder['pay_price'], 'presell', '尾款支付');
  116. return app('json')->status('weixin', ['config' => $config, 'order_id' => $groupOrder['presell_order_id']]);
  117. }
  118. /**
  119. * @param User $user
  120. * @param PresellOrder $groupOrder
  121. * @return \think\response\Json
  122. * @author xaboy
  123. * @day 2020/6/9
  124. */
  125. public function payRoutine(User $user, PresellOrder $groupOrder)
  126. {
  127. $wechatUserRepository = app()->make(WechatUserRepository::class);
  128. $openId = $wechatUserRepository->idByRoutineId($user['wechat_user_id']);
  129. if (!$openId)
  130. new ValidateException('请关联微信小程序!');
  131. $config = MiniProgramService::create()->jsPay($openId, $groupOrder['presell_order_sn'], $groupOrder['pay_price'], 'presell', '尾款支付');
  132. return app('json')->status('routine', ['config' => $config, 'order_id' => $groupOrder['presell_order_id']]);
  133. }
  134. /**
  135. * @param User $user
  136. * @param PresellOrder $groupOrder
  137. * @return mixed
  138. * @author xaboy
  139. * @day 2020/6/9
  140. */
  141. public function payH5(User $user, PresellOrder $groupOrder)
  142. {
  143. $config = WechatService::create()->paymentPrepare(null, $groupOrder['presell_order_sn'], $groupOrder['pay_price'], 'presell', '尾款支付', '', 'MWEB');
  144. return app('json')->status('h5', ['config' => $config, 'order_id' => $groupOrder['presell_order_id']]);
  145. }
  146. /**
  147. * @param User $user
  148. * @param PresellOrder $groupOrder
  149. * @param $return_url
  150. * @return \think\response\Json
  151. * @author xaboy
  152. * @day 2020/10/22
  153. */
  154. public function payAlipay(User $user, PresellOrder $groupOrder, $return_url)
  155. {
  156. $url = AlipayService::create('presell')->wapPaymentPrepare($groupOrder['presell_order_sn'], $groupOrder['pay_price'], '尾款支付', $return_url);
  157. $pay_key = md5($url);
  158. Cache::store('file')->set('pay_key' . $pay_key, $url, 3600);
  159. return app('json')->status('alipay', ['config' => $url, 'pay_key' => $pay_key, 'order_id' => $groupOrder['presell_order_id']]);
  160. }
  161. /**
  162. * @param User $user
  163. * @param PresellOrder $groupOrder
  164. * @return \think\response\Json
  165. * @author xaboy
  166. * @day 2020/10/22
  167. */
  168. public function payAlipayQr(User $user, PresellOrder $groupOrder)
  169. {
  170. $url = AlipayService::create('presell')->qrPaymentPrepare($groupOrder['presell_order_sn'], $groupOrder['pay_price'], '尾款支付');
  171. return app('json')->status('alipayQr', ['config' => $url, 'order_id' => $groupOrder['presell_order_id']]);
  172. }
  173. public function paySuccess(PresellOrder $order)
  174. {
  175. Db::transaction(function () use ($order) {
  176. $time = date('Y-m-d H:i:s');
  177. $order->paid = 1;
  178. $order->pay_time = $time;
  179. $order->order->status = 0;
  180. if ($order->order->order_type == 1) {
  181. $order->order->verify_code = app()->make(StoreOrderRepository::class)->verifyCode();
  182. }
  183. $order->order->save();
  184. $order->save();
  185. $orderStatus = [
  186. 'order_id' => $order->order_id,
  187. 'change_message' => '订单尾款支付成功',
  188. 'change_type' => 'presell'
  189. ];
  190. $i = 1;
  191. $finance = [];
  192. $pay_price = bcadd($order->order->pay_price, $order->pay_price, 2);
  193. $sn = app()->make(FinancialRecordRepository::class)->getSn();
  194. $finance[] = [
  195. 'order_id' => $order->order_id,
  196. 'order_sn' => $order->presell_order_sn,
  197. 'user_info' => $order->user->nickname,
  198. 'user_id' => $order->uid,
  199. 'financial_type' => 'presell',
  200. 'financial_pm' => 1,
  201. 'type' => 2,
  202. 'number' => $order->pay_price,
  203. 'mer_id' => $order->mer_id,
  204. 'financial_record_sn' => $sn . ($i++)
  205. ];
  206. $finance[] = [
  207. 'order_id' => $order->order->order_id,
  208. 'order_sn' => $order->order->order_sn,
  209. 'user_info' => $order->user->nickname,
  210. 'user_id' => $order->uid,
  211. 'financial_type' => 'mer_presell',
  212. 'financial_pm' => 1,
  213. 'type' => 0,
  214. 'number' => $pay_price,
  215. 'mer_id' => $order->mer_id,
  216. 'financial_record_sn' => $sn . ($i++)
  217. ];
  218. $pay_price = bcsub($pay_price, bcadd($order->order['extension_one'], $order->order['extension_two'], 3), 2);
  219. if ($order->order['extension_one'] > 0) {
  220. $finance[] = [
  221. 'order_id' => $order->order->order_id,
  222. 'order_sn' => $order->order->order_sn,
  223. 'user_info' => $order->user->nickname,
  224. 'user_id' => $order->uid,
  225. 'financial_type' => 'brokerage_one',
  226. 'financial_pm' => 0,
  227. 'type' => 1,
  228. 'number' => $order->order['extension_one'],
  229. 'mer_id' => $order->mer_id,
  230. 'financial_record_sn' => $sn . ($i++)
  231. ];
  232. }
  233. if ($order->order['extension_two'] > 0) {
  234. $finance[] = [
  235. 'order_id' => $order->order->order_id,
  236. 'order_sn' => $order->order->order_sn,
  237. 'user_info' => $order->user->nickname,
  238. 'user_id' => $order->uid,
  239. 'financial_type' => 'brokerage_two',
  240. 'financial_pm' => 0,
  241. 'type' => 1,
  242. 'number' => $order->order['extension_two'],
  243. 'mer_id' => $order->mer_id,
  244. 'financial_record_sn' => $sn . ($i++)
  245. ];
  246. }
  247. if ($order->order->commission_rate > 0) {
  248. $commission_rate = ($order->order->commission_rate / 100);
  249. $ratePrice = bcmul($pay_price, $commission_rate, 2);
  250. $finance[] = [
  251. 'order_id' => $order->order->order_id,
  252. 'order_sn' => $order->order->order_sn,
  253. 'user_info' => $order->user->nickname,
  254. 'user_id' => $order->uid,
  255. 'financial_type' => 'presell_charge',
  256. 'financial_pm' => 1,
  257. 'type' => 1,
  258. 'number' => $ratePrice,
  259. 'mer_id' => $order->mer_id,
  260. 'financial_record_sn' => $sn . ($i++)
  261. ];
  262. $pay_price = bcsub($pay_price, $ratePrice, 2);
  263. }
  264. $finance[] = [
  265. 'order_id' => $order->order->order_id,
  266. 'order_sn' => $order->order->order_sn,
  267. 'user_info' => $order->user->nickname,
  268. 'user_id' => $order->uid,
  269. 'financial_type' => 'presell_true',
  270. 'financial_pm' => 1,
  271. 'type' => 2,
  272. 'number' => $pay_price,
  273. 'mer_id' => $order->mer_id,
  274. 'financial_record_sn' => $sn . ($i++)
  275. ];
  276. app()->make(MerchantRepository::class)->addMoney($order->mer_id, $pay_price);
  277. app()->make(UserRepository::class)->update($order->uid, [
  278. 'pay_price' => Db::raw('pay_price+' . $order->pay_price),
  279. ]);
  280. app()->make(ProductPresellSkuRepository::class)->incCount($order->order->orderProduct[0]['activity_id'], $order->order->orderProduct[0]['product_sku'], 'two_pay');
  281. app()->make(UserMerchantRepository::class)->updatePayTime($order->uid, $order->mer_id, $order->pay_price, false);
  282. app()->make(FinancialRecordRepository::class)->insertAll($finance);
  283. app()->make(StoreOrderStatusRepository::class)->create($orderStatus);
  284. });
  285. }
  286. public function cancel($id)
  287. {
  288. $order = $this->dao->getWhere(['presell_order_id' => $id, 'paid' => 0]);
  289. if (!$order) return;
  290. $orderStatus = [
  291. 'order_id' => $order->order_id,
  292. 'change_message' => '预售订单超时支付自动关闭',
  293. 'change_type' => 'presell_close'
  294. ];
  295. $productRepository = app()->make(ProductRepository::class);
  296. Db::transaction(function () use ($productRepository, $order, $orderStatus) {
  297. app()->make(StoreOrderStatusRepository::class)->create($orderStatus);
  298. $order->order->status = 11;
  299. $order->status = 0;
  300. $order->save();
  301. $order->order->save();
  302. foreach ($order->order->orderProduct as $cart) {
  303. $productRepository->orderProductIncStock($cart);
  304. }
  305. });
  306. }
  307. }