企微短剧业务系统

CallBackController.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/3/16
  6. * Time: 13:43
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Console\Commands\JiaShuOrderToRds;
  10. use App\Console\Commands\MaiBuOrderToRds;
  11. use App\Http\Controllers\Controller;
  12. use App\Log;
  13. use App\Models\PlatformOrderConf;
  14. use App\RedisModel;
  15. use App\Service\CapacityService;
  16. use App\Service\DjOrderService;
  17. use App\Service\HttpService;
  18. use App\Service\MsgService;
  19. use App\Support\EmailQueue;
  20. use Illuminate\Http\Request;
  21. class CallBackController extends Controller
  22. {
  23. private $token = 'oPIOwRVCWLDi5wXR'; // 柚子分销token
  24. private $jiashuToken = 'OHDj8EJfoYrWF5rX';// 嘉书分销token
  25. /*
  26. * 杭州猎豆 柚子分销平台
  27. */
  28. public function youziOrderCallBack(Request $request)
  29. {
  30. $msgType = $request->input('msgType');
  31. $timestamp = $request->input('timestamp');
  32. $nonce = $request->input('nonce');
  33. $signature = $request->input('signature');
  34. $orderPay = $request->input('orderPay');
  35. $platformId = $request->input('platformId');
  36. $sysPlatformId = 'BcJV6mb504Kt2lML';
  37. if(empty($orderPay)) {
  38. // 在柚子端填写回调地址回调使用
  39. return 'success';
  40. }
  41. // 将接收到的数据保存的日志文件中
  42. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [$platformId], 'youziOrder');
  43. # 此处逻辑为在柚子后台配置回传链接时使用
  44. if($msgType != 'orderPay') {
  45. return 'success';
  46. }
  47. if($platformId != $sysPlatformId) {
  48. EmailQueue::rPush('柚子订单回传platformId与预期不符', json_encode([
  49. 'request_platform_id' => $platformId,
  50. 'sys_platform_id' => $sysPlatformId,
  51. 'desc' => '杭州猎豆'
  52. ]), ['song.shen@kuxuan-inc.com', 'xiaohua.hou@kuxuan-inc.com'], '猎羽');
  53. }
  54. // 查询app_id对应的token
  55. $conf = PlatformOrderConf::getTokenByPlatformId($platformId);
  56. if(empty($conf->token)) {
  57. return 'fail';
  58. }
  59. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  60. $orderPay['sys_group_id'] = $conf->sys_group_id;
  61. // 数据校验
  62. $check = MsgService::checkSignature($nonce, $timestamp, $conf->token, $signature);
  63. if($check === false) {
  64. return 'fail';
  65. }
  66. try{
  67. // 将数据保存入数据库
  68. DjOrderService::youziOrderSave($orderPay, 1, 1, $platformId);
  69. } catch (\Exception $exception) {
  70. Log::logError('保存柚子订单数据异常', [
  71. 'params' => json_decode(json_encode($request->all()), 1),
  72. 'file' => $exception->getFile(),
  73. 'line' => $exception->getLine(),
  74. 'message' => $exception->getMessage(),
  75. 'trace' => $exception->getTraceAsString()], 'callBackOrderError');
  76. $this->exceptionHandler('callBackOrder', json_encode([
  77. 'err_msg' => '保存柚子订单数据异常',
  78. 'params' => json_decode(json_encode($request->all()), 1),
  79. 'file' => $exception->getFile(),
  80. 'line' => $exception->getLine(),
  81. 'message' => $exception->getMessage(),
  82. 'trace' => $exception->getTraceAsString(),
  83. ]));
  84. return 'fail';
  85. }
  86. return 'success';
  87. }
  88. // 嘉书平台 小程序订单
  89. public function jiashuOrderCallBack(Request $request)
  90. {
  91. $timestamp = $request->input('timestamp');
  92. $nonce = $request->input('nonce');
  93. $signature = $request->input('signature');
  94. $orderPay = $request->input('orderPay');
  95. $appId = '3a8vJ2t0K5XkH5Wl';
  96. // 将接收到的数据保存的日志文件中
  97. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [], 'jiashuOrder');
  98. // 查询app_id对应的token
  99. $conf = PlatformOrderConf::getTokenByPlatformId($appId);
  100. if(empty($conf->token)) {
  101. return 'fail';
  102. }
  103. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  104. $orderPay['sys_group_id'] = $conf->sys_group_id;
  105. // 数据校验
  106. $check = MsgService::checkSignature($nonce, $timestamp, $conf->token, $signature);
  107. if($check === false) {
  108. return 'fail';
  109. }
  110. try{
  111. DjOrderService::jiashuOrderSave($orderPay, $appId);
  112. } catch (\Exception $exception) {
  113. Log::logError('保存嘉书订单数据异常', [
  114. 'params' => json_decode(json_encode($request->all()), 1),
  115. 'file' => $exception->getFile(),
  116. 'line' => $exception->getLine(),
  117. 'message' => $exception->getMessage(),
  118. 'trace' => $exception->getTraceAsString()], 'callBackOrderError');
  119. $this->exceptionHandler('callBackOrder', json_encode([
  120. 'err_msg' => '保存嘉书订单数据异常',
  121. 'params' => json_decode(json_encode($request->all()), 1),
  122. 'file' => $exception->getFile(),
  123. 'line' => $exception->getLine(),
  124. 'message' => $exception->getMessage(),
  125. 'trace' => $exception->getTraceAsString(),
  126. ]));
  127. return 'fail';
  128. }
  129. return 'success';
  130. }
  131. # 迈步平台 小程序订单
  132. public function orderCallBack(Request $request)
  133. {
  134. $platformId = $request->input('platformId');
  135. $timestamp = $request->input('timestamp');
  136. $nonce = $request->input('nonce');
  137. $signature = $request->input('signature');
  138. $orderPay = $request->input('orderPay');
  139. // 将接收到的数据保存的日志文件中
  140. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [], 'callBackOrder');
  141. // 查询app_id对应的token
  142. $conf = PlatformOrderConf::getTokenByPlatformId($platformId);
  143. if(empty($conf->token)) {
  144. return 'fail';
  145. }
  146. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  147. $orderPay['sys_group_id'] = $conf->sys_group_id;
  148. // 数据校验
  149. $check = MsgService::checkSignatureNew($nonce, $timestamp, $conf->token, $platformId, $signature);
  150. if($check === false) {
  151. return 'fail';
  152. }
  153. try{
  154. DjOrderService::unifyOrderSave($orderPay, $platformId);
  155. } catch (\Exception $exception) {
  156. Log::logError('保存其他平台订单数据异常', [
  157. 'params' => json_decode(json_encode($request->all()), 1),
  158. 'file' => $exception->getFile(),
  159. 'line' => $exception->getLine(),
  160. 'message' => $exception->getMessage(),
  161. 'trace' => $exception->getTraceAsString()], 'callBackOrderError');
  162. $this->exceptionHandler('callBackOrder', json_encode([
  163. 'err_msg' => '保存其他平台订单数据异常',
  164. 'params' => json_decode(json_encode($request->all()), 1),
  165. 'file' => $exception->getFile(),
  166. 'line' => $exception->getLine(),
  167. 'message' => $exception->getMessage(),
  168. 'trace' => $exception->getTraceAsString(),
  169. ]));
  170. return 'fail';
  171. }
  172. return 'success';
  173. }
  174. public function exceptionHandler($key, $message)
  175. {
  176. $val = RedisModel::get($key);
  177. if(empty($val)) {
  178. EmailQueue::rPush('订单保存异常', $message, ['song.shen@kuxuan-inc.com'], []);
  179. RedisModel::set($key, 1);
  180. RedisModel::expire($key, 30*60);
  181. }
  182. }
  183. // 嘉书平台 H5订单
  184. public function jiashuWebOrderCallBack(Request $request)
  185. {
  186. // 将接收到的数据保存的日志文件中
  187. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [], 'callBackOrderWeb');
  188. # 因为嘉书方不提供签名逻辑以及失败重试逻辑,因此要尽量保证该接口稳定
  189. try{
  190. DjOrderService::jiashuWebOrderSave($request->all());
  191. } catch (\Exception $exception) {
  192. Log::logError('保存嘉书H5订单数据异常', [
  193. 'params' => json_decode(json_encode($request->all()), 1),
  194. 'file' => $exception->getFile(),
  195. 'line' => $exception->getLine(),
  196. 'message' => $exception->getMessage(),
  197. 'trace' => $exception->getTraceAsString()], 'callBackOrderError');
  198. $this->exceptionHandler('callBackOrder', json_encode([
  199. 'err_msg' => '保存嘉书H5订单数据异常',
  200. 'params' => json_decode(json_encode($request->all()), 1),
  201. 'file' => $exception->getFile(),
  202. 'line' => $exception->getLine(),
  203. 'message' => $exception->getMessage(),
  204. 'trace' => $exception->getTraceAsString(),
  205. ]));
  206. return ['code' => '-1', 'message' => 'system error'];
  207. }
  208. return ['code' => 0, 'message' => 'ok'];
  209. }
  210. // 杭州剧有戏-柚子平台
  211. public function juyouxiYouziOrderCallBack(Request $request)
  212. {
  213. $msgType = $request->input('msgType');
  214. $timestamp = $request->input('timestamp');
  215. $nonce = $request->input('nonce');
  216. $signature = $request->input('signature');
  217. $orderPay = $request->input('orderPay');
  218. $platformId = $request->input('platformId');
  219. $sysPlatformId = 'mNUdMhDNC20CvYLe';
  220. if(empty($orderPay)) {
  221. // 在柚子端填写回调地址回调使用
  222. return 'success';
  223. }
  224. // 将接收到的数据保存的日志文件中
  225. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [$platformId], 'youziOrder');
  226. if($msgType != 'orderPay') {
  227. return 'success';
  228. }
  229. if($platformId != $sysPlatformId) {
  230. EmailQueue::rPush('柚子订单回传platformId与预期不符', json_encode([
  231. 'request_platform_id' => $platformId,
  232. 'sys_platform_id' => $sysPlatformId,
  233. 'desc' => '剧有戏'
  234. ]), ['song.shen@kuxuan-inc.com', 'xiaohua.hou@kuxuan-inc.com'], '猎羽');
  235. }
  236. // 查询app_id对应的token
  237. $conf = PlatformOrderConf::getTokenByPlatformId($platformId);
  238. if(empty($conf->token)) {
  239. Log::logError('保存杭州剧有戏柚子订单数据异常', ['未查询到token'], 'orderTest');
  240. return 'fail';
  241. }
  242. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  243. $orderPay['sys_group_id'] = $conf->sys_group_id;
  244. // 数据校验
  245. $check = MsgService::checkSignature($nonce, $timestamp, $conf->token, $signature);
  246. if($check === false) {
  247. Log::logError('保存杭州剧有戏柚子订单数据异常', ['数据校验失败'], 'orderTest');
  248. return 'fail';
  249. }
  250. try{
  251. // 将数据保存入数据库
  252. DjOrderService::youziOrderSave($orderPay, 1, 1, $platformId, 2);
  253. } catch (\Exception $exception) {
  254. Log::logError('保存杭州剧有戏柚子订单数据异常', [
  255. 'params' => json_decode(json_encode($request->all()), 1),
  256. 'file' => $exception->getFile(),
  257. 'line' => $exception->getLine(),
  258. 'message' => $exception->getMessage(),
  259. 'trace' => $exception->getTraceAsString()], 'callBackOrderError');
  260. $this->exceptionHandler('callBackOrder', json_encode([
  261. 'err_msg' => '保存杭州剧有戏柚子订单数据异常',
  262. 'params' => json_decode(json_encode($request->all()), 1),
  263. 'file' => $exception->getFile(),
  264. 'line' => $exception->getLine(),
  265. 'message' => $exception->getMessage(),
  266. 'trace' => $exception->getTraceAsString(),
  267. ]));
  268. return 'fail';
  269. }
  270. return 'success';
  271. }
  272. # 点众阳光
  273. public function platformOrderCallBack(Request $request)
  274. {
  275. $timestamp = $request->input('timestamp');
  276. $nonce = $request->input('nonce');
  277. $signature = $request->input('signature');
  278. $orderPay = $request->input('orderPay');
  279. $platformId = $request->input('platformId');
  280. // 将接收到的数据保存的日志文件中
  281. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [$platformId], 'platformOrder');
  282. $taskId = $request->input('taskId', '');
  283. $xurl = $request->input('xurl', '');
  284. if(!empty($taskId)) {
  285. # 主动同步下来的订单
  286. return HttpService::httpGet('https://idea-ad.wenxingshuju.com/sunshine/orderList?'.http_build_query($request->all()));
  287. }
  288. // 查询app_id对应的token
  289. $conf = PlatformOrderConf::getTokenByPlatformId($platformId);
  290. if(empty($conf->token)) {
  291. Log::logError('保存订单数据异常', ['未查询到token'], 'platformOrder');
  292. return 'fail';
  293. }
  294. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  295. $orderPay['sys_group_id'] = $conf->sys_group_id;
  296. $orderPay['order_source'] = $conf->order_source;
  297. $orderPay['platform_id'] = $platformId;
  298. // 数据校验
  299. $check = MsgService::checkSignatureNew($nonce, $timestamp, $conf->token, $platformId, $signature);
  300. if($check === false) {
  301. Log::logError('保存订单数据异常', ['数据校验失败'], 'platformOrder');
  302. return 'fail';
  303. }
  304. try{
  305. // 将数据保存入数据库
  306. DjOrderService::platformOrderCallBack($orderPay);
  307. } catch (\Exception $exception) {
  308. Log::logError('保存第三方订单数据异常', [
  309. 'params' => json_decode(json_encode($request->all()), 1),
  310. 'file' => $exception->getFile(),
  311. 'line' => $exception->getLine(),
  312. 'message' => $exception->getMessage(),
  313. 'trace' => $exception->getTraceAsString()], 'platformOrder');
  314. $this->exceptionHandler('callBackOrder', json_encode([
  315. 'err_msg' => '保存第三方订单数据异常',
  316. 'params' => json_decode(json_encode($request->all()), 1),
  317. 'file' => $exception->getFile(),
  318. 'line' => $exception->getLine(),
  319. 'message' => $exception->getMessage(),
  320. 'trace' => $exception->getTraceAsString(),
  321. ]));
  322. return 'fail';
  323. }
  324. return 'success';
  325. }
  326. # 接收容量平台用户和订单信息
  327. public function capacityUserCallBack(Request $request)
  328. {
  329. // 将接收到的数据保存的日志文件中
  330. Log::logInfo(json_encode($request->all(), JSON_UNESCAPED_UNICODE), [], 'CapacityUser');
  331. $platformId = $request->input('platformId');
  332. try{
  333. // 查询app_id对应的token
  334. $conf = PlatformOrderConf::getTokenByPlatformId($platformId);
  335. if(empty($conf->token)) {
  336. Log::logError('保存订单数据异常', ['未查询到token'], 'CapacityUserError');
  337. return json_encode($response = [
  338. 'code' => 1102,
  339. 'msg' => '',
  340. ]);
  341. }
  342. // 根据配置找到对应的系统账号组,用于在数据看板中区分订单所属账号
  343. $data = $request->all();
  344. $data['sys_group_id'] = $conf->sys_group_id;
  345. $data['order_source'] = $conf->order_source;
  346. $data['platform_id'] = $platformId;
  347. // 将数据保存入数据库
  348. list($code, $msg) = CapacityService::saveUserList($data);
  349. $response = [
  350. 'code' => $code,
  351. 'msg' => $msg,
  352. ];
  353. } catch (\Exception $exception) {
  354. Log::logError('保存容量用户数据异常', [
  355. 'params' => json_decode(json_encode($request->all()), 1),
  356. 'file' => $exception->getFile(),
  357. 'line' => $exception->getLine(),
  358. 'message' => $exception->getMessage(),
  359. 'trace' => $exception->getTraceAsString()], 'CapacityUserError');
  360. $this->exceptionHandler('callBackOrder', json_encode([
  361. 'err_msg' => '保存容量用户数据异常',
  362. 'params' => json_decode(json_encode($request->all()), 1),
  363. 'file' => $exception->getFile(),
  364. 'line' => $exception->getLine(),
  365. 'message' => $exception->getMessage(),
  366. 'trace' => $exception->getTraceAsString(),
  367. ]));
  368. $response = [
  369. 'code' => 1102,
  370. 'msg' => '',
  371. ];
  372. }
  373. return json_encode($response);
  374. }
  375. }