1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Stripe Connect

This commit is contained in:
David Bomba 2021-04-20 21:30:52 +10:00
parent 81c90fb1a8
commit e6c083cf29
25 changed files with 41 additions and 1397 deletions

View File

@ -1,55 +0,0 @@
<?php
namespace App\Events\ClientSubscription;
use App\Models\ClientSubscription;
use App\Models\Company;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ClientSubscriptionWasCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* @var ClientSubscription
*/
public $client_subscription;
/**
* @var Company
*/
public $company;
/**
* @var array
*/
public $event_vars;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(ClientSubscription $client_subscription, Company $company, array $event_vars)
{
$this->client_subscription = $client_subscription;
$this->company = $company;
$this->event_vars = $event_vars;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

View File

@ -1,410 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers;
use App\Events\ClientSubscription\ClientSubscriptionWasCreated;
use App\Factory\ClientSubscriptionFactory;
use App\Http\Requests\ClientSubscription\CreateClientSubscriptionRequest;
use App\Http\Requests\ClientSubscription\DestroyClientSubscriptionRequest;
use App\Http\Requests\ClientSubscription\EditClientSubscriptionRequest;
use App\Http\Requests\ClientSubscription\ShowClientSubscriptionRequest;
use App\Http\Requests\ClientSubscription\StoreClientSubscriptionRequest;
use App\Http\Requests\ClientSubscription\UpdateClientSubscriptionRequest;
use App\Models\ClientSubscription;
use App\Repositories\ClientSubscriptionRepository;
use App\Transformers\ClientSubscriptionTransformer;
use App\Utils\Ninja;
class ClientSubscriptionController extends BaseController
{
protected $entity_type = ClientSubscription::class;
protected $entity_transformer = ClientSubscriptionTransformer::class;
protected $client_subscription_repo;
public function __construct(ClientSubscriptionRepository $client_subscription_repo)
{
parent::__construct();
$this->client_subscription_repo = $client_subscription_repo;
}
/**
* Show the list of ClientSubscriptions.
*
* @return Response
*
* @OA\Get(
* path="/api/v1/client_subscriptions",
* operationId="getClientSubscriptions",
* tags={"client_subscriptions"},
* summary="Gets a list of client_subscriptions",
* description="Lists client_subscriptions.",
*
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A list of client_subscriptions",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function index(): \Illuminate\Http\Response
{
$client_subscriptions = ClientSubscription::query()->company();
return $this->listResponse($client_subscriptions);
}
/**
* Show the form for creating a new resource.
*
* @param CreateClientSubscriptionRequest $request The request
*
* @return Response
*
*
* @OA\Get(
* path="/api/v1/client_subscriptions/create",
* operationId="getClientSubscriptionsCreate",
* tags={"client_subscriptions"},
* summary="Gets a new blank client_subscriptions object",
* description="Returns a blank object with default values",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank client_subscriptions object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function create(CreateClientSubscriptionRequest $request): \Illuminate\Http\Response
{
$client_subscription = ClientSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id);
return $this->itemResponse($client_subscription);
}
/**
* Store a newly created resource in storage.
*
* @param StoreClientSubscriptionRequest $request The request
*
* @return Response
*
*
* @OA\Post(
* path="/api/v1/client_subscriptions",
* operationId="storeClientSubscription",
* tags={"client_subscriptions"},
* summary="Adds a client_subscriptions",
* description="Adds an client_subscriptions to the system",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved client_subscriptions object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function store(StoreClientSubscriptionRequest $request): \Illuminate\Http\Response
{
$client_subscription = $this->client_subscription_repo->save($request->all(), ClientSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id));
event(new ClientsubscriptionWasCreated($client_subscription, $client_subscription->company, Ninja::eventVars()));
return $this->itemResponse($client_subscription);
}
/**
* Display the specified resource.
*
* @param ShowClientSubscriptionRequest $request The request
* @param Invoice $client_subscription The invoice
*
* @return Response
*
*
* @OA\Get(
* path="/api/v1/client_subscriptions/{id}",
* operationId="showClientSubscription",
* tags={"client_subscriptions"},
* summary="Shows an client_subscriptions",
* description="Displays an client_subscriptions by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ClientSubscription Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the ClientSubscription object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function show(ShowClientSubscriptionRequest $request, ClientSubscription $client_subscription): \Illuminate\Http\Response
{
return $this->itemResponse($client_subscription);
}
/**
* Show the form for editing the specified resource.
*
* @param EditClientSubscriptionRequest $request The request
* @param Invoice $client_subscription The invoice
*
* @return Response
*
* @OA\Get(
* path="/api/v1/client_subscriptions/{id}/edit",
* operationId="editClientSubscription",
* tags={"client_subscriptions"},
* summary="Shows an client_subscriptions for editting",
* description="Displays an client_subscriptions by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ClientSubscription Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the invoice object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function edit(EditClientSubscriptionRequest $request, ClientSubscription $client_subscription): \Illuminate\Http\Response
{
return $this->itemResponse($client_subscription);
}
/**
* Update the specified resource in storage.
*
* @param UpdateClientSubscriptionRequest $request The request
* @param ClientSubscription $client_subscription The invoice
*
* @return Response
*
*
* @OA\Put(
* path="/api/v1/client_subscriptions/{id}",
* operationId="updateClientSubscription",
* tags={"client_subscriptions"},
* summary="Updates an client_subscriptions",
* description="Handles the updating of an client_subscriptions by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ClientSubscription Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the client_subscriptions object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/ClientSubscription"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function update(UpdateClientSubscriptionRequest $request, ClientSubscription $client_subscription)
{
if ($request->entityIsDeleted($client_subscription)) {
return $request->disallowUpdate();
}
$client_subscription = $this->client_subscription_repo->save($request->all(), $client_subscription);
return $this->itemResponse($client_subscription);
}
/**
* Remove the specified resource from storage.
*
* @param DestroyClientSubscriptionRequest $request
* @param ClientSubscription $invoice
*
* @return Response
*
* @throws \Exception
* @OA\Delete(
* path="/api/v1/client_subscriptions/{id}",
* operationId="deleteClientSubscription",
* tags={"client_subscriptions"},
* summary="Deletes a client_subscriptions",
* description="Handles the deletion of an client_subscriptions by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The ClientSubscription Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function destroy(DestroyClientSubscriptionRequest $request, ClientSubscription $client_subscription): \Illuminate\Http\Response
{
$this->client_subscription_repo->delete($client_subscription);
return $this->itemResponse($client_subscription->fresh());
}
}

View File

@ -13,6 +13,8 @@ namespace App\Http\Controllers;
use App\Http\Requests\OneTimeToken\OneTimeRouterRequest;
use App\Http\Requests\OneTimeToken\OneTimeTokenRequest;
use App\Models\Company;
use App\Models\CompanyUser;
use App\Models\User;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
@ -23,7 +25,6 @@ class OneTimeTokenController extends BaseController
{
private $contexts = [
'stripe_connect_test' => 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_J2FhIhcf9GT5BlWUNeQ1FhnZACaYZrOI&scope=read_write',
'stripe_connect' => 'https://connect.stripe.com/oauth/authorize?response_type=code&client_id=ca_J2Fh2tZfMlaaItUfbUwBBx4JPss8jCz9&scope=read_write'
];
@ -72,11 +73,11 @@ class OneTimeTokenController extends BaseController
$data = [
'user_id' => auth()->user()->id,
'company_key'=> auth()->company()->company_key,
'context' => $requst->input('context'),
'company_key'=> auth()->user()->company()->company_key,
'context' => $request->input('context'),
];
Cache::put( $hash, $data, 3600 );
Cache::put( $hash, $data, 3600);
return response()->json(['hash' => $hash], 200);
@ -88,8 +89,13 @@ class OneTimeTokenController extends BaseController
MultiDB::findAndSetDbByCompanyKey($data['company_key']);
$company = Company::where('company_key', $data['company_key'])->first();
$user = User::findOrFail($data['user_id']);
$company_user = CompanyUser::where('company_id', $company->id)
->where('user_id', $user->id)
->firstOrFail();
Auth::login($user, true);
// Cache::forget($request->input('hash'));

View File

@ -1,19 +0,0 @@
<?php
/**
* @OA\Schema(
* schema="ClientSubscription",
* type="object",
* @OA\Property(property="id", type="string", example="Opnel5aKBz", description="______"),
* @OA\Property(property="subscription_id", type="string", example="Opnel5aKBz", description="______"),
* @OA\Property(property="recurring_invoice_id", type="string", example="Opnel5aKBz", description="______"),
* @OA\Property(property="company_id", type="string", example="Opnel5aKBz", description="______"),
* @OA\Property(property="client_id", type="string", example="Opnel5aKBz", description="______"),
* @OA\Property(property="trial_started", type="string", example="10-10-2021", description="______"),
* @OA\Property(property="trial_ends", type="string", example="12-10-2021", description="______"),
* @OA\Property(property="is_deleted", type="boolean", example="true", description="______"),
* @OA\Property(property="archived_at", type="number", format="integer", example="1434342123", description="Timestamp"),
* @OA\Property(property="created_at", type="number", format="integer", example="134341234234", description="Timestamp"),
* @OA\Property(property="updated_at", type="number", format="integer", example="134341234234", description="Timestamp"),
* )
*/

View File

@ -1,40 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use App\Models\ClientSubscription;
class CreateClientSubscriptionRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return auth()->user()->can('create', ClientSubscription::class);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -1,40 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class DestroyClientSubscriptionRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('edit', $this->client_subscription);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -1,40 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class EditClientSubscriptionRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('edit', $this->client_subscription);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -1,40 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class ShowClientSubscriptionRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return auth()->user()->can('view', $this->client_subscription);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -1,38 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use App\Models\ClientSubscription;
class StoreClientSubscriptionRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('create', ClientSubscription::class);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [];
}
}

