StoreOrderController.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. <?php
  2. namespace app\api\controller\order;
  3. use app\models\user\WechatUser;
  4. use app\phpPay\bank\pay\UnionSDK;
  5. use app\phpPay\hpay\pay\AliLife;
  6. use app\phpPay\wexin\pay\Common;
  7. use app\phpPay\wexin\pay\WechatPay;
  8. use app\admin\model\system\{
  9. SystemAttachment, ShippingTemplates
  10. };
  11. use app\admin\model\user\User;
  12. use app\models\routine\RoutineFormId;
  13. use crmeb\repositories\OrderRepository;
  14. use app\models\store\{
  15. StoreBargainUser,
  16. StoreCart,
  17. StoreCoupon,
  18. StoreCouponIssue,
  19. StoreCouponUser,
  20. StoreOrder,
  21. StoreOrderCartInfo,
  22. StoreOrderStatus,
  23. StorePink,
  24. StoreProductReply
  25. };
  26. use app\models\system\SystemStore;
  27. use app\models\user\UserAddress;
  28. use app\models\user\UserLevel;
  29. use app\Request;
  30. use crmeb\services\{
  31. CacheService,
  32. ExpressService,
  33. SystemConfigService,
  34. UtilService
  35. };
  36. /**
  37. * 订单类
  38. * Class StoreOrderController
  39. * @package app\api\controller\order
  40. */
  41. class StoreOrderController
  42. {
  43. /**
  44. * 订单确认
  45. * @param Request $request
  46. * @return mixed
  47. */
  48. public function confirm(Request $request)
  49. {
  50. $temp = ShippingTemplates::get(1);
  51. if (!$temp) return app('json')->fail('默认模板未配置,无法下单');
  52. list($cartId) = UtilService::postMore(['cartId'], $request, true);
  53. if (!is_string($cartId) || !$cartId) return app('json')->fail('请提交购买的商品');
  54. $uid = $request->uid();
  55. $cartGroup = StoreCart::getUserProductCartList($uid, $cartId, 1);
  56. if (count($cartGroup['invalid'])) return app('json')->fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!');
  57. if (!$cartGroup['valid']) return app('json')->fail('请提交购买的商品');
  58. $cartInfo = $cartGroup['valid'];
  59. $addr = UserAddress::where('uid', $uid)->where('is_default', 1)->find();
  60. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo, $addr);
  61. if ($priceGroup === false) {
  62. return app('json')->fail(StoreOrder::getErrorInfo('运费模板不存在'));
  63. }
  64. $other = [
  65. 'offlinePostage' => sys_config('offline_postage'),
  66. 'integralRatio' => sys_config('integral_ratio')
  67. ];
  68. $usableCoupons = StoreCouponUser::getUsableCouponList($uid, $cartGroup, $priceGroup['totalPrice']);
  69. $usableCoupon = isset($usableCoupons[0]) ? $usableCoupons[0] : null;
  70. $cartIdA = explode(',', $cartId);
  71. $seckill_id = 0;
  72. $combination_id = 0;
  73. $bargain_id = 0;
  74. if (count($cartIdA) == 1) {
  75. $seckill_id = StoreCart::where('id', $cartId)->value('seckill_id');
  76. $combination_id = StoreCart::where('id', $cartId)->value('combination_id');
  77. $bargain_id = StoreCart::where('id', $cartId)->value('bargain_id');
  78. }
  79. $data['deduction'] = $seckill_id || $combination_id || $bargain_id;
  80. $data['usableCoupon'] = $usableCoupon;
  81. $data['addressInfo'] = UserAddress::getUserDefaultAddress($uid);
  82. $data['seckill_id'] = $seckill_id;
  83. $data['combination_id'] = $combination_id;
  84. $data['bargain_id'] = $bargain_id;
  85. $data['cartInfo'] = $cartInfo;
  86. $data['priceGroup'] = $priceGroup;
  87. $data['orderKey'] = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other);
  88. $data['offlinePostage'] = $other['offlinePostage'];
  89. $vipId = UserLevel::getUserLevel($uid);
  90. $user = $request->user();
  91. if (isset($user['pwd'])) unset($user['pwd']);
  92. $user['vip'] = $vipId !== false ? true : false;
  93. if ($user['vip']) {
  94. $user['vip_id'] = $vipId;
  95. $user['discount'] = UserLevel::getUserLevelInfo($vipId, 'discount');
  96. }
  97. $data['userInfo'] = $user;
  98. $data['integralRatio'] = $other['integralRatio'];
  99. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  100. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  101. $data['system_store'] = ($res = SystemStore::getStoreDispose()) ? $res : [];//门店信息
  102. return app('json')->successful($data);
  103. }
  104. /**
  105. * 计算订单金额
  106. * @param Request $request
  107. * @param $key
  108. * @return mixed
  109. * @throws \think\Exception
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. * @throws \think\exception\DbException
  113. */
  114. public function computedOrder(Request $request, $key)
  115. {
  116. // $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo);
  117. if (!$key) return app('json')->fail('参数错误!');
  118. $uid = $request->uid();
  119. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  120. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  121. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $shipping_type) = UtilService::postMore([
  122. 'addressId', 'couponId', ['payType', 'yue'], ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''],
  123. ['shipping_type', 1],
  124. ], $request, true);
  125. $payType = strtolower($payType);
  126. if ($bargainId) {
  127. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  128. if (!$bargainUserTableId)
  129. return app('json')->fail('砍价失败');
  130. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  131. if ($status == 3)
  132. return app('json')->fail('砍价已支付');
  133. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  134. }
  135. if ($pinkId) {
  136. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  137. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  138. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  139. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  140. }
  141. $priceGroup = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, true, 0, $shipping_type);
  142. if ($priceGroup)
  143. return app('json')->status('NONE', 'ok', $priceGroup);
  144. else
  145. return app('json')->fail(StoreOrder::getErrorInfo('计算失败'));
  146. }
  147. /**
  148. * 订单创建
  149. * @param Request $request
  150. * @param $key
  151. * @return mixed
  152. * @throws \think\Exception
  153. * @throws \think\db\exception\DataNotFoundException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @throws \think\exception\DbException
  156. */
  157. public function create(Request $request, $key)
  158. {
  159. if (!$key) return app('json')->fail('参数错误!');
  160. $uid = $request->uid();
  161. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  162. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  163. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $from, $shipping_type, $real_name, $phone, $storeId) = UtilService::postMore([
  164. 'addressId', 'couponId', 'payType', ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''], ['from', 'weixin'],
  165. ['shipping_type', 1], ['real_name', ''], ['phone', ''], ['store_id', 0]
  166. ], $request, true);
  167. $payType = strtolower($payType);
  168. if ($bargainId) {
  169. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  170. if (!$bargainUserTableId)
  171. return app('json')->fail('砍价失败');
  172. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  173. if ($status == 3)
  174. return app('json')->fail('砍价已支付');
  175. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  176. }
  177. if ($pinkId) {
  178. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  179. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  180. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  181. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  182. }
  183. $isChannel = 1;
  184. if ($from == 'weixin')
  185. $isChannel = 0;
  186. elseif ($from == 'weixinh5')
  187. $isChannel = 2;
  188. $order = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, false, $isChannel, $shipping_type, $real_name, $phone, $storeId);
  189. if ($order === false) return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败'));
  190. $orderId = $order['order_id'];
  191. $order_id = $order['order_id'];
  192. $info = compact('order_id', 'key');
  193. if ($orderId) {
  194. event('OrderCreated', [$order]); //订单创建成功事件
  195. event('ShortMssageSend', [$orderId, 'AdminPlaceAnOrder']);//发送管理员通知
  196. switch ($payType) {
  197. case "weixin":
  198. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  199. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  200. $orderInfo = $orderInfo->toArray();
  201. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  202. //支付金额为0
  203. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  204. //创建订单jspay支付
  205. $payPriceStatus = StoreOrder::jsPayPrice($orderId, $uid, $formId);
  206. if ($payPriceStatus)//0元支付成功
  207. return app('json')->status('success', '微信支付成功', $info);
  208. else
  209. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  210. } else {
  211. try {
  212. if ($from == 'routine') {
  213. $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
  214. } else if ($from == 'weixinh5') {
  215. $jsConfig = OrderRepository::h5Pay($orderId);
  216. } else if ($from == 'app') {
  217. $jsConfig = OrderRepository::appPay($order);
  218. }else {
  219. $jsConfig = OrderRepository::wxPay($orderId);
  220. }
  221. } catch (\Exception $e) {
  222. return app('json')->status('pay_error', $e->getMessage(), $info);
  223. }
  224. $info['jsConfig'] = $jsConfig;
  225. if ($from == 'weixinh5') {
  226. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  227. } else {
  228. return app('json')->status('wechat_pay', '订单创建成功', $info);
  229. }
  230. }
  231. break;
  232. case 'yue':
  233. if (StoreOrder::yuePay($orderId, $request->uid(), $formId))
  234. return app('json')->status('success', '余额支付成功', $info);
  235. else {
  236. $errorinfo = StoreOrder::getErrorInfo();
  237. if (is_array($errorinfo))
  238. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  239. else
  240. return app('json')->status('pay_error', $errorinfo);
  241. }
  242. break;
  243. case 'offline':
  244. return app('json')->status('success', '订单创建成功', $info);
  245. break;
  246. case "ali":
  247. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  248. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  249. $orderInfo = $orderInfo->toArray();
  250. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  251. //支付金额为0
  252. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  253. //创建订单jspay支付
  254. $payPriceStatus = StoreOrder::jsPayPrice($orderId, $request->uid());
  255. if ($payPriceStatus)//0元支付成功
  256. return app('json')->status('success', '支付成功');
  257. else
  258. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  259. } else {
  260. try {
  261. $jsConfig = OrderRepository::aliPay($orderId); //创建订单jspay
  262. } catch (\Exception $e) {
  263. return app('json')->status('pay_error', $e->getMessage());
  264. }
  265. $info['jsConfig'] = $jsConfig;
  266. return app('json')->status('ali_pay', '订单创建成功', $info);
  267. }
  268. break;
  269. case "ailpay":
  270. $data = [
  271. 'version' => '10',
  272. 'mer_no' => '6888804045113',
  273. 'mer_key' => 'Ids+y1lR4thuaHGxXaCwCLbkdFXTjze96Tua3U88ZHsvaHvYxNi7HiMBOnabYUckONdHUdUR/0g=',
  274. 'mer_order_no' => $order['order_id'].'-'.time(),
  275. 'create_time' => date('YmdHis',time()),
  276. 'order_amt' => $order['pay_price'],
  277. 'notify_url' => SystemConfigService::get('site_url').'/api/sdNotify',
  278. 'return_url' => '',
  279. 'create_ip' => str_replace('.','_', $_SERVER['REMOTE_ADDR']),
  280. 'store_id' => '000000',
  281. 'pay_extra' => json_encode(['mer_app_id' => '', 'openid' => '', 'buyer_id' => '', 'wx_app_id' => '', 'gh_ori_id' => '', 'path_url'=> '', 'miniProgramType' => '']),
  282. 'accsplit_flag' => 'NO',
  283. 'sign_type' => 'MD5',
  284. ];
  285. $data['sign'] = sing($data, 'FQ7AZy8uve6bp7yed0MK2HqZ+qlwE798t0L0HZm0Xv7oBE8zyyB5QcTIc2iJcGXdmcDy+d3mf/N0m2NjLxy8qsME+l5b4jCqLOcCP6oEE2mYiitZIKIfvpsHc7nNZKB3yc5tMRDMqKxixuz+UKFTiw==');
  286. $data['expire_time'] = date('YmdHis',time()+1800);
  287. $data['goods_name'] = StoreOrder::getProductTitle($order['cart_id']);
  288. $data['product_code'] = '02020004';
  289. $data['clear_cycle'] = '0';
  290. $data['jump_scheme'] = 'sandcash://scpay';
  291. $data['meta_option'] = json_encode([
  292. ['sc' => 'wzsc','s' => 'Android', 'n' => '测试', 'id' => 'com.tencent.tmgp.sgame'],
  293. ['sc' => 'wzsc','s' => 'IOS', 'n' => '测试', 'id' => 'com.tencent.tmgp.sgame'],
  294. ]);
  295. return app('json')->status('ailpay', '订单创建成功',['order' => $data, 'order_id' => $order['order_id']]);
  296. case "ylpay":
  297. $data = [
  298. 'version' => '10',
  299. 'mer_no' => '6888804045113',
  300. 'mer_key' => 'Ids+y1lR4thuaHGxXaCwCLbkdFXTjze96Tua3U88ZHsvaHvYxNi7HiMBOnabYUckONdHUdUR/0g=',
  301. 'mer_order_no' => $order['order_id'].'-'.time(),
  302. 'create_time' => date('YmdHis',time()),
  303. 'order_amt' => $order['pay_price'],
  304. 'notify_url' => SystemConfigService::get('site_url').'/api/sdNotify',
  305. 'return_url' => '',
  306. 'create_ip' => str_replace('.','_', $_SERVER['REMOTE_ADDR']),
  307. 'store_id' => '000000',
  308. 'pay_extra' => '',
  309. 'accsplit_flag' => 'NO',
  310. 'sign_type' => 'MD5',
  311. ];
  312. $data['sign'] = sing($data, 'FQ7AZy8uve6bp7yed0MK2HqZ+qlwE798t0L0HZm0Xv7oBE8zyyB5QcTIc2iJcGXdmcDy+d3mf/N0m2NjLxy8qsME+l5b4jCqLOcCP6oEE2mYiitZIKIfvpsHc7nNZKB3yc5tMRDMqKxixuz+UKFTiw==');
  313. $data['expire_time'] = date('YmdHis',time()+1800);
  314. $data['goods_name'] = StoreOrder::getProductTitle($order['cart_id']);
  315. $data['product_code'] = '02030001';
  316. $data['clear_cycle'] = 0;
  317. $data['jump_scheme'] = 'sandcash://scpay';
  318. $data['meta_option'] = json_encode([
  319. ['sc' => '','s' => 'Android', 'n' => '', 'id' => ''],
  320. ['sc' => '','s' => 'IOS', 'n' => '', 'id' => ''],
  321. ]);
  322. return app('json')->status('ylpay', '订单创建成功',['order' => $data, 'order_id' => $order['order_id']]);
  323. }
  324. } else return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败!'));
  325. }
  326. /**
  327. * 订单 再次下单
  328. * @param Request $request
  329. * @return mixed
  330. */
  331. public function again(Request $request)
  332. {
  333. list($uni) = UtilService::postMore([
  334. ['uni', ''],
  335. ], $request, true);
  336. if (!$uni) return app('json')->fail('参数错误!');
  337. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  338. if (!$order) return app('json')->fail('订单不存在!');
  339. $order = StoreOrder::tidyOrder($order, true);
  340. $res = [];
  341. foreach ($order['cartInfo'] as $v) {
  342. if ($v['combination_id']) return app('json')->fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  343. else if ($v['bargain_id']) return app('json')->fail('砍价产品不能再来一单,请在砍价产品内自行下单!');
  344. else if ($v['seckill_id']) return app('json')->ail('秒杀产品不能再来一单,请在秒杀产品内自行下单!');
  345. else $res[] = StoreCart::setCart($request->uid(), $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  346. }
  347. $cateId = [];
  348. foreach ($res as $v) {
  349. if (!$v) return app('json')->fail('再来一单失败,请重新下单!');
  350. $cateId[] = $v['id'];
  351. }
  352. event('OrderCreateAgain', implode(',', $cateId));
  353. return app('json')->successful('ok', ['cateId' => implode(',', $cateId)]);
  354. }
  355. /**
  356. * 订单支付
  357. * @param Request $request
  358. * @return mixed
  359. */
  360. public function pay(Request $request)
  361. {
  362. list($uni, $paytype, $from) = UtilService::postMore([
  363. ['uni', ''],
  364. ['paytype', 'weixin'],
  365. ['from', 'weixin']
  366. ], $request, true);
  367. if (!$uni) return app('json')->fail('参数错误!');
  368. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  369. if (!$order)
  370. return app('json')->fail('订单不存在!');
  371. if ($order['paid'])
  372. return app('json')->fail('该订单已支付!');
  373. if ($order['pink_id']) if (StorePink::isPinkStatus($order['pink_id']))
  374. return app('json')->fail('该订单已失效!');
  375. if ($from == 'weixin') {//0
  376. if (in_array($order->is_channel, [1, 2]))
  377. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  378. }
  379. if ($from == 'weixinh5') {//2
  380. if (in_array($order->is_channel, [0, 1]))
  381. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  382. }
  383. if ($from == 'routine') {//1
  384. if (in_array($order->is_channel, [0, 2]))
  385. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  386. }
  387. $order['pay_type'] = $paytype; //重新支付选择支付方式
  388. switch ($order['pay_type']) {
  389. case 'weixin':
  390. try {
  391. if ($from == 'routine') {
  392. $jsConfig = OrderRepository::jsPay($order); //订单列表发起支付
  393. } else if ($from == 'weixinh5') {
  394. $jsConfig = OrderRepository::h5Pay($order);
  395. } else if ($from == 'app') {
  396. $jsConfig = OrderRepository::appPay($order);
  397. } else {
  398. $jsConfig = OrderRepository::wxPay($order);
  399. }
  400. } catch (\Exception $e) {
  401. return app('json')->fail($e->getMessage());
  402. }
  403. if ($from == 'weixinh5') {
  404. return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  405. } else {
  406. return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  407. }
  408. break;
  409. case 'yue':
  410. if (StoreOrder::yuePay($order['order_id'], $request->uid()))
  411. return app('json')->status('success', '余额支付成功');
  412. else {
  413. $error = StoreOrder::getErrorInfo();
  414. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  415. }
  416. break;
  417. case 'app':
  418. $res = StoreOrder::appPay($order, $request->uid());
  419. break;
  420. case 'offline':
  421. StoreOrder::createOrderTemplate($order);
  422. if (StoreOrder::setOrderTypePayOffline($order['order_id']))
  423. return app('json')->status('success', '订单创建成功');
  424. else
  425. return app('json')->status('success', '支付失败');
  426. break;
  427. case "ali":
  428. $orderInfo = StoreOrder::where('order_id', $order['order_id'])->find();
  429. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  430. $orderInfo = $orderInfo->toArray();
  431. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  432. //支付金额为0
  433. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  434. //创建订单jspay支付
  435. $payPriceStatus = StoreOrder::jsPayPrice($order['order_id'], $request->uid());
  436. if ($payPriceStatus)//0元支付成功
  437. return app('json')->status('success', '支付成功');
  438. else
  439. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  440. } else {
  441. try {
  442. $jsConfig = OrderRepository::aliPay($order['order_id']); //创建订单jspay
  443. } catch (\Exception $e) {
  444. return app('json')->status('pay_error', $e->getMessage());
  445. }
  446. $info['jsConfig'] = $jsConfig;
  447. return app('json')->status('ali_pay', '订单创建成功', $info);
  448. }
  449. break;
  450. case "ailpay":
  451. $data = [
  452. 'version' => '10',
  453. 'mer_no' => '6888804045113',
  454. 'mer_key' => 'Ids+y1lR4thuaHGxXaCwCLbkdFXTjze96Tua3U88ZHsvaHvYxNi7HiMBOnabYUckONdHUdUR/0g=',
  455. 'mer_order_no' => $order['order_id'].'-'.time(),
  456. 'create_time' => date('YmdHis',time()),
  457. 'order_amt' => $order['pay_price'],
  458. 'notify_url' => SystemConfigService::get('site_url').'/api/sdNotify',
  459. 'return_url' => '',
  460. 'create_ip' => str_replace('.','_', $_SERVER['REMOTE_ADDR']),
  461. 'store_id' => '000000',
  462. 'pay_extra' => '',
  463. 'accsplit_flag' => 'NO',
  464. 'sign_type' => 'MD5',
  465. ];
  466. $data['sign'] = sing($data, 'FQ7AZy8uve6bp7yed0MK2HqZ+qlwE798t0L0HZm0Xv7oBE8zyyB5QcTIc2iJcGXdmcDy+d3mf/N0m2NjLxy8qsME+l5b4jCqLOcCP6oEE2mYiitZIKIfvpsHc7nNZKB3yc5tMRDMqKxixuz+UKFTiw==');
  467. $data['expire_time'] = date('YmdHis',time()+1800);
  468. $data['goods_name'] = StoreOrder::getProductTitle($order['cart_id']);
  469. $data['product_code'] = '02020004';
  470. $data['clear_cycle'] = 0;
  471. $data['jump_scheme'] = 'sandcash://scpay';
  472. $data['meta_option'] = json_encode([
  473. ['sc' => '','s' => 'Android', 'n' => '', 'id' => ''],
  474. ['sc' => '','s' => 'IOS', 'n' => '', 'id' => ''],
  475. ]);
  476. return app('json')->status('ailpay', '订单创建成功',['order' => $data, 'order_id' => $order['order_id']]);
  477. case "ylpay":
  478. $data = [
  479. 'version' => '10',
  480. 'mer_no' => '6888804045113',
  481. 'mer_key' => 'Ids+y1lR4thuaHGxXaCwCLbkdFXTjze96Tua3U88ZHsvaHvYxNi7HiMBOnabYUckONdHUdUR/0g=',
  482. 'mer_order_no' => $order['order_id'].'-'.time(),
  483. 'create_time' => date('YmdHis',time()),
  484. 'order_amt' => $order['pay_price'],
  485. 'notify_url' => SystemConfigService::get('site_url').'/api/sdNotify',
  486. 'return_url' => '',
  487. 'create_ip' => str_replace('.','_', $_SERVER['REMOTE_ADDR']),
  488. 'store_id' => '000000',
  489. 'pay_extra' => '',
  490. 'accsplit_flag' => 'NO',
  491. 'sign_type' => 'MD5',
  492. ];
  493. $data['sign'] = sing($data, 'FQ7AZy8uve6bp7yed0MK2HqZ+qlwE798t0L0HZm0Xv7oBE8zyyB5QcTIc2iJcGXdmcDy+d3mf/N0m2NjLxy8qsME+l5b4jCqLOcCP6oEE2mYiitZIKIfvpsHc7nNZKB3yc5tMRDMqKxixuz+UKFTiw==');
  494. $data['expire_time'] = date('YmdHis',time()+1800);
  495. $data['goods_name'] = StoreOrder::getProductTitle($order['cart_id']);
  496. $data['product_code'] = '02030001';
  497. $data['clear_cycle'] = 0;
  498. $data['jump_scheme'] = 'sandcash://scpay';
  499. $data['meta_option'] = json_encode([
  500. ['sc' => '','s' => 'Android', 'n' => '', 'id' => ''],
  501. ['sc' => '','s' => 'IOS', 'n' => '', 'id' => ''],
  502. ]);
  503. return app('json')->status('ylpay', '订单创建成功',['order' => $data, 'order_id' => $order['order_id']]);
  504. }
  505. return app('json')->fail('支付方式错误');
  506. }
  507. /**
  508. * 订单列表
  509. * @param Request $request
  510. * @return mixed
  511. */
  512. public function lst(Request $request)
  513. {
  514. list($type, $page, $limit, $search) = UtilService::getMore([
  515. ['type', ''],
  516. ['page', 0],
  517. ['limit', ''],
  518. ['search', ''],
  519. ], $request, true);
  520. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), $type, $page, $limit, $search));
  521. }
  522. /**
  523. * 订单详情
  524. * @param Request $request
  525. * @param $uni
  526. * @return mixed
  527. */
  528. public function detail(Request $request, $uni)
  529. {
  530. if (!strlen(trim($uni))) return app('json')->fail('参数错误');
  531. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  532. if (!$order) return app('json')->fail('订单不存在');
  533. $order = $order->toArray();
  534. //是否开启门店自提
  535. $store_self_mention = sys_config('store_self_mention');
  536. //关闭门店自提后 订单隐藏门店信息
  537. if ($store_self_mention == 0) $order['shipping_type'] = 1;
  538. if ($order['verify_code']) {
  539. $verify_code = $order['verify_code'];
  540. $verify[] = substr($verify_code, 0, 4);
  541. $verify[] = substr($verify_code, 4, 4);
  542. $verify[] = substr($verify_code, 8);
  543. $order['_verify_code'] = implode(' ', $verify);
  544. }
  545. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  546. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  547. $order['system_store'] = SystemStore::getStoreDispose($order['store_id']);
  548. if ($order['shipping_type'] === 2 && $order['verify_code']) {
  549. $name = $order['verify_code'] . '.jpg';
  550. $imageInfo = SystemAttachment::getInfo($name, 'name');
  551. $siteUrl = sys_config('site_url');
  552. if (!$imageInfo) {
  553. $imageInfo = UtilService::getQRCodePath($order['verify_code'], $name);
  554. if (is_array($imageInfo)) {
  555. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  556. $url = $imageInfo['dir'];
  557. } else
  558. $url = '';
  559. } else $url = $imageInfo['att_dir'];
  560. if (isset($imageInfo['image_type']) && $imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  561. $order['code'] = $url;
  562. }
  563. $order['mapKey'] = sys_config('tengxun_map_key');
  564. return app('json')->successful('ok', StoreOrder::tidyOrder($order, true, true));
  565. }
  566. /**
  567. * 订单删除
  568. * @param Request $request
  569. * @return mixed
  570. */
  571. public function del(Request $request)
  572. {
  573. list($uni) = UtilService::postMore([
  574. ['uni', ''],
  575. ], $request, true);
  576. if (!$uni) return app('json')->fail('参数错误!');
  577. $res = StoreOrder::removeOrder($uni, $request->uid());
  578. if ($res)
  579. return app('json')->successful();
  580. else
  581. return app('json')->fail(StoreOrder::getErrorInfo());
  582. }
  583. /**
  584. * 订单收货
  585. * @param Request $request
  586. * @return mixed
  587. */
  588. public function take(Request $request)
  589. {
  590. list($uni) = UtilService::postMore([
  591. ['uni', ''],
  592. ], $request, true);
  593. if (!$uni) return app('json')->fail('参数错误!');
  594. $res = StoreOrder::takeOrder($uni, $request->uid());
  595. if ($res) {
  596. $order_info = StoreOrder::where('order_id', $uni)->find();
  597. $gain_integral = intval($order_info['gain_integral']);
  598. $gain_coupon = StoreCouponIssue::alias('a')
  599. ->join('store_coupon b', 'a.cid = b.id')
  600. ->where('a.status', 1)
  601. ->where('a.is_full_give', 1)
  602. ->where('a.is_del', 0)
  603. ->where('a.full_reduction', '<=', $order_info['total_price'])
  604. ->sum('b.coupon_price');
  605. return app('json')->successful(['gain_integral' => $gain_integral, 'gain_coupon' => $gain_coupon]);
  606. } else
  607. return app('json')->fail(StoreOrder::getErrorInfo());
  608. }
  609. /**
  610. * 订单 查看物流
  611. * @param Request $request
  612. * @param $uni
  613. * @return mixed
  614. */
  615. public function express(Request $request, $uni)
  616. {
  617. if (!$uni || !($order = StoreOrder::getUserOrderDetail($request->uid(), $uni))) return app('json')->fail('查询订单不存在!');
  618. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return app('json')->fail('该订单不存在快递单号!');
  619. $cacheName = $uni . $order['delivery_id'];
  620. $result = CacheService::get($cacheName, null);
  621. if ($result === NULL) {
  622. $result = ExpressService::query($order['delivery_id']);
  623. if (is_array($result) &&
  624. isset($result['result']) &&
  625. isset($result['result']['deliverystatus']) &&
  626. $result['result']['deliverystatus'] >= 3)
  627. $cacheTime = 0;
  628. else
  629. $cacheTime = 1800;
  630. CacheService::set($cacheName, $result, $cacheTime);
  631. }
  632. $orderInfo = [];
  633. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'unique') ?? [];
  634. $info = [];
  635. $cartNew = [];
  636. foreach ($cartInfo as $k => $cart) {
  637. $cart = json_decode($cart, true);
  638. $cartNew['cart_num'] = $cart['cart_num'];
  639. $cartNew['truePrice'] = $cart['truePrice'];
  640. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  641. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  642. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  643. array_push($info, $cartNew);
  644. unset($cart);
  645. }
  646. $orderInfo['delivery_id'] = $order['delivery_id'];
  647. $orderInfo['delivery_name'] = $order['delivery_name'];
  648. $orderInfo['delivery_type'] = $order['delivery_type'];
  649. $orderInfo['cartInfo'] = $info;
  650. return app('json')->successful(['order' => $orderInfo, 'express' => $result ? $result : []]);
  651. }
  652. /**
  653. * 订单评价
  654. * @param Request $request
  655. * @return mixed
  656. * @throws \think\db\exception\DataNotFoundException
  657. * @throws \think\db\exception\ModelNotFoundException
  658. * @throws \think\exception\DbException
  659. */
  660. public function comment(Request $request)
  661. {
  662. $group = UtilService::postMore([
  663. ['unique', ''], ['comment', ''], ['pics', ''], ['product_score', 5], ['service_score', 5]
  664. ], $request);
  665. $unique = $group['unique'];
  666. unset($group['unique']);
  667. if (!$unique) return app('json')->fail('参数错误!');
  668. $cartInfo = StoreOrderCartInfo::where('unique', $unique)->find();
  669. $uid = $request->uid();
  670. $user_info = User::get($uid);
  671. $group['nickname'] = $user_info['nickname'];
  672. $group['avatar'] = $user_info['avatar'];
  673. if (!$cartInfo) return app('json')->fail('评价产品不存在!');
  674. $orderUid = StoreOrder::getOrderInfo($cartInfo['oid'], 'uid')['uid'];
  675. if ($uid != $orderUid) return app('json')->fail('评价产品不存在!');
  676. if (StoreProductReply::be(['oid' => $cartInfo['oid'], 'unique' => $unique]))
  677. return app('json')->fail('该产品已评价!');
  678. $group['comment'] = htmlspecialchars(trim($group['comment']));
  679. if ($group['product_score'] < 1) return app('json')->fail('请为产品评分');
  680. else if ($group['service_score'] < 1) return app('json')->fail('请为商家服务评分');
  681. if ($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  682. else if ($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  683. else if ($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  684. else $productId = $cartInfo['product_id'];
  685. if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
  686. $group = array_merge($group, [
  687. 'uid' => $uid,
  688. 'oid' => $cartInfo['oid'],
  689. 'unique' => $unique,
  690. 'product_id' => $productId,
  691. 'add_time' => time(),
  692. 'reply_type' => 'product'
  693. ]);
  694. StoreProductReply::beginTrans();
  695. $res = StoreProductReply::reply($group, 'product');
  696. if (!$res) {
  697. StoreProductReply::rollbackTrans();
  698. return app('json')->fail('评价失败!');
  699. }
  700. try {
  701. StoreOrder::checkOrderOver($cartInfo['oid']);
  702. } catch (\Exception $e) {
  703. StoreProductReply::rollbackTrans();
  704. return app('json')->fail($e->getMessage());
  705. }
  706. StoreProductReply::commitTrans();
  707. event('UserCommented', $res);
  708. event('AdminNewPush');
  709. return app('json')->successful();
  710. }
  711. /**
  712. * 订单统计数据
  713. * @param Request $request
  714. * @return mixed
  715. */
  716. public function data(Request $request)
  717. {
  718. return app('json')->successful(StoreOrder::getOrderData($request->uid()));
  719. }
  720. /**
  721. * 订单退款理由
  722. * @return mixed
  723. */
  724. public function refund_reason()
  725. {
  726. $reason = sys_config('stor_reason') ?: [];//退款理由
  727. $reason = str_replace("\r\n", "\n", $reason);//防止不兼容
  728. $reason = explode("\n", $reason);
  729. return app('json')->successful($reason);
  730. }
  731. /**
  732. * 订单退款审核
  733. * @param Request $request
  734. * @return mixed
  735. */
  736. public function refund_verify(Request $request)
  737. {
  738. $data = UtilService::postMore([
  739. ['text', ''],
  740. ['refund_reason_wap_img', ''],
  741. ['refund_reason_wap_explain', ''],
  742. ['uni', '']
  743. ], $request);
  744. $uni = $data['uni'];
  745. unset($data['uni']);
  746. if ($data['refund_reason_wap_img']) $data['refund_reason_wap_img'] = explode(',', $data['refund_reason_wap_img']);
  747. if (!$uni || $data['text'] == '') return app('json')->fail('参数错误!');
  748. $res = StoreOrder::orderApplyRefund($uni, $request->uid(), $data['text'], $data['refund_reason_wap_explain'], $data['refund_reason_wap_img']);
  749. if ($res)
  750. return app('json')->successful('提交申请成功');
  751. else
  752. return app('json')->fail(StoreOrder::getErrorInfo());
  753. }
  754. /**
  755. * 订单取消 未支付的订单回退积分,回退优惠券,回退库存
  756. * @param Request $request
  757. * @return mixed
  758. * @throws \think\db\exception\DataNotFoundException
  759. * @throws \think\db\exception\ModelNotFoundException
  760. * @throws \think\exception\DbException
  761. */
  762. public function cancel(Request $request)
  763. {
  764. list($id) = UtilService::postMore([['id', 0]], $request, true);
  765. if (!$id) return app('json')->fail('参数错误');
  766. if (StoreOrder::cancelOrder($id, $request->uid()))
  767. return app('json')->successful('取消订单成功');
  768. return app('json')->fail(StoreOrder::getErrorInfo('取消订单失败'));
  769. }
  770. /**
  771. * 订单产品信息
  772. * @param Request $request
  773. * @return mixed
  774. * @throws \think\db\exception\DataNotFoundException
  775. * @throws \think\db\exception\ModelNotFoundException
  776. * @throws \think\exception\DbException
  777. */
  778. public function product(Request $request)
  779. {
  780. list($unique) = UtilService::postMore([['unique', '']], $request, true);
  781. if (!$unique || !StoreOrderCartInfo::be(['unique' => $unique]) || !($cartInfo = StoreOrderCartInfo::where('unique', $unique)->find())) return app('json')->fail('评价产品不存在!');
  782. $cartInfo = $cartInfo->toArray();
  783. $cartProduct = [];
  784. $cartProduct['cart_num'] = $cartInfo['cart_info']['cart_num'];
  785. $cartProduct['productInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['image']) ? $cartInfo['cart_info']['productInfo']['image'] : '';
  786. $cartProduct['productInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['price']) ? $cartInfo['cart_info']['productInfo']['price'] : 0;
  787. $cartProduct['productInfo']['store_name'] = isset($cartInfo['cart_info']['productInfo']['store_name']) ? $cartInfo['cart_info']['productInfo']['store_name'] : '';
  788. if (isset($cartInfo['cart_info']['productInfo']['attrInfo'])) {
  789. $cartProduct['productInfo']['attrInfo']['product_id'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['product_id']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['product_id'] : '';
  790. $cartProduct['productInfo']['attrInfo']['suk'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['suk']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['suk'] : '';
  791. $cartProduct['productInfo']['attrInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['price']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['price'] : '';
  792. $cartProduct['productInfo']['attrInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['image']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['image'] : '';
  793. }
  794. $cartProduct['product_id'] = isset($cartInfo['cart_info']['product_id']) ? $cartInfo['cart_info']['product_id'] : 0;
  795. $cartProduct['combination_id'] = isset($cartInfo['cart_info']['combination_id']) ? $cartInfo['cart_info']['combination_id'] : 0;
  796. $cartProduct['seckill_id'] = isset($cartInfo['cart_info']['seckill_id']) ? $cartInfo['cart_info']['seckill_id'] : 0;
  797. $cartProduct['bargain_id'] = isset($cartInfo['cart_info']['bargain_id']) ? $cartInfo['cart_info']['bargain_id'] : 0;
  798. $cartProduct['order_id'] = StoreOrder::where('id', $cartInfo['oid'])->value('order_id');
  799. return app('json')->successful($cartProduct);
  800. }
  801. /**
  802. * 首页获取未支付订单
  803. */
  804. public function get_noPay(Request $request)
  805. {
  806. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), 0, 0, 0, ''));
  807. }
  808. }