OrderRepository.php 10 KB

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