View File

@ -1,42 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Requests\ClientSubscription;
use App\Http\Requests\Request;
use App\Utils\Traits\ChecksEntityStatus;
class UpdateClientSubscriptionRequest extends Request
{
use ChecksEntityStatus;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->user()->can('edit', $this->client_subscription);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
}

View File

@ -1,55 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class ClientSubscription extends BaseModel
{
use HasFactory;
protected $fillable = [
// 'subscription_id',
// 'recurring_invoice_id',
// 'client_id',
// 'trial_started',
// 'trial_ends',
// 'is_deleted',
];
protected $casts = [
'is_deleted' => 'boolean',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Company::class);
}
public function recurring_invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(RecurringInvoice::class);
}
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Client::class);
}
public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(BillingSubscription::class);
}
}

View File

@ -1,72 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Observers;
use App\Models\ClientSubscription;
class ClientSubscriptionObserver
{
/**
* Handle the client_subscription "created" event.
*
* @param ClientSubscription $client_subscription
* @return void
*/
public function created(ClientSubscription $client_subscription)
{
//
}
/**
* Handle the client_subscription "updated" event.
*
* @param ClientSubscription $client_subscription
* @return void
*/
public function updated(ClientSubscription $client_subscription)
{
//
}
/**
* Handle the client_subscription "deleted" event.
*
* @param ClientSubscription $client_subscription
* @return void
*/
public function deleted(ClientSubscription $client_subscription)
{
//
}
/**
* Handle the client_subscription "restored" event.
*
* @param ClientSubscription $client_subscription
* @return void
*/
public function restored(ClientSubscription $client_subscription)
{
//
}
/**
* Handle the client_subscription "force deleted" event.
*
* @param ClientSubscription $client_subscription
* @return void
*/
public function forceDeleted(ClientSubscription $client_subscription)
{
//
}
}

