OrderRepository.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 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. * 用户确认收货
  89. * @param $order
  90. * @param $uid
  91. * @throws \think\Exception
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \think\exception\DbException
  95. */
  96. public static function storeProductOrderUserTakeDelivery($order, $uid)
  97. {
  98. $res1 = StoreOrder::gainUserIntegral($order);
  99. $res2 = User::backOrderBrokerage($order);
  100. StoreOrder::orderTakeAfter($order);
  101. //满赠优惠券
  102. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  103. if (!($res1 && $res2)) exception('收货失败!');
  104. }
  105. /**
  106. * 修改状态 为已收货 admin模块
  107. * @param $order
  108. * @throws \Exception
  109. */
  110. public static function storeProductOrderTakeDeliveryAdmin($order)
  111. {
  112. $res1 = AdminStoreOrder::gainUserIntegral($order);
  113. $res2 = User::backOrderBrokerage($order);
  114. AdminStoreOrder::orderTakeAfter($order);
  115. //满赠优惠券
  116. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  117. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  118. if (!($res1 && $res2)) exception('收货失败!');
  119. }
  120. /**
  121. * 修改状态 为已收货 定时任务使用
  122. * @param $order
  123. * @throws \Exception
  124. */
  125. public static function storeProductOrderTakeDeliveryTimer($order)
  126. {
  127. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  128. $res2 = User::backOrderBrokerage($order, false);
  129. AdminStoreOrder::orderTakeAfter($order);
  130. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  131. if (!($res1 && $res2)) exception('收货失败!');
  132. }
  133. /**
  134. * 修改状态为 已退款 admin模块
  135. * @param $data
  136. * @param $oid
  137. * @return bool|mixed
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\ModelNotFoundException
  140. * @throws \think\exception\DbException
  141. */
  142. public static function storeProductOrderRefundY($data, $oid)
  143. {
  144. $order = AdminStoreOrder::where('id', $oid)->find();
  145. if ($order['is_channel'] == 1)
  146. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  147. else
  148. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  149. }
  150. /**
  151. * TODO 后台余额退款
  152. * @param $product
  153. * @param $refund_data
  154. * @throws \Exception
  155. */
  156. public static function storeOrderYueRefund($product, $refund_data)
  157. {
  158. $res = AdminStoreOrder::integralBack($product['id']);
  159. if (!$res) exception('退积分失败!');
  160. }
  161. /**
  162. * 订单退积分
  163. * @param $product $product 商品信息
  164. * @param $back_integral $back_integral 退多少积分
  165. */
  166. public static function storeOrderIntegralBack($product, $back_integral)
  167. {
  168. }
  169. }