diff --git a/app/Events/ClientSubscription/ClientSubscriptionWasCreated.php b/app/Events/ClientSubscription/ClientSubscriptionWasCreated.php deleted file mode 100644 index ce08f8793247..000000000000 --- a/app/Events/ClientSubscription/ClientSubscriptionWasCreated.php +++ /dev/null @@ -1,55 +0,0 @@ -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'); - } -} diff --git a/app/Http/Controllers/ClientSubscriptionController.php b/app/Http/Controllers/ClientSubscriptionController.php deleted file mode 100644 index bf0a9b39b167..000000000000 --- a/app/Http/Controllers/ClientSubscriptionController.php +++ /dev/null @@ -1,410 +0,0 @@ -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()); - } -} diff --git a/app/Http/Controllers/OneTimeTokenController.php b/app/Http/Controllers/OneTimeTokenController.php index 34b6ca8fb974..130da74196b1 100644 --- a/app/Http/Controllers/OneTimeTokenController.php +++ b/app/Http/Controllers/OneTimeTokenController.php @@ -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')); diff --git a/app/Http/Controllers/OpenAPI/ClientSubscription.php b/app/Http/Controllers/OpenAPI/ClientSubscription.php deleted file mode 100644 index 9ea5beae440d..000000000000 --- a/app/Http/Controllers/OpenAPI/ClientSubscription.php +++ /dev/null @@ -1,19 +0,0 @@ -user()->can('create', ClientSubscription::class); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } -} diff --git a/app/Http/Requests/ClientSubscription/DestroyClientSubscriptionRequest.php b/app/Http/Requests/ClientSubscription/DestroyClientSubscriptionRequest.php deleted file mode 100644 index 609b88bcf1e6..000000000000 --- a/app/Http/Requests/ClientSubscription/DestroyClientSubscriptionRequest.php +++ /dev/null @@ -1,40 +0,0 @@ -user()->can('edit', $this->client_subscription); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } -} diff --git a/app/Http/Requests/ClientSubscription/EditClientSubscriptionRequest.php b/app/Http/Requests/ClientSubscription/EditClientSubscriptionRequest.php deleted file mode 100644 index bf152757d735..000000000000 --- a/app/Http/Requests/ClientSubscription/EditClientSubscriptionRequest.php +++ /dev/null @@ -1,40 +0,0 @@ -user()->can('edit', $this->client_subscription); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } -} diff --git a/app/Http/Requests/ClientSubscription/ShowClientSubscriptionRequest.php b/app/Http/Requests/ClientSubscription/ShowClientSubscriptionRequest.php deleted file mode 100644 index bf2430e7ba5e..000000000000 --- a/app/Http/Requests/ClientSubscription/ShowClientSubscriptionRequest.php +++ /dev/null @@ -1,40 +0,0 @@ -user()->can('view', $this->client_subscription); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } -} diff --git a/app/Http/Requests/ClientSubscription/StoreClientSubscriptionRequest.php b/app/Http/Requests/ClientSubscription/StoreClientSubscriptionRequest.php deleted file mode 100644 index 5c29cd1a44a5..000000000000 --- a/app/Http/Requests/ClientSubscription/StoreClientSubscriptionRequest.php +++ /dev/null @@ -1,38 +0,0 @@ -user()->can('create', ClientSubscription::class); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return []; - } -} diff --git a/app/Http/Requests/ClientSubscription/UpdateClientSubscriptionRequest.php b/app/Http/Requests/ClientSubscription/UpdateClientSubscriptionRequest.php deleted file mode 100644 index 47f989631d8b..000000000000 --- a/app/Http/Requests/ClientSubscription/UpdateClientSubscriptionRequest.php +++ /dev/null @@ -1,42 +0,0 @@ -user()->can('edit', $this->client_subscription); - } - - /** - * Get the validation rules that apply to the request. - * - * @return array - */ - public function rules() - { - return [ - // - ]; - } -} diff --git a/app/Models/ClientSubscription.php b/app/Models/ClientSubscription.php deleted file mode 100644 index bf38456dbc53..000000000000 --- a/app/Models/ClientSubscription.php +++ /dev/null @@ -1,55 +0,0 @@ - '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); - } -} diff --git a/app/Observers/ClientSubscriptionObserver.php b/app/Observers/ClientSubscriptionObserver.php deleted file mode 100644 index ccdb7fafcbd1..000000000000 --- a/app/Observers/ClientSubscriptionObserver.php +++ /dev/null @@ -1,72 +0,0 @@ -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()); diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index 84900b720d38..917aeeaab2db 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -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; diff --git a/app/PaymentDrivers/StripeConnectPaymentDriver.php b/app/PaymentDrivers/StripeConnectPaymentDriver.php index 6162e1556fbe..8f1a4a9ea770 100644 --- a/app/PaymentDrivers/StripeConnectPaymentDriver.php +++ b/app/PaymentDrivers/StripeConnectPaymentDriver.php @@ -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); - } - } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 2bdc4f5330c9..2cbcee27f033 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -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')); + + $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')); + Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); + + } } public function setPaymentMethod($payment_method_id) diff --git a/app/Policies/ClientSubscriptionPolicy.php b/app/Policies/ClientSubscriptionPolicy.php deleted file mode 100644 index e85df1028e4d..000000000000 --- a/app/Policies/ClientSubscriptionPolicy.php +++ /dev/null @@ -1,31 +0,0 @@ -isAdmin() || $user->hasPermission('create_client_subscription') || $user->hasPermission('create_all'); - } -} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 058ec2835aa0..b25d4d7a0cab 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 5057e976c64b..eb3554a3d79c 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -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; diff --git a/app/Repositories/ClientSubscriptionRepository.php b/app/Repositories/ClientSubscriptionRepository.php deleted file mode 100644 index 4cb88cb2ca53..000000000000 --- a/app/Repositories/ClientSubscriptionRepository.php +++ /dev/null @@ -1,28 +0,0 @@ -fill($data) - ->save(); - - return $client_subscription; - } -} diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index f372bcd7c07c..39b52015de4a 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -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; diff --git a/app/Transformers/ClientSubscriptionTransformer.php b/app/Transformers/ClientSubscriptionTransformer.php deleted file mode 100644 index 5ed3c31d5539..000000000000 --- a/app/Transformers/ClientSubscriptionTransformer.php +++ /dev/null @@ -1,78 +0,0 @@ - $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); - } -} - diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 2541dfd0f90d..77a3e859faaa 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -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; diff --git a/config/ninja.php b/config/ninja.php index 81376365d85b..0cda9c64040d 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -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), ]; 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 110c36582dde..560383e36090 100644 --- a/database/migrations/2021_04_12_095424_stripe_connect_gateway.php +++ b/database/migrations/2021_04_12_095424_stripe_connect_gateway.php @@ -22,7 +22,7 @@ class StripeConnectGateway extends Migration 'provider' => 'StripeConnect', 'sort_order' => 1, 'key' => 'd14dd26a47cecc30fdd65700bfb67b34', - 'fields' => '{"apiKey":"", "publishableKey":""}' + 'fields' => '{"account_id":""}' ]; Gateway::create($gateway);