PHP code example of allanadan05 / laravel-payments

1. Go to this page and download the library: Download allanadan05/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/ */

    

allanadan05 / laravel-payments example snippets


use Payments\Facades\Payment;
use Payments\DataTransferObjects\ChargeRequest;

// Client token for Braintree's Drop-in UI / hosted fields
$clientToken = Payment::gateway()->generateClientToken();

// Charge a payment method nonce collected from the frontend
$result = Payment::gateway()->charge(ChargeRequest::make(
    amount: '49.00',
    paymentMethodNonceOrToken: $request->input('payment_method_nonce'),
    options: ['order_id' => $order->id, 'customer_id' => $customer->braintree_id]
));

if ($result->successful) {
    // $result->transactionId, $result->status, $result->amount
} else {
    // $result->message
}

// Refund / void
Payment::gateway()->refund($result->transactionId);
Payment::gateway()->void($result->transactionId);

// Vault a customer + payment method
$customer = Payment::gateway()->createCustomer(
    ['firstName' => 'Jane', 'lastName' => 'Doe', 'email' => '[email protected]'],
    paymentMethodNonce: $nonce
);

use Payments\Contracts\PaymentGateway;

public function __construct(private PaymentGateway $gateway) {}

use Payments\Contracts\SupportsSubscriptions;

$gateway = Payment::gateway();

if ($gateway instanceof SupportsSubscriptions) {
    $gateway->subscribe(/* ... */);
}

use Payments\DataTransferObjects\SubscriptionRequest;

// Subscriptions — paymentMethodToken is a *stored* token, not a one-time nonce
$result = Payment::gateway()->subscribe(SubscriptionRequest::make(
    planId: 'monthly_plan',
    paymentMethodToken: $customer->braintree_payment_method_token,
));

// Webhooks — in the controller handling your gateway's webhook URL
$event = Payment::gateway()->parseWebhook($request->input('bt_signature'), $request->input('bt_payload'));
match ($event->kind) {
    'subscription_canceled' => /* ... */,
    default => null,
};

// Stored payment methods
$methods = Payment::gateway()->listPaymentMethods($customer->braintree_id);
Payment::gateway()->deletePaymentMethod($token);

// Reporting
$transactions = Payment::gateway()->searchTransactions(['customer_id' => $customer->braintree_id]);

// Customer listing
$customers = Payment::gateway()->listCustomers(['email' => '[email protected]']);

app(\Payments\PaymentManager::class)
    ->extend('stripe', fn (array $config) => new \App\Payments\StripeGateway($config));