OrderRepository.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\admin\model\diagnosis\DiagnosisOrder;
  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. * TODO 小程序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 jsDiaPay($orderId, $field = 'order_id')
  50. {
  51. if (is_string($orderId))
  52. $orderInfo = DiagnosisOrder::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::getOpenId($orderInfo['uid']);
  59. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  60. $site_name = sys_config('site_name');
  61. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  62. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'diagnosis', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  63. }
  64. /**
  65. * 微信公众号JS支付
  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 wxDiaPay($orderId, $field = 'order_id')
  74. {
  75. if (is_string($orderId))
  76. $orderInfo = DiagnosisOrder::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. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  83. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  84. $site_name = sys_config('site_name');
  85. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  86. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'diagnosis', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  87. }
  88. /**
  89. * 微信公众号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 wxPay($orderId, $field = 'order_id')
  98. {
  99. if (is_string($orderId))
  100. $orderInfo = StoreOrder::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::uidToOpenid($orderInfo['uid'], 'openid');
  107. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  108. $site_name = sys_config('site_name');
  109. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  110. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  111. }
  112. /**
  113. * 微信h5支付
  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 h5Pay($orderId, $field = 'order_id')
  122. {
  123. if (is_string($orderId))
  124. $orderInfo = StoreOrder::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. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  131. $site_name = sys_config('site_name');
  132. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  133. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  134. }
  135. /**
  136. * 用户确认收货
  137. * @param $order
  138. * @param $uid
  139. * @throws \think\Exception
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. * @throws \think\exception\DbException
  143. * @throws \think\db\exception\DbException
  144. * @throws \Exception
  145. */
  146. public static function storeProductOrderUserTakeDelivery($order, $uid)
  147. {
  148. $res1 = StoreOrder::gainUserIntegral($order);
  149. // $res2 = User::backOrderBrokerage($order);
  150. $res2 = User::sendBackOrderBrokerage($order);
  151. StoreOrder::orderTakeAfter($order);
  152. //满赠优惠券
  153. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  154. if (!($res1 && $res2)) exception('收货失败!');
  155. }
  156. /**
  157. * 修改状态 为已收货 admin模块
  158. * @param $order
  159. * @throws \Exception
  160. */
  161. public static function storeProductOrderTakeDeliveryAdmin($order)
  162. {
  163. $res1 = AdminStoreOrder::gainUserIntegral($order);
  164. // $res2 = User::backOrderBrokerage($order);
  165. $res2 = User::sendBackOrderBrokerage($order);
  166. AdminStoreOrder::orderTakeAfter($order);
  167. if (!($res1 && $res2)) exception('收货失败!');
  168. }
  169. /**
  170. * 修改状态 为已收货 定时任务使用
  171. * @param $order
  172. * @throws \Exception
  173. */
  174. public static function storeProductOrderTakeDeliveryTimer($order)
  175. {
  176. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  177. $res2 = User::sendBackOrderBrokerage($order);
  178. AdminStoreOrder::orderTakeAfter($order);
  179. if (!($res1 && $res2)) exception('收货失败!');
  180. }
  181. /**
  182. * 修改状态为 已退款 admin模块
  183. * @param $data
  184. * @param $oid
  185. * @return bool|mixed
  186. * @throws \think\db\exception\DataNotFoundException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. * @throws \think\exception\DbException
  189. */
  190. public static function storeProductOrderRefundY($data, $oid)
  191. {
  192. $order = AdminStoreOrder::where('id', $oid)->find();
  193. if ($order['is_channel'] == 1)
  194. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  195. else
  196. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  197. }
  198. /**
  199. * TODO 后台余额退款
  200. * @param $product
  201. * @param $refund_data
  202. * @throws \Exception
  203. */
  204. public static function storeOrderYueRefund($product, $refund_data)
  205. {
  206. $res = AdminStoreOrder::integralBack($product['id']);
  207. if (!$res) exception('退积分失败!');
  208. }
  209. /**
  210. * 订单退积分
  211. * @param $product $product 商品信息
  212. * @param $back_integral $back_integral 退多少积分
  213. */
  214. public static function storeOrderIntegralBack($product, $back_integral)
  215. {
  216. }
  217. }