OrderRepository.php 7.1 KB

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