OrderRepository.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <<<<<<< HEAD
  2. <?php
  3. namespace crmeb\repositories;
  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\AlipayService;
  9. use crmeb\services\MiniProgramService;
  10. use crmeb\services\SystemConfigService;
  11. use crmeb\services\WechatService;
  12. /**
  13. * Class OrderRepository
  14. * @package crmeb\repositories
  15. */
  16. class OrderRepository
  17. {
  18. /**
  19. * TODO 小程序JS支付
  20. * @param $orderId
  21. * @param string $field
  22. * @return array|string
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. * @throws \think\exception\DbException
  26. */
  27. public static function jsPay($orderId, $field = 'order_id')
  28. {
  29. if (is_string($orderId))
  30. $orderInfo = StoreOrder::where($field, $orderId)->find();
  31. else
  32. $orderInfo = $orderId;
  33. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  34. if ($orderInfo['paid']) exception('支付已支付!');
  35. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  36. $openid = WechatUser::getOpenId($orderInfo['uid']);
  37. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  38. $site_name = sys_config('site_name');
  39. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  40. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  41. }
  42. /**
  43. * 微信公众号JS支付
  44. * @param $orderId
  45. * @param string $field
  46. * @return array|string
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. */
  51. public static function wxPay($orderId, $field = 'order_id')
  52. {
  53. if (is_string($orderId))
  54. $orderInfo = StoreOrder::where($field, $orderId)->find();
  55. else
  56. $orderInfo = $orderId;
  57. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  58. if ($orderInfo['paid']) 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['paid']) exception('支付已支付!');
  83. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  84. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  85. $site_name = sys_config('site_name');
  86. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  87. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  88. }
  89. public static function aliPay($orderId, $field = 'order_id')
  90. {
  91. if (is_string($orderId))
  92. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  93. else
  94. $orderInfo = $orderId;
  95. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  96. if ($orderInfo['paid']) exception('支付已支付!');
  97. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  98. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  99. $site_name = sys_config('site_name');
  100. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  101. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  102. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  103. $aliPay = new AlipayService();
  104. $aliPay->setAppid($alipay['alipay_app_id']);
  105. $aliPay->setNotifyUrl($notifyUrl);
  106. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  107. $aliPay->setTotalFee($orderInfo['pay_price']);
  108. $aliPay->setOutTradeNo($orderInfo['order_id']);
  109. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  110. $aliPay->setPassbackParams(['attach' => 'product']);
  111. $orderStr = $aliPay->getOrderStr();
  112. return $orderStr;
  113. }
  114. /**
  115. * 用户确认收货
  116. * @param $order
  117. * @param $uid
  118. * @throws \think\Exception
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. * @throws \think\exception\DbException
  122. * @throws \think\db\exception\DbException
  123. * @throws \Exception
  124. */
  125. public static function storeProductOrderUserTakeDelivery($order, $uid)
  126. {
  127. $res1 = StoreOrder::gainUserIntegral($order);
  128. // $res2 = User::backOrderBrokerage($order);
  129. $res2 = User::sendBackOrderBrokerage($order);
  130. StoreOrder::orderTakeAfter($order);
  131. //满赠优惠券
  132. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  133. if (!($res1 && $res2)) exception('收货失败!');
  134. }
  135. /**
  136. * 修改状态 为已收货 admin模块
  137. * @param $order
  138. * @throws \Exception
  139. */
  140. public static function storeProductOrderTakeDeliveryAdmin($order)
  141. {
  142. $res1 = AdminStoreOrder::gainUserIntegral($order);
  143. // $res2 = User::backOrderBrokerage($order);
  144. $res2 = User::sendBackOrderBrokerage($order);
  145. AdminStoreOrder::orderTakeAfter($order);
  146. if (!($res1 && $res2)) exception('收货失败!');
  147. }
  148. /**
  149. * 修改状态 为已收货 定时任务使用
  150. * @param $order
  151. * @throws \Exception
  152. */
  153. public static function storeProductOrderTakeDeliveryTimer($order)
  154. {
  155. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  156. $res2 = User::sendBackOrderBrokerage($order);
  157. AdminStoreOrder::orderTakeAfter($order);
  158. if (!($res1 && $res2)) exception('收货失败!');
  159. }
  160. /**
  161. * 修改状态为 已退款 admin模块
  162. * @param $data
  163. * @param $oid
  164. * @return bool|mixed
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. * @throws \think\exception\DbException
  168. */
  169. public static function storeProductOrderRefundY($data, $oid)
  170. {
  171. $order = AdminStoreOrder::where('id', $oid)->find();
  172. if ($order['is_channel'] == 1)
  173. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  174. else
  175. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  176. }
  177. /**
  178. * TODO 后台余额退款
  179. * @param $product
  180. * @param $refund_data
  181. * @throws \Exception
  182. */
  183. public static function storeOrderYueRefund($product, $refund_data)
  184. {
  185. $res = AdminStoreOrder::integralBack($product['id']);
  186. if (!$res) exception('退积分失败!');
  187. }
  188. /**
  189. * 订单退积分
  190. * @param $product $product 商品信息
  191. * @param $back_integral $back_integral 退多少积分
  192. */
  193. public static function storeOrderIntegralBack($product, $back_integral)
  194. {
  195. }
  196. =======
  197. <?php
  198. namespace crmeb\repositories;
  199. use app\models\store\StoreOrder;
  200. use app\models\user\User;
  201. use app\models\user\WechatUser;
  202. use app\admin\model\order\StoreOrder as AdminStoreOrder;
  203. use crmeb\services\AlipayService;
  204. use crmeb\services\MiniProgramService;
  205. use crmeb\services\SystemConfigService;
  206. use crmeb\services\WechatService;
  207. /**
  208. * Class OrderRepository
  209. * @package crmeb\repositories
  210. */
  211. class OrderRepository
  212. {
  213. /**
  214. * TODO 小程序JS支付
  215. * @param $orderId
  216. * @param string $field
  217. * @return array|string
  218. * @throws \think\db\exception\DataNotFoundException
  219. * @throws \think\db\exception\ModelNotFoundException
  220. * @throws \think\exception\DbException
  221. */
  222. public static function jsPay($orderId, $field = 'order_id')
  223. {
  224. if (is_string($orderId))
  225. $orderInfo = StoreOrder::where($field, $orderId)->find();
  226. else
  227. $orderInfo = $orderId;
  228. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  229. if ($orderInfo['paid']) exception('支付已支付!');
  230. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  231. $openid = WechatUser::getOpenId($orderInfo['uid']);
  232. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  233. $site_name = sys_config('site_name');
  234. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  235. return MiniProgramService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  236. }
  237. /**
  238. * 微信公众号JS支付
  239. * @param $orderId
  240. * @param string $field
  241. * @return array|string
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. public static function wxPay($orderId, $field = 'order_id')
  247. {
  248. if (is_string($orderId))
  249. $orderInfo = StoreOrder::where($field, $orderId)->find();
  250. else
  251. $orderInfo = $orderId;
  252. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  253. if ($orderInfo['paid']) exception('支付已支付!');
  254. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  255. $openid = WechatUser::uidToOpenid($orderInfo['uid'], 'openid');
  256. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  257. $site_name = sys_config('site_name');
  258. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  259. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  260. }
  261. /**
  262. * 微信h5支付
  263. * @param $orderId
  264. * @param string $field
  265. * @return array|string
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. * @throws \think\exception\DbException
  269. */
  270. public static function h5Pay($orderId, $field = 'order_id')
  271. {
  272. if (is_string($orderId))
  273. $orderInfo = StoreOrder::where($field, $orderId)->find();
  274. else
  275. $orderInfo = $orderId;
  276. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  277. if ($orderInfo['paid']) exception('支付已支付!');
  278. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  279. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  280. $site_name = sys_config('site_name');
  281. if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  282. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'product', StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30), '', 'MWEB');
  283. }
  284. public static function aliPay($orderId, $field = 'order_id')
  285. {
  286. if (is_string($orderId))
  287. $orderInfo = StoreOrder::where($field, $orderId)->where('is_del', 0)->find();
  288. else
  289. $orderInfo = $orderId;
  290. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  291. if ($orderInfo['paid']) exception('支付已支付!');
  292. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  293. $bodyContent = StoreOrder::getProductTitle($orderInfo['cart_id']);
  294. $site_name = sys_config('site_name');
  295. if (!$site_name || !$bodyContent) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  296. $alipay = SystemConfigService::more(['alipay_app_id', 'alipay_pub_key', 'alipay_private_key', 'alipay_key']);
  297. $notifyUrl = sys_config('site_url') . '/api/alipay/notify';
  298. $aliPay = new AlipayService();
  299. $aliPay->setAppid($alipay['alipay_app_id']);
  300. $aliPay->setNotifyUrl($notifyUrl);
  301. $aliPay->setRsaPrivateKey($alipay['alipay_private_key']);
  302. $aliPay->setTotalFee($orderInfo['pay_price']);
  303. $aliPay->setOutTradeNo($orderInfo['order_id']);
  304. $aliPay->setOrderName(StoreOrder::getSubstrUTf8($site_name . ' - ' . $bodyContent, 30));
  305. $aliPay->setPassbackParams(['attach' => 'product']);
  306. $orderStr = $aliPay->getOrderStr();
  307. return $orderStr;
  308. }
  309. /**
  310. * 用户确认收货
  311. * @param $order
  312. * @param $uid
  313. * @throws \think\Exception
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\ModelNotFoundException
  316. * @throws \think\exception\DbException
  317. * @throws \think\db\exception\DbException
  318. * @throws \Exception
  319. */
  320. public static function storeProductOrderUserTakeDelivery($order, $uid)
  321. {
  322. $res1 = StoreOrder::gainUserIntegral($order);
  323. // $res2 = User::backOrderBrokerage($order);
  324. $res2 = User::sendBackOrderBrokerage($order);
  325. StoreOrder::orderTakeAfter($order);
  326. //满赠优惠券
  327. WechatUser::userTakeOrderGiveCoupon($uid, $order['total_price']);
  328. if (!($res1 && $res2)) exception('收货失败!');
  329. }
  330. /**
  331. * 修改状态 为已收货 admin模块
  332. * @param $order
  333. * @throws \Exception
  334. */
  335. public static function storeProductOrderTakeDeliveryAdmin($order)
  336. {
  337. $res1 = AdminStoreOrder::gainUserIntegral($order);
  338. // $res2 = User::backOrderBrokerage($order);
  339. $res2 = User::sendBackOrderBrokerage($order);
  340. AdminStoreOrder::orderTakeAfter($order);
  341. if (!($res1 && $res2)) exception('收货失败!');
  342. }
  343. /**
  344. * 修改状态 为已收货 定时任务使用
  345. * @param $order
  346. * @throws \Exception
  347. */
  348. public static function storeProductOrderTakeDeliveryTimer($order)
  349. {
  350. $res1 = AdminStoreOrder::gainUserIntegral($order, false);
  351. $res2 = User::sendBackOrderBrokerage($order);
  352. AdminStoreOrder::orderTakeAfter($order);
  353. if (!($res1 && $res2)) exception('收货失败!');
  354. }
  355. /**
  356. * 修改状态为 已退款 admin模块
  357. * @param $data
  358. * @param $oid
  359. * @return bool|mixed
  360. * @throws \think\db\exception\DataNotFoundException
  361. * @throws \think\db\exception\ModelNotFoundException
  362. * @throws \think\exception\DbException
  363. */
  364. public static function storeProductOrderRefundY($data, $oid)
  365. {
  366. $order = AdminStoreOrder::where('id', $oid)->find();
  367. if ($order['is_channel'] == 1)
  368. return AdminStoreOrder::refundRoutineTemplate($oid); //TODO 小程序余额退款模板消息
  369. else
  370. return AdminStoreOrder::refundTemplate($data, $oid);//TODO 公众号余额退款模板消息
  371. }
  372. /**
  373. * TODO 后台余额退款
  374. * @param $product
  375. * @param $refund_data
  376. * @throws \Exception
  377. */
  378. public static function storeOrderYueRefund($product, $refund_data)
  379. {
  380. $res = AdminStoreOrder::integralBack($product['id']);
  381. if (!$res) exception('退积分失败!');
  382. }
  383. /**
  384. * 订单退积分
  385. * @param $product $product 商品信息
  386. * @param $back_integral $back_integral 退多少积分
  387. */
  388. public static function storeOrderIntegralBack($product, $back_integral)
  389. {
  390. }
  391. >>>>>>> 386b37d33e5ba817cba00df29efaefbd692e4dd1
  392. }