OrderRepository.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\admin\model\vote\VoteOrder;
  4. use app\models\member\MemberCheck;
  5. use app\models\store\StoreOrder;
  6. use app\models\user\User;
  7. use app\models\user\WechatUser;
  8. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  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. * 微信公众号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 wxPay($orderId, $field = 'order_id')
  51. {
  52. if (is_string($orderId))
  53. $orderInfo = StoreOrder::where($field, $orderId)->find();
  54. else
  55. $orderInfo = $orderId;
  56. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  57. if ($orderInfo['paid']) exception('支付已支付!');
  58. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  59. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  60. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  61. $site_name = sys_config('site_name');
  62. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  63. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  64. }
  65. /**
  66. * 微信h5支付
  67. * @param $orderId
  68. * @param string $field
  69. * @return array|string
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @throws \think\exception\DbException
  73. */
  74. public static function h5Pay($orderId, $field = 'order_id')
  75. {
  76. if (is_string($orderId))
  77. $orderInfo = StoreOrder::where($field, $orderId)->find();
  78. else
  79. $orderInfo = $orderId;
  80. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  81. if ($orderInfo['paid']) exception('支付已支付!');
  82. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  83. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  84. $site_name = sys_config('site_name');
  85. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  86. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  87. }
  88. /**
  89. * TODO 小程序JS支付
  90. * @param $orderId
  91. * @param string $field
  92. * @return array|string
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. */
  97. public static function jsVotePay($orderId, $field = 'order_id')
  98. {
  99. if (is_string($orderId))
  100. $orderInfo = VoteOrder::where($field, $orderId)->find();
  101. else
  102. $orderInfo = $orderId;
  103. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  104. if ($orderInfo['paid']) exception('支付已支付!');
  105. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  106. $openid = WechatUser::getOpenId($orderInfo['uid']);
  107. $bodyContent = '参与投票';
  108. $site_name = sys_config('site_name');
  109. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  110. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'vote', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  111. }
  112. /**
  113. * 微信公众号JS支付
  114. * @param $orderId
  115. * @param string $field
  116. * @return array|string
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @throws \think\exception\DbException
  120. */
  121. public static function wxVotePay($orderId, $field = 'order_id')
  122. {
  123. if (is_string($orderId))
  124. $orderInfo = VoteOrder::where($field, $orderId)->find();
  125. else
  126. $orderInfo = $orderId;
  127. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  128. if ($orderInfo['paid']) exception('支付已支付!');
  129. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  130. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  131. $bodyContent = '参与投票';
  132. $site_name = sys_config('site_name');
  133. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  134. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'vote', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  135. }
  136. /**
  137. * 微信h5支付
  138. * @param $orderId
  139. * @param string $field
  140. * @return array|string
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\ModelNotFoundException
  143. * @throws \think\exception\DbException
  144. */
  145. public static function h5VotePay($orderId, $field = 'order_id')
  146. {
  147. if (is_string($orderId))
  148. $orderInfo = VoteOrder::where($field, $orderId)->find();
  149. else
  150. $orderInfo = $orderId;
  151. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  152. if ($orderInfo['paid']) exception('支付已支付!');
  153. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  154. $bodyContent = '参与投票';
  155. $site_name = sys_config('site_name');
  156. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  157. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'vote', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  158. }
  159. /**
  160. * TODO 小程序JS支付
  161. * @param $orderId
  162. * @param string $field
  163. * @return array|string
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. */
  168. public static function jsMemberPay($orderId, $field = 'order_id')
  169. {
  170. if (is_string($orderId))
  171. $orderInfo = MemberCheck::where($field, $orderId)->find();
  172. else
  173. $orderInfo = $orderId;
  174. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  175. if ($orderInfo['paid']) exception('支付已支付!');
  176. if ($orderInfo['pay_money'] <= 0) exception('该支付无需支付!');
  177. $openid = WechatUser::getOpenId($orderInfo['uid']);
  178. $bodyContent = '申请会员';
  179. $site_name = sys_config('site_name');
  180. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  181. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_money'], 'member', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  182. }
  183. /**
  184. * 微信公众号JS支付
  185. * @param $orderId
  186. * @param string $field
  187. * @return array|string
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. * @throws \think\exception\DbException
  191. */
  192. public static function wxMemberPay($orderId, $field = 'order_id')
  193. {
  194. if (is_string($orderId))
  195. $orderInfo = MemberCheck::where($field, $orderId)->find();
  196. else
  197. $orderInfo = $orderId;
  198. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  199. if ($orderInfo['paid']) exception('支付已支付!');
  200. if ($orderInfo['pay_money'] <= 0) exception('该支付无需支付!');
  201. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  202. $bodyContent = '申请会员';
  203. $site_name = sys_config('site_name');
  204. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  205. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_money'], 'member', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  206. }
  207. /**
  208. * 微信h5支付
  209. * @param $orderId
  210. * @param string $field
  211. * @return array|string
  212. * @throws \think\db\exception\DataNotFoundException
  213. * @throws \think\db\exception\ModelNotFoundException
  214. * @throws \think\exception\DbException
  215. */
  216. public static function h5MemberPay($orderId, $field = 'order_id')
  217. {
  218. if (is_string($orderId))
  219. $orderInfo = MemberCheck::where($field, $orderId)->find();
  220. else
  221. $orderInfo = $orderId;
  222. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  223. if ($orderInfo['paid']) exception('支付已支付!');
  224. if ($orderInfo['pay_money'] <= 0) exception('该支付无需支付!');
  225. $bodyContent = '参与投票';
  226. $site_name = sys_config('site_name');
  227. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  228. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_money'], 'member', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  229. }
  230. /**
  231. * 用户确认收货
  232. * @param $order
  233. * @param $uid
  234. * @throws \think\Exception
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. * @throws \think\exception\DbException
  238. * @throws \think\db\exception\DbException
  239. * @throws \Exception
  240. */
  241. public static function storeProductOrderUserTakeDelivery($order, $uid)
  242. {
  243. $res1 = StoreOrder::gainUserIntegral($order);
  244. // $res2 = User::backOrderBrokerage($order);
  245. $res2 = User::sendBackOrderBrokerage($order);
  246. StoreOrder::orderTakeAfter($order);
  247. //满赠优惠券
  248. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  249. if (!($res1 && $res2)) exception('收货失败!');
  250. }
  251. /**
  252. * 修改状态 为已收货 admin模块
  253. * @param $order
  254. * @throws \Exception
  255. */
  256. public static function storeProductOrderTakeDeliveryAdmin($order)
  257. {
  258. $res1 = AdminStoreOrder::gainUserIntegral($order);
  259. // $res2 = User::backOrderBrokerage($order);
  260. $res2 = User::sendBackOrderBrokerage($order);
  261. AdminStoreOrder::orderTakeAfter($order);
  262. if (!($res1 && $res2)) exception('收货失败!');
  263. }
  264. /**
  265. * 修改状态 为已收货 定时任务使用
  266. * @param $order
  267. * @throws \Exception
  268. */
  269. public static function storeProductOrderTakeDeliveryTimer($order)
  270. {
  271. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  272. $res2 = User::sendBackOrderBrokerage($order);
  273. AdminStoreOrder::orderTakeAfter($order);
  274. if (!($res1 && $res2)) exception('收货失败!');
  275. }
  276. /**
  277. * 修改状态为 已退款 admin模块
  278. * @param $data
  279. * @param $oid
  280. * @return bool|mixed
  281. * @throws \think\db\exception\DataNotFoundException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. * @throws \think\exception\DbException
  284. */
  285. public static function storeProductOrderRefundY($data, $oid)
  286. {
  287. $order = AdminStoreOrder::where('id', $oid)->find();
  288. if ($order['is_channel'] == 1)
  289. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  290. else
  291. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  292. }
  293. /**
  294. * TODO 后台余额退款
  295. * @param $product
  296. * @param $refund_data
  297. * @throws \Exception
  298. */
  299. public static function storeOrderYueRefund($product, $refund_data)
  300. {
  301. $res = AdminStoreOrder::integralBack($product['id']);
  302. if (!$res) exception('退积分失败!');
  303. }
  304. /**
  305. * 订单退积分
  306. * @param $product $product 商品信息
  307. * @param $back_integral $back_integral 退多少积分
  308. */
  309. public static function storeOrderIntegralBack($product, $back_integral)
  310. {
  311. }
  312. }