geekabel / mobile-money-payment
一个灵活且可扩展的PHP包,用于集成移动货币支付服务,支持Tmoney和Flooz支付提供商。
v1.0.0
2024-09-23 10:11 UTC
Requires
- php: >=8.2
- symfony/http-client: ^7.1
- symfony/uid: ^7.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^10
- rector/rector: ^1.0
This package is auto-updated.
Last update: 2024-09-24 14:23:32 UTC
README
概述
移动货币支付包为将移动货币支付服务集成到PHP应用程序提供了一个灵活且可扩展的解决方案。它目前支持Tmoney和Flooz支付服务,具有易于添加新支付提供商的架构。
功能
- 支持多种支付服务(目前为Tmoney和Flooz)
- 使用策略模式的可扩展架构
- 跨不同服务的标准化支付响应
- 灵活的Flooz计数器管理系统
- 易于与各种PHP应用程序集成,包括Symfony
要求
- PHP 8.2或更高版本
- Symfony HTTP客户端
安装
通过Composer安装此包
composer geekabel/mobile-money-payment
基本用法
设置支付管理器
use MobileMoneyPayment\PaymentManager; use MobileMoneyPayment\Service\TmoneyService; use MobileMoneyPayment\Service\FloozService; use MobileMoneyPayment\Service\DefaultFloozCounterManager; // Create service instances $tmoneyService = new TmoneyService( $httpClient, $logger, 'tmoney_username', 'tmoney_password', 'tmoney_alias', 'https://tmoney-api-url.com' ); $floozService = new FloozService( $httpClient, $logger, new DefaultFloozCounterManager(), 'flooz_username', 'flooz_password', 'flooz_key', 'flooz_merchant_name', 'flooz_partner_msisdn', 'https://flooz-api-url.com' ); // Create and set up the Payment Manager $paymentManager = new PaymentManager(); $paymentManager->addService('tmoney', $tmoneyService); $paymentManager->addService('flooz', $floozService);
进行支付
$response = $paymentManager->pay('tmoney', '1234567890', 100.00, 'REF123', 'Payment for order #123'); if ($response->success) { echo "Payment successful! Transaction ID: " . $response->transactionId; } else { echo "Payment failed: " . $response->message; }
检查支付状态
$status = $paymentManager->checkStatus('flooz', 'REF123'); echo "Payment status: " . $status->status;
扩展包
添加新支付服务
- 创建一个实现
PaymentServiceInterface
的新类
use MobileMoneyPayment\Interface\PaymentServiceInterface; use MobileMoneyPayment\Model\PaymentResponse; class NewPaymentService implements PaymentServiceInterface { public function pay(string $phone, float $amount, string $reference, string $description = ''): PaymentResponse { // Implement payment logic } public function checkStatus(string $reference): PaymentResponse { // Implement status check logic } }
- 将新服务添加到支付管理器中
$newService = new NewPaymentService(/* ... */); $paymentManager->addService('new_service', $newService);
自定义Flooz计数器管理器
- 创建一个实现
FloozCounterManagerInterface
的类
use MobileMoneyPayment\Interface\FloozCounterManagerInterface; class CustomFloozCounterManager implements FloozCounterManagerInterface { public function getAndIncrementCounter(): int { // Implement custom counter logic } }
- 在创建Flooz服务时使用自定义管理器
$customCounterManager = new CustomFloozCounterManager(); $floozService = new FloozService( $httpClient, $logger, $customCounterManager, // ... other parameters );
高级用法
错误处理
此包使用PaymentException
处理特定的支付相关错误。建议捕获这些异常
use MobileMoneyPayment\Exception\PaymentException; try { $response = $paymentManager->pay('tmoney', '1234567890', 100.00, 'REF123'); } catch (PaymentException $e) { echo "Payment error: " . $e->getMessage(); } catch (\Exception $e) { echo "Unexpected error: " . $e->getMessage(); }
日志记录
此包接受PSR-3兼容的记录器。您可以为自定义日志记录行为提供自己的记录器实现
use Monolog\Logger; use Monolog\Handler\StreamHandler; $logger = new Logger('payment'); $logger->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG)); $tmoneyService = new TmoneyService( $httpClient, $logger, // ... other parameters );
Symfony集成
有关如何将此包与Symfony 6.4、7.0、7.1及更高版本集成的详细说明,请参阅我们的Symfony集成指南。
贡献
欢迎贡献!请随时提交拉取请求。
许可证
此包是开源软件,根据MIT许可证许可。