OrderRepository.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\user\User;
  5. use app\models\user\WechatUser;
  6. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  7. use crmeb\services\MiniProgramService;
  8. use crmeb\services\WechatService;
  9. /**
  10. * Class OrderRepository
  11. * @package crmeb\repositories
  12. */
  13. class OrderRepository
  14. {
  15. /**
  16. * TODO 小程序JS支付
  17. * @param $orderId
  18. * @param string $field
  19. * @return array|string
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. */
  24. public static function jsPay($orderId, $field = 'order_id')
  25. {
  26. if (is_string($orderId))
  27. $orderInfo = StoreOrder::where($field, $orderId)->find();
  28. else
  29. $orderInfo = $orderId;
  30. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  31. if ($orderInfo['paid']) exception('支付已支付!');
  32. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  33. $openid = WechatUser::getOpenId($orderInfo['uid']);
  34. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  35. $site_name = sys_config('site_name');
  36. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  37. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  38. }
  39. /**
  40. * 微信公众号JS支付
  41. * @param $orderId
  42. * @param string $field
  43. * @return array|string
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. public static function wxPay($orderId, $field = 'order_id')
  49. {
  50. if (is_string($orderId))
  51. $orderInfo = StoreOrder::where($field, $orderId)->find();
  52. else
  53. $orderInfo = $orderId;
  54. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  55. if ($orderInfo['paid']) exception('支付已支付!');
  56. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  57. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  58. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  59. $site_name = sys_config('site_name');
  60. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  61. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  62. }
  63. /**
  64. * 微信h5支付
  65. * @param $orderId
  66. * @param string $field
  67. * @return array|string
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\ModelNotFoundException
  70. * @throws \think\exception\DbException
  71. */
  72. public static function h5Pay($orderId, $field = 'order_id')
  73. {
  74. if (is_string($orderId))
  75. $orderInfo = StoreOrder::where($field, $orderId)->find();
  76. else
  77. $orderInfo = $orderId;
  78. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  79. if ($orderInfo['paid']) exception('支付已支付!');
  80. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  81. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  82. $site_name = sys_config('site_name');
  83. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  84. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  85. }
  86. /**
  87. * 用户确认收货
  88. * @param $order
  89. * @param $uid
  90. * @throws \think\Exception
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. * @throws \think\exception\DbException
  94. * @throws \think\db\exception\DbException
  95. * @throws \Exception
  96. */
  97. public static function storeProductOrderUserTakeDelivery($order, $uid)
  98. {
  99. // $res1 = StoreOrder::gainUserIntegral($order);
  100. $res1 = User::sendBackOrderIntegral($order);
  101. // $res2 = User::backOrderBrokerage($order);
  102. $res2 = User::sendBackOrderBrokerage($order);
  103. $ratio = bcdiv(sys_config('normal_order_award_lake', 5, true), 100, 4);
  104. if (!$order['is_gift']) $res2 = $res2 && StoreOrder::addAwardLake(bcmul($order['pay_price'], $ratio, 2), $order['id'], 'order');
  105. StoreOrder::orderTakeAfter($order);
  106. //满赠优惠券
  107. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  108. if (!($res1 && $res2)) exception('收货失败!');
  109. }
  110. /**
  111. * 修改状态 为已收货 admin模块
  112. * @param $order
  113. * @throws \Exception
  114. */
  115. public static function storeProductOrderTakeDeliveryAdmin($order)
  116. {
  117. // $res1 = AdminStoreOrder::gainUserIntegral($order);
  118. $res1 = User::sendBackOrderIntegral($order);
  119. // $res2 = User::backOrderBrokerage($order);
  120. $res2 = User::sendBackOrderBrokerage($order);
  121. $ratio = bcdiv(sys_config('normal_order_award_lake', 5, true), 100, 4);
  122. if (!$order['is_gift']) $res2 = $res2 && StoreOrder::addAwardLake(bcmul($order['pay_price'], $ratio, 2), $order['id'], 'order');
  123. AdminStoreOrder::orderTakeAfter($order);
  124. if (!($res1 && $res2)) exception('收货失败!');
  125. }
  126. /**
  127. * 修改状态 为已收货 定时任务使用
  128. * @param $order
  129. * @throws \Exception
  130. */
  131. public static function storeProductOrderTakeDeliveryTimer($order)
  132. {
  133. // $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  134. $res1 = User::sendBackOrderIntegral($order);
  135. $res2 = User::sendBackOrderBrokerage($order);
  136. $ratio = bcdiv(sys_config('normal_order_award_lake', 5, true), 100, 4);
  137. if (!$order['is_gift']) $res2 = $res2 && StoreOrder::addAwardLake(bcmul($order['pay_price'], $ratio, 2), $order['id'], 'order');
  138. AdminStoreOrder::orderTakeAfter($order);
  139. if (!($res1 && $res2)) exception('收货失败!');
  140. }
  141. /**
  142. * 修改状态为 已退款 admin模块
  143. * @param $data
  144. * @param $oid
  145. * @return bool|mixed
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @throws \think\exception\DbException
  149. */
  150. public static function storeProductOrderRefundY($data, $oid)
  151. {
  152. $order = AdminStoreOrder::where('id', $oid)->find();
  153. if ($order['is_channel'] == 1)
  154. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  155. else
  156. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  157. }
  158. /**
  159. * TODO 后台余额退款
  160. * @param $product
  161. * @param $refund_data
  162. * @throws \Exception
  163. */
  164. public static function storeOrderYueRefund($product, $refund_data)
  165. {
  166. $res = AdminStoreOrder::integralBack($product['id']);
  167. if (!$res) exception('退积分失败!');
  168. }
  169. /**
  170. * 订单退积分
  171. * @param $product $product 商品信息
  172. * @param $back_integral $back_integral 退多少积分
  173. */
  174. public static function storeOrderIntegralBack($product, $back_integral)
  175. {
  176. }
  177. }