diff --git a/app/Http/Controllers/OneTimeTokenController.php b/app/Http/Controllers/OneTimeTokenController.php index 130da74196b1..fd816b7b92f4 100644 --- a/app/Http/Controllers/OneTimeTokenController.php +++ b/app/Http/Controllers/OneTimeTokenController.php @@ -68,19 +68,18 @@ class OneTimeTokenController extends BaseController */ public function create(OneTimeTokenRequest $request) { - $hash = Str::random(64); $data = [ 'user_id' => auth()->user()->id, - 'company_key'=> auth()->user()->company()->company_key, + 'company_key'=> auth()->user()->company->company_key, 'context' => $request->input('context'), ]; Cache::put( $hash, $data, 3600); return response()->json(['hash' => $hash], 200); - + } public function router(OneTimeRouterRequest $request) diff --git a/app/Http/Controllers/StripeConnectController.php b/app/Http/Controllers/StripeConnectController.php new file mode 100644 index 000000000000..a109cde3fee8 --- /dev/null +++ b/app/Http/Controllers/StripeConnectController.php @@ -0,0 +1,45 @@ +getTokenContent(); + + $data = [ + 'email' => 'user@example.com', + 'country' => 'US', + ]; + + $account = Account::create($data); + + $link = Account::link($account->id); + + // Store account->id into company_gateways. + + return redirect($link['url']); + } +} diff --git a/app/Http/Requests/StripeConnect/InitializeStripeConnectRequest.php b/app/Http/Requests/StripeConnect/InitializeStripeConnectRequest.php new file mode 100644 index 000000000000..0d3a333c80e9 --- /dev/null +++ b/app/Http/Requests/StripeConnect/InitializeStripeConnectRequest.php @@ -0,0 +1,55 @@ +input('token')); + + abort_if(!$data, 404); + + return $data; + } +} diff --git a/app/PaymentDrivers/Stripe/Connect/Account.php b/app/PaymentDrivers/Stripe/Connect/Account.php index a9bbff1bdc09..f039a90aaefd 100644 --- a/app/PaymentDrivers/Stripe/Connect/Account.php +++ b/app/PaymentDrivers/Stripe/Connect/Account.php @@ -12,24 +12,40 @@ namespace App\PaymentDrivers\Stripe\Connect; -use App\Exceptions\PaymentFailed; -use App\Http\Requests\Request; -use App\Jobs\Mail\PaymentFailureMailer; -use App\Jobs\Util\SystemLogger; -use App\Models\ClientGatewayToken; -use App\Models\GatewayType; -use App\Models\Payment; -use App\Models\PaymentType; -use App\Models\SystemLog; -use App\PaymentDrivers\StripePaymentDriver; -use App\Utils\Traits\MakesHash; -use Exception; -use Stripe\Customer; -use Stripe\Exception\CardException; -use Stripe\Exception\InvalidRequestException; - class Account { + /** + * @throws \Stripe\Exception\ApiErrorException + */ + public static function create(array $payload): \Stripe\Account + { + $stripe = new \Stripe\StripeClient( + config('ninja.stripe_private_key') + ); + + return $stripe->accounts->create([ + 'type' => 'standard', + 'country' => $payload['country'], + 'email' => $payload['email'], + ]); + } + + /** + * @throws \Stripe\Exception\ApiErrorException + */ + public static function link(string $account_id): \Stripe\AccountLink + { + $stripe = new \Stripe\StripeClient( + config('ninja.stripe_private_key') + ); + + return $stripe->accountLinks->create([ + 'account' => $account_id, + 'refresh_url' => 'http://localhost:8080/stripe_connect/reauth', + 'return_url' => 'http://localhost:8080/stripe_connect/return', + 'type' => 'account_onboarding', + ]); + } /*** If this is a new account (ie there is no account_id in company_gateways.config, the we need to create an account as below. @@ -148,7 +164,7 @@ class Account // now we start the stripe onboarding flow // https://stripe.com/docs/api/account_links/object -// +// /** * $stripe = new \Stripe\StripeClient( 'sk_test_4eC39HqLyjWDarjtT1zdp7dc' @@ -162,7 +178,7 @@ $stripe->accountLinks->create([ */ /** - * Response = + * Response = * { "object": "account_link", "created": 1618869558, @@ -177,11 +193,11 @@ $stripe->accountLinks->create([ // What next? -// +// // Now we need to create a superclass of the StripePaymentDriver, i believe the only thing we need to change is the way we initialize the gateway.. /** - * + * \Stripe\Stripe::setApiKey("{{PLATFORM_SECRET_KEY}}"); <--- platform secret key = Invoice Ninja secret key \Stripe\Customer::create( ["email" => "person@example.edu"], @@ -191,4 +207,4 @@ $stripe->accountLinks->create([ */ -} \ No newline at end of file +} diff --git a/config/ninja.php b/config/ninja.php index 0cda9c64040d..e7c6c2e9b2b9 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -147,5 +147,5 @@ return [ 'webcron_secret' => env('WEBCRON_SECRET', false), 'disable_auto_update' => env('DISABLE_AUTO_UPDATE', false), 'invoiceninja_hosted_pdf_generation' => env('NINJA_HOSTED_PDF', false), - 'ninja_stripe_key' => env('NINJA_STRIPE_KEY', false), + 'stripe_private_key' => env('STRIPE_PRIVATE_KEY', null), ]; diff --git a/database/migrations/2021_04_12_095424_stripe_connect_gateway.php b/database/migrations/2021_04_12_095424_stripe_connect_gateway.php index f9c4db95d118..0f2a72242151 100644 --- a/database/migrations/2021_04_12_095424_stripe_connect_gateway.php +++ b/database/migrations/2021_04_12_095424_stripe_connect_gateway.php @@ -29,12 +29,10 @@ class StripeConnectGateway extends Migration Gateway::create($gateway); - if(Ninja::isNinja()) - { + if (Ninja::isNinja()) { Gateway::where('id', 20)->update(['visible' => 0]); Gateway::where('id', 56)->update(['visible' => 1]); } - } /** diff --git a/routes/api.php b/routes/api.php index a527ad0a6ae4..7a471a4c3178 100644 --- a/routes/api.php +++ b/routes/api.php @@ -187,6 +187,11 @@ Route::match(['get', 'post'], 'payment_webhook/{company_key}/{company_gateway_id ->name('payment_webhook'); Route::post('api/v1/postmark_webhook', 'PostMarkController@webhook'); + Route::get('token_hash_router', 'OneTimeTokenController@router'); + Route::get('webcron', 'WebCronController@index'); + +Route::get('stripe_connect/{token}', 'StripeConnectController@initialize')->name('stripe_connect.initialization'); + Route::fallback('BaseController@notFound');