View File

@ -80,7 +80,7 @@ class ACH
{
$this->stripe->init();
$bank_account = Customer::retrieveSource($request->customer, $request->source);
$bank_account = Customer::retrieveSource($request->customer, $request->source, $this->stripe->stripe_connect_auth);
try {
$bank_account->verify(['amounts' => request()->transactions]);
@ -110,6 +110,7 @@ class ACH
public function paymentResponse($request)
{
$this->stripe->init();
$source = ClientGatewayToken::query()
@ -141,7 +142,7 @@ class ACH
'currency' => $state['currency'],
'customer' => $state['customer'],
'source' => $state['source'],
]);
], $this->stripe->stripe_connect_auth);
$state = array_merge($state, $request->all());

View File

@ -62,9 +62,9 @@ class Charge
$this->stripe->init();
$local_stripe = new StripeClient(
$this->stripe->company_gateway->getConfigField('apiKey')
);
// $local_stripe = new StripeClient(
// $this->stripe->company_gateway->getConfigField('apiKey')
// );
$response = null;

View File

@ -38,399 +38,15 @@ use Stripe\SetupIntent;
use Stripe\Stripe;
use Stripe\StripeClient;
class StripeConnectPaymentDriver extends BaseDriver
class StripeConnectPaymentDriver extends StripePaymentDriver
{
use MakesHash, Utilities;
public $refundable = true;
public $token_billing = true;
public $can_authorise_credit_card = true;
/** @var \Stripe\StripeClient */
public $stripe;
protected $customer_reference = 'customerReferenceParam';
public $payment_method;
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class,
GatewayType::BANK_TRANSFER => ACH::class,
GatewayType::ALIPAY => Alipay::class,
GatewayType::SOFORT => SOFORT::class,
GatewayType::APPLE_PAY => 1, // TODO
GatewayType::SEPA => 1, // TODO
];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE;
/**
* Initializes the Stripe API.
* @return void
*/
public function init(): void
public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false)
{
$this->stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
parent::__construct($company_gateway, $client, $invitation);
$this->stripe_connect = true;
}
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
return $this;
}
/**
* Returns the gateway types.
*/
public function gatewayTypes(): array
{
$types = [
GatewayType::CREDIT_CARD,
GatewayType::CRYPTO,
// GatewayType::SEPA, // TODO: Missing implementation
// GatewayType::APPLE_PAY, // TODO:: Missing implementation
];
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['AUT', 'BEL', 'DEU', 'ITA', 'NLD', 'ESP'])) {
$types[] = GatewayType::SOFORT;
}
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['USA'])) {
$types[] = GatewayType::BANK_TRANSFER;
}
if ($this->client
&& isset($this->client->country)
&& in_array($this->client->country->iso_3166_3, ['AUS', 'DNK', 'DEU', 'ITA', 'LUX', 'NOR', 'SVN', 'GBR', 'AUT', 'EST', 'GRC', 'JPN', 'MYS', 'PRT', 'ESP', 'USA', 'BEL', 'FIN', 'HKG', 'LVA', 'NLD', 'SGP', 'SWE', 'CAN', 'FRA', 'IRL', 'LTU', 'NZL', 'SVK', 'CHE'])) {
$types[] = GatewayType::ALIPAY;
}
return $types;
}
public function viewForType($gateway_type_id)
{
switch ($gateway_type_id) {
case GatewayType::CREDIT_CARD:
return 'gateways.stripe.credit_card';
break;
case GatewayType::SOFORT:
return 'gateways.stripe.sofort';
break;
case GatewayType::BANK_TRANSFER:
return 'gateways.stripe.ach';
break;
case GatewayType::SEPA:
return 'gateways.stripe.sepa';
break;
case GatewayType::CRYPTO:
case GatewayType::ALIPAY:
case GatewayType::APPLE_PAY:
return 'gateways.stripe.other';
break;
default:
break;
}
}
public function getClientRequiredFields(): array
{
$fields = [
['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'],
];
if ($this->company_gateway->require_client_name) {
$fields[] = ['name' => 'client_name', 'label' => ctrans('texts.client_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_client_phone) {
$fields[] = ['name' => 'client_phone', 'label' => ctrans('texts.client_phone'), 'type' => 'tel', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_name) {
$fields[] = ['name' => 'contact_first_name', 'label' => ctrans('texts.first_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_last_name', 'label' => ctrans('texts.last_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_contact_email) {
$fields[] = ['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required,email:rfc'];
}
if ($this->company_gateway->require_billing_address) {
$fields[] = ['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_address_line_2', 'label' => ctrans('texts.address2'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_shipping_address) {
$fields[] = ['name' => 'client_shipping_address_line_1', 'label' => ctrans('texts.shipping_address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_address_line_2', 'label' => ctrans('texts.shipping_address2'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_city', 'label' => ctrans('texts.shipping_city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_state', 'label' => ctrans('texts.shipping_state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_postal_code', 'label' => ctrans('texts.shipping_postal_code'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_country_id', 'label' => ctrans('texts.shipping_country'), 'type' => 'text', 'validation' => 'required'];
}
return $fields;
}
/**
* Proxy method to pass the data into payment method authorizeView().
*
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function authorizeView(array $data)
{
return $this->payment_method->authorizeView($data);
}
/**
* Processes the gateway response for credit card authorization.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function authorizeResponse($request)
{
return $this->payment_method->authorizeResponse($request);
}
/**
* Process the payment with gateway.
*
* @param array $data
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data);
}
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request);
}
/**
* Creates a new String Payment Intent.
*
* @param array $data The data array to be passed to Stripe
* @return PaymentIntent The Stripe payment intent object
* @throws ApiErrorException
*/
public function createPaymentIntent($data): ?PaymentIntent
{
$this->init();
return PaymentIntent::create($data);
}
/**
* Returns a setup intent that allows the user
* to enter card details without initiating a transaction.
*
* @return SetupIntent
* @throws ApiErrorException
*/
public function getSetupIntent(): SetupIntent
{
$this->init();
return SetupIntent::create();
}
/**
* Returns the Stripe publishable key.
* @return null|string The stripe publishable key
*/
public function getPublishableKey(): ?string
{
return $this->company_gateway->getPublishableKey();
}
/**
* Finds or creates a Stripe Customer object.
*
* @return null|Customer A Stripe customer object
* @throws \Laracasts\Presenter\Exceptions\PresenterException
* @throws ApiErrorException
*/
public function findOrCreateCustomer(): ?Customer
{
$customer = null;
$this->init();
$client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first();
if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) {
$customer = Customer::retrieve($client_gateway_token->gateway_customer_reference);
} else {
$data['name'] = $this->client->present()->name();
$data['phone'] = $this->client->present()->phone();
if (filter_var($this->client->present()->email(), FILTER_VALIDATE_EMAIL)) {
$data['email'] = $this->client->present()->email();
}
$customer = Customer::create($data);
}
if (!$customer) {
throw new Exception('Unable to create gateway customer');
}
return $customer;
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
$this->init();
/** Response from Stripe SDK/API. */
$response = null;
try {
$response = $this->stripe
->refunds
->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision)]);
if ($response->status == $response::STATUS_SUCCEEDED) {
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->client);
return [
'transaction_reference' => $response->charge,
'transaction_response' => json_encode($response),
'success' => $response->status == $response::STATUS_SUCCEEDED ? true : false,
'description' => $response->metadata,
'code' => $response,
];
}
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client);
return [
'transaction_reference' => null,
'transaction_response' => json_encode($response),
'success' => false,
'description' => $response->failure_reason,
'code' => 422,
];
} catch (Exception $e) {
SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client);
nlog($e->getMessage());
return [
'transaction_reference' => null,
'transaction_response' => json_encode($response),
'success' => false,
'description' => $e->getMessage(),
'code' => 422,
];
}
}
public function verificationView(ClientGatewayToken $payment_method)
{
return $this->payment_method->verificationView($payment_method);
}
public function processVerification(Request $request, ClientGatewayToken $payment_method)
{
return $this->payment_method->processVerification($request, $payment_method);
}
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment)
{
if ($request->type == 'source.chargeable') {
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->save();
}
return response([], 200);
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
return (new Charge($this))->tokenBilling($cgt, $payment_hash);
}
/**
* Attach Stripe payment method to Stripe client.
*
* @param string $payment_method
* @param mixed $customer
*
* @return void
*/
public function attach(string $payment_method, $customer): void
{
try {
$stripe_payment_method = $this->getStripePaymentMethod($payment_method);
$stripe_payment_method->attach(['customer' => $customer->id]);
} catch (ApiErrorException | Exception $e) {
$this->processInternallyFailedPayment($this, $e);
}
}
/**
* Detach payment method from the Stripe.
* https://stripe.com/docs/api/payment_methods/detach
*
* @param ClientGatewayToken $token
* @return void
*/
public function detach(ClientGatewayToken $token)
{
$stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
try {
$stripe->paymentMethods->detach($token->token);
} catch (Exception $e) {
SystemLogger::dispatch([
'server_response' => $e->getMessage(), 'data' => request()->all(),
], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client);
}
}
public function getCompanyGatewayId(): int
{
return $this->company_gateway->id;
}
/**
* Retrieve payment method from Stripe.
*
* @param string $source
*
* @return PaymentMethod|void
*/
public function getStripePaymentMethod(string $source)
{
try {
return PaymentMethod::retrieve($source);
} catch (ApiErrorException | Exception $e) {
return $this->processInternallyFailedPayment($this, $e);
}
}
}

View File

@ -55,6 +55,10 @@ class StripePaymentDriver extends BaseDriver
public $payment_method;
public $stripe_connect = false;
public $stripe_connect_auth = [];
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class,
GatewayType::BANK_TRANSFER => ACH::class,
@ -72,11 +76,21 @@ class StripePaymentDriver extends BaseDriver
*/
public function init(): void
{
$this->stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
if($this->stripe_connect)
{
Stripe::setApiKey(config('ninja.ninja_stripe_key'));
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
$this->stripe_connect_auth = ["stripe_account" => $this->company_gateway->getConfigField('account_id')];
}
else
{
$this->stripe = new StripeClient(
$this->company_gateway->getConfigField('apiKey')
);
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
}
}
public function setPaymentMethod($payment_method_id)

View File

@ -1,31 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Policies;
use App\Models\User;
/**
* Class ClientSubscriptionPolicy.
*/
class ClientSubscriptionPolicy extends EntityPolicy
{
/**
* Checks if the user has create permissions.
*
* @param User $user
* @return bool
*/
public function create(User $user) : bool
{
return $user->isAdmin() || $user->hasPermission('create_client_subscription') || $user->hasPermission('create_all');
}
}

View File

@ -14,7 +14,6 @@ namespace App\Providers;
use App\Models\Account;
use App\Models\Subscription;
use App\Models\Client;
use App\Models\ClientSubscription;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\CompanyToken;
@ -30,7 +29,6 @@ use App\Models\User;
use App\Observers\AccountObserver;
use App\Observers\SubscriptionObserver;
use App\Observers\ClientObserver;
use App\Observers\ClientSubscriptionObserver;
use App\Observers\CompanyGatewayObserver;
use App\Observers\CompanyObserver;
use App\Observers\CompanyTokenObserver;
@ -82,7 +80,6 @@ class AppServiceProvider extends ServiceProvider
Account::observe(AccountObserver::class);
Subscription::observe(SubscriptionObserver::class);
Client::observe(ClientObserver::class);
ClientSubscription::observe(ClientSubscriptionObserver::class);
Company::observe(CompanyObserver::class);
CompanyGateway::observe(CompanyGatewayObserver::class);
CompanyToken::observe(CompanyTokenObserver::class);

View File

@ -40,7 +40,6 @@ use App\Models\Webhook;
use App\Policies\ActivityPolicy;
use App\Policies\SubscriptionPolicy;
use App\Policies\ClientPolicy;
use App\Policies\ClientSubscriptionPolicy;
use App\Policies\CompanyGatewayPolicy;
use App\Policies\CompanyPolicy;
use App\Policies\CompanyTokenPolicy;

View File

@ -1,28 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Repositories;
use App\Models\ClientSubscription;
class ClientSubscriptionRepository extends BaseRepository
{
public function save($data, ClientSubscription $client_subscription): ?ClientSubscription
{
$client_subscription
->fill($data)
->save();
return $client_subscription;
}
}

View File

@ -19,7 +19,6 @@ use App\Jobs\Util\SubscriptionWebhookHandler;
use App\Jobs\Util\SystemLogger;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\ClientSubscription;
use App\Models\Invoice;
use App\Models\PaymentHash;
use App\Models\Product;

View File

@ -1,78 +0,0 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Transformers;
use App\Models\BillingSubscription;
use App\Models\Client;
use App\Models\ClientSubscription;
use App\Models\RecurringInvoice;
use App\Utils\Traits\MakesHash;
class ClientSubscriptionTransformer extends EntityTransformer
{
use MakesHash;
/**
* @var array
*/
protected $defaultIncludes = [];
/**
* @var array
*/
protected $availableIncludes = [
'client',
'recurring_invoice',
'subscription',
];
public function transform(ClientSubscription $client_subscription): array
{
return [
'id' => $this->encodePrimaryKey($client_subscription->id),
'subscription_id' => $this->encodePrimaryKey($client_subscription->subscription_id),
'company_id' => $this->encodePrimaryKey($client_subscription->company_id),
'recurring_invoice_id' => $this->encodePrimaryKey($client_subscription->recurring_invoice_id),
'client_id' => $this->encodePrimaryKey($client_subscription->client_id),
'trial_started' => (string)$client_subscription->trial_started ?: '',
'trial_ends' => (string)$client_subscription->trial_ends ?: '',
'is_deleted' => (bool)$client_subscription->is_deleted,
'created_at' => (int)$client_subscription->created_at,
'updated_at' => (int)$client_subscription->updated_at,
'archived_at' => (int)$client_subscription->deleted_at,
];
}
public function includeClient(ClientSubscription $client_subscription): \League\Fractal\Resource\Item
{
$transformer = new ClientTransformer($this->serializer);
return $this->includeItem($client_subscription->client, $transformer, Client::class);
}
public function includeRecurringInvoice(ClientSubscription $client_subscription): \League\Fractal\Resource\Item
{
$transformer = new RecurringInvoiceTransformer($this->serializer);
return $this->includeItem($client_subscription->recurring_invoice, $transformer, RecurringInvoice::class);
}
public function includeSubscription(ClientSubscription $client_subscription): \League\Fractal\Resource\Item
{
$transformer = new BillingSubscriptionTransformer($this->serializer);
return $this->includeItem($client_subscription->subscription, $transformer, BillingSubscription::class);
}
}

View File

@ -15,7 +15,6 @@ use App\Models\Account;
use App\Models\Activity;
use App\Models\Subscription;
use App\Models\Client;
use App\Models\ClientSubscription;
use App\Models\Company;
use App\Models\CompanyGateway;
use App\Models\CompanyLedger;

View File

@ -147,4 +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),
];

View File

@ -22,7 +22,7 @@ class StripeConnectGateway extends Migration
'provider' => 'StripeConnect',
'sort_order' => 1,
'key' => 'd14dd26a47cecc30fdd65700bfb67b34',
'fields' => '{"apiKey":"", "publishableKey":""}'
'fields' => '{"account_id":""}'
];
Gateway::create($gateway);