OrderRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\trade\CashTradeOrder;
  5. use app\models\trade\CashTradeOrderPayment;
  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\db\exception\DbException
  96. */
  97. public static function tradeJsPay($orderId, $field = 'order_id')
  98. {
  99. if (is_string($orderId))
  100. $orderInfo = CashTradeOrderPayment::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_money'] <= 0) exception('该支付无需支付!');
  106. $openid = WechatUser::getOpenId($orderInfo['uid']);
  107. $bodyContent = $orderInfo['order_type'] == 1 ? '挂买' : '购买' . CashTradeOrder::get($orderInfo['bind_order']);
  108. $site_name = sys_config('site_name');
  109. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  110. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_money'], 'trade', 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\db\exception\DbException
  120. */
  121. public static function tradeWxPay($orderId, $field = 'order_id')
  122. {
  123. if (is_string($orderId))
  124. $orderInfo = CashTradeOrderPayment::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_money'] <= 0) exception('该支付无需支付!');
  130. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  131. $bodyContent = $orderInfo['order_type'] == 1 ? '挂买' : '购买' . CashTradeOrder::get($orderInfo['bind_order']);
  132. $site_name = sys_config('site_name');
  133. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  134. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_money'], 'trade', 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\db\exception\DbException
  144. */
  145. public static function tradeH5Pay($orderId, $field = 'order_id')
  146. {
  147. if (is_string($orderId))
  148. $orderInfo = CashTradeOrderPayment::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_money'] <= 0) exception('该支付无需支付!');
  154. $bodyContent = $orderInfo['order_type'] == 1 ? '挂买' : '购买' . CashTradeOrder::get($orderInfo['bind_order']);
  155. $site_name = sys_config('site_name');
  156. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  157. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_money'], 'trade', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  158. }
  159. /**
  160. * 用户确认收货
  161. * @param $order
  162. * @param $uid
  163. * @throws \think\Exception
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\ModelNotFoundException
  166. * @throws \think\exception\DbException
  167. * @throws \think\db\exception\DbException
  168. * @throws \Exception
  169. */
  170. public static function storeProductOrderUserTakeDelivery($order, $uid)
  171. {
  172. $res1 = StoreOrder::gainUserIntegral($order);
  173. // $res2 = User::backOrderBrokerage($order);
  174. $res2 = User::sendBackOrderBrokerage($order);
  175. StoreOrder::orderTakeAfter($order);
  176. //满赠优惠券
  177. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  178. if (!($res1 && $res2)) exception('收货失败!');
  179. }
  180. /**
  181. * 修改状态 为已收货 admin模块
  182. * @param $order
  183. * @throws \Exception
  184. */
  185. public static function storeProductOrderTakeDeliveryAdmin($order)
  186. {
  187. $res1 = AdminStoreOrder::gainUserIntegral($order);
  188. // $res2 = User::backOrderBrokerage($order);
  189. $res2 = User::sendBackOrderBrokerage($order);
  190. AdminStoreOrder::orderTakeAfter($order);
  191. if (!($res1 && $res2)) exception('收货失败!');
  192. }
  193. /**
  194. * 修改状态 为已收货 定时任务使用
  195. * @param $order
  196. * @throws \Exception
  197. */
  198. public static function storeProductOrderTakeDeliveryTimer($order)
  199. {
  200. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  201. $res2 = User::sendBackOrderBrokerage($order);
  202. AdminStoreOrder::orderTakeAfter($order);
  203. if (!($res1 && $res2)) exception('收货失败!');
  204. }
  205. /**
  206. * 修改状态为 已退款 admin模块
  207. * @param $data
  208. * @param $oid
  209. * @return bool|mixed
  210. * @throws \think\db\exception\DataNotFoundException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. * @throws \think\exception\DbException
  213. */
  214. public static function storeProductOrderRefundY($data, $oid)
  215. {
  216. $order = AdminStoreOrder::where('id', $oid)->find();
  217. if ($order['is_channel'] == 1)
  218. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  219. else
  220. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  221. }
  222. /**
  223. * TODO 后台余额退款
  224. * @param $product
  225. * @param $refund_data
  226. * @throws \Exception
  227. */
  228. public static function storeOrderYueRefund($product, $refund_data)
  229. {
  230. $res = AdminStoreOrder::integralBack($product['id']);
  231. if (!$res) exception('退积分失败!');
  232. }
  233. /**
  234. * 订单退积分
  235. * @param $product $product 商品信息
  236. * @param $back_integral $back_integral 退多少积分
  237. */
  238. public static function storeOrderIntegralBack($product, $back_integral)
  239. {
  240. }
  241. }