承接 hydrogenpay/hydrogenpay-sdk 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hydrogenpay/hydrogenpay-sdk

Composer 安装命令:

composer require hydrogenpay/hydrogenpay-sdk

包简介

A straightforward SDK for seamlessly integrating Hydrogenpay Payment Gateway into your PHP applications, including popular PHP frameworks such as Laravel, Symfony and Codeigniter

README 文档

README

Hydrogen Payment Gateway helps you process payments using cards and account transfers for faster delivery of goods and services on your PHP site.
It abstracts the complexity involved in direct integration and allows you to make quick calls to the APIs.

This SDK communicates with Hydrogen API. You need to have a Hydrogen merchant account and Auth Key to use this SDK.

Requirements

  • Composer

  • An IDE

  • Hydrogen Auth Token

  • Acceptable PHP versions: >= 7.4.0. for older versions of PHP

Sign up account here: https://dashboard.hydrogenpay.com/signup 

Installation 

To get started, First you need to install the package into your existing project.

To install the package via Composer, run the following command.


composer require hydrogenpay/hydrogenpay-sdk:@dev


Alternatively, you can add the package to your composer.json file and run the command composer install on your editor terminal.

{
    "require": {

        "hydrogenpay/hydrogenpay-sdk": "@dev"

    }
}

This command installs the package. The package can be found in the vendor folder. if you get an error message while running the command, ensure you have composer installed.

Initialization 

After installation, create a .env file in the root of the project. Most frameworks (such as Laravel and Symfony support the use of .env files). if you are not using a framework you need to create one.

Create a .env file and follow the format of the .env.example file.Save your, SANDBOX, LIVE_API_KEY, MODE  in the .env fileYour .env file should look like the below:

SANDBOX=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

LIVE_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

MODE=test

# MODE=live // test mode for payment testing and live mode is for production

The SDK provides two easy methods of making collections via the Payment Gateway modal. 

  • REDIRECT

  • POPUP

try {
    HydrogenpayAfrica::bootstrap();

    $customHandler = new PaymentHandler();

    $client = new HydrogenpayAfrica();

    $modalType = Modal::REDIRECT; // Modal::POPUP or Modal::REDIRECT

    $controller = new TransactionController( $client, $customHandler, $modalType );

} catch(\Exception $e ) {

    echo $e->getMessage();

}

Edit the processTransaction.php files to suite your purpose by either changing the Modal to POPUP or REDIRECT.

Transaction Resources 

Edit the transactionForm.php and processTransaction.php files to suit your purpose. Both files are well documented.

Simply redirect to the transactionForm.php file on your browser to process a payment.

In this implementation, we are expecting a form-encoded POST request.The request will contain the following parameters. Request parameters

Request Parameters

Mandatory Name Comment
Yes amount The amount to be charged for the transaction.
Yes email The customer's email address.
Yes currency The currency in which the transaction is processed.
No description A brief description of the transaction.
No meta Additional metadata or information related to the transaction.
Yes callback Callback redirection
{
    "amount": "The amount to be charged for the transaction",
    "customerName": "Dev Test",
    "email": "devtest@randomuser.com",
    "currency": "NGN",
    "description": "test desc",
    "meta": "test meta",
    "callback": "https://docs.hydrogenpay.com/reference/payment-gateway",
  }

The script in processTransaction.php handles the request data via the TransactionController. If you are using a Framework like Laravel or CodeIgniter you might want to take a look at the TransactionController. 

<?php

declare(strict_types=1);

namespace HydrogenpayAfrica\Controller;

use HydrogenpayAfrica\EventHandlers\ModalEventHandler;
use HydrogenpayAfrica\EventHandlers\EventHandlerInterface;
use HydrogenpayAfrica\HydrogenpayAfrica;
use HydrogenpayAfrica\Entities\Payload;
use HydrogenpayAfrica\Library\Modal;
use HydrogenpayAfrica\Service\Transactions;

final class TransactionController
{
    private string $requestMethod;

    private EventHandlerInterface $handler;
    private HydrogenpayAfrica $client;
    private string $modalType;
    protected array $routes = [
        'process' => 'POST',
        'callback' => 'GET'
    ];

    public function __construct(
        HydrogenpayAfrica $client,
        EventHandlerInterface $handler,
        string $modalType
    ) {
        HydrogenpayAfrica::bootstrap();
        $this->requestMethod =  $this->getRequestMethod();
        $this->handler = $handler;
        $this->client = $client;
        $this->modalType = $modalType;
    }

    private function getRequestMethod(): string
    {
        return ($_SERVER["REQUEST_METHOD"] === "POST") ? 'POST' : 'GET';
    }

