OrderRepository.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\store\StoreOrder;
  4. use app\models\system\SystemAwardLevel;
  5. use app\models\system\SystemGroupLevel;
  6. use app\models\user\User;
  7. use app\models\user\UserBill;
  8. use app\models\user\WechatUser;
  9. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  10. use crmeb\services\JiaLieService;
  11. use crmeb\services\MiniProgramService;
  12. use crmeb\services\WechatService;
  13. /**
  14. * Class OrderRepository
  15. * @package crmeb\repositories
  16. */
  17. class OrderRepository
  18. {
  19. /**
  20. * TODO 小程序JS支付
  21. * @param $orderId
  22. * @param string $field
  23. * @return array|string
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public static function jsPay($orderId, $field = 'order_id')
  29. {
  30. if (is_string($orderId))
  31. $orderInfo = StoreOrder::where($field, $orderId)->find();
  32. else
  33. $orderInfo = $orderId;
  34. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  35. if ($orderInfo['paid']) exception('支付已支付!');
  36. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  37. $openid = WechatUser::getOpenId($orderInfo['uid']);
  38. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  39. $site_name = sys_config('site_name');
  40. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  41. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  42. }
  43. /**
  44. * 微信公众号JS支付
  45. * @param $orderId
  46. * @param string $field
  47. * @return array|string
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. public static function wxPay($orderId, $field = 'order_id')
  53. {
  54. if (is_string($orderId))
  55. $orderInfo = StoreOrder::where($field, $orderId)->find();
  56. else
  57. $orderInfo = $orderId;
  58. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  59. if ($orderInfo['paid']) exception('支付已支付!');
  60. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  61. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  62. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  63. $site_name = sys_config('site_name');
  64. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  65. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  66. }
  67. /**
  68. * 微信h5支付
  69. * @param $orderId
  70. * @param string $field
  71. * @return array|string
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function h5Pay($orderId, $field = 'order_id')
  77. {
  78. if (is_string($orderId))
  79. $orderInfo = StoreOrder::where($field, $orderId)->find();
  80. else
  81. $orderInfo = $orderId;
  82. if (!$orderInfo || !isset($orderInfo['paid'])) 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 $orderId
  93. * @param string $field
  94. */
  95. public static function barcodePay($orderId, $auth_code, $field = 'order_id')
  96. {
  97. if (is_string($orderId))
  98. $orderInfo = StoreOrder::where($field, $orderId)->find();
  99. else
  100. $orderInfo = $orderId;
  101. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  102. if ($orderInfo['paid']) exception('支付已支付!');
  103. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  104. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  105. $site_name = sys_config('site_name');
  106. $device_info = "myc" . $orderInfo['store_id'];
  107. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  108. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB', compact('auth_code', 'device_info'));
  109. }
  110. /**
  111. * 嘉联支付
  112. * @param $orderId
  113. * @param $auth_code
  114. * @param string $field
  115. */
  116. public static function jialiePay($orderId, $auth_code, $field = 'order_id|re_order_id')
  117. {
  118. if (is_string($orderId))
  119. $orderInfo = StoreOrder::where($field, $orderId)->find();
  120. else
  121. $orderInfo = $orderId;
  122. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  123. if ($orderInfo['paid']) exception('支付已支付!');
  124. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  125. $bodyContent = '商品订单';
  126. $site_name = sys_config('site_name');
  127. $device_info = "myc" . $orderInfo['store_id'];
  128. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  129. $data['pay_type'] = 'jialie';
  130. $data['auth_code'] = $auth_code;
  131. $data['body'] = $bodyContent;
  132. $data['total_fee'] = bcmul($orderInfo['pay_price'], 100, 0);
  133. $data['nonce_str'] = uniqid();
  134. $data['out_trade_no'] = !empty($orderInfo['re_order_id']) ? $orderInfo['re_order_id'] : $orderInfo['order_id'];
  135. $data['mch_create_ip'] = app('request')->ip();
  136. $data['attach'] = 'product';
  137. $rs = JiaLieService::initialize()::paymentOrder(JiaLieService::MICROPAY, $data);
  138. return $rs;
  139. }
  140. /**
  141. * 用户确认收货
  142. * @param $order
  143. * @param $uid
  144. * @throws \think\Exception
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. */
  149. public static function storeProductOrderUserTakeDelivery($order, $uid)
  150. {
  151. $res1 = StoreOrder::gainUserIntegral($order);
  152. $res2 = User::backOrderBrokerage($order);
  153. $res3 = User::backStoreBrokerage($order);
  154. StoreOrder::orderTakeAfter($order);
  155. //满赠优惠券
  156. WechatUser::userTakeOrderGiveCoupon($uid, $order['pay_price']);
  157. if (!($res1 && $res2 && $res3)) exception('收货失败!');
  158. }
  159. /**
  160. * 修改状态 为已收货 admin模块
  161. * @param $order
  162. * @throws \Exception
  163. */
  164. public static function storeProductOrderTakeDeliveryAdmin($order)
  165. {
  166. $res1 = AdminStoreOrder::gainUserIntegral($order);
  167. $res2 = User::backOrderBrokerage($order);
  168. $res3 = User::backStoreBrokerage($order);
  169. AdminStoreOrder::orderTakeAfter($order);
  170. //满赠优惠券
  171. WechatUser::userTakeOrderGiveCoupon($order['uid'], $order['pay_price']);
  172. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  173. if (!($res1 && $res2 && $res3)) exception('收货失败!');
  174. }
  175. /**
  176. * 修改状态 为已收货 定时任务使用
  177. * @param $order
  178. * @throws \Exception
  179. */
  180. public static function storeProductOrderTakeDeliveryTimer($order)
  181. {
  182. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  183. $res2 = User::backOrderBrokerage($order, false);
  184. $res3 = User::backStoreBrokerage($order);
  185. AdminStoreOrder::orderTakeAfter($order);
  186. UserBill::where('uid', $order['uid'])->where('link_id', $order['id'])->where('type', 'pay_money')->update(['take' => 1]);
  187. if (!($res1 && $res2 && $res3)) exception('收货失败!');
  188. }
  189. /**
  190. * 修改状态为 已退款 admin模块
  191. * @param $data
  192. * @param $oid
  193. * @return bool|mixed
  194. * @throws \think\db\exception\DataNotFoundException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. * @throws \think\exception\DbException
  197. */
  198. public static function storeProductOrderRefundY($data, $oid)
  199. {
  200. $order = AdminStoreOrder::where('id', $oid)->find();
  201. if ($order['is_channel'] == 1)
  202. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  203. else
  204. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  205. }
  206. /**
  207. * TODO 后台余额退款
  208. * @param $product
  209. * @param $refund_data
  210. * @throws \Exception
  211. */
  212. public static function storeOrderYueRefund($product, $refund_data)
  213. {
  214. $res = AdminStoreOrder::integralBack($product['id']);
  215. if (!$res) exception('退积分失败!');
  216. }
  217. /**
  218. * 订单退积分
  219. * @param $product $product 商品信息
  220. * @param $back_integral $back_integral 退多少积分
  221. */
  222. public static function storeOrderIntegralBack($product, $back_integral)
  223. {
  224. }
  225. }