Last active
December 14, 2021 09:35
-
-
Save muzafarali/b16769faf24601d56c1f3558fa79507a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Stripe\Customer; | |
use Stripe\Account; | |
use Stripe\Stripe; | |
use Stripe\AccountLink; | |
use Stripe\StripeClient; | |
use Stripe\OAuth; | |
// This trait will help us to do following tasks | |
// Create new connect account | |
// Delete connect account | |
// Connect connect account with stripe connect through onboard process | |
// Start onboard process through account links | |
// Disconnect the stripe connect account | |
class StripeConnect | |
{ | |
public static function prepare() | |
{ | |
Stripe::setApiKey("use your secret key here"); | |
} | |
public static function getOrCreateAccount($user, $params = [], $config = []) | |
{ | |
self::prepare(); | |
$params = array_merge([ | |
"type" => $config['account_type'] ?? 'standard' | |
], $params); | |
return self::create($user, 'account_id', function () use ($params) { | |
return Account::create($params); | |
}); | |
} | |
public static function getOrCreateCustomer($token, $user, $params = []) | |
{ | |
self::prepare(); | |
$params = array_merge([ | |
"email" => $user->email, | |
'source' => $token, | |
], $params); | |
return self::create($user, 'customer_id', function () use ($params) { | |
return Customer::create($params); | |
}); | |
} | |
public static function deleteAccount( String $account_id) { | |
try { | |
$stripe = new StripeClient( env("STRIPE_SECRET") ); | |
$stripe->accounts->delete($account_id, [] ); | |
return true; | |
} catch (\Throwable $th) { | |
throw $th; | |
return false; | |
} | |
} | |
public static function createAccountLink( $accountId, $config = [] ) { | |
self::prepare(); | |
$account_links = AccountLink::create([ | |
'account' => $accountId, | |
'refresh_url' => $config['refresh_url'] ?? '', | |
'return_url' => $config['return_url'] ?? '', | |
'type' => 'account_onboarding', | |
]); | |
return $account_links; | |
} | |
public static function getAccount( $accountId ) { | |
self::prepare(); | |
return Account::retrieve($accountId); | |
} | |
public static function getCustomer( $customerId ) { | |
self::prepare(); | |
return Customer::retrieve($customerId); | |
} | |
public static function disconnectStripeAccount( $account_id ) { | |
self::prepare(); | |
return OAuth::deauthorize([ | |
'client_id' => 'CLIENT_ID', | |
'stripe_user_id' => $account_id, | |
]); | |
} | |
private static function create($user, $id_key, $callback) { | |
$account = $user->stripeAccount; | |
if (!$account || !$account->$id_key) { | |
$id = call_user_func($callback)->id; | |
if (!$account) { | |
$account = $user->stripeAccount()->create([$id_key => $id]); | |
} else { | |
$account->$id_key = $id; | |
$account->save(); | |
} | |
} | |
return $account; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment