DouYinService.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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\goods\Goods;
  11. use app\common\model\recharge\RechargeOrder;
  12. use app\common\model\user\User;
  13. use app\common\model\user\UserAuth;
  14. use app\common\model\works\ServiceWork;
  15. use app\common\service\ConfigService;
  16. use app\common\service\FileService;
  17. use think\facade\Db;
  18. use think\facade\Log;
  19. class DouYinService
  20. {
  21. protected static int $terminal = \app\common\enum\user\UserTerminalEnum::DOUYIN;
  22. protected static int $external_platform_id = 6;
  23. // ********************************* 注册登录
  24. public static function register(array $params)
  25. {
  26. $userSn = User::createUserSn();
  27. $params['password'] = !empty($params['password'])?$params['password']:rand(100000,999999);
  28. $passwordSalt = \think\facade\Config::get('project.unique_identification');
  29. $password = create_password($params['password'], $passwordSalt);
  30. $avatar = ConfigService::get('default_image', 'user_avatar');
  31. $user = User::create([
  32. 'sn' => $userSn,
  33. 'avatar' => $avatar,
  34. 'nickname' => '用户' . $userSn,
  35. 'account' => $params['account'],
  36. 'mobile' => !empty($params['mobile'])?$params['mobile']:'',
  37. 'password' => $password,
  38. 'channel' => self::$terminal,
  39. 'user_type' => $params['user_type']??0,
  40. ]);
  41. return $user;
  42. }
  43. public static function phoneLogin(array $params)
  44. {
  45. try {
  46. $where = ['mobile' => $params['mobile']];
  47. $params['account'] = $params['mobile'];
  48. $user = User::where($where)->findOrEmpty();
  49. if ($user->isEmpty()) {
  50. //直接注册用户
  51. $params['channel'] = self::$terminal;
  52. $user = self::register($params);
  53. }
  54. //更新登录信息
  55. $user->login_time = time();
  56. $user->login_ip = request()->ip();
  57. $user->save();
  58. $userInfo = UserTokenService::setToken($user->id, self::$terminal);
  59. //返回登录信息
  60. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  61. $avatar = FileService::getFileUrl($avatar);
  62. return [
  63. 'nickname' => $userInfo['nickname'],
  64. 'sn' => $userInfo['sn'],
  65. 'mobile' => $userInfo['mobile'],
  66. 'avatar' => $avatar,
  67. 'token' => $userInfo['token'],
  68. ];
  69. } catch (\Exception $e) {
  70. throw new \Exception($e->getMessage());
  71. }
  72. }
  73. // **************************** 商品管理
  74. public static function addProduct($params)
  75. {
  76. $goods_category_id = $params['goods_category_id']??0;
  77. $product_url = config('douyin.host').'goodlife/v1/goods/product/save/';
  78. $res = http_request($product_url,self::getProductParams($params),['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
  79. return [];
  80. }
  81. public static function addSku($params)
  82. {
  83. $goods_id = $params['goods_id']??0;
  84. $sku_url = config('douyin.host').'goodlife/v1/goods/sku/batch_save/';
  85. $res = http_request($sku_url,self::getSkuParams($params),['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
  86. // 根据返回后更新本地库对应关系
  87. return [];
  88. }
  89. /*/product/save/ 先调
  90. goods/sku/batch_save/ 再调
  91. 生成 产品组装
  92. 生成 sku 组装
  93. 生成对应的 attr_key_value_map 组装*/
  94. public static function getProductParams($params)
  95. {
  96. $goods_category_id = $params['goods_category_id']??0;
  97. return [
  98. "account_id" => '7511543640776017961',
  99. "product" => [
  100. "out_id" => $goods_category_id,
  101. "product_name" => $params['name'],
  102. "product_type" => 22,
  103. "category_id" => 6004003,
  104. "category_full_name" => "其他维修服务",
  105. "biz_line" => 5,
  106. "account_name" => "亿蜂快修·武汉市",
  107. //{\"params\":\"{\"spuId\":\"xxxxx\",\"skuId\":\"xxxxxx\"}\",\"path\":\"pages/any/path\",\"app_id\":\"xxxxx\"}
  108. "out_url" => json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$goods_category_id])]),
  109. "pois" =>[
  110. [
  111. "poi_id" => "7511543640776017961"
  112. ]
  113. ],
  114. "product_ext" => [
  115. "auto_online" => true,
  116. "display_price"=> ["low_price" => 30,"high_price" => 300],
  117. "test_extra"=> ["test_flag" => true,"uids" => ["1548********2888"]]
  118. ],
  119. "attr_key_value_map" => self::getAttrKeyValueMapParams('product',$params),
  120. "sold_end_time" => time() + 180 * 86400,
  121. "sold_start_time" => time()
  122. ]
  123. ];
  124. }
  125. public static function getSkuParams($params)
  126. {
  127. return [];
  128. }
  129. public static function getAttrKeyValueMapParams($type,$params)
  130. {
  131. $res = [];
  132. if($type === 'product'){
  133. $res = [
  134. "appointment"=>json_encode([
  135. "need_appointment"=>true,
  136. "ahead_time_type"=>2,
  137. "ahead_hour_num"=> 5,
  138. "external_link"=>"pages/detail",
  139. "order_appointment_time_url"=>"pages/order"
  140. ]),
  141. "auto_renew"=>true,
  142. "can_no_use_date" => json_encode(["enable"=>false]),
  143. "Description" => json_encode([]),
  144. "image_list" => json_encode([["url"=>$params['picture']??'']]),
  145. "limit_use_rule" => json_encode(["is_limit_use"=>true, "use_num_per_consume"=>1]),
  146. "Notification" => json_encode([["title"=>$params['name']??'',"content"=>$params['name']??'']]),
  147. "RefundPolicy"=> "2",
  148. "refund_need_merchant_confirm"=> true,
  149. "show_channel"=> 1,
  150. "superimposed_discounts"=> false,
  151. "trade_url"=> json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['goods_category_id']??0])]),
  152. "use_date"=> json_encode(["use_date_type"=>2, "day_duration"=>15]),
  153. "use_time"=> json_encode(["use_time_type"=>1]),
  154. //"user_num_limit"=> ,
  155. "code_source_type"=> 3,
  156. "settle_type"=> 1,
  157. "use_type"=> 1,
  158. "limit_rule"=> json_encode(["is_limit"=>false]),
  159. "out_id"=> $params['goods_category_id']??0
  160. ];
  161. }
  162. if($type === 'sku'){
  163. $res = [
  164. /*"appointment"=>json_encode([
  165. "need_appointment"=>true,
  166. "ahead_time_type"=>2,
  167. "ahead_hour_num"=> 5,
  168. "external_link"=>"pages/detail",
  169. "order_appointment_time_url"=>"pages/order"
  170. ]),
  171. "auto_renew"=>true,
  172. "can_no_use_date" => json_encode(["enable"=>false]),
  173. "Description" => json_encode([]),
  174. "image_list" => json_encode([["url"=>$params['goods_image']]]),
  175. "limit_use_rule" => json_encode(["is_limit_use"=>true, "use_num_per_consume"=>1]),
  176. "Notification" => json_encode([["title"=>$params['goods_name'],"content"=>$params['goods_name']]]),
  177. "RefundPolicy"=> "2",
  178. "refund_need_merchant_confirm"=> true,
  179. "show_channel"=> 1,
  180. "superimposed_discounts"=> false,
  181. "trade_url"=> json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['goods_category_id']??0])]),
  182. "use_date"=> json_encode(["use_date_type"=>2, "day_duration"=>15]),
  183. "use_time"=> json_encode(["use_time_type"=>1]),
  184. //"user_num_limit"=> ,
  185. "code_source_type"=> 3,
  186. "settle_type"=> 1,
  187. "use_type"=> 1,
  188. "limit_rule"=> json_encode(["is_limit"=>false]),
  189. "out_id"=> $params['goods_category_id']??0*/
  190. ];
  191. }
  192. return $res;
  193. }
  194. /**
  195. * 提交订单
  196. * @param array $params
  197. * @return array|false
  198. */
  199. public static function submitOrder($params)
  200. {
  201. Db::startTrans();
  202. try {
  203. $goods = Goods::findOrEmpty($params['goods_id']);
  204. if($goods->isEmpty()){
  205. throw new \Exception('产品不存在!');
  206. }
  207. if(empty($params['user_info']['mobile'])){
  208. throw new \Exception('请先补充您的联系方式后在提交订单');
  209. }
  210. // TODO tmp防抖1m
  211. $isExist = DouyinOrder::where(['user_id'=>$params['user_id'],'goods_id'=>$goods['id']])->where('create_time','>',(time() - 60))->findOrEmpty();
  212. if(!$isExist->isEmpty()){
  213. throw new \Exception('请勿重复下单!');
  214. }
  215. $quantity = $params['quantity']??1;
  216. //生成订单
  217. $create_data = [
  218. 'user_id' => $params['user_id'],
  219. 'mobile' => $params['user_info']['mobile'],
  220. 'title' => $goods['goods_name'],
  221. 'goods_id'=>$goods['id'],
  222. 'unit_price' => $goods['service_fee'],
  223. 'quantity' => $quantity,
  224. 'total_amount' => $goods['service_fee'] * $quantity,
  225. 'order_number' => generate_sn(DouyinOrder::class, 'order_number'),
  226. ];
  227. $order = DouyinOrder::create($create_data);
  228. Db::commit();
  229. return $create_data['order_number'];
  230. } catch (\Exception $e) {
  231. Db::rollback();
  232. throw new \Exception($e->getMessage());
  233. }
  234. }
  235. public static function getByteAuthorization($order_number)
  236. {
  237. /*{
  238. "skuList": [{
  239. "skuId": "商品ID",
  240. "price": 100,//单价-分
  241. "quantity": 1,
  242. "title": "商品标题",
  243. "imageList": ["https://cdn.weixiu.kyjlkj.com/uploads/images/20240914/202409141528015aeaa2357.png"],
  244. "type": 701,
  245. "tagGroupId": "tag_group_7272625659887960076"
  246. }],
  247. "outOrderNo": "202411121413333930",
  248. "totalAmount": 100,//分
  249. "orderEntrySchema": {
  250. "path": "page/path/index",
  251. "params": '{"id":1234, "name":"hello"}'
  252. },
  253. "payNotifyUrl": "https://weixiudev.kyjlkj.com/api/dou_yin/payNotify"
  254. }*/
  255. try {
  256. $douyinOrder = DouyinOrder::where('order_number',$order_number)->findOrEmpty();
  257. if($douyinOrder->isEmpty()){
  258. throw new \Exception('订单不存在!');
  259. }
  260. $order = $douyinOrder->toArray();
  261. $goods_image = Goods::where('id',$order['goods_id'])->value('goods_image')??'';
  262. $data = [
  263. "skuList" => [
  264. [
  265. "skuId" => (string)$order['goods_id'],
  266. "price" => $order['unit_price'] * 100,
  267. "quantity" => $order['quantity'],
  268. "title" => $order['title'],
  269. "imageList" => [$goods_image],
  270. "type" => 701,
  271. "tagGroupId" => "tag_group_7272625659887960076"
  272. ]
  273. ],
  274. "outOrderNo" => $order['order_number'],
  275. "totalAmount" => $order['total_amount'] * 100,
  276. "orderEntrySchema" => [
  277. "path" => "page/index/index",
  278. "params" => json_encode(['order_number' => $order['order_number']])
  279. ],
  280. "payNotifyUrl" => config('douyin.payNotifyUrl'),
  281. ];
  282. $byteAuthorization = self::byteAuthorization(config('douyin.privateKeyStr'), json_encode($data), config('douyin.appId'), self::randStr(10), time(), 1);
  283. return ['byteAuthorization'=>$byteAuthorization,'data'=>json_encode($data)];
  284. } catch (\Exception $e) {
  285. throw new \Exception($e->getMessage());
  286. }
  287. }
  288. public static function cancelOrder($params)
  289. {
  290. // $params['order_number']
  291. Db::startTrans();
  292. try {
  293. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  294. if(!$order->isEmpty()){
  295. if($order->order_status == 1 && $order->pay_status == 0){
  296. $order->order_status = 4;
  297. $order->save();
  298. }else{
  299. throw new \Exception('订单状态不可取消!');
  300. }
  301. }
  302. Db::commit();
  303. return $order['id'];
  304. } catch (\Exception $e) {
  305. Db::rollback();
  306. throw new \Exception($e->getMessage());
  307. }
  308. }
  309. public static function payNotify($params)
  310. {
  311. Log::write(json_encode($params));
  312. // 查询抖音订单是否完成支付
  313. if ($params['status'] === 'SUCCESS') {
  314. $transaction_id = $params['order_id']??'';
  315. $paid_amount = bcdiv(bcsub($params['total_amount'] ,$params['discount_amount']), '100', 2)??0;
  316. $out_order_no = $params['out_order_no'];
  317. $pay_time = $params['event_time']??time();
  318. $order = DouyinOrder::where('order_number', $out_order_no)->findOrEmpty();
  319. if(!$order->isEmpty()){
  320. // 更新充值订单状态
  321. $order->transaction_id = $transaction_id;
  322. $order->order_status = 2;
  323. $order->pay_time = $pay_time;
  324. $order->paid_amount = $paid_amount;
  325. $user = User::where('id',$order->user_id)->findOrEmpty()->toArray();
  326. $form_detail = [
  327. 'user_name' => $user['real_name']??'',
  328. 'mobile' => $user['mobile'],
  329. 'transaction_id' => $transaction_id,
  330. 'out_trade_no' => $out_order_no,
  331. 'paid_amount' => $paid_amount,
  332. 'params' => $params,
  333. ];
  334. $consultation = ExternalConsultation::create([
  335. 'external_platform_id' => self::$external_platform_id,
  336. 'form_detail' => json_encode($form_detail),
  337. 'user_name' => $user['real_name']??'',
  338. 'mobile' => $user['mobile'],
  339. 'goods_id' => $order->goods_id,
  340. 'amount' => $paid_amount
  341. ]);
  342. $order->consultation_id = $consultation->id;
  343. $order->save();
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. public static function reservation($params)
  350. {
  351. /*$lon_lat = get_address_lat_lng($params['user_address']);
  352. $params['lon'] = $lon_lat['lon'];
  353. $params['lat'] = $lon_lat['lat'];*/
  354. // $params['order_number']
  355. Db::startTrans();
  356. try {
  357. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  358. if(!$order->isEmpty()){
  359. $consultation = ExternalConsultation::where('id', $order->consultation_id)->findOrEmpty()->toArray();
  360. $consultation['user_name'] = $params['user_name']??$consultation['user_name'];
  361. $consultation['mobile'] = $params['mobile']??$consultation['mobile'];
  362. $consultation['user_address'] = $params['user_address'];
  363. $consultation['lon'] = $params['lon'];
  364. $consultation['lat'] = $params['lat'];
  365. $consultation['appointment_time'] = $params['appointment_time'];
  366. $result = ExternalConsultationLogic::order($consultation);
  367. if (false === $result) {
  368. throw new \Exception('预约失败');
  369. }
  370. $consultationOrder = ExternalConsultationOrder::where('consultation_id', $order->consultation_id)->where('goods_id', $order->goods_id)->where('amount', $order->paid_amount)
  371. ->findOrEmpty()->toArray();
  372. $work_status = ServiceWork::where('id', $consultationOrder['work_id'])->value('work_status');
  373. $order->work_id = $consultationOrder['work_id'];
  374. $order->fulfillment_status = $work_status;
  375. $order->save();
  376. }
  377. Db::commit();
  378. return $order['id'];
  379. } catch (\Exception $e) {
  380. Db::rollback();
  381. throw new \Exception($e->getMessage());
  382. }
  383. }
  384. public static function upReservation($params)
  385. {
  386. // $params['order_number']
  387. Db::startTrans();
  388. try {
  389. $order = DouyinOrder::where('order_number', $params['order_number'])->findOrEmpty();
  390. if(!$order->isEmpty()){
  391. // sn appointment_time
  392. $result = ServiceOrderLogic::approvalChangeAppointment(['sn'=>RechargeOrder::where('work_id', $order->work_id)->value('sn'),'appointment_time'=>$params['appointment_time']]);
  393. if (false === $result) {
  394. throw new \Exception(ServiceOrderLogic::getError());
  395. }
  396. $order->fulfillment_status = ServiceWork::where('id', $order->work_id)->value('work_status');
  397. $order->save();
  398. }
  399. Db::commit();
  400. return $order['id'];
  401. } catch (\Exception $e) {
  402. Db::rollback();
  403. throw new \Exception($e->getMessage());
  404. }
  405. }
  406. public static function getOrderDetail($params)
  407. {
  408. //抖音订单信息/商品信息/预约信息(地址、时间、履约状态与信息)
  409. // $params['order_number'] user_id
  410. $order = DouyinOrder::with(['goods','serviceWork','douyinRefundOrder'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  411. if($order->isEmpty()){
  412. return [];
  413. }
  414. $orderInfo = $order->toArray();
  415. empty($orderInfo['goods']) && $orderInfo['goods'] = [];
  416. empty($orderInfo['serviceWork']) && $orderInfo['serviceWork'] = [];
  417. empty($orderInfo['douyinRefundOrder']) && $orderInfo['douyinRefundOrder'] = [];
  418. $work_status = $orderInfo['serviceWork']['work_status']??0;
  419. $performance = [];
  420. // tmp
  421. switch ($work_status){
  422. case 0:
  423. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  424. break;
  425. case 1:
  426. case 2:
  427. case 3:
  428. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  429. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  430. break;
  431. case 4:
  432. case 5:
  433. case 6:
  434. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  435. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  436. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  437. break;
  438. case 7:
  439. case 8:
  440. $performance[] = ['status' => '待派单','title' => '待派单','time' => date('Y-m-d H:i:s',time())];
  441. $performance[] = ['status' => '已派单','title' => '已派单','time' => date('Y-m-d H:i:s',time())];
  442. $performance[] = ['status' => '服务中','title' => '服务中','time' => date('Y-m-d H:i:s',time())];
  443. $performance[] = ['status' => '已完结','title' => '已完结','time' => date('Y-m-d H:i:s',time())];
  444. break;
  445. }
  446. $orderInfo['performance'] = $performance;
  447. return $orderInfo;
  448. }
  449. public static function refund($params)
  450. {
  451. Db::startTrans();
  452. try {
  453. // $params['order_number'] user_id
  454. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->where('user_id', $params['user_id'])->findOrEmpty();
  455. if($order->isEmpty()){
  456. throw new \Exception('订单不存在');
  457. }
  458. $orderInfo = $order->toArray();
  459. $work_status = $orderInfo['serviceWork']['work_status']??0;
  460. if(3 < $work_status){
  461. throw new \Exception('该订单禁止退款');
  462. }
  463. DouyinRefundOrder::create([
  464. 'refund_number' => generate_sn(DouyinRefundOrder::class, 'refund_number'),
  465. 'order_number' => $orderInfo['order_number'],
  466. 'transaction_id' => $orderInfo['transaction_id'],
  467. 'reason' => $params['reason']??'',
  468. 'refund_status' => 0,
  469. 'user_id' => $orderInfo['user_id'],
  470. 'refund_amount' => $orderInfo['paid_amount'],
  471. ]);
  472. Db::commit();
  473. return true;
  474. } catch (\Exception $e) {
  475. Db::rollback();
  476. throw new \Exception($e->getMessage());
  477. }
  478. }
  479. public static function refundExamine($params)
  480. {
  481. Db::startTrans();
  482. try {
  483. // $params['order_number']
  484. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $params['order_number'])->findOrEmpty();
  485. if($order->isEmpty()){
  486. throw new \Exception('订单不存在');
  487. }
  488. $orderInfo = $order->toArray();
  489. //$refund_number = $params['refund_number']??'';
  490. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $params['order_number'])->order('id', 'desc')->findOrEmpty();
  491. if($params['is_examine_ok'] === 'pass'){
  492. $douyinRefundOrder->refund_status = 2;
  493. RechargeOrder::where('work_id', $orderInfo['work_id'])->update([
  494. 'pay_status' => 2,
  495. 'pay_time' => 0,
  496. 'paid_amount' => 0,
  497. ]);
  498. ServiceWork::where('id', $orderInfo['work_id'])->update([
  499. 'work_status' => 0,
  500. 'user_confirm_status' => 0,
  501. 'service_status' => 4,
  502. 'work_pay_status' => 0
  503. ]);
  504. }else{
  505. $douyinRefundOrder->refund_status = 1;
  506. }
  507. $douyinRefundOrder->save();
  508. Db::commit();
  509. if($params['is_examine_ok'] === 'pass'){
  510. //通过后向抖音申请退款
  511. self::sendRefundCreate($params['order_number']);
  512. }
  513. return true;
  514. } catch (\Exception $e) {
  515. Db::rollback();
  516. throw new \Exception($e->getMessage());
  517. }
  518. }
  519. public static function sendRefundCreate($order_number)
  520. {
  521. try {
  522. // $params['order_number']
  523. $order = DouyinOrder::with(['goods','serviceWork'])->where('order_number', $order_number)->findOrEmpty();
  524. if($order->isEmpty()){
  525. throw new \Exception('订单不存在');
  526. }
  527. $orderInfo = $order->toArray();
  528. $douyinRefundOrder = DouyinRefundOrder::where('order_number', $order_number)->order('id', 'desc')->findOrEmpty();
  529. //通过后向抖音申请退款
  530. //getClientToken()
  531. $url = config('douyin.host').'api/trade_basic/v1/developer/refund_create/';
  532. $data = [
  533. "order_id" => $orderInfo['transaction_id'],
  534. "out_refund_no" => $douyinRefundOrder->refund_number,
  535. "cp_extra" => $orderInfo['id'].'|'.$douyinRefundOrder->id,
  536. "order_entry_schema" => [
  537. "path" => "page/index/index",
  538. "params" => json_encode(['refund_number'=>$douyinRefundOrder->refund_number])
  539. ],
  540. "refund_total_amount " => $douyinRefundOrder->refund_amount * 100,
  541. "notify_url" => config('douyin.refundNotifyUrl'),
  542. "refund_reason" => [
  543. [
  544. "code" => 101,
  545. "text" => "不想要了"
  546. ]
  547. ]
  548. ];
  549. $res = http_request($url,$data,['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
  550. if(isset($res['err_msg']) && $res['err_msg'] === 'success'){
  551. $douyinRefundOrder->transaction_id = $res['data']['refund_id'];
  552. $douyinRefundOrder->save();
  553. }
  554. return true;
  555. } catch (\Exception $e) {
  556. Log::info($e->getMessage());
  557. return false;
  558. }
  559. }
  560. public static function refundNotify($params)
  561. {
  562. Db::startTrans();
  563. try {
  564. $douyinRefundOrder = DouyinRefundOrder::where('refund_number', $params['out_refund_no'])->findOrEmpty();
  565. if($douyinRefundOrder->isEmpty()){
  566. throw new \Exception('退款订单不存在');
  567. }
  568. if($douyinRefundOrder->refund_status == 0){
  569. if($params['status'] === 'SUCCESS'){
  570. $douyinRefundOrder->refund_status = 3;
  571. DouyinOrder::where('order_number', $douyinRefundOrder->order_number)->update([
  572. 'order_status' => 4,
  573. 'pay_status' => 3,
  574. ]);
  575. }elseif($params['status'] === 'FAIL'){
  576. $douyinRefundOrder->refund_status = 4;
  577. }else{
  578. throw new \Exception('退款状态未知');
  579. }
  580. $douyinRefundOrder->save();
  581. }
  582. Db::commit();
  583. return true;
  584. } catch (\Exception $e) {
  585. Db::rollback();
  586. throw new \Exception($e->getMessage());
  587. }
  588. }
  589. /**
  590. * 扩展点回调提交订单
  591. * @param $params
  592. * @return bool
  593. * @throws \Exception
  594. * @author liugc <466014217@qq.com>
  595. * @date 2025/6/4 14:03
  596. */
  597. public static function submitOrderNotify($params)
  598. {
  599. Db::startTrans();
  600. try {
  601. Db::commit();
  602. return true;
  603. } catch (\Exception $e) {
  604. Db::rollback();
  605. throw new \Exception($e->getMessage());
  606. }
  607. }
  608. public static function byteAuthorization($privateKeyStr, $data, $appId, $nonceStr, $timestamp, $keyVersion) {
  609. $byteAuthorization = '';
  610. // 读取私钥
  611. $privateKey = openssl_pkey_get_private($privateKeyStr);
  612. if (!$privateKey) {
  613. throw new \Exception("Invalid private key");
  614. }
  615. // 生成签名
  616. $signature = self::getSignature("POST", "/requestOrder", $timestamp, $nonceStr, $data, $privateKey);
  617. if ($signature === false) {
  618. return null;
  619. }
  620. // 构造 byteAuthorization
  621. $byteAuthorization = sprintf("SHA256-RSA2048 appid=%s,nonce_str=%s,timestamp=%s,key_version=%s,signature=%s", $appId, $nonceStr, $timestamp, $keyVersion, $signature);
  622. return $byteAuthorization;
  623. }
  624. public static function getSignature($method, $url, $timestamp, $nonce, $data, $privateKey) {
  625. Log::info("method:{$method}\n url:{$url}\n timestamp:{$timestamp}\n nonce:{$nonce}\n data:{$data}");
  626. $targetStr = $method. "\n" . $url. "\n" . $timestamp. "\n" . $nonce. "\n" . $data. "\n";
  627. openssl_sign($targetStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
  628. $sign = base64_encode($sign);
  629. return $sign;
  630. }
  631. public static function randStr($length = 8) {
  632. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  633. $str = '';
  634. for ($i = 0; $i < $length; $i++) {
  635. $str .= $chars[mt_rand(0, strlen($chars) - 1)];
  636. }
  637. return $str;
  638. }
  639. public static function getClientToken($isRefresh = false) {
  640. $url = config('douyin.host').'oauth/client_token/';
  641. $cache_name = 'dy_client_token';
  642. $cache_data = cache($cache_name);
  643. if(empty($cache_data) || $cache_data == null || $isRefresh){
  644. $data = [
  645. 'client_key'=> config('douyin.appId'),
  646. 'client_secret'=> config('douyin.appSecret'),
  647. 'grant_type'=> "client_credential"
  648. ];
  649. $res = http_request($url,$data,['Content-Type' => 'application/json;charset=utf-8']);
  650. Log::info(json_encode($res));
  651. if($res['message'] === 'success'){
  652. cache($cache_name, json_encode($res['data']), (time()+$res['data']['expires_in']-1));
  653. $cache_data = $res['data'];
  654. }
  655. }
  656. return $cache_data['access_token'];
  657. }
  658. }