OrderRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 = 'order_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. * 用户确认收货
  167. * @param $order
  168. * @param $uid
  169. * @throws \think\Exception
  170. * @throws \think\db\exception\DataNotFoundException
  171. * @throws \think\db\exception\ModelNotFoundException
  172. * @throws \think\exception\DbException
  173. */
  174. public static function storeProductOrderUserTakeDelivery($order, $uid)
  175. {
  176. $res1 = StoreOrder::gainUserIntegral($order);
  177. $res2 = User::backOrderBrokerage($order);
  178. StoreOrder::orderTakeAfter($order);
  179. //满赠优惠券
  180. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  181. if (!($res1 && $res2)) exception('收货失败!');
  182. }
  183. /**
  184. * 修改状态 为已收货 admin模块
  185. * @param $order
  186. * @throws \Exception
  187. */
  188. public static function storeProductOrderTakeDeliveryAdmin($order)
  189. {
  190. $res1 = AdminStoreOrder::gainUserIntegral($order);
  191. $res2 = User::backOrderBrokerage($order);
  192. AdminStoreOrder::orderTakeAfter($order);
  193. //满赠优惠券
  194. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  195. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  196. if (!($res1 && $res2)) exception('收货失败!');
  197. }
  198. /**
  199. * 修改状态 为已收货 定时任务使用
  200. * @param $order
  201. * @throws \Exception
  202. */
  203. public static function storeProductOrderTakeDeliveryTimer($order)
  204. {
  205. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  206. $res2 = User::backOrderBrokerage($order, false);
  207. AdminStoreOrder::orderTakeAfter($order);
  208. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  209. if (!($res1 && $res2)) exception('收货失败!');
  210. }
  211. /**
  212. * 修改状态为 已退款 admin模块
  213. * @param $data
  214. * @param $oid
  215. * @return bool|mixed
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. * @throws \think\exception\DbException
  219. */
  220. public static function storeProductOrderRefundY($data, $oid)
  221. {
  222. $order = AdminStoreOrder::where('id', $oid)->find();
  223. if ($order['is_channel'] == 1)
  224. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  225. else
  226. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  227. }
  228. /**
  229. * TODO 后台余额退款
  230. * @param $product
  231. * @param $refund_data
  232. * @throws \Exception
  233. */
  234. public static function storeOrderYueRefund($product, $refund_data)
  235. {
  236. $res = AdminStoreOrder::integralBack($product['id']);
  237. if (!$res) exception('退积分失败!');
  238. }
  239. /**
  240. * 订单退积分
  241. * @param $product $product 商品信息
  242. * @param $back_integral $back_integral 退多少积分
  243. */
  244. public static function storeOrderIntegralBack($product, $back_integral)
  245. {
  246. }
  247. }