OrderRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StorePink;
  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['pink_id']>0 && StorePink::isPinkStatus($orderInfo['pink_id'])) 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['pink_id']>0 && StorePink::isPinkStatus($orderInfo['pink_id'])) exception('该团已关闭!');
  59. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  60. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  61. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  62. $site_name = sys_config('site_name');
  63. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  64. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  65. }
  66. /**
  67. * 微信h5支付
  68. * @param $orderId
  69. * @param string $field
  70. * @return array|string
  71. * @throws \think\db\exception\DataNotFoundException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @throws \think\exception\DbException
  74. */
  75. public static function h5Pay($orderId, $field = 'order_id')
  76. {
  77. if (is_string($orderId))
  78. $orderInfo = StoreOrder::where($field, $orderId)->find();
  79. else
  80. $orderInfo = $orderId;
  81. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  82. if($orderInfo['pink_id']>0 && StorePink::isPinkStatus($orderInfo['pink_id'])) exception('该团已关闭!');
  83. if ($orderInfo['paid']) exception('支付已支付!');
  84. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  85. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  86. $site_name = sys_config('site_name');
  87. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  88. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  89. }
  90. /**
  91. * 用户确认收货
  92. * @param $order
  93. * @param $uid
  94. * @throws \think\Exception
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @throws \think\exception\DbException
  98. */
  99. public static function storeProductOrderUserTakeDelivery($order, $uid)
  100. {
  101. //$res1 = StoreOrder::gainUserIntegral($order);
  102. $res2 = User::backOrderBrokerage($order);
  103. StoreOrder::orderTakeAfter($order);
  104. //满赠优惠券
  105. //User::directWdc($order);
  106. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  107. if (!($res2)) exception('收货失败!');
  108. }
  109. /**
  110. * 修改状态 为已收货 admin模块
  111. * @param $order
  112. * @throws \Exception
  113. */
  114. public static function storeProductOrderTakeDeliveryAdmin($order)
  115. {
  116. //$res1 = AdminStoreOrder::gainUserIntegral($order);
  117. $res2 = User::backOrderBrokerage($order);
  118. //User::directWdc($order);
  119. AdminStoreOrder::orderTakeAfter($order);
  120. if (!($res2)) exception('收货失败!');
  121. }
  122. /**
  123. * 修改状态 为已收货 定时任务使用
  124. * @param $order
  125. * @throws \Exception
  126. */
  127. public static function storeProductOrderTakeDeliveryTimer($order)
  128. {
  129. //$res1 = AdminStoreOrder::gainUserIntegral($order, false);
  130. $res2 = User::backOrderBrokerage($order, false);
  131. // User::directWdc($order);
  132. AdminStoreOrder::orderTakeAfter($order);
  133. if (!($res2)) exception('收货失败!');
  134. }
  135. /**
  136. * 修改状态为 已退款 admin模块
  137. * @param $data
  138. * @param $oid
  139. * @return bool|mixed
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. * @throws \think\exception\DbException
  143. */
  144. public static function storeProductOrderRefundY($data, $oid)
  145. {
  146. $order = AdminStoreOrder::where('id', $oid)->find();
  147. if ($order['is_channel'] == 1)
  148. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  149. else
  150. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  151. }
  152. /**
  153. * TODO 后台余额退款
  154. * @param $product
  155. * @param $refund_data
  156. * @throws \Exception
  157. */
  158. public static function storeOrderYueRefund($product, $refund_data)
  159. {
  160. $res = AdminStoreOrder::integralBack($product['id']);
  161. if (!$res) exception('退积分失败!');
  162. }
  163. /**
  164. * 订单退积分
  165. * @param $product $product 商品信息
  166. * @param $back_integral $back_integral 退多少积分
  167. */
  168. public static function storeOrderIntegralBack($product, $back_integral)
  169. {
  170. }
  171. }