DouYinService.php 30 KB

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