PHP code example of nguoingulanh / cashier-connect

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

    

nguoingulanh / cashier-connect example snippets


use Nguoingulanh\CashierConnect\Concerns\ConnectBillable;

class Shop extends Model
{
    use ConnectBillable;
}

use Nguoingulanh\CashierConnect\Facades\CashierConnect;

// With the trait                       // Without the trait
$shop->createConnectAccount();          CashierConnect::for($shop)->create();
$shop->hasConnectAccount();             CashierConnect::for($shop)->hasAccount();
$shop->connectAccount;                  CashierConnect::for($shop)->account();   // local model
$shop->asStripeConnectAccount();        CashierConnect::for($shop)->asStripeAccount(); // \Stripe\Account
$shop->updateConnectAccount([...]);
$shop->syncConnectAccount();
$shop->deleteConnectAccount();
$shop->isConnectReady();                // charges and payouts enabled
$shop->connectRequirements();           // currently_due, past_due, disabled_reason...

$shop->createConnectAccount(['type' => 'standard', 'country' => 'FR']);

$shop->createConnectAccount([
    'controller' => ['stripe_dashboard' => ['type' => 'express']],
]);

Route::get('/seller/onboarding', function (Request $request) {
    return redirect($request->user()->connectOnboardingUrl(route('seller.dashboard')));
});

$shop->connectUpdateUrl();          // let the seller update their details
$shop->connectDashboardUrl();       // Express dashboard login link
$shop->connectAccountSession(['payments', 'payouts']); // client_secret for embedded components

// One-off charge: $100, the platform keeps $10
$user->charge(10000, $paymentMethod, CashierConnect::destination($shop)->fee(1000)->toArray());

// Percentage fee needs the amount
$user->charge(10000, $paymentMethod, CashierConnect::destination($shop)->feePercent(10)->toArray(10000));

// Checkout
$user->checkout(['price_123' => 1], CashierConnect::destination($shop)->fee(500)->forCheckout());

// Subscriptions: a percentage of every invoice
$user->newSubscription('default', 'price_monthly')
    ->create($paymentMethod, [], CashierConnect::destination($shop)->feePercent(10)->forSubscription());

// Subscription Checkout
$user->newSubscription('default', 'price_monthly')
    ->checkout(CashierConnect::destination($shop)->feePercent(10)->forSubscriptionCheckout());

CashierConnect::onBehalfOf($shop)->paymentIntents->create([
    'amount' => 10000,
    'currency' => 'usd',
    'application_fee_amount' => 1000,
]);

$transfer = $shop->transferToConnect(5000, 'usd', ['transfer_group' => 'ORDER_1']);
$shop->reverseConnectTransfer($transfer->id, 2000);
$shop->connectTransfers();

$shop->connectBalance();        // \Stripe\Balance of the connected account
$shop->connectPayout(5000);     // pay out to the seller's bank account
$shop->connectPayouts();

// Destination charge: take the money back from the seller and refund your fee
CashierConnect::refund('pi_123', reverseTransfer: true, refundApplicationFee: true);

// Partial refund of a direct charge (made on the connected account)
CashierConnect::refund('pi_123', amount: 500, onAccount: $shop);

CashierConnect::handleWebhookUsing('balance.available', function (array $payload) {
    // ...
});
CashierConnect::handleWebhookUsing('account.updated', MyAccountHandler::class); // implements WebhookHandler

Event::listen(ConnectAccountReady::class, function ($event) {
    $event->account->connectable->notify(new PayoutsEnabled);
});

Route::middleware(['auth', 'connect.ready'])->group(...);          // 403 until onboarding is complete
Route::middleware(['auth', 'connect.ready:onboard'])->group(...);  // redirect to Stripe onboarding instead

$stripe = CashierConnect::fake();

$this->actingAs($seller)->get('/seller/onboarding')->assertRedirect();

$stripe->completeOnboarding($seller->connectAccountId());
$seller->syncConnectAccount();

$this->actingAs($buyer)->post("/orders/{$order->id}/pay");

CashierConnect::assertAccountCreatedFor($seller);
CashierConnect::assertTransferred($seller, 9000);
CashierConnect::assertPayoutCreated($seller, 9000);
CashierConnect::assertRefunded('pi_123');
bash
composer migrate
php artisan cashier-connect:webhook   # prints STRIPE_CONNECT_WEBHOOK_SECRET
bash
php artisan vendor:publish --tag=cashier-connect-config