1. Go to this page and download the library: Download sayed/payment-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Sayed\Payment\Facades\Payment;
use Sayed\Payment\Enums\PaymentMethod;
// Use default provider from config
$payment = Payment::driver();
// Use specific provider with string
$stripePayment = Payment::driver('stripe');
$paypalPayment = Payment::driver('paypal');
$paddlePayment = Payment::driver('paddle');
// Use specific provider with enum (type-safe)
$stripePayment = Payment::driver(PaymentMethod::STRIPE);
$paypalPayment = Payment::driver(PaymentMethod::PAYPAL);
$paddlePayment = Payment::driver(PaymentMethod::PADDLE);
// Dynamic selection with enum
$provider = PaymentMethod::from($request->payment_method);
$payment = Payment::driver($provider);
namespace App\Events;
use Sayed\Payment\Events\InvoiceEvent;
use App\Models\Invoice;
use Illuminate\Support\Facades\Log;
class InvoicePaymentSucceeded extends InvoiceEvent
{
public function getEventName(): string
{
return 'invoice.payment_succeeded';
}
public function handle(): void
{
Log::info('Invoice paid', ['invoice_id' => $this->invoiceId]);
// Update your database
Invoice::where('payment_invoice_id', $this->invoiceId)->update([
'status' => 'paid',
'paid_at' => now(),
]);
// Your business logic here
// - Grant access to product
// - Send confirmation email
// - Trigger fulfillment process
}
}
namespace App\Events;
use Sayed\Payment\Events\CheckoutEvent;
use App\Models\Order;
class CheckoutCompleted extends CheckoutEvent
{
public function getEventName(): string
{
return 'checkout.completed';
}
public function handle(): void
{
// Get order from metadata
$orderId = $this->metadata['order_id'] ?? null;
if ($orderId) {
Order::find($orderId)->update([
'status' => 'paid',
'transaction_id' => $this->transactionId,
'paid_at' => now(),
]);
}
}
}
namespace App\Events;
use Sayed\Payment\Events\SubscriptionEvent;
use App\Models\Subscription;
class SubscriptionCreated extends SubscriptionEvent
{
public function getEventName(): string
{
return 'subscription.created';
}
public function handle(): void
{
Subscription::create([
'payment_subscription_id' => $this->subscriptionId,
'customer_id' => $this->customerId,
'plan_id' => $this->planId,
'status' => $this->status,
'amount' => $this->amount,
'currency' => $this->currency,
]);
}
}
$this->provider; // 'stripe', 'paypal', or 'paddle'
$this->invoiceId; // Provider's invoice ID
$this->status; // Invoice status
$this->amount; // Amount in cents (integer)
$this->currency; // Currency code (e.g., 'usd')
$this->customerId; // Customer ID (if available)
$this->subscriptionId; // Subscription ID (if applicable)
$this->metadata; // Custom metadata array
$this->rawPayload; // Original webhook payload (JSON string)
$this->provider; // Payment provider
$this->transactionId; // Transaction/session ID
$this->status; // Payment status
$this->amount; // Amount in cents (integer)
$this->currency; // Currency code
$this->customerId; // Customer ID (if available)
$this->customerEmail; // Customer email (if available)
$this->metadata; // Custom metadata array
$this->rawPayload; // Original webhook payload (JSON string)
$this->provider; // Payment provider
$this->subscriptionId; // Subscription ID
$this->status; // Subscription status
$this->amount; // Amount in cents (integer, nullable)
$this->currency; // Currency code (nullable)
$this->customerId; // Customer ID (if available)
$this->customerEmail; // Customer email (if available)
$this->planId; // Plan/price ID (if available)
$this->currentPeriodStart; // Period start date (nullable)
$this->currentPeriodEnd; // Period end date (nullable)
$this->metadata; // Custom metadata array
$this->rawPayload; // Original webhook payload (JSON string)
use App\Events\InvoicePaymentSucceeded;
use App\Listeners\SendInvoicePaidNotification;
protected $listen = [
InvoicePaymentSucceeded::class => [
SendInvoicePaidNotification::class,
],
];
namespace App\Listeners;
use App\Events\InvoicePaymentSucceeded;
use Illuminate\Support\Facades\Mail;
class SendInvoicePaidNotification
{
public function handle(InvoicePaymentSucceeded $event)
{
// Send email notification
Mail::to($event->customerEmail)->send(
new InvoicePaidMail($event->invoiceId, $event->amount)
);
}
}
namespace App\Events;
use Sayed\Payment\Events\InvoiceEvent;
use App\Models\Invoice;
use App\Models\User;
use App\Jobs\SendInvoiceReceiptJob;
use App\Jobs\GrantProductAccessJob;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class InvoicePaymentSucceeded extends InvoiceEvent
{
public function getEventName(): string
{
return 'invoice.payment_succeeded';
}
public function handle(): void
{
DB::transaction(function () {
// 1. Update invoice in database
$invoice = Invoice::where('payment_invoice_id', $this->invoiceId)
->lockForUpdate()
->first();
if (!$invoice) {
Log::error('Invoice not found', ['invoice_id' => $this->invoiceId]);
return;
}
$invoice->update([
'status' => 'paid',
'paid_at' => now(),
'payment_provider' => $this->provider,
'payment_data' => $this->metadata,
]);
// 2. Get user
$user = $invoice->user;
// 3. Grant access to product/service
if ($invoice->product_id) {
GrantProductAccessJob::dispatch($user, $invoice->product_id);
}
// 4. Send receipt email
SendInvoiceReceiptJob::dispatch($user, $invoice);
// 5. Log for analytics
Log::info('Invoice payment processed successfully', [
'invoice_id' => $this->invoiceId,
'user_id' => $user->id,
'amount' => $this->amount,
'provider' => $this->provider,
]);
});
}
}