記錄一下這次串接的經驗
LINE Pay 確實很好串接,一切都非常順利
首先,寫一個取得LINE Pay 付款網址用的 class
class getLPUrl
{
private $channelId = "";
private $channelSecret = "";
private $queryHost = "";
private $queryUrl = "";
private $body = array();
private $packageInfoList = array();
private $data = array();
public function __construct($transactionId, $price, $title, $detailName, $url, $host, $channelId, $channelSecret)
{
$this->channelId = $channelId;
$this->channelSecret = $channelSecret;
$this->queryUrl = $url;
$this->queryHost = $host;
$this->body["orderId"] = $transactionId;
$this->body["currency"] = "TWD";
$this->body["amount"] = $price*1;
$this->body["packages"] = [[
"id" => "package1",
"amount" => $price*1,
"name" => $title,
"products" => [[
"name" => $detailName,
"quantity" => 1,
"price" => $price*1
]]]];
$this->body["redirectUrls"] = [
"confirmUrl" => "成功時打開的頁面",
"cancelUrl" => "失敗時打開的頁面"
];
}
public function send()
{
$bodyJson = json_encode($this->body, JSON_UNESCAPED_UNICODE);
// ==== 建立 nonce ====
$nonce = bin2hex(random_bytes(16)); // 亂數字串
// ==== 建立 HMAC 簽章 ====
$signatureStr = $this->channelSecret . $this->queryUrl . $bodyJson . $nonce;
$hash = hash_hmac('sha256', $signatureStr, $this->channelSecret, true);
$signature = base64_encode($hash);
// ==== 設定 Header(注意這裡改成 X-LINE-Authorization)====
$headers = [
"Content-Type: application/json",
"X-LINE-ChannelId: {$this->channelId}",
"X-LINE-Authorization-Nonce: {$nonce}",
"X-LINE-Authorization: {$signature}"
];
$ret = sendPostJsonLP($this->queryHost.$this->queryUrl, $bodyJson, $headers);
$this->data = json_decode($ret, true);
if (isset($this->data['returnCode']) && $this->data['returnCode'] === '0000') {
return true;
}
return false;
}
public function get($key)
{
if($key == "web"){
$paymentUrl = $this->data['info']['paymentUrl']['web'];
return $paymentUrl;
}else{
return $this->data;
}
}
}
public static function sendPostJsonLP($apiUrl, $bodyJson, $headers) {
// ==== 發送 API ====
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
return $response;
}
使用時
$tradeNo = generateUniqueString(); //亂數
$price = 1234; //價格
$title = "這是標題";
$detailName = "這是內文";
$LP_url = "/v3/payments/request"; //固定的
$LP_Sandbox = "https://sandbox-api-pay.line.me"; //測試環境
$LP_Production = "https://api-pay.line.me"; //線上環境
$LP_host = $LP_Production;
$LP_channelId = "跟Line Pay 申請";
$LP_channelSecret = "跟Line Pay 申請";
$tool = new getLPUrl($tradeNo, $price, $title, $detailName, $LP_url, $LP_host, $LP_channelId, $LP_channelSecret);
if($tool->send()){
$payUrl = $tool->get("web");
}
這個$payUrl 就可以拿來給客人打開付款了
另外付款後的成功頁面也很重要
$transactionId = $_GET['transactionId'];
$orderId = $_GET['orderId'];
$LP_channelId = "跟Line Pay 申請";
$LP_channelSecret = "跟Line Pay 申請";
$Sandbox = "https://sandbox-api-pay.line.me";
$Production = "https://api-pay.line.me";
$url = "/v3/payments/$transactionId/confirm"; //這邊要串上拿到的transactionId
$apiUrl = $Production.$url;
//建立回傳物件,這邊要我們確認金額,填我們真正想要的。
$body = [
"amount" => $amount*1,
"currency" => "TWD"
];
$bodyJson = json_encode($body, JSON_UNESCAPED_UNICODE);
// ==== 建立 nonce ====
$nonce = bin2hex(random_bytes(16)); // 亂數字串
// ==== 建立 HMAC 簽章 ====
$signatureStr = $channelSecret . $url . $bodyJson . $nonce;
$hash = hash_hmac('sha256', $signatureStr, $channelSecret, true);
$signature = base64_encode($hash);
// ==== 設定 Header(注意這裡改成 X-LINE-Authorization)====
$headers = [
"Content-Type: application/json",
"X-LINE-ChannelId: {$channelId}",
"X-LINE-Authorization-Nonce: {$nonce}",
"X-LINE-Authorization: {$signature}"
];
// ==== 發送 API ====
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if (!curl_errno($ch)) {
$data = json_decode($response, true);
$rtnCode = $data['returnCode'];
$rtnMsg =$data['returnMessage'];
if (isset($data['returnCode']) && $data['returnCode'] === '0000') {
$payStatus = true; // 這邊算是完成收款了
}
}
收款開心
