-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from liberu-billing/sweep/Add-payment-gateway-…
…support Add payment gateway support
- Loading branch information
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Payment; | ||
use App\Services\PaymentGatewayService; | ||
use Illuminate\Http\Request; | ||
|
||
class PaymentController extends Controller | ||
{ | ||
protected $paymentGatewayService; | ||
|
||
public function __construct(PaymentGatewayService $paymentGatewayService) | ||
{ | ||
$this->paymentGatewayService = $paymentGatewayService; | ||
} | ||
|
||
public function processPayment(Request $request) | ||
{ | ||
// Validate the request | ||
$validatedData = $request->validate([ | ||
'invoice_id' => 'required|exists:invoices,id', | ||
'payment_gateway_id' => 'required|exists:payment_gateways,id', | ||
'amount' => 'required|numeric|min:0', | ||
'payment_method' => 'required|string', | ||
]); | ||
|
||
// Create a new payment | ||
$payment = Payment::create($validatedData); | ||
|
||
// Process the payment using the appropriate gateway | ||
try { | ||
$result = $this->paymentGatewayService->processPayment($payment); | ||
return response()->json(['message' => 'Payment processed successfully', 'result' => $result]); | ||
} catch (\Exception $e) { | ||
return response()->json(['message' => 'Payment processing failed', 'error' => $e->getMessage()], 400); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class PaymentGateway extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'api_key', | ||
'secret_key', | ||
'is_active', | ||
]; | ||
|
||
protected $casts = [ | ||
'is_active' => 'boolean', | ||
]; | ||
|
||
public function payments() | ||
{ | ||
return $this->hasMany(Payment::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\PaymentGateway; | ||
use App\Models\Payment; | ||
|
||
class PaymentGatewayService | ||
{ | ||
public function processPayment(Payment $payment) | ||
{ | ||
$gateway = $payment->paymentGateway; | ||
|
||
switch ($gateway->name) { | ||
case 'PayPal': | ||
return $this->processPayPalPayment($payment, $gateway); | ||
case 'Stripe': | ||
return $this->processStripePayment($payment, $gateway); | ||
case 'Authorize.net': | ||
return $this->processAuthorizeNetPayment($payment, $gateway); | ||
default: | ||
throw new \Exception('Unsupported payment gateway'); | ||
} | ||
} | ||
|
||
private function processPayPalPayment(Payment $payment, PaymentGateway $gateway) | ||
{ | ||
// Implement PayPal payment processing logic here | ||
} | ||
|
||
private function processStripePayment(Payment $payment, PaymentGateway $gateway) | ||
{ | ||
// Implement Stripe payment processing logic here | ||
} | ||
|
||
private function processAuthorizeNetPayment(Payment $payment, PaymentGateway $gateway) | ||
{ | ||
// Implement Authorize.net payment processing logic here | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
database/migrations/xxxx_xx_xx_create_payment_gateways_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('payment_gateways', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('api_key'); | ||
$table->string('secret_key'); | ||
$table->boolean('is_active')->default(true); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::table('payments', function (Blueprint $table) { | ||
$table->foreignId('payment_gateway_id')->after('invoice_id')->constrained(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table('payments', function (Blueprint $table) { | ||
$table->dropForeign(['payment_gateway_id']); | ||
$table->dropColumn('payment_gateway_id'); | ||
}); | ||
|
||
Schema::dropIfExists('payment_gateways'); | ||
} | ||
}; |