carllee/line-pay-core-v4 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

carllee/line-pay-core-v4

Composer 安装命令:

composer require carllee/line-pay-core-v4

包简介

Core library for LINE Pay API V4 SDK - Provides shared utilities, base client, types, and error handling for building LINE Pay integrations

README 文档

README

License: MIT PHP Version

Core library for LINE Pay API V4 SDK. Provides shared utilities, base client, configuration, and error handling that power the Online and Offline SDKs.

🌐 Language / 語言 / 言語 / ภาษา: English | 繁體中文 | 日本語 | ภาษาไทย

Architecture

graph TD
    subgraph "Your Application"
        A[Your Code]
    end
    
    subgraph "LINE Pay SDKs"
        B[line-pay-online-v4]
        C[line-pay-offline-v4]
    end
    
    subgraph "Core Layer"
        D[line-pay-core-v4]
        D1[LinePayBaseClient]
        D2[LinePayUtils]
        D3[Error Classes]
        D4[Configuration]
    end
    
    subgraph "LINE Pay API"
        E[LINE Pay Server]
    end
    
    A --> B
    A --> C
    B --> D
    C --> D
    D --> D1
    D --> D2
    D --> D3
    D --> D4
    D1 --> E
    
    style D fill:#f9f,stroke:#333,stroke-width:2px
    style B fill:#bbf,stroke:#333
    style C fill:#bbf,stroke:#333
Loading

Overview

This package is the shared foundation for building LINE Pay V4 integrations in PHP. It handles the "heavy lifting" so that the Online and Offline SDKs can focus on their specific API logic.

Key Responsibilities

Component What It Does Why It Matters
HMAC-SHA256 Signature Generates and verifies API signatures The most complex part of LINE Pay V4 API — one wrong byte and requests fail
HTTP Client Wrapper Encapsulates Guzzle with retry logic Handles timeouts, connection errors, and response parsing consistently
Unified Error Parsing Parses LINE Pay error codes into typed exceptions 1xxx = Auth, 2xxx = Payment, 9xxx = Internal — no more guessing
Configuration Management Type-safe config with environment support Prevents "oops, wrong credentials in production" mistakes

Requirements

  • PHP 8.1 or higher
  • ext-json
  • ext-openssl
  • Guzzle HTTP Client 7.0+

Installation

composer require carllee/line-pay-core-v4

⚠️ Note: This is a core library meant to be used as a dependency.

Most developers should use the ready-made SDKs instead:

Use this package directly only if you need to build a custom LINE Pay client with specialized behavior.

Usage

Creating a Custom Client

use LinePay\Core\LinePayBaseClient;
use LinePay\Core\Config\LinePayConfig;

class MyLinePayClient extends LinePayBaseClient
{
    public function requestPayment(array $body): array
    {
        return $this->sendRequest('POST', '/v3/payments/request', $body);
    }

    public function confirmPayment(string $transactionId, array $body): array
    {
        return $this->sendRequest(
            'POST',
            "/v3/payments/{$transactionId}/confirm",
            $body
        );
    }
}

// Usage
$config = new LinePayConfig(
    channelId: getenv('LINE_PAY_CHANNEL_ID'),
    channelSecret: getenv('LINE_PAY_CHANNEL_SECRET'),
    env: 'sandbox', // or 'production'
    timeout: 30
);

$client = new MyLinePayClient($config);

Utilities

use LinePay\Core\LinePayUtils;

// Generate signature for API requests
$signature = LinePayUtils::generateSignature(
    $channelSecret,
    '/v3/payments/request',
    json_encode($requestBody),
    $nonce
);

// Validate transaction ID format (must be 19 digits)
if (LinePayUtils::isValidTransactionId($transactionId)) {
    // Process transaction
}

// Parse callback query parameters
$result = LinePayUtils::parseConfirmQuery($_GET);
// $result['transactionId'], $result['orderId']

Security: Timing-Safe Signature Verification

The verifySignature method uses constant-time comparison to prevent timing attacks:

use LinePay\Core\LinePayUtils;

// ✓ SECURE: Uses hash_equals() internally (timing-safe)
$isValid = LinePayUtils::verifySignature($secret, $data, $receivedSignature);

// ✗ INSECURE: Never do direct string comparison for signatures
// $isValid = ($expectedSignature === $receivedSignature); // Vulnerable to timing attacks!

Why this matters: A timing attack can determine how many characters of a signature match by measuring response time. Constant-time comparison always takes the same amount of time regardless of how many characters match.

Error Handling

LINE Pay API error codes follow a pattern:

Code Range Category Description
1xxx Authentication Channel ID/Secret issues, invalid signatures
2xxx Payment Transaction errors, insufficient balance, expired
9xxx Internal LINE Pay server errors, maintenance
use LinePay\Core\Errors\LinePayError;
use LinePay\Core\Errors\LinePayTimeoutError;
use LinePay\Core\Errors\LinePayConfigError;
use LinePay\Core\Errors\LinePayValidationError;

try {
    $response = $client->requestPayment($body);
} catch (LinePayTimeoutError $e) {
    // Handle timeout - IMPORTANT: Check payment status!
    echo "Request timed out after {$e->getTimeout()} seconds";
} catch (LinePayValidationError $e) {
    // Handle validation errors (before API call)
    echo "Invalid input: {$e->getMessage()}";
} catch (LinePayError $e) {
    // Handle API errors
    echo "Error [{$e->getReturnCode()}]: {$e->getReturnMessage()}";
    
    if ($e->isAuthError()) {
        // 1xxx: Check your Channel ID/Secret, or signature generation
        error_log("Auth failed - verify credentials");
    } elseif ($e->isPaymentError()) {
        // 2xxx: Transaction-specific issue (e.g., already refunded)
        notifyUser("Payment could not be processed");
    } elseif ($e->isInternalError()) {
        // 9xxx: LINE Pay server issue - retry with backoff
        scheduleRetry($body);
    }
} catch (LinePayConfigError $e) {
    // Configuration error (missing/invalid credentials)
    echo "Configuration error: {$e->getMessage()}";
}

Configuration

Parameter Type Required Default Description
channelId string Yes - Channel ID from LINE Pay Merchant Center
channelSecret string Yes - Channel Secret from LINE Pay Merchant Center
env string No 'sandbox' Environment: 'production' or 'sandbox'
timeout int No 20 Request timeout in seconds

Related Packages

Development

# Install dependencies
composer install

# Run tests
composer test

# Run tests with coverage
composer test:coverage

# Run static analysis
composer analyze

# Fix code style
composer lint:fix

License

MIT License - see the LICENSE file for details.

Author

Carl Lee - GitHub

carllee/line-pay-core-v4 适用场景与选型建议

carllee/line-pay-core-v4 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 287 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「payment」 「sdk」 「line」 「core」 「japan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 carllee/line-pay-core-v4 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 carllee/line-pay-core-v4 我们能提供哪些服务?
定制开发 / 二次开发

基于 carllee/line-pay-core-v4 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 287
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-11