Victorzhn / line-pay-sdk
适用于PHP 7.0+和guzzle7.3的LINE Pay SDK
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^7.3
README
为PHP的LINE Pay SDK (for)
为PHP的LINE Pay SDK
英文 | 繁体中文
目录
演示
// Create LINE Pay client $linePay = new \victorzhn\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() );
要求
此库需要以下内容
- PHP 5.4.0+|7.0+
- guzzlehttp/guzzle 5.3.1+|6.0+
- LINE Pay商户身份验证
身份验证
每个LINE Pay商户都需要身份验证信息以集成LINE Pay,如下所示
- 渠道ID
- 渠道密钥
获取LINE Pay身份验证
- 商户的验证信息可以在评估过程完成后在LINE Pay商户中心查看。
- 登录LINE Pay商户中心,然后获取ChannelId/ChannelSecret(支付集成管理 > 管理链接密钥)。
- 在LINE Pay商户中心中,为您的服务器设置IP白名单(支付集成管理 > 管理支付服务器IP)(v3不需要)。
您可以通过LINE Pay沙盒创建立即创建一个沙盒商户账户进行测试。
安装
在您的项目中运行Composer
composer require victorzhn/line-pay-sdk ~3.0.0
然后您可以在PHP项目中加载Composer后使用SDK类
require __DIR__ . '/vendor/autoload.php'; use victorzhn\linePay\Client;
使用
在使用任何API方法之前,首先需要使用配置创建一个客户端,然后使用该客户端访问LINE Pay API方法。
客户端
使用API身份验证创建LINE Pay客户端
$linePay = new \victorzhn\linePay\Client([ 'channelId' => 'Your merchant X-LINE-ChannelId', 'channelSecret' => 'Your merchant X-LINE-ChannelSecret', 'isSandbox' => true, ]);
设备信息
您可以为客户端设置设备信息(可选)
$linePay = new \victorzhn\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', ]);
响应
每个API方法将返回victorzhn\linePay\Response
对象,可以检索与LINE Pay JSON响应数据结构相关的数据。
获取数据
响应对象提供通过对象属性或数组键访问响应体数据的方式
以对象形式获取
// 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'];
方法
响应对象提供一些有用的方法来使用
isSuccessful()
LINE Pay API结果成功状态(检查returnCode是否为"0000")
示例
if (!$response->isSuccessful()) { throw new Exception("Code {$response['returnCode']}: {$response['returnMessage']}"); }
getPaymentUrl()
获取LINE Pay API响应体的info.paymentUrl(默认类型为"web")
getPayInfo()
以数组形式获取LINE Pay API响应体的info.payInfo[]或info.[$param1].payInfo[]
toArray()
以数组形式获取LINE Pay响应体
toObject()
获取 LINE Pay 响应体作为对象
getStats()
获取 \GuzzleHttp\TransferStats
对象
在线API
针对 Web 集成。商家向客户请求支付并生成支付 URL(二维码),供客户通过 LINE App 扫描。
支付详情API
获取使用 LINE Pay 完成的支付详情。此 API 只获取已捕获的支付。
public Response details(array $queryParams=null)
示例
$response = $linePay->details([ "transactionId" => [$transactionId], ]);
请求API
在处理 LINE Pay 支付之前,商家将进行评估,如果是普通商家店铺,则请求支付信息。当支付请求成功时,商家将获得一个 "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', ], ]);
有关
$bodyParams
规范,请参阅 请求 API v3 请求体
确认API
此 API 用于商家完成其支付。商家必须调用确认支付 API 才能实际完成支付。但是,当支付预约中的 "capture" 参数为 "false" 时,支付状态变为授权,只有在调用 "捕获 API" 后才能完成支付。
public Response confirm(integer $transactionId, array $bodyParams=null)
示例
$response = $linePay->confirm($transactionId, [ "amount" => 250, "currency" => 'TWD', ]);
退款API
请求使用 LINE Pay 完成的支付退款。要退款,必须转发 LINE Pay 用户的支付事务 ID。根据退款金额,也可以进行部分退款。
public Response refund(integer $transactionId, array $bodyParams=null)
示例
$response = $linePay->refund($transactionId);
针对部分退款
$response = $linePay->refund($transactionId, [ 'refundAmount' => 200, ]);
检查支付状态API
一个用于检查 LINE Pay 支付请求状态的 API。商家应定期检查用户支付确认状态 不使用 ConfirmURL 并决定是否可以完成支付。
public Response check(integer $transactionId)
示例
$response = $linePay->check($transactionId);
捕获API
当商家调用 "请求 API" 时,如果 "capture" 为 "false",则只有在调用捕获 API 后才会完成支付。
public Response authorizationsCapture(integer $transactionId, array $bodyParams=null)
示例
$response = $linePay->authorizationsCapture($transactionId, [ "amount" => 250, "currency" => 'TWD', ]);
作废API
取消之前授权的支付。已捕获的支付可以使用 "退款 API" 进行退款。
public Response authorizationsVoid(integer $transactionId, array $bodyParams=null)
示例
$response = $linePay->authorizationsVoid($transactionId);
支付预批准API
当请求 API 的支付类型设置为 PREAPPROVED 时,支付结果会返回一个 regKey。预授权支付 API 使用此 regKey 直接完成支付,而无需使用 LINE 应用。
public Response preapproved(integer $regKey, array $bodyParams=null)
示例
$response = $linePay->preapproved([ 'productName' => 'Your product name', 'amount' => 250, 'currency' => 'TWD', 'orderId' => 'Your order ID', ]);
检查RegKey API
在使用预授权支付 API 之前,检查 regKey 是否可用。
public Response preapprovedCheck(integer $regKey, array $queryParams=null)
示例
$response = $linePay->preapprovedCheck($regKey);
过期RegKey API
使预授权支付注册的 regKey 信息过期。一旦调用 API,regKey 就不再用于预授权支付。
public Response preapprovedExpire(integer $regKey, array $bodyParams=null)
示例
$response = $linePay->preapprovedExpire($regKey);
离线API
针对 POS 集成。客户向商家展示其条形码或二维码,由商家在 POS 机上扫描。
流程:
OneTimeKeysPay
->OrdersCheck
->OrdersRefund
支付
此 API 用于通过商家的设备读取 LINE Pay App 提供的 MyCode 来处理支付。
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', ]);
支付状态检查
这是在读取超时导致无法检查最终支付状态时使用的 API。
- 需要定期调用以检查状态,建议间隔时间为 3~5 秒。
- 支付有效时间最长为 20 分钟,从支付 API 响应时间开始计算。因此,商家应检查支付状态最多 20 分钟。如果超过 20 分钟,则该交易将因有效时间超时而无法完成支付。
public Response ordersCheck(string $orderId, array $$queryParams=null)
示例
$response = $linePay->ordersCheck($orderId);
作废
此 API 用于取消授权。
public Response ordersVoid(string $orderId, array $bodyParams=null)
示例
$response = $linePay->ordersVoid($orderId);
捕获
此 API 用于捕获授权交易。
public Response ordersCapture(string $orderId, array $bodyParams=null)
示例
$response = $linePay->ordersCapture($orderId);
退款
此 API 用于支付完成后进行退款(捕获数据)。
public Response ordersRefund(string $orderId, array $bodyParams=null)
示例
$response = $linePay->ordersRefund($orderId);
授权详情
此API用于搜索授权详情。只能搜索已授权或已取消(作废或过期)的数据,捕获后的数据可以通过支付详情API进行搜索。
public Response authorizations(array $queryParams=null)
搜索transactionId的示例
$response = $linePay->authorizations([ "transactionId" => [$transactionId], ]);
搜索orderId的示例
$response = $linePay->authorizations([ "orderId" => $orderId, ]);
异常
客户端在API交易过程中发生错误时抛出异常。
ConnectException
在网络错误(超时)的情况下,会抛出victorzhn\linePay\exception\ConnectException
异常。
try { $response = $linePay->confirm($transactionId, $bodyParams); } catch (\victorzhn\linePay\exception\ConnectException $e) { // Process of confirm API timeout handling }
资源
LINE Pay OneTimeKeys模拟(针对台湾商家)