Skip to content

Instantly share code, notes, and snippets.

@AhmedHdeawy
Created May 10, 2024 03:57
Show Gist options
  • Save AhmedHdeawy/be8c19a485c2f958e1922f38c06b1f72 to your computer and use it in GitHub Desktop.
Save AhmedHdeawy/be8c19a485c2f958e1922f38c06b1f72 to your computer and use it in GitHub Desktop.
Action for processing a payment
<?php
namespace App\Http\Controllers;
use App\Services\Payment\PaymentDetails;
use App\Actions\PaymentAction;
use App\Models\Order;
class CheckoutController extends Controller
{
public function processPayment(Order $order, PaymentDetails $paymentDetails, PaymentAction $paymentAction)
{
// Delegate payment processing to PaymentAction
$paymentAction($order, $paymentDetails);
// Redirect or return response
}
}
<?php
namespace App\Actions;
use App\Services\Payment\PaymentGateway;
use App\Services\Payment\PaymentDetails;
use App\Models\Order;
class PaymentAction {
public function __construct(protected PaymentGateway $paymentGateway)
{
}
public function __invoke(Order $order, PaymentDetails $paymentDetails)
{
// Process payment using the injected payment gateway
$this->paymentGateway->process($order, $paymentDetails);
// Update order status, send email notifications, etc.
$order->update(['status' => 'paid']);
// Additional logic...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment