PHP code example of aqsaahsan301 / laravel-payments
1. Go to this page and download the library: Download aqsaahsan301/laravel-payments 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 Aqsaahsan301\LaravelPayments\Contracts\PaymentGateway;
class CheckoutController
{
public function pay(Request $request, PaymentGateway $gateway)
{
$result = $gateway->charge(
customerEmail: $request->user()->email,
amount: 4900, // smallest currency unit — $49.00
currency: 'usd',
options: [
'success_url' => route('orders.show', $order).'?paid=1',
'cancel_url' => route('orders.show', $order).'?paid=0',
],
);
return redirect()->away($result->url);
}
}
use Aqsaahsan301\LaravelPayments\Contracts\SupportsSubscriptions;
class BillingController
{
public function checkout(Request $request, SupportsSubscriptions $gateway)
{
$organization = $request->user()->organization;
$result = $gateway->checkout(
providerCustomerId: $organization->billing_customer_id, // null if they don't have one yet
customerEmail: $request->user()->email,
plan: 'starter',
options: [
'success_url' => route('billing.show').'?checkout=success',
'cancel_url' => route('billing.show').'?checkout=cancelled',
],
);
// Persist the customer id yourself — the package never does this for you.
$organization->update(['billing_customer_id' => $result->providerCustomerId]);
return redirect()->away($result->url);
}
}
// In a listener, registered however you normally register listeners:
use Aqsaahsan301\LaravelPayments\Events\SubscriptionCancelled;
class RevokeAccessOnCancellation
{
public function handle(SubscriptionCancelled $event): void
{
Organization::where('billing_customer_id', $event->providerCustomerId)
->update(['plan' => null]);
}
}