xentixar/khalti-sdk 问题修复 & 功能扩展

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

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

xentixar/khalti-sdk

Composer 安装命令:

composer require xentixar/khalti-sdk

包简介

README 文档

README

The KhaltiSdk is a PHP library for integrating the Khalti payment gateway into your application. This library simplifies the process of initializing payments, validating transactions, and more.

Installation

To use this library, you'll need to include it in your project. You can install it via Composer. If you don't have Composer installed, you can download it from getcomposer.org.

Step-by-step Installation

  1. Install Composer:

    curl -sS https://getcomposer.org/installer | php
    mv composer.phar /usr/local/bin/composer
  2. Add the package to your project:

    composer require xentixar/khalti-sdk
  3. Include Composer's autoloader in your script:

    require_once __DIR__ . '/vendor/autoload.php';

Usage

Setting Up the Khalti Instance

Create a new instance of the Khalti class and set your secret key using the setSecretKey method. The secret key is required for authorization when making API requests.

Example Code

<?php

use Xentixar\KhaltiSdk\Khalti;

require_once __DIR__ . "/vendor/autoload.php";

$khalti = new Khalti();

// Set your Khalti secret key
$khalti->setSecretKey('test_secret_key_1392a63451d740c59806685acd57730a');

// Configure the payment details
$khalti->config(
    'http://localhost:8000/decode.php',  // Return URL
    'http://localhost:8000',            // Website URL
    10,                                 // Amount in rupees
    '124',                              // Purchase Order ID
    'test',                             // Purchase Order Name
    [                                   // Customer Information
        "name" => "Khalti Bahadur",
        "email" => "example@gmail.com",
        "phone" => "9800000000"
    ]
);

// Initiate the payment
$khalti->init();
?>

Configuration Details

The config method allows you to specify several parameters:

  • $return_url: The URL where the user will be redirected after payment.
  • $website_url: Your website's URL.
  • $amount: The payment amount in rupees.
  • $purchase_order_id: A unique identifier for the purchase order.
  • $purchase_order_name: The name of the purchase order.
  • $customer_info: An optional array containing customer details (name, email, phone).
  • $amount_breakdown: Optional breakdown of the amount (e.g., tax, shipping).
  • $product_details: Optional product details.

Initiating a Payment

The init method sends a request to the Khalti API to initiate the payment process. It handles the request server-side, and if the response contains a payment_url, it redirects the user to the Khalti payment page.

Note: For production, set the $production flag to true in the init method.

$khalti->init(true); // Set to true for production

Validating a Transaction

To validate a transaction, use the validate method. This method sends a request to the Khalti API to check the status of a transaction.

$response = $khalti->validate('transaction_code_here');

if ($response) {
    // Handle the response
    echo $response;
} else {
    echo "Error: Invalid response from Khalti API.";
}

Note: For production, set the $production flag to true in the validate method.

$response = $khalti->validate('transaction_code_here', true); // Set to true for production

Decoding the Response

To decode the response data from Khalti:

$data = $khalti->decode();
if ($data) {
    // Process the data
    print_r($data);
} else {
    echo "Error: No data to decode.";
}

Error Handling

The init method includes basic error handling for cURL requests. You may want to add additional error handling based on your application's requirements.

Contribution

Contributions are welcome! Please submit a pull request or create an issue to report bugs or request features.

License

This library is licensed under the MIT License. See the LICENSE file for more details.

Additional Resources

xentixar/khalti-sdk 适用场景与选型建议

xentixar/khalti-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 xentixar/khalti-sdk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-18