Skip to content

Commit

Permalink
Merge pull request #70 from liberu-billing/sweep/Add-payment-gateway-…
Browse files Browse the repository at this point in the history
…support

Add payment gateway support
  • Loading branch information
curtisdelicata authored Jul 27, 2024
2 parents ffac5fb + 6971ed9 commit cfa20e1
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
39 changes: 39 additions & 0 deletions app/Http/Controllers/PaymentController.php
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);
}
}
}
7 changes: 6 additions & 1 deletion app/Models/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ class Payment extends Model

protected $fillable = [
'invoice_id',
'payment_gateway_id',
'payment_date',
'amount',
'payment_method',
'transaction_id',
];


public function invoice()
{
return $this->belongsTo(Invoice::class);
}

public function paymentGateway()
{
return $this->belongsTo(PaymentGateway::class);
}
}
27 changes: 27 additions & 0 deletions app/Models/PaymentGateway.php
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);
}
}
40 changes: 40 additions & 0 deletions app/Services/PaymentGatewayService.php
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 database/migrations/xxxx_xx_xx_create_payment_gateways_table.php
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');
}
};

0 comments on commit cfa20e1

Please sign in to comment.