定制 eboseogbidi/smartpaymentrouter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

eboseogbidi/smartpaymentrouter

最新稳定版本:1.0.1

Composer 安装命令:

composer require eboseogbidi/smartpaymentrouter

包简介

This package can intelligently route payment transactions to the most suitable payment processor based on various factors such as transaction cost, reliability, and currency support.

README 文档

README

A flexible and intelligent payment routing system that helps you manage multiple payment processors with automatic fallback and smart routing based on cost, reliability, and supported currencies.

Installation

You can install the package via composer:

composer require eboseogbidi/smartpaymentrouter

Migration

Migrate the schema for the payment transaction log this helps to improve the reliability of the different payment processors:

php artisan migrate

Configuration

Publish the configuration file:

php artisan vendor:publish --tag=smartpaymentrouter-config

This will create a config/smartpaymentrouter.php file where you can configure your payment processors.

Basic Configuration Structure

return [
    'routing_rules' => [
        'transaction_cost' => true,
        'reliability' => true,
        'currency_support' => true,
    ],

    'processors' => [
        'stripe' => [
            'name' => "Stripe",
            'class' => \Eboseogbidi\Smartpaymentrouter\Processors\StripeProcessor::class,
            'transaction_cost' => 1.8,
            'reliability' => 95,
            'currencies' => ['USD', 'GBP'],
        ],
        // Add more processors here
    ],
];

Configuration parameters:

  • name: Display name for the processor
  • class: The processor class that implements PaymentProcessorInterface
  • transaction_cost: Processing fee percentage
  • reliability: Estimated uptime percentage
  • currencies: Array of supported currencies

Extending PaymentProcessorInterface

To create a new payment processor, implement the PaymentProcessorInterface:

use Eboseogbidi\Smartpaymentrouter\Interfaces\PaymentProcessorInterface;

class StripeProcessor implements PaymentProcessorInterface{

    //If you want to extend the callback functionality
    use DefaultCallbackTrait;

    public function getName():string{
        return 'Stripe';
    }

    public function processPayment(array $paymentData): array{

        //process stripe payment
        return $paymentData;
    }

    public function callback($response){
        return back()->with('success','Works fine');
    }


}

Using the Payment Router

Example of how to use the PaymentRouter in a Controller :

namespace Eboseogbidi\Smartpaymentrouter\Http\Controllers;

use Illuminate\Http\Request;
use Eboseogbidi\Smartpaymentrouter\Services\PaymentRouter;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;

class ProcessPaymentController extends BaseController
{
    
    public function __construct(protected PaymentRouter $paymentRouter){}
    

    /**
     * Handle the payment transaction.
     *
     * @param Request $request
     * @return mixed
     */
    public function handlePayment(Request $request): mixed
    {
       
        $request->validate([
            'amount' => 'required|numeric|min:0.01',
            'currency' => 'required|string|max:3', 
        ]);

        try {
            $paymentData = $request->all();

            $result = $this->paymentRouter->routeTransaction($paymentData);

           return $result;

        } catch (\Exception $e) {
            
            Log::error("Payment transaction error: {$e->getMessage()}");

            return back()->with('error','Transaction failed'. $e->getMessage());
        }
    }
}

Using the ProcessorManager

The ProcessorManager provides methods to interact with and modify the payment configuration:

use Eboseogbidi\Smartpaymentrouter\Services\ProcessorManager;

class PaymentService
{

    public function __construct(protected ProcessorManager $processorManager){}

    public function updateProcessorConfig()
    {
        // Add new processor
        $this->processorManager->addProcessor('paypal', [
            'name' => 'PayPal',
            'class' => PayPalProcessor::class,
            'transaction_cost' => 2.0,
            'reliability' => 98,
            'currencies' => ['USD', 'EUR'],
        ]);

        // Update existing processor
        $this->processorManager->updateProcessor('stripe', [
            'transaction_cost' => 1.9,
            'reliability' => 97,
        ]);

        // Remove processor
        $this->processorManager->removeProcessor('square');

        //Get Processors
        $this->processorManager->getProcessors();
        
    }
}

Testing

The package includes a comprehensive test suite. Run the tests with:

composer test

Error Handling

The package provides built-in error handling for common scenarios:

try {
    $result = $processor->processPayment($paymentData);
} catch (PaymentProcessingException $e) {
    // Handle processor-specific errors
    Log::error('Payment processing failed: ' . $e->getMessage());
} catch (RoutingException $e) {
    // Handle processor-specific errors
    Log::error('Routing error failed: ' . $e->getMessage());
} catch (ValidationException $e) {
    // Handle validation errors
    $errors = $e->getValidationErrors();
}

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固