parent
c1fcbe524e
commit
8bb9d6eb98
@ -1,2 +0,0 @@
|
|||||||
*
|
|
||||||
!.gitignore
|
|
||||||
@ -0,0 +1,98 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace asc\line;
|
||||||
|
|
||||||
|
use think\facade\Db;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
class LineLogin
|
||||||
|
{
|
||||||
|
private $channelId;
|
||||||
|
private $channelSecret;
|
||||||
|
|
||||||
|
public function __construct($channelId, $channelSecret)
|
||||||
|
{
|
||||||
|
$this->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
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$body = $response->getBody()->getContents();
|
||||||
|
|
||||||
|
return json_decode($body, true);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// print_r($e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace asc\line;
|
||||||
|
|
||||||
|
use think\facade\Db;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
|
||||||
|
class LinePay
|
||||||
|
{
|
||||||
|
private $channelId;
|
||||||
|
private $channelSecret;
|
||||||
|
|
||||||
|
public function __construct($channelId, $channelSecret)
|
||||||
|
{
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue