OrderRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\UserBill;
  6. use app\models\user\WechatUser;
  7. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  8. use app\models\store\StoreOrderBatch;
  9. use crmeb\services\MiniProgramService;
  10. use crmeb\services\WechatService;
  11. /**
  12. * Class OrderRepository
  13. * @package crmeb\repositories
  14. */
  15. class OrderRepository
  16. {
  17. /**
  18. * TODO 小程序JS支付
  19. * @param $orderId
  20. * @param string $field
  21. * @return array|string
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public static function jsPay($orderId, $field = 'order_id')
  27. {
  28. if (is_string($orderId))
  29. $orderInfo = StoreOrder::where($field, $orderId)->find();
  30. else
  31. $orderInfo = $orderId;
  32. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  33. if ($orderInfo['paid']) exception('支付已支付!');
  34. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  35. $openid = WechatUser::getOpenId($orderInfo['uid']);
  36. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  37. $site_name = sys_config('site_name');
  38. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  39. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  40. }
  41. /**
  42. * TODO 小程序JS支付
  43. * @param $orderId
  44. * @param string $field
  45. * @return array|string
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public static function jsPayBatch($porderId, $field = 'porder_id')
  51. {
  52. if (is_string($porderId))
  53. $porderInfo = StoreOrderBatch::where($field, $porderId)->find();
  54. else
  55. $porderInfo = $porderId;
  56. $subOrders = StoreOrderBatch::getAllSubOrders($porderId);
  57. if (!$subOrders) exception('支付订单不存在!');
  58. $fieldValues = StoreOrderBatch::sumFields($subOrders, ['paid', 'pay_price']);
  59. if ($fieldValues['paid']) exception('支付已支付!');
  60. if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
  61. $openid = WechatUser::getOpenId($porderInfo['uid']);
  62. $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
  63. $site_name = sys_config('site_name');
  64. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  65. return MiniProgramService::jsPay($openid, $porderInfo['porder_id'], $fieldValues['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  66. }
  67. /**
  68. * 微信公众号JS支付
  69. * @param $orderId
  70. * @param string $field
  71. * @return array|string
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function wxPay($orderId, $field = 'order_id')
  77. {
  78. if (is_string($orderId))
  79. $orderInfo = StoreOrder::where($field, $orderId)->find();
  80. else
  81. $orderInfo = $orderId;
  82. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  83. if ($orderInfo['paid']) exception('支付已支付!');
  84. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  85. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  86. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  87. $site_name = sys_config('site_name');
  88. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  89. return WechatService::jsPay($openid, $orderInfo['porder_id'], $orderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  90. }
  91. /**
  92. * 微信公众号JS支付
  93. * @param $orderId
  94. * @param string $field
  95. * @return array|string
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public static function wxPayBatch($porderId, $field = 'porder_id')
  101. {
  102. if (is_string($porderId))
  103. $porderInfo = StoreOrderBatch::where($field, $porderId)->find();
  104. else
  105. $porderInfo = $porderId;
  106. $subOrders = StoreOrderBatch::getAllSubOrders($porderId);
  107. if (!$subOrders) exception('支付订单不存在!');
  108. $fieldValues = StoreOrderBatch::sumFields($subOrders, ['paid', 'pay_price']);
  109. if ($fieldValues['paid']) exception('支付已支付!');
  110. if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
  111. $openid = WechatUser::uidToOpenid($porderInfo['uid'], 'openid');
  112. $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
  113. $site_name = sys_config('site_name');
  114. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  115. return WechatService::jsPay($openid, $porderInfo['porder_id'], $porderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  116. }
  117. /**
  118. * 微信h5支付
  119. * @param $orderId
  120. * @param string $field
  121. * @return array|string
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. * @throws \think\exception\DbException
  125. */
  126. public static function h5Pay($orderId, $field = 'order_id')
  127. {
  128. if (is_string($orderId))
  129. $orderInfo = StoreOrder::where($field, $orderId)->find();
  130. else
  131. $orderInfo = $orderId;
  132. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  133. if ($orderInfo['paid']) exception('支付已支付!');
  134. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  135. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  136. $site_name = sys_config('site_name');
  137. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  138. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  139. }
  140. /**
  141. * 微信h5支付
  142. * @param $orderId
  143. * @param string $field
  144. * @return array|string
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. */
  149. public static function h5PayBatch($porderId, $field = 'porder_id')
  150. {
  151. if (is_string($porderId))
  152. $porderInfo = StoreOrderBatch::where($field, $porderId)->find();
  153. else
  154. $porderInfo = $porderId;
  155. $subOrders = StoreOrderBatch::getAllSubOrders($porderId);
  156. if (!$subOrders) exception('支付订单不存在!');
  157. $fieldValues = StoreOrderBatch::sumFields($subOrders, ['paid', 'pay_price']);
  158. if ($fieldValues['paid']) exception('支付已支付!');
  159. if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
  160. $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
  161. $site_name = sys_config('site_name');
  162. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  163. return WechatService::paymentPrepare(null, $porderInfo['porder_id'], $porderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  164. }
  165. /**
  166. * 微信 app 支付
  167. * @param $orderId
  168. * @param string $field
  169. * @return array|string
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public static function wxAppPay($orderId, $field = 'order_id')
  175. {
  176. if (is_string($orderId))
  177. $orderInfo = StoreOrder::where($field, $orderId)->find();
  178. else
  179. $orderInfo = $orderId;
  180. if (!$orderInfo || !isset($orderInfo['paid'])) {
  181. exception('支付订单不存在!');
  182. }
  183. if ($orderInfo['paid']) exception('订单已支付');
  184. if ($orderInfo['pay_price'] <= 0) exception('该订单无需支付');
  185. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  186. $site_name = sys_config('site_name');
  187. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  188. return WechatService::appPay(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  189. }
  190. /**
  191. * 微信 app 支付
  192. * @param $orderId
  193. * @param string $field
  194. * @return array|string
  195. * @throws \think\db\exception\DataNotFoundException
  196. * @throws \think\db\exception\ModelNotFoundException
  197. * @throws \think\exception\DbException
  198. */
  199. public static function wxAppPayBatch($porderId, $field = 'porder_id')
  200. {
  201. if (is_string($porderId))
  202. $porderInfo = StoreOrderBatch::where($field, $porderId)->find();
  203. else
  204. $porderInfo = $porderId;
  205. $subOrders = StoreOrderBatch::getAllSubOrders($porderId);
  206. if (!$subOrders) exception('支付订单不存在!');
  207. $fieldValues = StoreOrderBatch::sumFields($subOrders, ['paid', 'pay_price']);
  208. if ($fieldValues['paid']) exception('支付已支付!');
  209. if ($fieldValues['pay_price'] <= 0) exception('该支付无需支付!');
  210. $bodyContent = StoreOrderBatch::getProductTitleBatch($subOrders);
  211. $site_name = sys_config('site_name');
  212. if (!$bodyContent && !$site_name) exception('缺少支付参数');
  213. return WechatService::appPay(null, $porderInfo['porder_id'], $porderInfo['total_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  214. }
  215. /**
  216. * 用户确认收货
  217. * @param $order
  218. * @param $uid
  219. * @throws \think\Exception
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\ModelNotFoundException
  222. * @throws \think\exception\DbException
  223. */
  224. public static function storeProductOrderUserTakeDelivery($order, $uid)
  225. {
  226. $res1 = StoreOrder::gainUserIntegral($order);
  227. $res2 = User::backOrderBrokerage($order);
  228. StoreOrder::orderTakeAfter($order);
  229. //满赠优惠券
  230. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  231. if (!($res1 && $res2)) exception('收货失败!');
  232. }
  233. /**
  234. * 修改状态 为已收货 admin模块
  235. * @param $order
  236. * @throws \Exception
  237. */
  238. public static function storeProductOrderTakeDeliveryAdmin($order)
  239. {
  240. $res1 = AdminStoreOrder::gainUserIntegral($order);
  241. $res2 = User::backOrderBrokerage($order);
  242. AdminStoreOrder::orderTakeAfter($order);
  243. //满赠优惠券
  244. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  245. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  246. if (!($res1 && $res2)) exception('收货失败!');
  247. }
  248. /**
  249. * 修改状态 为已收货 定时任务使用
  250. * @param $order
  251. * @throws \Exception
  252. */
  253. public static function storeProductOrderTakeDeliveryTimer($order)
  254. {
  255. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  256. $res2 = User::backOrderBrokerage($order, false);
  257. AdminStoreOrder::orderTakeAfter($order);
  258. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  259. if (!($res1 && $res2)) exception('收货失败!');
  260. }
  261. /**
  262. * 修改状态为 已退款 admin模块
  263. * @param $data
  264. * @param $oid
  265. * @return bool|mixed
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. * @throws \think\exception\DbException
  269. */
  270. public static function storeProductOrderRefundY($data, $oid)
  271. {
  272. $order = AdminStoreOrder::where('id', $oid)->find();
  273. if ($order['is_channel'] == 1)
  274. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  275. else
  276. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  277. }
  278. /**
  279. * TODO 后台余额退款
  280. * @param $product
  281. * @param $refund_data
  282. * @throws \Exception
  283. */
  284. public static function storeOrderYueRefund($product, $refund_data)
  285. {
  286. $res = AdminStoreOrder::integralBack($product['id']);
  287. if (!$res) exception('退积分失败!');
  288. }
  289. /**
  290. * 订单退积分
  291. * @param $product $product 商品信息
  292. * @param $back_integral $back_integral 退多少积分
  293. */
  294. public static function storeOrderIntegralBack($product, $back_integral)
  295. {
  296. }
  297. }