donsoft/smartpay
Composer 安装命令:
composer require donsoft/smartpay
包简介
A Laravel package to intelligently route payment transactions.
关键字:
README 文档
README
Donsoft SmartPay is a Laravel package that intelligently routes payment transactions to the most suitable payment processor based on configurable factors such as transaction cost, reliability, and currency support.
Features
- Multiple Payment Processor Support: Supports different payment processors with configurable parameters.
- Intelligent Routing: Routes transactions to the best processor based on defined priorities (cost, reliability, currency support).
- Customizable: Easily add new processors or modify existing ones.
- Extensible: Allows developers to extend processors and define custom behavior.
Installation
1. Install the package via Composer:
composer require donsoft/smartpay
2. Publish the configuration file:
php artisan vendor:publish --tag=config --provider="Donsoft\\SmartPay\\Providers\\SmartPayServiceProvider"
This will create a config/smartpay.php file where you can configure your payment processors, priorities, and default settings.
3. Add Service Provider (for Laravel versions below 5.5):
If you’re using a Laravel version below 5.5, add the following service provider to your config/app.php:
'providers' => [ Donsoft\\SmartPay\\Providers\\SmartPayServiceProvider::class, ],
Configuration
The configuration file smartpay.php allows you to define supported payment processors, the priority of routing factors, and more. Example configuration:
return [ /* |-------------------------------------------------------------------------- | Priority Weights for Payment Processor Selection |-------------------------------------------------------------------------- */ 'priorities' => [ 'currency_support' => 3, // Most important 'reliability' => 2, // Second most important 'transaction_cost' => 1, // Least important ], /* |-------------------------------------------------------------------------- | Supported Payment Processors |-------------------------------------------------------------------------- */ 'processors' => [ 'processorA' => [ 'class' => \\Donsoft\\SmartPay\\Services\\Processors\\ProcessorA::class, 'cost_per_transaction' => 0.5, 'supported_currencies' => ['USD', 'EUR'], 'min_transaction_amount' => 1, 'reliability' => 0.85, ], 'processorB' => [ 'class' => \\Donsoft\\SmartPay\\Services\\Processors\\ProcessorB::class, 'cost_per_transaction' => 1.0, 'supported_currencies' => ['USD'], 'min_transaction_amount' => 100, 'reliability' => 0.90, ], ], 'default_currency' => 'USD', ];
Usage
This is how to use Donsoft SmartPay in your Laravel project:
1. Inject the Payment Router into a Controller
You can inject the PaymentRouter service into your controller and use it to route transactions to the appropriate processor.
use Donsoft\\SmartPay\\Services\\Routing\\PaymentRouter; use Illuminate\\Http\\Request; class PaymentController extends Controller { protected $paymentRouter; public function __construct(PaymentRouter $paymentRouter) { $this->paymentRouter = $paymentRouter; } public function processPayment(Request $request) { $transaction = (object) [ 'amount' => $request->input('amount'), 'currency' => $request->input('currency', config('smartpay.default_currency')), ]; try { $processor = $this->paymentRouter->route($transaction); $response = $processor->process($transaction); return response()->json($response); } catch (\Exception $e) { return response()->json(['status' => 'error', 'message' => $e->getMessage()], 400); } } }
2. Example Request to Process Payment:
POST /api/process-payment
Content-Type: application/json
{
"amount": 150,
"currency": "USD"
}
This will route the payment to the best processor based on the configuration and transaction details.
Extending the Processor
You can add custom payment processors by extending the base processor class (BaseProcessor.php) and adding it to the configuration.
Step 1: Create Your Custom Processor
namespace App\\Services\\Processors; use Donsoft\\SmartPay\\Services\\Processors\\BaseProcessor; class CustomProcessor extends BaseProcessor { public function __construct() { parent::__construct( 'CustomProcessor', 0.25, // Cost per transaction ['USD', 'GBP'], // Supported currencies 0.95, // Reliability 10 // Minimum transaction amount ); } public function process($transaction) { // Custom processing logic here return $this->successResponse($transaction); } }
Step 2: Add Your Processor to the Configuration
return [ 'processors' => [ 'customProcessor' => [ 'class' => \\App\\Services\\Processors\\CustomProcessor::class, 'cost_per_transaction' => 0.25, 'supported_currencies' => ['USD', 'GBP'], 'min_transaction_amount' => 10, 'reliability' => 0.95, ], ], ];
Now, CustomProcessor will be included in the intelligent routing when a payment is processed.
Customizing the Configuration
You can adjust the priorities and processors directly in the smartpay.php config file:
-
Priorities: Adjust the weight of each factor in the routing decision (currency support, reliability, transaction cost).
-
Processors: Define new or existing payment processors and their configurations (cost per transaction, supported currencies, minimum transaction amounts).
'priorities' => [ 'currency_support' => 4, // Higher priority 'reliability' => 2, 'transaction_cost' => 1, ],
Running Tests
The package includes tests to verify its functionality. To run the tests:
vendor/bin/phpunit
Make sure your test classes are under the tests/ directory.
License
This package is open-source software licensed under the MIT license.
Conclusion
This Donsoft SmartPay package provides a flexible, customizable solution for routing payment transactions in Laravel applications. By allowing easy configuration and extensibility, developers can adapt it to suit their specific business logic and requirements.
If you have any questions or need support, feel free to reach out to the author
donsoft/smartpay 适用场景与选型建议
donsoft/smartpay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「payment」 「payment processing」 「laravel」 「payment gateway」 「laravel package」 「transaction routing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 donsoft/smartpay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 donsoft/smartpay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 donsoft/smartpay 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
paywant api php client
Convert and operate with FIPS codes for states, counties, etc.
Image processing for PHP 5.3
PHP client for the PAYMENNT api
Alfabank REST API integration
This repository contains Saaspose.SDK for PHP source code. This SDK allows you to work with Saaspose REST API in your PHP applications quickly and easily.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-11