<?php
namespace App\Controller;
use App\Entity\RepeatLineWebhook;
use DateTime;
use GuzzleHttp\Client;
use Kzl\FrameworkBundle\Common\KzlFrameworkStatic;
use Kzl\FrameworkBundle\Controller\BaseController as KzlBaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class WebhookController extends KzlBaseController
{
public function indexAction(Request $request)
{
if ($this->isPost()) {
try{
// dump($request);
$content = json_decode($request->getContent());
// トークンの取得
// $url = 'https://api.cs-cloud.jp/v1/auth/access_token';
// $headers = [
// 'Accept' => 'application/ld+json',
// 'Content-Type' => 'application/json',
// ];
// $body = [
// 'client_id' => 'f74e9e2c-9c2f-4350-9bc6-da9274bea6c5',
// 'client_secret' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSJ9.SYlLaku3SQ1RRP7qMJoLLaCcPePwmKohpOzisoKGoH0',
// ];
// $client = new Client();
// $response = $client->request('POST', $url, [
// 'headers' => $headers,
// 'verify' => false,
// 'body' => json_encode($body),
// ]);
$em = $this->getDoctrine()->getManager();
// 2重送信対策
$action = $content->action;
$lineUserId = $content->cart_link->line_user_id;
$repeatLineWebhook = $em->getRepository(RepeatLineWebhook::class)->findOneBy([
'repeatLineAction' => $action,
'lineUserId' => $lineUserId,
], ['created_at' => 'DESC']);
$today = new DateTime();
if ($repeatLineWebhook) {
$diff = $repeatLineWebhook->getCreatedAt()->diff($today);
$diffS = $diff->days * 24 * 60 * 60 + $diff->h * 60 * 60 + $diff->i * 60 + $diff->s;
if ($diffS < 60) {
return;
}
}
// DBへ保存
// $newRequest = new RepeatLineWebhook();
// KzlFrameworkStatic::setNewDNo($newRequest);
// $con = $em->getConnection();
// $con->executeQuery('INSERT INTO repeat_line_webhook (tenant_cd, d_no, cart_link, line_user_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?);', [
// 1,
// $newRequest->getDNo(),
// json_encode($content->cart_link),
// $lineUserId,
// $newRequest->getCreatedAt(),
// $newRequest->getUpdatedAt(),
// ]);
$newRequest = new RepeatLineWebhook();
$newRequest->setTenantCd(1);
$newRequest->setRepeatLineTrigger($content->trigger);
$newRequest->setRepeatLineAction($content->action);
$newRequest->setCartLink(json_encode($content->cart_link));
$newRequest->setLineUserId($lineUserId);
$em->persist($newRequest);
$em->flush($newRequest);
// ユーザ連携API呼出
// $url = 'http://host.docker.internal:61400/api/repeat_line/cart_link';
$url = 'https://nihon-daiichi.com/api/repeat_line/cart_link';
$headers = [
'Accept' => 'application/ld+json',
'Content-Type' => 'application/json',
];
$body = [
'client_id' => 'f74e9e2c-9c2f-4350-9bc6-da9274bea6c5',
'client_secret' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSJ9.SYlLaku3SQ1RRP7qMJoLLaCcPePwmKohpOzisoKGoH0',
'cart_link' => json_encode($content->cart_link),
];
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => $headers,
'verify' => false,
'body' => json_encode($body),
]);
} catch(\Exception $e){
return new JsonResponse([
'result' => 'ng',
]);
}
}
return new JsonResponse([
'result' => 'ok',
]);
// $list = json_decode($response->getBody()->getContents(), true);
// dump($list);
// $token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoX2tleSI6ImY3NGU5ZTJjLTljMmYtNDM1MC05YmM2LWRhOTI3NGJlYTZjNSIsImV4cCI6MTY1NzAwNzg4N30.TnNllYfdbOV0rJXpvjWAfrDfY7DgbuwMN3bKhRUHva0';
// // メッセージ送信
// $url = 'https://api.cs-cloud.jp/v1/line/7475638148/messages';
// $headers = [
// 'Accept' => 'application/ld+json',
// 'Content-Type' => 'application/json',
// 'Authorization' => $token,
// ];
// $body = [
// 'line_user_id' => 'U377db6bae942aa5d1d35016fbfd67fd2',
// 'messages' => '申し訳ありません。
// お客様情報が確認できませんでした😭
// 大変お手数ですが、入力内容をご確認の上
// 再度正しい情報をご入力ください。
// <よくあるパターン>
// 携帯番号と固定電話を間違えて入力している
// 電話番号にハイフンが入っている',
// ];
// $client = new Client();
// $response = $client->request('POST', $url, [
// 'headers' => $headers,
// 'verify' => false,
// 'body' => json_encode($body),
// ]);
// dump($client);
// dump($response);
// return $this->render('top/index.html.twig', [
// 'controller_name' => 'TopController',
// ]);
}
}