A comprehensive Laravel payment package with strategy pattern for multiple payment gateways
fereydooni/money-port is a Laravel package for a comprehensive laravel payment package with strategy pattern for multiple payment gateways.
It currently has 0 GitHub stars and 0 downloads on Packagist (latest version v1.0.0-beta.1).
Install it with composer require fereydooni/money-port.
Discover more Laravel packages by fereydooni
or browse all Laravel packages to compare alternatives.
Last updated
A comprehensive Laravel payment package with strategy pattern for multiple payment gateways, specifically designed for Iranian payment systems.
This package is fully compatible with Laravel Octane. It uses scoped() bindings instead of singleton() to ensure that:
composer require fereydooni/money-port
php artisan vendor:publish --provider="Fereydooni\MoneyPort\MoneyPortServiceProvider"
# Default Gateway
MONEY_PORT_DEFAULT_GATEWAY=zarinpal
# Zarinpal Configuration
ZARINPAL_ENABLED=true
ZARINPAL_MERCHANT_ID=your_merchant_id
ZARINPAL_BASE_URL=https://www.zarinpal.com/pg
ZARINPAL_SANDBOX=false
# Mellat Configuration
MELLAT_ENABLED=false
MELLAT_TERMINAL_ID=your_terminal_id
MELLAT_USERNAME=your_username
MELLAT_PASSWORD=your_password
# Parsian Configuration
PARSIAN_ENABLED=false
PARSIAN_PIN=your_pin
PARSIAN_ORIGINATOR=your_originator
# Saman Configuration
SAMAN_ENABLED=false
SAMAN_TERMINAL_ID=your_terminal_id
SAMAN_SECRET_KEY=your_secret_key
# Pasargad Configuration
PASARGAD_ENABLED=false
PASARGAD_MERCHANT_CODE=your_merchant_code
PASARGAD_TERMINAL_CODE=your_terminal_code
PASARGAD_PRIVATE_KEY=your_private_key
# IDPay Configuration
IDPAY_ENABLED=false
IDPAY_API_KEY=your_api_key
IDPAY_MERCHANT_NAME=your_merchant_name
IDPAY_MERCHANT_PHONE=your_phone
IDPAY_MERCHANT_EMAIL=your_email
# NextPay Configuration
NEXTPAY_ENABLED=false
NEXTPAY_API_KEY=your_api_key
NEXTPAY_CUSTOMER_PHONE=your_phone
# Sepehr Configuration
SEPEHR_ENABLED=false
SEPEHR_TERMINAL_ID=your_terminal_id
SEPEHR_SECRET_KEY=your_secret_key
# Shaparak Configuration
SHAPARAK_ENABLED=false
SHAPARAK_MERCHANT_ID=your_merchant_id
# BehPardakht Configuration
BEHPARDAKHT_ENABLED=false
BEHPARDAKHT_USERNAME=your_username
BEHPARDAKHT_PASSWORD=your_password
BEHPARDAKHT_PAYER_ID=your_payer_id
# Logging Configuration
MONEY_PORT_LOGGING=true
MONEY_PORT_LOG_CHANNEL=payment
MONEY_PORT_LOG_LEVEL=info
# HTTP Configuration
MONEY_PORT_TIMEOUT=30
MONEY_PORT_RETRY_ATTEMPTS=3
MONEY_PORT_RETRY_DELAY=1000
The package now uses a smart gateway selection system:
initialize(), it becomes the current gateway for subsequent operationsuse Fereydooni\MoneyPort\Facades\MoneyPort;
// Initialize payment with default gateway (Zarinpal)
$payment = MoneyPort::initialize([
'amount' => 100000, // Amount in Rials
'description' => 'Payment for order #123',
'callback_url' => 'https://yoursite.com/payment/callback',
'currency' => 'IRR'
]);
// Initialize payment with specific gateway
$payment = MoneyPort::initialize([
'amount' => 100000,
'description' => 'Payment for order #123',
'callback_url' => 'https://yoursite.com/payment/callback',
'currency' => 'IRR'
], 'mellat'); // This will set Mellat as current gateway for subsequent operations
// Verify payment (will use the current gateway set in initialize, or default if none specified)
$verification = MoneyPort::verify([
'token' => 'payment_token_from_callback',
'amount' => 100000,
'transaction_id' => 'order_123'
]);
// Or specify a different gateway for verification
$verification = MoneyPort::verify([
'token' => 'payment_token_from_callback',
'amount' => 100000,
'transaction_id' => 'order_123'
], 'mellat');
// Get available gateways
$gateways = MoneyPort::getAvailableGateways();
use Fereydooni\MoneyPort\Services\PaymentManager;
class PaymentController extends Controller
{
public function __construct(
private PaymentManager $paymentManager
) {}
public function processPayment(Request $request)
{
$payment = $this->paymentManager->initialize([
'amount' => $request->amount,
'description' => $request->description,
'callback_url' => route('payment.callback'),
'currency' => 'IRR'
], 'zarinpal');
return redirect($payment['payment_url']);
}
public function verifyPayment(Request $request)
{
$verification = $this->paymentManager->verify([
'token' => $request->token,
'amount' => $request->amount,
'transaction_id' => $request->transaction_id
], 'zarinpal');
if ($verification['success']) {
// Payment successful
return response()->json(['message' => 'Payment verified']);
}
return response()->json(['error' => 'Payment verification failed'], 400);
}
}
To add a new payment gateway:
<?php
namespace App\Gateways;
use Fereydooni\MoneyPort\Abstracts\AbstractPaymentGateway;
use Fereydooni\MoneyPort\Contracts\PaymentDataInterface;
class CustomGateway extends AbstractPaymentGateway
{
protected function validateConfig(): void
{
// Validate required configuration
}
public function initialize(array $data): array
{
// Initialize payment logic
}
public function verify(array $data): array
{
// Verify payment logic
}
public function refund(array $data): array
{
// Refund logic
}
public function getStatus(string $transactionId): array
{
// Get status logic
}
public function getGatewayName(): string
{
return 'custom';
}
}
// In src/Enums/Gateway.php
case CUSTOM = 'custom';
public function getDisplayName(): string
{
return match($this) {
// ... existing cases
self::CUSTOM => 'Custom Gateway',
};
}
// In src/Services/PaymentManager.php
if (($config['custom']['enabled'] ?? false)) {
$this->registerGateway(Gateway::CUSTOM, new CustomGateway($config['custom'] ?? []));
}
// In config/money-port.php
'custom' => [
'enabled' => env('CUSTOM_ENABLED', false),
// ... other configuration
],
use App\Gateways\CustomGateway;
// In your service provider or controller
MoneyPort::registerGateway('custom', new CustomGateway());
The package configuration file (config/money-port.php) contains:
Each gateway has an enabled flag that controls whether it's registered and available:
# Enable/disable gateways
ZARINPAL_ENABLED=true # Default: true (always enabled)
MELLAT_ENABLED=false # Default: false
PARSIAN_ENABLED=false # Default: false
# ... other gateways default to false
Only enabled gateways will be registered and available for use. This prevents unnecessary gateway instantiation and improves performance.
The package includes comprehensive error handling:
use Fereydooni\MoneyPort\Exceptions\PaymentException;
try {
$payment = MoneyPort::initialize($data);
} catch (PaymentException $e) {
// Handle payment-specific errors
Log::error('Payment failed: ' . $e->getMessage());
} catch (Exception $e) {
// Handle general errors
Log::error('Unexpected error: ' . $e->getMessage());
}
Enable logging to track payment operations:
// Logs are automatically created for all payment operations
// Configure in config/money-port.php
'logging' => [
'enabled' => true,
'channel' => 'payment',
'level' => 'info',
],
Run the package tests:
composer test
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This package is open-sourced software licensed under the MIT license.
If you encounter any issues or have questions: