LINE Pay SDK for PHP
English | 繁體中文
- Demonstration
- Requirements
- Installation
- Usage
- Resources
- References
Sample Codes Site for LINE Pay (Reserve, Confirm, Refund)
// Create LINE Pay client
$linePay = new \yidas\linePay\Client([
'channelId' => 'Your merchant X-LINE-ChannelId',
'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
'isSandbox' => true,
]);
// Online Reserve API
$response = $linePay->reserve([
'productName' => 'Your product name',
'amount' => 250,
'currency' => 'TWD',
'confirmUrl' => 'https://yourname.com/line-pay/confirm',
'productImageUrl' => 'https://yourname.com/assets/img/product.png',
'cancelUrl' => 'https://yourname.com/line-pay/cancel',
'orderId' => 'Your order ID',
]);
// Check Reserve API result (returnCode "0000" check method)
if (!$response->isSuccessful()) {
throw new Exception("ErrorCode {$response['returnCode']}: {$response['returnMessage']}");
}
// Redirect to LINE Pay payment URL
header('Location: '. $response->getPaymentUrl() );
This library requires the following:
- PHP 5.4.0+|7.0+
- guzzlehttp/guzzle 5.3.1+|6.0+
- LINE Pay merchant authentication
Each LINE Pay merchant Requires authentication information for LINE Pay integration is as below.
- channel id
- channel secret key
To get an LINE Pay Authentication:
- Merchant's verification information can be viewed at LINE Pay Merchant Center after evaluation process is complete.
- Login into LINE Pay Merchant Center, then get the ChannelId/ChannelSecret (Payment Integration Management > Manage Link Key).
- In LINE Pay Merchant Center, set IP white list for your servers (Payment Integration Management > Manage Payment Server IP).
You can immediately create a sandbox merchant account for test through LINE Pay Sandbox Creation.
Run Composer in your project:
composer require yidas/line-pay-sdk
Then you could use SDK class after Composer is loaded on your PHP project:
require __DIR__ . '/vendor/autoload.php';
use yidas\linePay\Client;
Before using any API methods, first you need to create a Client with configuration, then use the client to access LINE Pay API methods.
Create a LINE Pay Client with API Authentication:
$linePay = new \yidas\linePay\Client([
'channelId' => 'Your merchant X-LINE-ChannelId',
'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
'isSandbox' => true,
]);
You could set device information for Client (Optional):
$linePay = new \yidas\linePay\Client([
'channelId' => 'Your merchant X-LINE-ChannelId',
'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
'isSandbox' => ($env=='real') ? false : true,
'merchantDeviceType' => 'Device type string',
'merchantDeviceProfileId' => 'Device profile ID string',
]);
Each API methods will return yidas\linePay\Response
object, which can retrieve data referred to LINE Pay JSON response data structure.
Response object provides response body data accessing by object attributes or array keys:
// Get LINE Pay results code from response
$returnCode = $response->returnCode;
// Get LINE Pay info.payInfo[] from response
$payinfo = $response->info->payinfo;
// Get LINE Pay results code from response
$returnCode = $response['returnCode'];
// Get LINE Pay info.payInfo[] from response
$payinfo = $response['info']['payinfo'];
Response object has some helpful methods to use:
LINE Pay API result successful status (Check that returnCode is "0000")
Example:
if (!$response->isSuccessful()) {
throw new Exception("Code {$response['returnCode']}: {$response['returnMessage']}");
}
Get LINE Pay API response body's info.paymentUrl (Default type is "web")
Get LINE Pay API response body's info.payInfo[] or info.[$param1].payInfo[] as array
Get LINE Pay response body as array
Get LINE Pay response body as object
For Web integration. Merchant will reserves a payment and generates payment URL(QR code) to customer to scan by LINE App.
Gets the details of payments made with LINE Pay. This API only gets the payments that have been captured.
public Response details(array $queryParams=null)
Example:
$response = $linePay->details([
"transactionId" => [$transactionId],
]);
Prior to processing payments with LINE Pay, the Merchant is evaluated if it is a normal Merchant store then the information is reserved for payment. When a payment is successfully reserved, the Merchant gets a "transactionId" that is a key value used until the payment is completed or refunded.
public Response reserve(array $optParams=null)
Example:
$response = $linePay->reserve([
'productName' => 'Your product name',
'amount' => 250,
'currency' => 'TWD',
'confirmUrl' => 'https://yourname.com/line-pay/confirm',
'productImageUrl' => 'https://yourname.com/assets/img/product.png',
'cancelUrl' => 'https://yourname.com/line-pay/cancel',
'orderId' => 'Your order ID',
]);
This API is used for a Merchant to complete its payment. The Merchant must call Confirm Payment API to actually complete the payment. However, when "capture" parameter is "false" on payment reservation, the payment status becomes AUTHORIZATION, and the payment is completed only after "Capture API" is called.
public Response confirm(integer $transactionId, array $optParams=null)
Example:
$response = $linePay->confirm($transactionId, [
"amount" => 250,
"currency" => 'TWD',
]);
Requests refund of payments made with LINE Pay. To refund a payment, the LINE Pay user's payment transactionId must be forwarded. A partial refund is also possible depending on the refund amount.
public Response refund(integer $transactionId, array $optParams=null)
Example:
$response = $linePay->refund($transactionId);
For Partial refund:
$response = $linePay->refund($transactionId, [
'refundAmount' => 200,
]);
Gets the details authorized with LINE Pay. This API only gets data that is authorized or whose authorization is voided; the one that is already captured can be viewed by using "Get Payment Details API”.
public Response authorizations(array $queryParams=null)
Example:
$response = $linePay->authorizations([
"transactionId" => [$transactionId],
]);
If "capture" is "false" when the Merchant calls the “Reserve Payment API” , the payment is completed only after the Capture API is called.
public Response authorizationsCapture(integer $transactionId, array $optParams=null)
Example:
$response = $linePay->authorizationsCapture($transactionId, [
"amount" => 250,
"currency" => 'TWD',
]);
Voids a previously authorized payment. A payment that has been already captured can be refunded by using the “Refund Payment API”.
public Response authorizationsVoid(integer $transactionId, array $optParams=null)
Example:
$response = $linePay->authorizationsVoid($transactionId);
When the payment type of the Reserve Payment API was set as PREAPPROVED, a regKey is returned with the payment result. Preapproved Payment API uses this regKey to directly complete a payment without using the LINE app.
public Response preapproved(integer $regKey, array $optParams=null)
Example:
$response = $linePay->preapproved([
'productName' => 'Your product name',
'amount' => 250,
'currency' => 'TWD',
'orderId' => 'Your order ID',
]);
Checks if regKey is available before using the preapproved payment API.
public Response preapprovedCheck(integer $regKey, array $queryParams=null)
Example:
$response = $linePay->preapprovedCheck($regKey);
Expires the regKey information registered for preapproved payment. Once the API is called, the regKey is no longer used for preapproved payments.
public Response preapprovedExpire(integer $regKey, array $optParams=null)
Example:
$response = $linePay->preapprovedExpire($regKey);
For POS integration. Customer presents their barcode or QR code to merchants to scan at the POS machine.
Flow:
OneTimeKeysPay
->OrdersCheck
->OrdersRefund
This API is to process payment by reading MyCode provided from LINE Pay App with Merchant's device.
public Response oneTimeKeysPay(array $optParams=null)
Example:
$response = $linePay->oneTimeKeysPay([
'productName' => 'Your product name',
'amount' => 250,
'currency' => 'TWD',
'productImageUrl' => 'https://yourname.com/assets/img/product.png',
'orderId' => 'Your order ID',
"oneTimeKey"=> 'LINE Pay MyCode',
]);
It's the API used when the final payment status can't be checked due to read timeout.
- The status needs to be checked by calling it at regular intervals and 3~5 seconds are recommended for the interval time.
- Payment valid time is maximum 20 minutes and it's calculated from the Payment API Response time. Therefore, a merchant should check the payment status for maximum 20 minutes. In case 20 minutes are exceeded, that transaction will be the payment that couldn't be completed due to exceeded valid time.
public Response ordersCheck(string $orderId, array $$queryParams=null)
Example:
$response = $linePay->ordersCheck($orderId);
This API is to void the authorization.
public Response ordersVoid(string $orderId, array $optParams=null)
Example:
$response = $linePay->ordersVoid($orderId);
This API is to capture the authorized transaction.
public Response ordersCapture(string $orderId, array $optParams=null)
Example:
$response = $linePay->ordersCapture($orderId);
This API is to refund after the payment completion (Captured data).
public Response ordersRefund(string $orderId, array $optParams=null)
Example:
$response = $linePay->ordersRefund($orderId);
Refer to Online Get Authorization Details API
This API is to search the authorization details. Only authorized or cancelled (Void or Expire) data can be searched and the data after capturing can be searched by The Payment Details API.
Example:
$response = $linePay->authorizations([
"orderId" => $orderId,
]);
LINE Pay Integration Guide v1.1.2 (EN)
LINE Pay Integration Guide (For Offline Merchant) application email
LINE Pay OneTimeKeys Simulation
LINE Pay OneTimeKeys Simulation (For TW Merchant)