PHP code example of yidas / line-pay-sdk

1. Go to this page and download the library: Download yidas/line-pay-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yidas / line-pay-sdk example snippets


// Create LINE Pay client
$linePay = new \yidas\linePay\Client([
    'channelId' => 'Your merchant X-LINE-ChannelId',
    'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
    'isSandbox' => true, 
]);

// Online Request API
$response = $linePay->request([
    'amount' => 250,
    'currency' => 'TWD',
    'orderId' => 'Your order ID',
    'packages' => [
        [
            'id' => 'Your package ID',
            'amount' => 250,
            'name' => 'Your package name',
            'products' => [
                [
                    'name' => 'Your product name',
                    'quantity' => 1,
                    'price' => 250,
                    'imageUrl' => 'https://yourname.com/assets/img/product.png',
                ],
            ],
        ],
    ],
    'redirectUrls' => [
        'confirmUrl' => 'https://yourname.com/line-pay/confirm',
        'cancelUrl' => 'https://yourname.com/line-pay/cancel',
    ],
]);

// Check Request 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() );



use yidas\linePay\Client;

$linePay = new \yidas\linePay\Client([
    'channelId' => 'Your merchant X-LINE-ChannelId',
    'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
    'isSandbox' => true, 
]);

$linePay = new \yidas\linePay\Client([
    'channelId' => 'Your merchant X-LINE-ChannelId',
    'channelSecret' => 'Your merchant X-LINE-ChannelSecret',
    'isSandbox' => true, 
    'merchantDeviceType' => 'Device type string',
    'merchantDeviceProfileId' => 'Device profile ID string',
]);

// Get object of response body
$bodyObject = response->toObject();
// Get LINE Pay results code from response
$returnCode = $response->returnCode;
// Get LINE Pay info.payInfo[] from response
$payInfo = $response->info->payInfo;

// Get array of response body
$bodyArray = response->toArray();
// Get LINE Pay results code from response
$returnCode = $response['returnCode'];
// Get LINE Pay info.payInfo[] from response
$payInfo = $response['info']['payInfo'];

if (!$response->isSuccessful()) {
    
    throw new Exception("Code {$response['returnCode']}: {$response['returnMessage']}");
}

public Response details(array $queryParams=null)

$response = $linePay->details([
    "transactionId" => [$transactionId],
]);

public Response request(array $bodyParams=null)
 
$response = $linePay->request([
    'amount' => 250,
    'currency' => 'TWD',
    'orderId' => 'Your order ID',
    'packages' => [
        [
            'id' => 'Your package ID',
            'amount' => 250,
            'name' => 'Your package name',
            'products' => [
                [
                    'name' => 'Your product name',
                    'quantity' => 1,
                    'price' => 250,
                    'imageUrl' => 'https://yourname.com/assets/img/product.png',
                ],
            ],
        ],
    ],
    'redirectUrls' => [
        'confirmUrl' => 'https://yourname.com/line-pay/confirm',
        'cancelUrl' => 'https://yourname.com/line-pay/cancel',
    ],
]);

public Response confirm(integer $transactionId, array $bodyParams=null)

$response = $linePay->confirm($transactionId, [
    "amount" => 250,
    "currency" => 'TWD',
]);

public Response refund(integer $transactionId, array $bodyParams=null)

$response = $linePay->refund($transactionId);

$response = $linePay->refund($transactionId, [
    'refundAmount' => 200,
]);

public Response check(integer $transactionId)

$response = $linePay->check($transactionId);

public Response authorizationsCapture(integer $transactionId, array $bodyParams=null)

$response = $linePay->authorizationsCapture($transactionId, [
    "amount" => 250,
    "currency" => 'TWD',
]);

public Response authorizationsVoid(integer $transactionId, array $bodyParams=null)

$response = $linePay->authorizationsVoid($transactionId);

public Response preapproved(integer $regKey, array $bodyParams=null)

$response = $linePay->preapproved([
    'productName' => 'Your product name',
    'amount' => 250,
    'currency' => 'TWD',
    'orderId' => 'Your order ID',
]);

public Response preapprovedCheck(integer $regKey, array $queryParams=null)

$response = $linePay->preapprovedCheck($regKey);

public Response preapprovedExpire(integer $regKey, array $bodyParams=null)

$response = $linePay->preapprovedExpire($regKey);

public Response oneTimeKeysPay(array $bodyParams=null)

$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',
]);

public Response ordersCheck(string $orderId, array $$queryParams=null)

$response = $linePay->ordersCheck($orderId);

public Response ordersVoid(string $orderId, array $bodyParams=null)

$response = $linePay->ordersVoid($orderId);

public Response ordersCapture(string $orderId, array $bodyParams=null)

$response = $linePay->ordersCapture($orderId);

public Response ordersRefund(string $orderId, array $bodyParams=null)

$response = $linePay->ordersRefund($orderId);

public Response authorizations(array $queryParams=null)

$response = $linePay->authorizations([
    "transactionId" => [$transactionId],
]);

$response = $linePay->authorizations([
    "orderId" => $orderId,
]);

try {

    $response = $linePay->confirm($transactionId, $bodyParams);
    
} catch (\yidas\linePay\exception\ConnectException $e) {

    // Process of confirm API timeout handling
}