src/Controller/WebhookController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\RepeatLineWebhook;
  4. use DateTime;
  5. use GuzzleHttp\Client;
  6. use Kzl\FrameworkBundle\Common\KzlFrameworkStatic;
  7. use Kzl\FrameworkBundle\Controller\BaseController as KzlBaseController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. class WebhookController extends KzlBaseController
  11. {
  12.     public function indexAction(Request $request)
  13.     {
  14.         if ($this->isPost()) {
  15.         try{
  16.             // dump($request);
  17.             $content json_decode($request->getContent());
  18.             // トークンの取得
  19.             // $url = 'https://api.cs-cloud.jp/v1/auth/access_token';
  20.             // $headers = [
  21.             //     'Accept' => 'application/ld+json',
  22.             //     'Content-Type' => 'application/json',
  23.             // ];
  24.             // $body = [
  25.             //     'client_id' => 'f74e9e2c-9c2f-4350-9bc6-da9274bea6c5',
  26.             //     'client_secret' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSJ9.SYlLaku3SQ1RRP7qMJoLLaCcPePwmKohpOzisoKGoH0',
  27.             // ];
  28.             // $client = new Client();
  29.             // $response = $client->request('POST', $url, [
  30.             //     'headers' => $headers,
  31.             //     'verify' => false,
  32.             //     'body' => json_encode($body),
  33.             // ]);
  34.             $em $this->getDoctrine()->getManager();
  35.             // 2重送信対策
  36.             $action $content->action;
  37.             $lineUserId $content->cart_link->line_user_id;
  38.             $repeatLineWebhook $em->getRepository(RepeatLineWebhook::class)->findOneBy([
  39.                 'repeatLineAction' => $action,
  40.                 'lineUserId' => $lineUserId,
  41.             ], ['created_at' => 'DESC']);
  42.             $today = new DateTime();
  43.             if ($repeatLineWebhook) {
  44.                 $diff $repeatLineWebhook->getCreatedAt()->diff($today);
  45.                 $diffS $diff->days 24 60 60 $diff->60 60 $diff->60 $diff->s;
  46.                 if ($diffS 60) {
  47.                     return;
  48.                 }
  49.             }
  50.             // DBへ保存
  51.             // $newRequest = new RepeatLineWebhook();
  52.             // KzlFrameworkStatic::setNewDNo($newRequest);
  53.             // $con = $em->getConnection();
  54.             // $con->executeQuery('INSERT INTO repeat_line_webhook (tenant_cd, d_no, cart_link, line_user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?);', [
  55.             //     1,
  56.             //     $newRequest->getDNo(),
  57.             //     json_encode($content->cart_link),
  58.             //     $lineUserId,
  59.             //     $newRequest->getCreatedAt(),
  60.             //     $newRequest->getUpdatedAt(),
  61.             // ]);
  62.             $newRequest = new RepeatLineWebhook();
  63.             $newRequest->setTenantCd(1);
  64.             $newRequest->setRepeatLineTrigger($content->trigger);
  65.             $newRequest->setRepeatLineAction($content->action);
  66.             $newRequest->setCartLink(json_encode($content->cart_link));
  67.             $newRequest->setLineUserId($lineUserId);
  68.             $em->persist($newRequest);
  69.             $em->flush($newRequest);
  70.             // ユーザ連携API呼出
  71.             // $url = 'http://host.docker.internal:61400/api/repeat_line/cart_link';
  72.             $url 'https://nihon-daiichi.com/api/repeat_line/cart_link';
  73.             $headers = [
  74.                 'Accept' => 'application/ld+json',
  75.                 'Content-Type' => 'application/json',
  76.             ];
  77.             $body = [
  78.                 'client_id' => 'f74e9e2c-9c2f-4350-9bc6-da9274bea6c5',
  79.                 'client_secret' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSJ9.SYlLaku3SQ1RRP7qMJoLLaCcPePwmKohpOzisoKGoH0',
  80.                 'cart_link' => json_encode($content->cart_link),
  81.             ];
  82.             $client = new Client();
  83.             $response $client->request('POST'$url, [
  84.                 'headers' => $headers,
  85.                 'verify' => false,
  86.                 'body' => json_encode($body),
  87.             ]);
  88.         } catch(\Exception $e){
  89.             return new JsonResponse([
  90.                 'result' => 'ng',
  91.             ]); 
  92.         }
  93.         }
  94.         return new JsonResponse([
  95.                 'result' => 'ok',
  96.             ]);
  97.         // $list = json_decode($response->getBody()->getContents(), true);
  98.         // dump($list);
  99. //         $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSIsImV4cCI6MTY1NzAwNzg4N30.TnNllYfdbOV0rJXpvjWAfrDfY7DgbuwMN3bKhRUHva0';
  100. //         // メッセージ送信
  101. //         $url = 'https://api.cs-cloud.jp/v1/line/7475638148/messages';
  102. //         $headers = [
  103. //             'Accept' => 'application/ld+json',
  104. //             'Content-Type' => 'application/json',
  105. //             'Authorization' => $token,
  106. //         ];
  107. //         $body = [
  108. //             'line_user_id' => 'U377db6bae942aa5d1d35016fbfd67fd2',
  109. //             'messages' => '申し訳ありません。
  110. // お客様情報が確認できませんでした😭
  111. // 大変お手数ですが、入力内容をご確認の上
  112. // 再度正しい情報をご入力ください。
  113. // <よくあるパターン>
  114. // 携帯番号と固定電話を間違えて入力している
  115. // 電話番号にハイフンが入っている',
  116. //         ];
  117. //         $client = new Client();
  118. //         $response = $client->request('POST', $url, [
  119. //             'headers' => $headers,
  120. //             'verify' => false,
  121. //             'body' => json_encode($body),
  122. //         ]);
  123. //         dump($client);
  124. //         dump($response);
  125.         // return $this->render('top/index.html.twig', [
  126.         //     'controller_name' => 'TopController',
  127.         // ]);
  128.     }
  129. }