diff --git a/extend/.gitignore b/extend/.gitignore deleted file mode 100644 index c96a04f..0000000 --- a/extend/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -* -!.gitignore \ No newline at end of file diff --git a/extend/asc/line/LineLogin.php b/extend/asc/line/LineLogin.php new file mode 100644 index 0000000..51461b6 --- /dev/null +++ b/extend/asc/line/LineLogin.php @@ -0,0 +1,97 @@ +channelId = $channelId; + $this->channelSecret = $channelSecret; + } + + public function getLoginUrl($site_code) + { + $url = 'https://access.line.me/oauth2/v2.1/authorize?'; + $url .= 'response_type=code'; + $url .= '&client_id='.$this->channelId; + $url .= '&redirect_uri=https://'.$_SERVER['HTTP_HOST'].'/login/linecallback/vip/'.$site_code; + $url .= '&state='.$_SERVER['REQUEST_URI']; + $url .= '&bot_prompt=aggressive'; + $url .= '&scope=openid%20profile'; + + return $url; + } + + public function getLineToken($code) + { + $client = new Client(); + + $response = $client->request('POST', 'https://api.line.me/oauth2/v2.1/token', [ + 'form_params' => [ + 'grant_type' => 'authorization_code', + 'code' => $code, + 'redirect_uri' => 'https://c3d3-220-129-67-224.jp.ngrok.io/m/login', + 'client_id' => $this->channelId, + 'client_secret' => $this->channelSecret + ] + ]); + + return json_decode($response->getBody()->getContents(), true); + } + + public function getUserProfile($token) + { + $client = new Client(); + $headers = [ + 'Authorization' => 'Bearer ' . $token, + 'Accept' => 'application/json', + ]; + $response = $client->request('GET', 'https://api.line.me/v2/profile', [ + 'headers' => $headers + ]); + return json_decode($response->getBody()->getContents(), true); + } + + public function verifyToken($token) + { + $client = new Client(); + $response = $client->request('GET', 'https://api.line.me/oauth2/v2.1/verify', [ + 'query' => [ + 'access_token' => $token + ] + ]); + return json_decode($response->getBody()->getContents(), true); + } + + /** + * 驗證 id token + */ + public static function verifyIdToken($client_id, $id_token) + { + try{ + $client = new Client(); + $response = $client->request('POST', 'https://api.line.me/oauth2/v2.1/verify', [ + 'form_params' => [ + 'id_token' => $id_token, + 'client_id'=> $client_id + ] + ]); + // print_r($response); + $body = $response->getBody()->getContents(); + + return json_decode($body, true); + + } catch (\Exception $e) { + return false; + } + + } +} diff --git a/extend/asc/line/LinePay.php b/extend/asc/line/LinePay.php new file mode 100644 index 0000000..7f831dd --- /dev/null +++ b/extend/asc/line/LinePay.php @@ -0,0 +1,68 @@ +channelId = $channelId; + $this->channelSecret = $channelSecret; + } + + public function request($data){ + $uri = '/v3/payments/request'; + $Nonce = date('c') . uniqid('-'); + + $authMacText = $this->channelSecret . $uri . json_encode($data) . $Nonce; + $Authorization = base64_encode(hash_hmac('sha256', $authMacText, $this->channelSecret, true)); + + $headers = [ + 'Content-Type' => 'application/json', + 'X-LINE-ChannelId' => $this->channelId, + 'X-LINE-Authorization-Nonce' => $Nonce, + 'X-LINE-Authorization' => $Authorization + ]; + + $client = new Client([ + 'headers' => $headers + ]); + + $response = $client->post('https://sandbox-api-pay.line.me'.$uri, ['body' => json_encode($data)]); + + return json_decode($response->getBody()->getContents(), true); + + } + + public function confirm($transation_id, $data){ + $uri = '/v3/payments/'.$transation_id.'/confirm'; + $Nonce = date('c') . uniqid('-'); + + $authMacText = $this->channelSecret . $uri . json_encode($data) . $Nonce; + $Authorization = base64_encode(hash_hmac('sha256', $authMacText, $this->channelSecret, true)); + + $headers = [ + 'Content-Type' => 'application/json', + 'X-LINE-ChannelId' => $this->channelId, + 'X-LINE-Authorization-Nonce' => $Nonce, + 'X-LINE-Authorization' => $Authorization + ]; + + $client = new Client([ + 'headers' => $headers + ]); + + $response = $client->post('https://sandbox-api-pay.line.me'.$uri, ['body' => json_encode($data)]); + + return json_decode($response->getBody()->getContents(), true); + + } + +}