    public function __call(string $name, array $args)
    {
        if ($this->routes[$name] !== $this->requestMethod) {

            echo "Unauthorized page!";
        }

        call_user_func_array(array($this, $name), $args);
    }

    private function handleSessionData(array $request)
    {
        $_SESSION['success_url'] = $request['success_url'];
        $_SESSION['failure_url'] = $request['failure_url'];
    }
    public function process(array $request)
    {
        $this->handleSessionData($request);

        try {
            $_SESSION['p'] = $this->client;

            if ('inline' === $this->modalType) {
                echo $this->client
                    ->eventHandler($this->handler)
                    ->render(Modal::POPUP)->with($request)->getHtml();
            } else {
                $paymentLink = $this->client
                    ->eventHandler($this->handler)
                    ->render(Modal::REDIRECT)->with($request)->getUrl();
                header('Location: ' . $paymentLink);
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function callback(array $request)
    {
        $transactionRef = $request['TransactionRef'];

        if (empty($transactionRef)) {
            session_destroy();
        }

        if (!isset($_SESSION['p'])) {
            echo "session expired!. please refresh you browser.";
            exit();
        }

        $payment = $_SESSION['p'];

        $payment::bootstrap();

        if (isset($request['TransactionRef'])) {
            $transactionRef = $request['TransactionRef'];

            $payment->logger->notice('Payment completed. Now requerying payment.');

            $payment
                ->eventHandler($this->handler)
                ->requeryTransaction($transactionRef);
        }
    }
}

Verifying Transaction

$request = $_GET;
# Confirming Transaction.
if(isset($request['TransactionRef'])) {
    $controller->callback( $request );
} else {
    
}
    /**
     * Requery initiation for a previous transaction from the hydrogen payment gateway
     * @param  string $transactionRef This should be the reference number of the transaction you want to requery
     * @throws ClientExceptionInterface
     * @throws ApiException
     */
    public function requeryTransaction(string $transactionRef): object
    {
        // Transaction reference
        $this->transactionRef = $transactionRef;

        // Log requery attempt
        $this->logger->notice('Requerying Transaction....' . $this->transactionRef);

        // Call the requery handler if it is set
        if (isset($this->handler)) {
            $this->handler->onRequery($this->transactionRef);
        }

        // Data Prepare for the requery
        $data = [
            'transactionRef' => $transactionRef,
        ];

        // Sending a POST request
        $response = $this->postURL(static::$config, $data);

        // Response to determine the status of the transaction
        $transConfirmation = $responseObj = (object) [
            'status' => $response, // Paid or Failed
        ];

        // Check if the transaction was successful or failed
        if ($transConfirmation->status == 'Paid') {
            $this->logger->notice('Requeryed a successful transaction....' . $response);

            // Handle successful transaction
            if (isset($this->handler)) {
                $this->handler->onSuccessful($transConfirmation->status);
            }
        } elseif ($transConfirmation->status == 'Failed') {
            // Log failed requery
            $this->logger->warning('Requeryed a failed transaction....' . $response);

            // Handle failed transaction
            if (isset($this->handler)) {
                $this->handler->onFailure($transConfirmation->status);
            }
        }

        // Return method
        return $this;
    }
    <!-- Enter the URL where you want your customers to be redirected after completing the payment process. 
        Ensure that this URL routes to your 'processTransaction.php' file. -->
    <input type="hidden" name="callback" value="https://yourwebsite.com/processTransaction.php">

    <!-- Enter the URL where you want your customers to be redirected after a successful transaction. -->
    <input type="hidden" name="success_url" value="https://yourwebsite.com/?status=success">

    <!-- Enter the URL where you want your customers to be redirected if the transaction fails. -->
    <input type="hidden" name="failure_url" value="https://yourwebsite.com/?status=failed">

                   

In transactionFrom.php, you can pass your desired URL for your project in the value:

  • Callback: Redirect URL after payment has been completed on the gateway.

  • Success_url : Redirect URL for Payment Success

  • Failure_url : Redirect URL for Payment Failed

Test

All of the SDK's tests are written with PHP's phpunit module.

Navigate to the SDK directory:

cd hydrogenpay/hydrogenpay-sdk

Install PHPUnit as a development dependency using Composer:

composer require --dev phpunit/phpunit

Run PHPUnit to execute the tests:

./vendor/bin/phpunit

Features

  • Accept payment via Mastercard, Visa, Verve, and Bank Account.

  • Seamless integration into your site checkout page. Accept payment directly on your site.

License

The MIT License (MIT). Please see License file for more information.

Hydrogenpay Api References

 - Hydrogenpay API Documentation

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-03