DouYinService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. namespace app\api\service;
  3. use app\adminapi\logic\external\ExternalConsultationLogic;
  4. use app\api\logic\ServiceOrderLogic;
  5. use app\common\model\Config;
  6. use app\common\model\external\DouyinOrder;
  7. use app\common\model\external\DouyinRefundOrder;
  8. use app\common\model\external\ExternalConsultation;
  9. use app\common\model\external\ExternalConsultationOrder;
  10. use app\common\model\external\ExternalPlatformGoods;
  11. use app\common\model\goods\Goods;
  12. use app\common\model\goods_category\GoodsCategory;
  13. use app\common\model\recharge\RechargeOrder;
  14. use app\common\model\user\User;
  15. use app\common\model\user\UserAuth;
  16. use app\common\model\works\ServiceWork;
  17. use app\common\service\ConfigService;
  18. use app\common\service\FileService;
  19. use think\facade\Db;
  20. use think\facade\Log;
  21. class DouYinService
  22. {
  23. protected static int $terminal = \app\common\enum\user\UserTerminalEnum::DOUYIN;
  24. protected static int $external_platform_id = 6;
  25. protected CONST EXTERNAL_PLATFORM_ID = 6;
  26. // ********************************* 注册登录
  27. public static function register(array $params)
  28. {
  29. $userSn = User::createUserSn();
  30. $params['password'] = !empty($params['password'])?$params['password']:rand(100000,999999);
  31. $passwordSalt = \think\facade\Config::get('project.unique_identification');
  32. $password = create_password($params['password'], $passwordSalt);
  33. $avatar = ConfigService::get('default_image', 'user_avatar');
  34. $user = User::create([
  35. 'sn' => $userSn,
  36. 'avatar' => $avatar,
  37. 'nickname' => '用户' . $userSn,
  38. 'account' => $params['account'],
  39. 'mobile' => !empty($params['mobile'])?$params['mobile']:'',
  40. 'password' => $password,
  41. 'channel' => self::$terminal,
  42. 'user_type' => $params['user_type']??0,
  43. ]);
  44. return $user;
  45. }
  46. public static function phoneLogin(array $params)
  47. {
  48. try {
  49. $where = ['mobile' => $params['mobile']];
  50. $params['account'] = $params['mobile'];
  51. $user = User::where($where)->findOrEmpty();
  52. if ($user->isEmpty()) {
  53. //直接注册用户
  54. $params['channel'] = self::$terminal;
  55. $user = self::register($params);
  56. }
  57. //更新登录信息
  58. $user->login_time = time();
  59. $user->login_ip = request()->ip();
  60. $user->save();
  61. $userInfo = UserTokenService::setToken($user->id, self::$terminal);
  62. //返回登录信息
  63. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  64. $avatar = FileService::getFileUrl($avatar);
  65. return [
  66. 'nickname' => $userInfo['nickname'],
  67. 'sn' => $userInfo['sn'],
  68. 'mobile' => $userInfo['mobile'],
  69. 'avatar' => $avatar,
  70. 'token' => $userInfo['token'],
  71. ];
  72. } catch (\Exception $e) {
  73. throw new \Exception($e->getMessage());
  74. }
  75. }
  76. // **************************** 商品管理 goods_category_id goods_id external_platform_id
  77. public static function addProduct($params)
  78. {
  79. $send_url = env('internal_api.api_url_host').'platf/dou_yin/addGoods';
  80. $res = http_request($send_url,http_build_query($params));
  81. Log::info('addProduct:'
  82. .'url:'.$send_url
  83. .'|data:'.json_encode($params,JSON_UNESCAPED_UNICODE)
  84. .'|res:'.json_encode([$res],JSON_UNESCAPED_UNICODE)
  85. );
  86. return $res?:[];
  87. }
  88. // ******************************** 订单业务
  89. public static function getOrderDetail($params)
  90. {
  91. //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
  92. // $params['order_number'] user_id
  93. $order = DouyinOrder::with(['goods','serviceWork','douyinRefundOrder'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  94. if($order->isEmpty()){
  95. return [];
  96. }
  97. $orderInfo = $order->toArray();
  98. empty($orderInfo['goods']) && $orderInfo['goods'] = [];
  99. empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
  100. empty($orderInfo['douyinRefundOrder']) && $orderInfo['douyinRefundOrder'] = [];
  101. $work_status = $orderInfo['serviceWork']['work_status']??0;
  102. $performance = [];
  103. // tmp
  104. switch ($work_status){
  105. case 0:
  106. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  107. break;
  108. case 1:
  109. case 2:
  110. case 3:
  111. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  112. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  113. break;
  114. case 4:
  115. case 5:
  116. case 6:
  117. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  118. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  119. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  120. break;
  121. case 7:
  122. case 8:
  123. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  124. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  125. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  126. $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
  127. break;
  128. }
  129. $orderInfo['performance'] = $performance;
  130. return $orderInfo;
  131. }
  132. // 预下单接口 - 前端 首次/尾款
  133. public static function getPluginCreateOrderData($goods_id, $quantity, $douyinOrderId)
  134. {
  135. try {
  136. $goods = Goods::where('id',$goods_id)->findOrEmpty();
  137. if($goods->isEmpty()){
  138. throw new \Exception('商品不存在!');
  139. }
  140. $goods = $goods->toArray();
  141. $platformGoods = ExternalPlatformGoods::where('goods_id', $goods_id)->where('external_platform_id', self::EXTERNAL_PLATFORM_ID)->findOrEmpty();
  142. if($platformGoods->isEmpty()){
  143. throw new \Exception('外部商品不存在!');
  144. }
  145. $platformGoods = $platformGoods->toArray();
  146. $quantity = $quantity?:1;
  147. $data = [
  148. /*"goodsList" => [
  149. "quantity" => $quantity,
  150. "price" => $platformGoods['service_fee'] * 100,
  151. "goodsName" => $goods['goods_name'],
  152. "goodsPhoto" => $goods['goods_image']??'',
  153. "goodsId" => '',
  154. "goodsType" => 1
  155. ],*/
  156. "skuList" => [
  157. [
  158. "quantity" => $quantity,
  159. "skuId" => (string)$platformGoods['external_goods_sn'],
  160. "skuType" => 2,
  161. "price" => $platformGoods['service_fee'] * 100,
  162. ]
  163. ],
  164. /*"bookInfo" => [
  165. "itemBookInfoList"=>[
  166. [
  167. "poiId" => '7511543640776017961',
  168. "shopName" => '亿蜂快修·武汉市',
  169. "outShopId" => self::EXTERNAL_PLATFORM_ID,
  170. "skuId" => (string)$platformGoods['external_goods_sn'],
  171. "bookStartTime" => $bookStartTime?:'',
  172. "bookEndTime" => $bookEndTime?:'',
  173. ]
  174. ]
  175. ],*/
  176. "payment" => [
  177. "totalAmount" => $quantity * $platformGoods['service_fee'] * 100,
  178. ],
  179. //"callbackUrl" => $callbackUrl?:'',
  180. "callbackData" => [
  181. "outShopId" => self::EXTERNAL_PLATFORM_ID,
  182. "skuId" => (string)$platformGoods['external_goods_sn'],
  183. "quantity" => $quantity,
  184. "douyinOrderId" => $douyinOrderId?:0,
  185. ],
  186. /*"tradeOption" => json_encode([
  187. "life_trade_flag" => 1,
  188. "order_relation_info" => [
  189. "related_order_id" => 0, // 加价时上个订单号
  190. "relation_type" => 'multi_buy_as_one'
  191. ]
  192. ])*/
  193. ];
  194. if($douyinOrderId){ // 说明是来自首单订单即要创建尾款
  195. $data['callbackUrl'] = '';
  196. $data['tradeOption'] = [
  197. "life_trade_flag" => 1,
  198. "order_relation_info" => [
  199. "related_order_id" => '', // 加价时上个订单号
  200. "relation_type" => 'multi_buy_as_one'
  201. ]
  202. ];
  203. }
  204. /*else{ // 创建首单订单
  205. $data['callbackUrl'] = '';
  206. }*/
  207. return $data;
  208. } catch (\Exception $e) {
  209. throw new \Exception($e->getMessage());
  210. }
  211. }
  212. // ++++++++++++++++++++ 首次/尾款 扩展点
  213. /**
  214. * 预下单扩展点
  215. * @param $params
  216. * @throws \Exception
  217. * @author liugc <466014217@qq.com>
  218. * @date 2025/6/4 14:03
  219. */
  220. public static function submitOrderNotify($params = [])
  221. {
  222. try {
  223. $params['external_platform_id'] = self::EXTERNAL_PLATFORM_ID;
  224. // order_id goods total_amount discount cp_extra create_order_time phone_num contact_name open_id
  225. if(empty($params['phone_num'])){
  226. throw new \Exception('手机号不能为空!');
  227. }
  228. $user_id = $params['user_id'];
  229. if($params['cp_extra']['douyinOrderId']>0){ // 说明是尾款单
  230. // 创建尾款单
  231. $order_number = self::tailOrder([
  232. 'douyinOrderId'=>$params['cp_extra']['douyinOrderId'],
  233. 'user_id'=>$user_id??0,
  234. 'total_amount'=>$params['total_amount'],
  235. 'discount_amount'=>$params['discount_amount'],
  236. ]);
  237. //$payNotifyUrl = 'payTailNotify';
  238. }else{
  239. // 创建首单 goods_id user_info.mobile user_id quantity
  240. $order_number = self::submitOrder([
  241. 'goods_id'=>$params['cp_extra']['skuId'],
  242. 'user_id'=>$user_id??0,
  243. 'mobile'=>$params['phone_num'],
  244. 'quantity'=>$params['cp_extra']['quantity']
  245. ]);
  246. }
  247. return [
  248. "outOrderNo" => $order_number,
  249. "orderEntrySchema" => [
  250. "path" => "page/index/index",
  251. "params" => json_encode(['order_number' => $order_number])
  252. ],
  253. "payNotifyUrl" => $payNotifyUrl??''
  254. ];
  255. } catch (\Exception $e) {
  256. throw new \Exception($e->getMessage());
  257. }
  258. }
  259. // ++++++++++++++++++++ 首次创单
  260. /**
  261. * 预下单扩展点-子
  262. * @param array $params goods_id user_info.mobile user_id quantity
  263. * @return array|false
  264. */
  265. public static function submitOrder($params)
  266. {
  267. Db::startTrans();
  268. try {
  269. $goods = Goods::findOrEmpty($params['goods_id']);
  270. if($goods->isEmpty()){
  271. throw new \Exception('产品不存在!');
  272. }
  273. if(empty($params['mobile'])){
  274. throw new \Exception('请先补充您的联系方式后在提交订单');
  275. }
  276. // TODO tmp防抖1m
  277. $isExist = DouyinOrder::where(['user_id'=>$params['user_id'],'goods_id'=>$goods['id']])->where('create_time','>',(time() - 60))->findOrEmpty();
  278. if(!$isExist->isEmpty()){
  279. throw new \Exception('请勿重复下单!');
  280. }
  281. $quantity = $params['quantity']??1;
  282. //生成订单
  283. $create_data = [
  284. 'user_id' => $params['user_id'],
  285. 'mobile' => $params['mobile'],
  286. 'title' => $goods['goods_name'],
  287. 'goods_id'=>$goods['id'],
  288. 'unit_price' => $goods['service_fee'],
  289. 'quantity' => $quantity,
  290. 'total_amount' => $goods['service_fee'] * $quantity,
  291. 'order_number' => generate_sn(DouyinOrder::class, 'order_number'),
  292. ];
  293. $order = DouyinOrder::create($create_data);
  294. Db::commit();
  295. return $create_data['order_number'];
  296. } catch (\Exception $e) {
  297. Db::rollback();
  298. throw new \Exception($e->getMessage());
  299. }
  300. }
  301. // 支付成功回调
  302. public static function payNotify($params)
  303. {
  304. Log::write(json_encode($params));
  305. // 查询抖音订单是否完成支付
  306. if ($params['status'] === 'SUCCESS') {
  307. $transaction_id = $params['order_id']??'';
  308. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  309. $out_order_no = $params['out_order_no'];
  310. $pay_time = $params['event_time']??time();
  311. $order = DouyinOrder::where('order_number', $out_order_no)->findOrEmpty();
  312. if(!$order->isEmpty()){
  313. // 更新充值订单状态
  314. $order->transaction_id = $transaction_id;
  315. $order->order_status = 2;
  316. $order->pay_time = $pay_time;
  317. $order->paid_amount = $paid_amount;
  318. $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
  319. $form_detail = [
  320. 'user_name' => $user['real_name']??'',
  321. 'mobile' => $user['mobile'],
  322. 'transaction_id' => $transaction_id,
  323. 'out_trade_no' => $out_order_no,
  324. 'paid_amount' => $paid_amount,
  325. 'params' => $params,
  326. ];
  327. $consultation = ExternalConsultation::create([
  328. 'external_platform_id' => self::EXTERNAL_PLATFORM_ID,
  329. 'form_detail' => json_encode($form_detail),
  330. 'user_name' => $user['real_name']??'',
  331. 'mobile' => $user['mobile'],
  332. 'goods_id' => $order->goods_id,
  333. 'amount' => $paid_amount
  334. ]);
  335. $order->consultation_id = $consultation->id;
  336. $order->save();
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. // ++++++++++++++++++++ 首次创单 end
  343. // ++++++++++++++++++++ 尾款创单
  344. /**
  345. * 预下单扩展点-子
  346. * @param array $params goods_id user_info.mobile user_id quantity
  347. * @return array|false
  348. */
  349. public static function tailOrder($params)
  350. {
  351. Db::startTrans();
  352. try {
  353. $amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  354. $work_id = DouyinOrder::where(['id'=>$params['douyinOrderId']])->value('work_id');
  355. $sn = '';
  356. $rechargeOrder = RechargeOrder::where(['work_id'=>$work_id,'payment_type'=>2])->findOrEmpty();
  357. if($rechargeOrder->isEmpty()){
  358. //新增待支付尾款
  359. $sn = generate_sn(RechargeOrder::class, 'sn');
  360. $order_data = [
  361. 'order_type' => 0,
  362. 'sn' => $sn,
  363. 'order_terminal' => 1,
  364. 'work_id' => $work_id,
  365. 'user_id' => $params['user_id'],
  366. 'payment_type' => 2,
  367. 'order_total' => $amount,
  368. 'order_amount' => $amount,
  369. 'pay_status' => 0,
  370. 'paid_amount' => 0,
  371. 'pay_way' => 4
  372. ];
  373. RechargeOrder::create($order_data);
  374. }
  375. Db::commit();
  376. return $sn;
  377. } catch (\Exception $e) {
  378. Db::rollback();
  379. throw new \Exception($e->getMessage());
  380. }
  381. }
  382. // 尾款支付成功回调
  383. public static function payTailNotify($params)
  384. {
  385. Log::write(json_encode($params));
  386. // 查询抖音订单是否完成支付
  387. if ($params['status'] === 'SUCCESS') {
  388. $transaction_id = $params['order_id']??'';
  389. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  390. $out_order_no = $params['out_order_no'];
  391. $pay_time = $params['event_time']??time();
  392. $rechargeOrder = RechargeOrder::where(['sn'=>$out_order_no,'payment_type'=>2])->findOrEmpty();
  393. if(!$rechargeOrder->isEmpty()){
  394. // 更新充值订单状态
  395. $rechargeOrder->transaction_id = $transaction_id;
  396. $rechargeOrder->pay_status = 1;
  397. $rechargeOrder->pay_time = $pay_time;
  398. $rechargeOrder->paid_amount = $paid_amount;
  399. $rechargeOrder->save();
  400. return true;
  401. }
  402. }
  403. return false;
  404. }
  405. // ++++++++++++++++++++ 尾款创单 end
  406. // ******************************** 订单预约/改约
  407. // 创建预约单
  408. public static function reservation($params)
  409. {
  410. /*$lon_lat = get_address_lat_lng($params['user_address']);
  411. $params['lon'] = $lon_lat['lon'];
  412. $params['lat'] = $lon_lat['lat'];*/
  413. // $params['order_number']
  414. Db::startTrans();
  415. try {
  416. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  417. if(!$order->isEmpty()){
  418. $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
  419. $consultation['user_name'] = $params['user_name']??$consultation['user_name'];
  420. $consultation['mobile'] = $params['mobile']??$consultation['mobile'];
  421. $consultation['user_address'] = $params['user_address'];
  422. $consultation['lon'] = $params['lon'];
  423. $consultation['lat'] = $params['lat'];
  424. $consultation['appointment_time'] = $params['appointment_time'];
  425. $result = ExternalConsultationLogic::order($consultation);
  426. if (false === $result) {
  427. throw new \Exception('预约失败');
  428. }
  429. if(!empty($result)){
  430. $consultationOrder = ExternalConsultationOrder::where('consultation_id', $order->consultation_id)->where('goods_id', $order->goods_id)->where('amount', $order->paid_amount)
  431. ->findOrEmpty()->toArray();
  432. $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
  433. $order->work_id = $consultationOrder['work_id'];
  434. $order->fulfillment_status = $work_status;
  435. $order->save();
  436. }
  437. }
  438. Db::commit();
  439. // 抖音创建预约单
  440. $url = 'api/apps/trade/v2/book/create_book';
  441. $resData = self::toDyRequestUrl($url,self::getCreateBookParams($params));
  442. //book_id result
  443. // 抖音预约接单结果回调
  444. $bookurl = 'api/apps/trade/v2/book/book_result_callback';
  445. $res = self::toDyRequestUrl($bookurl,[
  446. 'book_id' => $resData['book_id']??'',
  447. 'result' => 1,
  448. ]);
  449. return $order['id']??0;
  450. } catch (\Exception $e) {
  451. Db::rollback();
  452. throw new \Exception($e->getMessage());
  453. }
  454. }
  455. public static function getCreateBookParams($params)
  456. {
  457. try {
  458. $data = [
  459. "order_id"=> $params['order_number']??'',
  460. "out_book_no"=> time()??'',
  461. "open_id"=> $params['open_id']??'',
  462. "item_book_info_list" => [
  463. [
  464. "poi_id" => '7511543640776017961',
  465. "shop_name" => '亿蜂快修·武汉市',
  466. "ext_shop_id" => self::EXTERNAL_PLATFORM_ID,
  467. "goods_id" => (string)$params['external_goods_sn'],
  468. "book_start_time" => $params['book_start_time']??'',
  469. "book_end_time" => $params['book_end_time']??'',
  470. ]
  471. ]
  472. ];
  473. return $data;
  474. } catch (\Exception $e) {
  475. return [];
  476. }
  477. }
  478. public static function upReservation($params)
  479. {
  480. // $params['order_number']
  481. Db::startTrans();
  482. try {
  483. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  484. if(!$order->isEmpty()){
  485. // sn appointment_time
  486. $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
  487. if (false === $result) {
  488. throw new \Exception(ServiceOrderLogic::getError());
  489. }
  490. $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
  491. $order->save();
  492. }
  493. Db::commit();
  494. return $order['id'];
  495. } catch (\Exception $e) {
  496. Db::rollback();
  497. throw new \Exception($e->getMessage());
  498. }
  499. }
  500. // ******************************** 订单退款
  501. public static function cancelOrder($params)
  502. {
  503. // $params['order_number']
  504. Db::startTrans();
  505. try {
  506. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  507. if(!$order->isEmpty()){
  508. if($order->order_status == 1 && $order->pay_status == 0){
  509. $order->order_status = 4;
  510. $order->save();
  511. }else{
  512. throw new \Exception('订单状态不可取消!');
  513. }
  514. }
  515. Db::commit();
  516. return $order['id'];
  517. } catch (\Exception $e) {
  518. Db::rollback();
  519. throw new \Exception($e->getMessage());
  520. }
  521. }
  522. public static function refund($params)
  523. {
  524. Db::startTrans();
  525. try {
  526. // $params['order_number'] user_id
  527. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  528. if($order->isEmpty()){
  529. throw new \Exception('订单不存在');
  530. }
  531. $orderInfo = $order->toArray();
  532. $work_status = $orderInfo['serviceWork']['work_status']??0;
  533. if(3 < $work_status){
  534. throw new \Exception('该订单禁止退款');
  535. }
  536. DouyinRefundOrder::create([
  537. 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
  538. 'order_number' => $orderInfo['order_number'],
  539. 'transaction_id' => $orderInfo['transaction_id'],
  540. 'reason' => $params['reason']??'',
  541. 'refund_status' => 0,
  542. 'user_id' => $orderInfo['user_id'],
  543. 'refund_amount' => $orderInfo['paid_amount'],
  544. ]);
  545. Db::commit();
  546. return true;
  547. } catch (\Exception $e) {
  548. Db::rollback();
  549. throw new \Exception($e->getMessage());
  550. }
  551. }
  552. public static function refundExamine($params)
  553. {
  554. Db::startTrans();
  555. try {
  556. // $params['order_number']
  557. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->findOrEmpty();
  558. if($order->isEmpty()){
  559. throw new \Exception('订单不存在');
  560. }
  561. $orderInfo = $order->toArray();
  562. //$refund_number = $params['refund_number']??'';
  563. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $params['order_number'])->order('id', 'desc')->findOrEmpty();
  564. if($params['is_examine_ok'] === 'pass'){
  565. $douyinRefundOrder->refund_status = 2;
  566. RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
  567. 'pay_status' => 2,
  568. 'pay_time' => 0,
  569. 'paid_amount' => 0,
  570. ]);
  571. ServiceWork::where('id', $orderInfo['work_id'])->update([
  572. 'work_status' => 0,
  573. 'user_confirm_status' => 0,
  574. 'service_status' => 4,
  575. 'work_pay_status' => 0
  576. ]);
  577. }else{
  578. $douyinRefundOrder->refund_status = 1;
  579. }
  580. $douyinRefundOrder->save();
  581. Db::commit();
  582. if($params['is_examine_ok'] === 'pass'){
  583. //通过后向抖音申请退款
  584. self::sendRefundCreate($params['order_number']);
  585. }
  586. return true;
  587. } catch (\Exception $e) {
  588. Db::rollback();
  589. throw new \Exception($e->getMessage());
  590. }
  591. }
  592. public static function sendRefundCreate($order_number)
  593. {
  594. try {
  595. // $params['order_number']
  596. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $order_number)->findOrEmpty();
  597. if($order->isEmpty()){
  598. throw new \Exception('订单不存在');
  599. }
  600. $orderInfo = $order->toArray();
  601. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $order_number)->order('id', 'desc')->findOrEmpty();
  602. //通过后向抖音申请退款
  603. $url = 'api/trade_basic/v1/developer/refund_create/';
  604. $data = [
  605. "order_id" => $orderInfo['transaction_id'],
  606. "out_refund_no" => $douyinRefundOrder->refund_number,
  607. "cp_extra" => $orderInfo['id'].'|'.$douyinRefundOrder->id,
  608. "order_entry_schema" => [
  609. "path" => "page/index/index",
  610. "params" => json_encode(['refund_number'=>$douyinRefundOrder->refund_number])
  611. ],
  612. "refund_total_amount " => $douyinRefundOrder->refund_amount * 100,
  613. //"notify_url" => config('douyin.refundNotifyUrl'),
  614. "refund_reason" => [
  615. [
  616. "code" => 101,
  617. "text" => "不想要了"
  618. ]
  619. ]
  620. ];
  621. $resData = self::toDyRequestUrl($url,$data);
  622. if(isset($resData['data']) && $resData['data']){
  623. $douyinRefundOrder->transaction_id = $resData['data']['refund_id'];
  624. $douyinRefundOrder->save();
  625. }
  626. return true;
  627. } catch (\Exception $e) {
  628. Log::info($e->getMessage());
  629. return false;
  630. }
  631. }
  632. public static function refundNotify($params)
  633. {
  634. Db::startTrans();
  635. try {
  636. $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
  637. if($douyinRefundOrder->isEmpty()){
  638. throw new \Exception('退款订单不存在');
  639. }
  640. if($douyinRefundOrder->refund_status == 0){
  641. if($params['status'] === 'SUCCESS'){
  642. $douyinRefundOrder->refund_status = 3;
  643. DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
  644. 'order_status' => 4,
  645. 'pay_status' => 3,
  646. ]);
  647. }elseif($params['status'] === 'FAIL'){
  648. $douyinRefundOrder->refund_status = 4;
  649. }else{
  650. throw new \Exception('退款状态未知');
  651. }
  652. $douyinRefundOrder->save();
  653. }
  654. Db::commit();
  655. return true;
  656. } catch (\Exception $e) {
  657. Db::rollback();
  658. throw new \Exception($e->getMessage());
  659. }
  660. }
  661. public static function toDyRequestUrl($url,$data)
  662. {
  663. $toData = [
  664. 'url' => $url,
  665. 'data' => $data
  666. ];
  667. $res = http_request(env('internal_api.api_url_host').'platf/dou_yin/toDyRequestUrl',$toData,['Content-Type' => 'application/json;charset=utf-8']);
  668. Log::info(json_encode($res));
  669. if($res['code'] === 0){
  670. Log::info("toDyRequestUrl:".json_encode($res));
  671. return $res['data'];
  672. }else{
  673. Log::info("toDyRequestUrl:".$res['msg']);
  674. throw new \Exception($res['msg']);
  675. }
  676. }
  677. }