DouYinService.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. <?php
  2. namespace app\api\service;
  3. use app\adminapi\logic\external\ExternalConsultationLogic;
  4. use app\api\logic\ServiceOrderLogic;
  5. use app\common\enum\PayEnum;
  6. use app\common\logic\PayNotifyLogic;
  7. use app\common\model\Config;
  8. use app\common\model\external\DouyinOrder;
  9. use app\common\model\external\DouyinRefundOrder;
  10. use app\common\model\external\DouyinUserAuth;
  11. use app\common\model\external\ExternalConsultation;
  12. use app\common\model\external\ExternalConsultationOrder;
  13. use app\common\model\external\ExternalPlatformGoods;
  14. use app\common\model\goods\Goods;
  15. use app\common\model\goods_category\GoodsCategory;
  16. use app\common\model\recharge\RechargeOrder;
  17. use app\common\model\user\User;
  18. use app\common\model\user\UserAuth;
  19. use app\common\model\works\ServiceWork;
  20. use app\common\service\ConfigService;
  21. use app\common\service\FileService;
  22. use GuzzleHttp\Client;
  23. use think\facade\Db;
  24. use think\facade\Log;
  25. class DouYinService
  26. {
  27. protected static int $terminal = \app\common\enum\user\UserTerminalEnum::DOUYIN;
  28. protected static int $external_platform_id = 6;
  29. protected CONST EXTERNAL_PLATFORM_ID = 6;
  30. // ********************************* 注册登录
  31. public static function register(array $params)
  32. {
  33. $userSn = User::createUserSn();
  34. $params['password'] = !empty($params['password'])?$params['password']:rand(100000,999999);
  35. $passwordSalt = \think\facade\Config::get('project.unique_identification');
  36. $password = create_password($params['password'], $passwordSalt);
  37. $avatar = ConfigService::get('default_image', 'user_avatar');
  38. $user = User::create([
  39. 'sn' => $userSn,
  40. 'avatar' => $avatar,
  41. 'nickname' => '用户' . $userSn,
  42. 'account' => $params['account'],
  43. 'mobile' => !empty($params['mobile'])?$params['mobile']:'',
  44. 'password' => $password,
  45. 'channel' => self::$terminal,
  46. 'user_type' => $params['user_type']??0,
  47. ]);
  48. return $user;
  49. }
  50. public static function phoneLogin(array $params)
  51. {
  52. try {
  53. $where = ['mobile' => $params['mobile']];
  54. $params['account'] = $params['mobile'];
  55. $user = User::where($where)->findOrEmpty();
  56. if ($user->isEmpty()) {
  57. //直接注册用户
  58. $params['channel'] = self::$terminal;
  59. $user = self::register($params);
  60. }
  61. //更新登录信息
  62. $user->login_time = time();
  63. $user->login_ip = request()->ip();
  64. $user->save();
  65. $userInfo = UserTokenService::setToken($user->id, self::$terminal);
  66. //返回登录信息
  67. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  68. $avatar = FileService::getFileUrl($avatar);
  69. return [
  70. 'nickname' => $userInfo['nickname'],
  71. 'sn' => $userInfo['sn'],
  72. 'mobile' => $userInfo['mobile'],
  73. 'avatar' => $avatar,
  74. 'token' => $userInfo['token'],
  75. ];
  76. } catch (\Exception $e) {
  77. throw new \Exception($e->getMessage());
  78. }
  79. }
  80. public static function getDouyinUserByOpenId(array $openId)
  81. {
  82. try {
  83. $user = DouyinUserAuth::where('openid',$openId)->findOrEmpty();
  84. if ($user->isEmpty()) {
  85. //直接注册用户
  86. $params['channel'] = self::$terminal;
  87. $user = self::register($params);
  88. }
  89. //更新登录信息
  90. $user->login_time = time();
  91. $user->login_ip = request()->ip();
  92. $user->save();
  93. $userInfo = UserTokenService::setToken($user->id, self::$terminal);
  94. //返回登录信息
  95. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  96. $avatar = FileService::getFileUrl($avatar);
  97. return [
  98. 'nickname' => $userInfo['nickname'],
  99. 'sn' => $userInfo['sn'],
  100. 'mobile' => $userInfo['mobile'],
  101. 'avatar' => $avatar,
  102. 'token' => $userInfo['token'],
  103. ];
  104. } catch (\Exception $e) {
  105. throw new \Exception($e->getMessage());
  106. }
  107. }
  108. // **************************** 商品管理 goods_category_id goods_id external_platform_id
  109. public static function addProduct($params)
  110. {
  111. $send_url = env('internal_api.api_url_host').'platf/dou_yin/addGoods';
  112. $res = http_request($send_url,http_build_query($params));
  113. Log::info('addProduct:'
  114. .'url:'.$send_url
  115. .'|data:'.json_encode($params,JSON_UNESCAPED_UNICODE)
  116. .'|res:'.json_encode([$res],JSON_UNESCAPED_UNICODE)
  117. );
  118. return $res?:[];
  119. }
  120. // ******************************** 订单业务
  121. public static function getOrderDetail($params)
  122. {
  123. //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
  124. // $params['order_number'] user_id
  125. $order = DouyinOrder::with(['goods','serviceWork','douyinRefundOrder'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  126. if($order->isEmpty()){
  127. return [];
  128. }
  129. $orderInfo = $order->toArray();
  130. empty($orderInfo['goods']) && $orderInfo['goods'] = [];
  131. empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
  132. empty($orderInfo['douyinRefundOrder']) && $orderInfo['douyinRefundOrder'] = [];
  133. $orderInfo['book_info'] = json_decode($orderInfo['book_info']?:'{}',true);
  134. $rechargeOrder = RechargeOrder::where(['work_id'=>$orderInfo['work_id']?:0,'payment_type'=>2])->findOrEmpty();
  135. if($rechargeOrder->isEmpty()){
  136. $orderInfo['tail_order'] = [];
  137. }else{
  138. $orderInfo['tail_order'] = $rechargeOrder->toArray();
  139. // 尾款未支付时 展示尾单信息加入临时总金额用于展示
  140. if(isset($orderInfo['tail_order']['pay_status']) && $orderInfo['tail_order']['pay_status'] == 0){
  141. $orderInfo['total_amount'] += $orderInfo['tail_order']['order_amount'];
  142. }
  143. }
  144. $work_status = $orderInfo['serviceWork']['work_status']??0;
  145. $performance = [];
  146. // tmp
  147. switch ($work_status){
  148. case 0:
  149. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  150. break;
  151. case 1:
  152. case 2:
  153. case 3:
  154. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  155. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  156. break;
  157. case 4:
  158. case 5:
  159. case 6:
  160. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  161. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  162. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  163. break;
  164. case 7:
  165. case 8:
  166. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  167. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  168. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  169. $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
  170. break;
  171. }
  172. $orderInfo['performance'] = $performance;
  173. return $orderInfo;
  174. }
  175. public static function createOrder($params)
  176. {
  177. try {
  178. $goods_id = $params['goods_id']??0;
  179. $open_id = '_000o7ntqTR--_hCTBOBCSR_NJkyp_hiqlEK';
  180. $goods = Goods::where('id',$goods_id)->findOrEmpty();
  181. if($goods->isEmpty()){
  182. throw new \Exception('商品不存在!');
  183. }
  184. $goods = $goods->toArray();
  185. $platformGoods = ExternalPlatformGoods::where('goods_id', $goods_id)->where('external_platform_id', self::EXTERNAL_PLATFORM_ID)->findOrEmpty();
  186. if($platformGoods->isEmpty()){
  187. throw new \Exception('外部商品不存在!');
  188. }
  189. $platformGoods = $platformGoods->toArray();
  190. $quantity = $params['quantity']?:1;
  191. $appointment_time = strtotime($params['appointment_time']??date('Y-m-d H:i:s',time()));
  192. $bookStartTime = $appointment_time * 1000;
  193. $bookEndTime = ($appointment_time + (2 * 86400)) * 1000;
  194. $order_number = self::submitOrder([
  195. 'open_id'=>$open_id,
  196. 'goods_id'=>$goods_id,
  197. 'user_id'=>$params['user_id']??0,
  198. 'mobile'=>$params['user_info']['mobile']??'',
  199. 'quantity'=>$quantity
  200. ]);
  201. $data = [
  202. "total_amount" => $platformGoods['service_fee'] * $quantity * 100,
  203. "open_id" => $open_id,
  204. "out_order_no" => $order_number,
  205. "order_entry_schema" => ['path'=>'pages/order/detail','params'=>json_encode(['order_number'=>$order_number??0])],
  206. "cp_extra" => json_encode([
  207. "outShopId" => self::EXTERNAL_PLATFORM_ID,
  208. "skuId" => (string)$platformGoods['external_goods_sn'],
  209. "quantity" => $quantity,
  210. "user_id" => $params['user_id'],
  211. "user_address" => $params['user_address'],
  212. "lon" => $params['lon'],
  213. "lat" => $params['lat'],
  214. "appointment_time" => $params['appointment_time']
  215. ]),
  216. "goods_list" => [
  217. [
  218. "goods_id_type" => 1,
  219. "goods_id" => (string)$platformGoods['external_goods_sn'],
  220. "quantity" => 1
  221. ]
  222. ],
  223. "goods_book_info" => [
  224. "book_type" => 2,
  225. "cancel_policy" => 3,
  226. "cancel_advance_hour" => 5
  227. ],
  228. ];
  229. // 服务器向抖音发起创建订单
  230. $url = 'api/apps/trade/v2/order/create_order';
  231. $resData = self::toDyRequestUrl($url,$data);
  232. return $resData;
  233. } catch (\Exception $e) {
  234. throw new \Exception($e->getMessage());
  235. }
  236. }
  237. // 预下单接口 - 前端 首次/尾款
  238. public static function getPluginCreateOrderData($goods_id, $quantity, $douyinOrderId,$params)
  239. {
  240. try {
  241. $goods = Goods::where('id',$goods_id)->findOrEmpty();
  242. if($goods->isEmpty()){
  243. throw new \Exception('商品不存在!');
  244. }
  245. $goods = $goods->toArray();
  246. $platformGoods = ExternalPlatformGoods::where('goods_id', $goods_id)->where('external_platform_id', self::EXTERNAL_PLATFORM_ID)->findOrEmpty();
  247. if($platformGoods->isEmpty()){
  248. throw new \Exception('外部商品不存在!');
  249. }
  250. $platformGoods = $platformGoods->toArray();
  251. $quantity = $quantity?:1;
  252. $appointment_time = strtotime($params['appointment_time']??date('Y-m-d H:i:s',time()));
  253. $bookStartTime = $appointment_time * 1000;
  254. $bookEndTime = ($appointment_time + (2 * 86400)) * 1000;
  255. $data = [
  256. /*"goodsList" => [
  257. "quantity" => $quantity,
  258. "price" => $platformGoods['service_fee'] * 100,
  259. "goodsName" => $goods['goods_name'],
  260. "goodsPhoto" => $goods['goods_image']??'',
  261. "goodsId" => '',
  262. "goodsType" => 1
  263. ],*/
  264. "skuList" => [
  265. [
  266. "quantity" => (int)$quantity,
  267. "skuId" => (string)$platformGoods['external_goods_sn'],
  268. "skuType" => 1, // 1:商品库商品 2:非商品库商品(融合预约品走加价时,固定传2)
  269. "price" => $platformGoods['service_fee'] * 100,
  270. ]
  271. ],
  272. // "bookInfo" => [
  273. // "itemBookInfoList"=>[
  274. // [
  275. // "poiId" => '7511543640776017961',
  276. // "shopName" => '亿蜂快修·武汉市',
  277. // "outShopId" => (string)self::EXTERNAL_PLATFORM_ID,
  278. // "skuId" => (string)$platformGoods['external_goods_sn'],
  279. // "bookStartTime" => $bookStartTime?:'',
  280. // "bookEndTime" => $bookEndTime?:'',
  281. // ]
  282. // ]
  283. // ],
  284. "payment" => [
  285. "totalAmount" => $quantity * $platformGoods['service_fee'] * 100,
  286. ],
  287. //"callbackUrl" => $callbackUrl?:'',
  288. "callbackData" => [
  289. "outShopId" => self::EXTERNAL_PLATFORM_ID,
  290. "skuId" => (string)$platformGoods['external_goods_sn'],
  291. "quantity" => $quantity,
  292. "user_id" => $params['user_id'],
  293. "douyinOrderId" => $douyinOrderId?:0,
  294. "user_name" => $params['user_name']??'',
  295. "mobile" => $params['mobile']??'',
  296. "user_address" => $params['user_address']??'',
  297. "lon" => $params['lon']??'',
  298. "lat" => $params['lat']??'',
  299. "appointment_time" => $params['appointment_time']??''
  300. ],
  301. /*"tradeOption" => json_encode([
  302. "life_trade_flag" => 1,
  303. "order_relation_info" => [
  304. "related_order_id" => 0, // 加价时上个订单号
  305. "relation_type" => 'multi_buy_as_one'
  306. ]
  307. ])*/
  308. ];
  309. if($douyinOrderId){ // 说明是来自首单订单即要创建尾款
  310. $data['callbackUrl'] = env('douyin.pay_tail_notify_url')??'';
  311. $data['tradeOption'] = [
  312. "life_trade_flag" => 1,
  313. "order_relation_info" => [
  314. "related_order_id" => $douyinOrderId?:0, // 加价时上个订单号
  315. "relation_type" => 'multi_buy_as_one'
  316. ]
  317. ];
  318. }
  319. /*else{ // 创建首单订单
  320. $data['callbackUrl'] = '';
  321. }*/
  322. return $data;
  323. } catch (\Exception $e) {
  324. throw new \Exception($e->getMessage());
  325. }
  326. }
  327. // ++++++++++++++++++++ 首次/尾款 扩展点
  328. /**
  329. * 预下单扩展点
  330. * @param $params
  331. * @throws \Exception
  332. * @author liugc <466014217@qq.com>
  333. * @date 2025/6/4 14:03
  334. */
  335. public static function submitOrderNotify($params = [])
  336. {
  337. try {
  338. $params['external_platform_id'] = self::EXTERNAL_PLATFORM_ID;
  339. // order_id goods total_amount discount cp_extra create_order_time phone_num contact_name open_id
  340. $params['cp_extra'] = json_decode($params['cp_extra'], true);
  341. $user_id = $params['cp_extra']['user_id'];
  342. $user = User::where('id',$user_id)->findOrEmpty();
  343. if($params['cp_extra']['douyinOrderId']>0){ // 说明是尾款单
  344. // 创建尾款单
  345. $order_number = self::tailOrder([
  346. 'douyinOrderId'=>$params['cp_extra']['douyinOrderId'],
  347. 'user_id'=>$user_id??0,
  348. 'total_amount'=>$params['total_amount'],
  349. 'discount_amount'=>$params['discount_amount'],
  350. ]);
  351. $payNotifyUrl = 'https://weixiudev.kyjlkj.com/api/dou_yin/payTailNotify';
  352. }else{
  353. // 创建首单 goods_id user_info.mobile user_id quantity
  354. $order_number = self::submitOrder([
  355. 'open_id'=>$params['open_id'],
  356. 'order_id'=>$params['order_id'], // 抖音订单号
  357. 'goods_id'=>$params['cp_extra']['skuId'],
  358. 'user_id'=>$user_id??0,
  359. 'mobile'=>$user['mobile']??'',
  360. 'quantity'=>$params['cp_extra']['quantity'],
  361. 'book_info'=>$params['cp_extra'],
  362. ]);
  363. $payNotifyUrl = 'https://weixiudev.kyjlkj.com/api/dou_yin/payNotify';
  364. }
  365. return [
  366. "out_order_no" => $order_number,
  367. "order_entry_schema" => [
  368. "path" => "pages/order/detail",
  369. "params" => json_encode(['order_number' => $order_number])
  370. ],
  371. "payNotifyUrl" => $payNotifyUrl
  372. ];
  373. } catch (\Exception $e) {
  374. throw new \Exception($e->getMessage());
  375. }
  376. }
  377. // ++++++++++++++++++++ 首次创单
  378. /**
  379. * 预下单扩展点-子
  380. * @param array $params goods_id user_info.mobile user_id quantity
  381. * @return array|false
  382. */
  383. public static function submitOrder($params)
  384. {
  385. Db::startTrans();
  386. try {
  387. $platformGoods = ExternalPlatformGoods::where('external_goods_sn', $params['goods_id'])->where('external_platform_id', self::EXTERNAL_PLATFORM_ID)->findOrEmpty();
  388. if($platformGoods->isEmpty()){
  389. throw new \Exception('产品不存在!');
  390. }
  391. $goods = Goods::findOrEmpty($platformGoods['goods_id']);
  392. if($goods->isEmpty()){
  393. throw new \Exception('产品不存在!');
  394. }
  395. if(empty($params['mobile'])){
  396. throw new \Exception('请先补充您的联系方式后在提交订单');
  397. }
  398. // TODO tmp防抖1m
  399. $isExist = DouyinOrder::where(['user_id'=>$params['user_id'],'goods_id'=>$goods['id']])->where('create_time','>',(time() - 60))->findOrEmpty();
  400. if(!$isExist->isEmpty()){
  401. //throw new \Exception('请勿重复下单!');
  402. }
  403. $quantity = $params['quantity']??1;
  404. //生成订单
  405. $create_data = [
  406. 'user_id' => $params['user_id'],
  407. 'mobile' => $params['mobile'],
  408. 'open_id' => $params['open_id'],
  409. 'goods_id'=>$goods['id'],
  410. 'title' => $goods['goods_name'],
  411. 'book_info' => json_encode($params['book_info']??[]),
  412. 'unit_price' => $platformGoods['service_fee'],
  413. 'quantity' => $quantity,
  414. 'total_amount' => $platformGoods['service_fee'] * $quantity,
  415. 'dy_order_id' => $params['order_id']??'',
  416. 'order_number' => generate_sn(DouyinOrder::class, 'order_number'),
  417. ];
  418. $order = DouyinOrder::create($create_data);
  419. Db::commit();
  420. return $create_data['order_number'];
  421. } catch (\Exception $e) {
  422. Db::rollback();
  423. throw new \Exception($e->getMessage());
  424. }
  425. }
  426. // 支付成功回调
  427. public static function payNotify($params)
  428. {
  429. Log::write(json_encode($params));
  430. // 查询抖音订单是否完成支付
  431. if ($params['status'] === 'SUCCESS') {
  432. $params['cp_extra'] = json_decode($params['cp_extra'], true);
  433. $transaction_id = $params['order_id']??'';
  434. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  435. $out_order_no = $params['out_order_no'];
  436. $pay_time = time();
  437. $order = DouyinOrder::where('order_number', $out_order_no)->findOrEmpty();
  438. if(!$order->isEmpty()){
  439. // 更新充值订单状态
  440. $order->transaction_id = $transaction_id;
  441. $order->order_status = 2;
  442. $order->pay_time = $pay_time;
  443. $order->paid_amount = $paid_amount;
  444. $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
  445. $form_detail = [
  446. 'user_name' => $user['real_name']??'',
  447. 'mobile' => $user['mobile'],
  448. 'transaction_id' => $transaction_id,
  449. 'out_trade_no' => $out_order_no,
  450. 'paid_amount' => $paid_amount,
  451. 'params' => $params,
  452. ];
  453. $consultation = ExternalConsultation::create([
  454. 'external_platform_id' => self::EXTERNAL_PLATFORM_ID,
  455. 'form_detail' => json_encode($form_detail),
  456. 'user_name' => $user['real_name']??'',
  457. 'mobile' => $user['mobile'],
  458. 'goods_id' => $order->goods_id,
  459. 'amount' => $paid_amount
  460. ]);
  461. $order->consultation_id = $consultation->id;
  462. $order->save();
  463. //order_number user_address lon lat appointment_time
  464. self::reservation([
  465. 'order_number' => $out_order_no,
  466. 'user_address' => $params['cp_extra']['user_address'],
  467. 'lon' => $params['cp_extra']['lon'],
  468. 'lat' => $params['cp_extra']['lat'],
  469. 'appointment_time' => $params['cp_extra']['appointment_time'],
  470. ]);
  471. return true;
  472. }
  473. }elseif ($params['status'] === 'CANCEL' && $params['message'] == 'TIME_OUT'){
  474. // 超时取消支付
  475. Log::info('支付超时取消:order_id'.$params['order_id']."--out_order_no:" .$params['out_order_no']);
  476. return true;
  477. }
  478. return false;
  479. }
  480. // ++++++++++++++++++++ 首次创单 end
  481. // ++++++++++++++++++++ 尾款创单
  482. /**
  483. * 预下单扩展点-子
  484. * @param array $params goods_id user_info.mobile user_id quantity
  485. * @return array|false
  486. */
  487. public static function tailOrder($params)
  488. {
  489. Db::startTrans();
  490. try {
  491. $amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  492. $work_id = DouyinOrder::where(['id'=>$params['douyinOrderId']])->value('work_id')??0;
  493. $sn = '';
  494. $rechargeOrder = RechargeOrder::where(['work_id'=>$work_id,'payment_type'=>2])->findOrEmpty();
  495. if($work_id && $rechargeOrder->isEmpty()){
  496. //新增待支付尾款
  497. $sn = generate_sn(RechargeOrder::class, 'sn');
  498. $order_data = [
  499. 'order_type' => 0,
  500. 'sn' => $sn,
  501. 'order_terminal' => 1,
  502. 'work_id' => $work_id,
  503. 'user_id' => $params['user_id'],
  504. 'payment_type' => 2,
  505. 'order_total' => $amount,
  506. 'order_amount' => $amount,
  507. 'pay_status' => 0,
  508. 'paid_amount' => 0,
  509. 'pay_way' => 4
  510. ];
  511. RechargeOrder::create($order_data);
  512. }else{
  513. $sn = $rechargeOrder->sn;
  514. }
  515. Db::commit();
  516. return $sn;
  517. } catch (\Exception $e) {
  518. Db::rollback();
  519. throw new \Exception($e->getMessage());
  520. }
  521. }
  522. // 尾款支付成功回调
  523. public static function payTailNotify($params)
  524. {
  525. Log::write(json_encode($params));
  526. // 查询抖音订单是否完成支付
  527. if ($params['status'] === 'SUCCESS') {
  528. $transaction_id = $params['order_id']??'';
  529. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  530. $out_order_no = $params['out_order_no'];
  531. $pay_time = time();
  532. $params['cp_extra'] = json_decode($params['cp_extra'], true);
  533. $work_id = DouyinOrder::where(['id'=>$params['cp_extra']['douyinOrderId']])->value('work_id')??0;
  534. $rechargeOrder = RechargeOrder::where(['work_id'=>$work_id,'sn'=>$out_order_no,'payment_type'=>2])->findOrEmpty();
  535. if(!$rechargeOrder->isEmpty()){
  536. // 更新充值订单状态
  537. $rechargeOrder->transaction_id = $transaction_id;
  538. $rechargeOrder->pay_status = 1;
  539. $rechargeOrder->pay_time = $pay_time;
  540. $rechargeOrder->paid_amount = $paid_amount;
  541. $rechargeOrder->save();
  542. // 尾款订单支付成功后续操作 fun
  543. //抖音订单服务完成
  544. $order = DouyinOrder::where('id', $params['cp_extra']['douyinOrderId'])->findOrEmpty();
  545. $order->order_status = 4;
  546. $order->pay_status = 1;
  547. $order->total_amount += $paid_amount;
  548. $order->paid_amount += $paid_amount;
  549. $order->save();
  550. //工单完结
  551. self::paymentSuccessful(['sn'=>$out_order_no,'pay_way'=>4]);
  552. return true;
  553. }
  554. }elseif ($params['status'] === 'CANCEL' && $params['message'] == 'TIME_OUT'){
  555. // 超时取消支付
  556. Log::info('支付超时取消:order_id'.$params['order_id']."--out_order_no:" .$params['out_order_no']);
  557. return true;
  558. }
  559. return false;
  560. }
  561. public static function paymentSuccessful($data = [])
  562. {
  563. try {
  564. $params = $data;
  565. $params['sn'] = mb_substr($params['sn'], 0, 18);
  566. $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
  567. if($order->isEmpty()) {
  568. throw new \Exception('内部订单不存在:'.$params['sn'],404);
  569. }
  570. if($order->pay_status == PayEnum::ISPAID) {
  571. return true;
  572. }
  573. if(!empty($params['pay_way']??'')) $params['extra']['pay_way'] = $params['pay_way'];
  574. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  575. if($payNotifyLogic === true){
  576. // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
  577. $order = RechargeOrder::where('sn', $params['sn'])
  578. ->where('payment_type','IN',[0,1])
  579. ->where('pay_status','=',1)
  580. ->findOrEmpty();
  581. if(!$order->isEmpty()){
  582. $workDetail = ServiceWork::findOrEmpty($order->work_id);
  583. if(!$workDetail->isEmpty()){
  584. event('Notice', [
  585. 'scene_id' => 100,
  586. 'params' => [
  587. 'user_id' => 0,
  588. 'order_id' => $workDetail['id'],
  589. 'thing3' => $workDetail['title'],
  590. 'time6' => $workDetail['appointment_time'],
  591. 'phone_number8' => asteriskString($workDetail['mobile']),
  592. 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  593. ]
  594. ]);
  595. }
  596. }
  597. return true;
  598. }
  599. throw new \Exception($payNotifyLogic,404);
  600. }catch(\Exception $e){
  601. throw new \Exception($e->getMessage());
  602. }
  603. }
  604. // ++++++++++++++++++++ 尾款创单 end
  605. // ******************************** 订单预约/改约
  606. // 创建预约单
  607. public static function reservation($params)
  608. {
  609. /*$lon_lat = get_address_lat_lng($params['user_address']);
  610. $params['lon'] = $lon_lat['lon'];
  611. $params['lat'] = $lon_lat['lat'];*/
  612. // $params['order_number'] user_address lon lat appointment_time
  613. Db::startTrans();
  614. try {
  615. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  616. if(!$order->isEmpty()){
  617. $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
  618. $consultation['user_name'] = $params['user_name']??$consultation['user_name'];
  619. $consultation['mobile'] = $params['mobile']??$consultation['mobile'];
  620. $consultation['user_address'] = $params['user_address'];
  621. $consultation['lon'] = $params['lon'];
  622. $consultation['lat'] = $params['lat'];
  623. $consultation['appointment_time'] = $params['appointment_time'];
  624. $consultationOrderId = ExternalConsultationLogic::order($consultation);
  625. if (false === $consultationOrderId) {
  626. throw new \Exception('预约失败');
  627. }
  628. if(!empty($consultationOrderId)){
  629. $consultationOrder = ExternalConsultationOrder::where('id', $consultationOrderId)->findOrEmpty()->toArray();
  630. $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
  631. $order->work_id = $consultationOrder['work_id'];
  632. $order->fulfillment_status = $work_status;
  633. $order->save();
  634. }
  635. $params['dy_order_id'] = $order->dy_order_id;
  636. $params['consultationOrderId'] = $consultationOrderId;
  637. $params['open_id'] = $order->open_id;
  638. $params['goods_id'] = $order->goods_id;
  639. }
  640. Db::commit();
  641. // 抖音创建预约单
  642. //$url = 'api/apps/trade/v2/book/create_book';
  643. //$resData = self::toDyRequestUrl($url,self::getCreateBookParams($params));
  644. //book_id result
  645. // 抖音预约接单结果回调
  646. $bookurl = 'api/apps/trade/v2/book/book_result_callback';
  647. $res = self::toDyRequestUrl($bookurl,[
  648. 'book_id' => $resData['book_id']??$params['dy_order_id'],
  649. 'result' => 1,
  650. ]);
  651. return $order['id']??0;
  652. } catch (\Exception $e) {
  653. Db::rollback();
  654. throw new \Exception($e->getMessage());
  655. }
  656. }
  657. public static function getCreateBookParams($params)
  658. {
  659. try {
  660. $platformGoods = ExternalPlatformGoods::where('goods_id', $params['goods_id'])->where('external_platform_id', self::EXTERNAL_PLATFORM_ID)->findOrEmpty();
  661. if($platformGoods->isEmpty()){
  662. throw new \Exception('产品不存在!');
  663. }
  664. $appointment_time = strtotime($params['appointment_time']);
  665. $book_start_time = $appointment_time * 1000;
  666. $book_end_time = ($appointment_time + (2 * 86400)) * 1000;
  667. $data = [
  668. "order_id"=> (string)$params['dy_order_id']??'',
  669. "out_book_no"=> (string)$params['consultationOrderId']??'',
  670. "open_id"=> (string)$params['open_id']??'',
  671. "item_book_info_list" => [
  672. [
  673. "poi_id" => '7511543640776017961',
  674. "shop_name" => '亿蜂快修·武汉市',
  675. "ext_shop_id" => (string)self::EXTERNAL_PLATFORM_ID,
  676. "goods_id" => (string)$platformGoods->external_goods_sn,
  677. "book_start_time" => (int)$book_start_time??0,
  678. "book_end_time" => (int)$book_end_time??0,
  679. ]
  680. ]
  681. ];
  682. return $data;
  683. } catch (\Exception $e) {
  684. return [];
  685. }
  686. }
  687. public static function upReservation($params)
  688. {
  689. // $params['order_number']
  690. Db::startTrans();
  691. try {
  692. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  693. if(!$order->isEmpty()){
  694. // sn appointment_time
  695. $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
  696. if (false === $result) {
  697. throw new \Exception(ServiceOrderLogic::getError());
  698. }
  699. $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
  700. $order->save();
  701. }
  702. Db::commit();
  703. return $order['id'];
  704. } catch (\Exception $e) {
  705. Db::rollback();
  706. throw new \Exception($e->getMessage());
  707. }
  708. }
  709. // ******************************** 订单退款
  710. public static function cancelOrder($params)
  711. {
  712. // $params['order_number']
  713. Db::startTrans();
  714. try {
  715. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  716. if(!$order->isEmpty()){
  717. if($order->order_status == 1 && $order->pay_status == 0){
  718. $order->order_status = 4;
  719. $order->save();
  720. }else{
  721. throw new \Exception('订单状态不可取消!');
  722. }
  723. }
  724. Db::commit();
  725. return $order['id'];
  726. } catch (\Exception $e) {
  727. Db::rollback();
  728. throw new \Exception($e->getMessage());
  729. }
  730. }
  731. public static function refund($params)
  732. {
  733. Db::startTrans();
  734. try {
  735. // $params['order_number'] user_id
  736. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  737. if($order->isEmpty()){
  738. throw new \Exception('订单不存在');
  739. }
  740. $orderInfo = $order->toArray();
  741. $work_status = $orderInfo['serviceWork']['work_status']??0;
  742. if(3 < $work_status){
  743. throw new \Exception('该订单禁止退款');
  744. }
  745. DouyinRefundOrder::create([
  746. 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
  747. 'order_number' => $orderInfo['order_number'],
  748. 'transaction_id' => $orderInfo['transaction_id'],
  749. 'reason' => $params['reason']??'',
  750. 'refund_status' => 0,
  751. 'user_id' => $orderInfo['user_id'],
  752. 'refund_amount' => $orderInfo['paid_amount'],
  753. ]);
  754. Db::commit();
  755. return true;
  756. } catch (\Exception $e) {
  757. Db::rollback();
  758. throw new \Exception($e->getMessage());
  759. }
  760. }
  761. public static function refundExamine($params)
  762. {
  763. Db::startTrans();
  764. try {
  765. // $params['order_number']
  766. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->findOrEmpty();
  767. if($order->isEmpty()){
  768. throw new \Exception('订单不存在');
  769. }
  770. $orderInfo = $order->toArray();
  771. //$refund_number = $params['refund_number']??'';
  772. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $params['order_number'])->order('id', 'desc')->findOrEmpty();
  773. if($params['is_examine_ok'] === 'pass'){
  774. $douyinRefundOrder->refund_status = 2;
  775. RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
  776. 'pay_status' => 2,
  777. 'pay_time' => 0,
  778. 'paid_amount' => 0,
  779. ]);
  780. ServiceWork::where('id', $orderInfo['work_id'])->update([
  781. 'work_status' => 0,
  782. 'user_confirm_status' => 0,
  783. 'service_status' => 4,
  784. 'work_pay_status' => 0
  785. ]);
  786. }else{
  787. $douyinRefundOrder->refund_status = 1;
  788. }
  789. $douyinRefundOrder->save();
  790. Db::commit();
  791. if($params['is_examine_ok'] === 'pass'){
  792. //通过后向抖音申请退款
  793. self::sendRefundCreate($params['order_number']);
  794. }
  795. return true;
  796. } catch (\Exception $e) {
  797. Db::rollback();
  798. throw new \Exception($e->getMessage());
  799. }
  800. }
  801. public static function sendRefundCreate($order_number)
  802. {
  803. try {
  804. // $params['order_number']
  805. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $order_number)->findOrEmpty();
  806. if($order->isEmpty()){
  807. throw new \Exception('订单不存在');
  808. }
  809. $orderInfo = $order->toArray();
  810. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $order_number)->order('id', 'desc')->findOrEmpty();
  811. //通过后向抖音申请退款
  812. $url = 'api/trade_basic/v1/developer/refund_create/';
  813. $data = [
  814. "order_id" => $orderInfo['transaction_id'],
  815. "out_refund_no" => $douyinRefundOrder->refund_number,
  816. "cp_extra" => $orderInfo['id'].'|'.$douyinRefundOrder->id,
  817. "order_entry_schema" => [
  818. "path" => "page/index/index",
  819. "params" => json_encode(['refund_number'=>$douyinRefundOrder->refund_number])
  820. ],
  821. "refund_total_amount " => $douyinRefundOrder->refund_amount * 100,
  822. //"notify_url" => config('douyin.refundNotifyUrl'),
  823. "refund_reason" => [
  824. [
  825. "code" => 101,
  826. "text" => "不想要了"
  827. ]
  828. ]
  829. ];
  830. $resData = self::toDyRequestUrl($url,$data);
  831. if(isset($resData['data']) && $resData['data']){
  832. $douyinRefundOrder->transaction_id = $resData['data']['refund_id'];
  833. $douyinRefundOrder->save();
  834. }
  835. return true;
  836. } catch (\Exception $e) {
  837. Log::info($e->getMessage());
  838. return false;
  839. }
  840. }
  841. public static function refundNotify($params)
  842. {
  843. Db::startTrans();
  844. try {
  845. $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
  846. if($douyinRefundOrder->isEmpty()){
  847. throw new \Exception('退款订单不存在');
  848. }
  849. if($douyinRefundOrder->refund_status == 0){
  850. if($params['status'] === 'SUCCESS'){
  851. $douyinRefundOrder->refund_status = 3;
  852. DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
  853. 'order_status' => 4,
  854. 'pay_status' => 3,
  855. ]);
  856. }elseif($params['status'] === 'FAIL'){
  857. $douyinRefundOrder->refund_status = 4;
  858. }else{
  859. throw new \Exception('退款状态未知');
  860. }
  861. $douyinRefundOrder->save();
  862. }
  863. Db::commit();
  864. return true;
  865. } catch (\Exception $e) {
  866. Db::rollback();
  867. throw new \Exception($e->getMessage());
  868. }
  869. }
  870. public static function toDyRequestUrl($url,$data,$headers = [],$resFunction = 'extraErrorCodeReturn',$isHost = 0)
  871. {
  872. $toData = [
  873. 'url' => $url,
  874. 'data' => $data,
  875. 'headers' => $headers,
  876. 'resFunction' => $resFunction,
  877. 'isHost' => $isHost
  878. ];
  879. $res = commonHttpClient(env('internal_api.api_url_host').'platf/dou_yin/toDyRequestUrl', $toData, 'post', 'json', ['Content-Type' => 'application/json']);
  880. Log::info(json_encode($res));
  881. if(isset($res['code']) && $res['code'] === 0){
  882. Log::info("toDyRequestUrl:".json_encode($res));
  883. return $res['data'];
  884. }else{
  885. Log::info("toDyRequestUrl:".$res['msg']);
  886. throw new \Exception($res['msg']);
  887. }
  888. }
  889. }