Change subscriptions to webhooks

This commit is contained in:
David Bomba 2020-07-06 21:22:36 +10:00
parent 1deb503929
commit 4c321d41c3
33 changed files with 281 additions and 363 deletions

View File

@ -307,8 +307,12 @@ class DemoMode extends Command
$invoice = InvoiceFactory::create($client->company->id, $client->user->id);//stub the company and user_id $invoice = InvoiceFactory::create($client->company->id, $client->user->id);//stub the company and user_id
$invoice->client_id = $client->id; $invoice->client_id = $client->id;
// $invoice->date = $faker->date();
$dateable = Carbon::now()->subDays(rand(0, 90)); if((bool)rand(0,1))
$dateable = Carbon::now()->subDays(rand(0, 90));
else
$dateable = Carbon::now()->addDays(rand(0, 90));
$invoice->date = $dateable; $invoice->date = $dateable;
$invoice->line_items = $this->buildLineItems(rand(1, 10)); $invoice->line_items = $this->buildLineItems(rand(1, 10));
@ -411,7 +415,13 @@ class DemoMode extends Command
//$quote = QuoteFactory::create($client->company->id, $client->user->id);//stub the company and user_id //$quote = QuoteFactory::create($client->company->id, $client->user->id);//stub the company and user_id
$quote =factory(\App\Models\Quote::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]); $quote =factory(\App\Models\Quote::class)->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]);
$quote->date = $faker->date();
if((bool)rand(0,1))
$dateable = Carbon::now()->subDays(rand(0, 90));
else
$dateable = Carbon::now()->addDays(rand(0, 90));
$quote->date = $dateable;
$quote->client_id = $client->id; $quote->client_id = $client->id;
$quote->setRelation('client', $client); $quote->setRelation('client', $client);

View File

@ -11,19 +11,19 @@
namespace App\Factory; namespace App\Factory;
use App\Models\Subscription; use App\Models\Webhook;
class SubscriptionFactory class WebhookFactory
{ {
public static function create(int $company_id, int $user_id) :Subscription public static function create(int $company_id, int $user_id) :Webhook
{ {
$subscription = new Subscription; $webhook = new Webhook;
$subscription->company_id = $company_id; $webhook->company_id = $company_id;
$subscription->user_id = $user_id; $webhook->user_id = $user_id;
$subscription->target_url = ''; $webhook->target_url = '';
$subscription->event_id = 1; $webhook->event_id = 1;
$subscription->format = 'JSON'; $webhook->format = 'JSON';
return $subscription; return $webhook;
} }
} }

View File

@ -11,7 +11,7 @@
namespace App\Filters; namespace App\Filters;
use App\Models\Subscription; use App\Models\Webhook;
use App\Models\User; use App\Models\User;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
@ -20,7 +20,7 @@ use Illuminate\Support\Facades\Gate;
/** /**
* TokenFilters * TokenFilters
*/ */
class SubscriptionFilters extends QueryFilters class WebhookFilters extends QueryFilters
{ {
/** /**
@ -38,7 +38,7 @@ class SubscriptionFilters extends QueryFilters
} }
return $this->builder->where(function ($query) use ($filter) { return $this->builder->where(function ($query) use ($filter) {
$query->where('subscriptions.target_url', 'like', '%'.$filter.'%'); $query->where('webhooks.target_url', 'like', '%'.$filter.'%');
}); });
} }
@ -55,7 +55,7 @@ class SubscriptionFilters extends QueryFilters
return $this->builder; return $this->builder;
} }
$table = 'subscriptions'; $table = 'webhooks';
$filters = explode(',', $filter); $filters = explode(',', $filter);
return $this->builder->where(function ($query) use ($filters, $table) { return $this->builder->where(function ($query) use ($filters, $table) {
@ -102,19 +102,19 @@ class SubscriptionFilters extends QueryFilters
*/ */
public function baseQuery(int $company_id, User $user) : Builder public function baseQuery(int $company_id, User $user) : Builder
{ {
$query = DB::table('subscriptions') $query = DB::table('webhooks')
->join('companies', 'companies.id', '=', 'subscriptions.company_id') ->join('companies', 'companies.id', '=', 'webhooks.company_id')
->where('subscriptions.company_id', '=', $company_id) ->where('webhooks.company_id', '=', $company_id)
//->whereRaw('(designs.name != "" or contacts.first_name != "" or contacts.last_name != "" or contacts.email != "")') // filter out buy now invoices //->whereRaw('(designs.name != "" or contacts.first_name != "" or contacts.last_name != "" or contacts.email != "")') // filter out buy now invoices
->select( ->select(
'subscriptions.id', 'webhooks.id',
'subscriptions.target_url', 'webhooks.target_url',
'subscriptions.event_id', 'webhooks.event_id',
'subscriptions.created_at', 'webhooks.created_at',
'subscriptions.created_at as token_created_at', 'webhooks.created_at as token_created_at',
'subscriptions.deleted_at', 'webhooks.deleted_at',
'subscriptions.format', 'webhooks.format',
'subscriptions.user_id', 'webhooks.user_id',
); );
@ -122,8 +122,8 @@ class SubscriptionFilters extends QueryFilters
* If the user does not have permissions to view all invoices * If the user does not have permissions to view all invoices
* limit the user to only the invoices they have created * limit the user to only the invoices they have created
*/ */
if (Gate::denies('view-list', Subscription::class)) { if (Gate::denies('view-list', Webhook::class)) {
$query->where('subscriptions.user_id', '=', $user->id); $query->where('webhooks.user_id', '=', $user->id);
} }

View File

@ -12,27 +12,27 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Factory\SubscriptionFactory; use App\Factory\WebhookFactory;
use App\Filters\SubscriptionFilters; use App\Filters\WebhookFilters;
use App\Http\Requests\Subscription\CreateSubscriptionRequest; use App\Http\Requests\Webhook\CreateWebhookRequest;
use App\Http\Requests\Subscription\DestroySubscriptionRequest; use App\Http\Requests\Webhook\DestroyWebhookRequest;
use App\Http\Requests\Subscription\EditSubscriptionRequest; use App\Http\Requests\Webhook\EditWebhookRequest;
use App\Http\Requests\Subscription\ShowSubscriptionRequest; use App\Http\Requests\Webhook\ShowWebhookRequest;
use App\Http\Requests\Subscription\StoreSubscriptionRequest; use App\Http\Requests\Webhook\StoreWebhookRequest;
use App\Http\Requests\Subscription\UpdateSubscriptionRequest; use App\Http\Requests\Webhook\UpdateWebhookRequest;
use App\Models\Subscription; use App\Models\Webhook;
use App\Repositories\BaseRepository; use App\Repositories\BaseRepository;
use App\Transformers\SubscriptionTransformer; use App\Transformers\WebhookTransformer;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class SubscriptionController extends BaseController class WebhookController extends BaseController
{ {
use MakesHash; use MakesHash;
protected $entity_type = Subscription::class; protected $entity_type = Webhook::class;
protected $entity_transformer = SubscriptionTransformer::class; protected $entity_transformer = WebhookTransformer::class;
public $base_repo; public $base_repo;
@ -45,13 +45,13 @@ class SubscriptionController extends BaseController
/** /**
* @OA\Get( * @OA\Get(
* path="/api/v1/subscriptions", * path="/api/v1/webhooks",
* operationId="getSubscriptions", * operationId="getWebhooks",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Gets a list of subscriptions", * summary="Gets a list of Webhooks",
* description="Lists subscriptions, search and filters allow fine grained lists to be generated. * description="Lists Webhooks, search and filters allow fine grained lists to be generated.
* *
* Query parameters can be added to performed more fine grained filtering of the subscriptions, these are handled by the SubscriptionFilters class which defines the methods available", * Query parameters can be added to performed more fine grained filtering of the Webhooks, these are handled by the WebhookFilters class which defines the methods available",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
@ -59,11 +59,11 @@ class SubscriptionController extends BaseController
* @OA\Parameter(ref="#/components/parameters/index"), * @OA\Parameter(ref="#/components/parameters/index"),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="A list of subscriptions", * description="A list of Webhooks",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -79,11 +79,11 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function index(SubscriptionFilters $filters) public function index(WebhookFilters $filters)
{ {
$subscriptions = Subscription::filter($filters); $webhooks = Webhook::filter($filters);
return $this->listResponse($subscriptions); return $this->listResponse($webhooks);
} }
/** /**
@ -94,11 +94,11 @@ class SubscriptionController extends BaseController
* *
* *
* @OA\Get( * @OA\Get(
* path="/api/v1/subscriptions/{id}", * path="/api/v1/webhooks/{id}",
* operationId="showSubscription", * operationId="showWebhook",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Shows a subscription", * summary="Shows a Webhook",
* description="Displays a subscription by id", * description="Displays a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
@ -106,7 +106,7 @@ class SubscriptionController extends BaseController
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="id",
* in="path", * in="path",
* description="The Subscription Hashed ID", * description="The Webhook Hashed ID",
* example="D2J234DFA", * example="D2J234DFA",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
@ -116,11 +116,11 @@ class SubscriptionController extends BaseController
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Returns the subscription object", * description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -136,9 +136,9 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function show(ShowSubscriptionRequest $request, Subscription $subscription) public function show(ShowWebhookRequest $request, Webhook $webhook)
{ {
return $this->itemResponse($subscription); return $this->itemResponse($webhook);
} }
/** /**
@ -149,11 +149,11 @@ class SubscriptionController extends BaseController
* *
* *
* @OA\Get( * @OA\Get(
* path="/api/v1/subscriptions/{id}/edit", * path="/api/v1/webhooks/{id}/edit",
* operationId="editSubscription", * operationId="editWebhook",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Shows a subscription for editting", * summary="Shows a Webhook for editting",
* description="Displays a subscription by id", * description="Displays a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
@ -161,7 +161,7 @@ class SubscriptionController extends BaseController
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="id",
* in="path", * in="path",
* description="The Subscription Hashed ID", * description="The Webhook Hashed ID",
* example="D2J234DFA", * example="D2J234DFA",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
@ -171,11 +171,11 @@ class SubscriptionController extends BaseController
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Returns the subscription object", * description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -191,26 +191,26 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function edit(EditSubscriptionRequest $request, Subscription $subscription) public function edit(EditWebhookRequest $request, Webhook $webhook)
{ {
return $this->itemResponse($subscription); return $this->itemResponse($webhook);
} }
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param App\Models\Subscription $subscription * @param App\Models\Webhook $Webhook
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
* *
* *
* *
* @OA\Put( * @OA\Put(
* path="/api/v1/subscriptions/{id}", * path="/api/v1/webhooks/{id}",
* operationId="updateSubscription", * operationId="updateWebhook",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Updates a subscription", * summary="Updates a Webhook",
* description="Handles the updating of a subscription by id", * description="Handles the updating of a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
@ -218,7 +218,7 @@ class SubscriptionController extends BaseController
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="id",
* in="path", * in="path",
* description="The Subscription Hashed ID", * description="The Webhook Hashed ID",
* example="D2J234DFA", * example="D2J234DFA",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
@ -228,11 +228,11 @@ class SubscriptionController extends BaseController
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Returns the subscription object", * description="Returns the Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -248,16 +248,16 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function update(UpdateSubscriptionRequest $request, Subscription $subscription) public function update(UpdateWebhookRequest $request, Webhook $webhook)
{ {
if ($request->entityIsDeleted($subscription)) { if ($request->entityIsDeleted($webhook)) {
return $request->disallowUpdate(); return $request->disallowUpdate();
} }
$subscription->fill($request->all()); $webhook->fill($request->all());
$subscription->save(); $webhook->save();
return $this->itemResponse($subscription); return $this->itemResponse($webhook);
} }
/** /**
@ -268,10 +268,10 @@ class SubscriptionController extends BaseController
* *
* *
* @OA\Get( * @OA\Get(
* path="/api/v1/subscriptions/create", * path="/api/v1/webhooks/create",
* operationId="getSubscriptionsCreate", * operationId="getWebhooksCreate",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Gets a new blank subscription object", * summary="Gets a new blank Webhook object",
* description="Returns a blank object with default values", * description="Returns a blank object with default values",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
@ -279,11 +279,11 @@ class SubscriptionController extends BaseController
* @OA\Parameter(ref="#/components/parameters/include"), * @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="A blank subscription object", * description="A blank Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -299,13 +299,13 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function create(CreateSubscriptionRequest $request) public function create(CreateWebhookRequest $request)
{ {
$subscription = SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); $webhook = WebhookFactory::create(auth()->user()->company()->id, auth()->user()->id);
$subscription->fill($request->all()); $webhook->fill($request->all());
$subscription->save(); $webhook->save();
return $this->itemResponse($subscription); return $this->itemResponse($webhook);
} }
/** /**
@ -317,22 +317,22 @@ class SubscriptionController extends BaseController
* *
* *
* @OA\Post( * @OA\Post(
* path="/api/v1/subscriptions", * path="/api/v1/webhooks",
* operationId="storeSubscription", * operationId="storeWebhook",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Adds a subscription", * summary="Adds a Webhook",
* description="Adds an subscription to a company", * description="Adds an Webhook to a company",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"), * @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="Returns the saved subscription object", * description="Returns the saved Webhook object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -348,13 +348,27 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function store(StoreSubscriptionRequest $request) public function store(StoreWebhookRequest $request)
{ {
$subscription = SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); $event_id = $request->input('event_id');
$subscription->fill($request->all()); $target_url = $request->input('target_url');
$subscription->save();
return $this->itemResponse($subscription); if (!in_array($event_id, Webhook::$valid_events)) {
return response()->json("Invalid event", 400);
}
$webhook = new Webhook;
$webhook->company_id = auth()->user()->company()->id;
$webhook->user_id = auth()->user()->id;
$webhook->event_id = $event_id;
$webhook->target_url = $target_url;
$webhook->save();
if (!$webhook->id) {
return response()->json('Failed to create Webhook', 400);
}
return $this->itemResponse($webhook);
} }
/** /**
@ -365,11 +379,11 @@ class SubscriptionController extends BaseController
* *
* *
* @OA\Delete( * @OA\Delete(
* path="/api/v1/subscriptions/{id}", * path="/api/v1/Webhooks/{id}",
* operationId="deleteSubscription", * operationId="deleteWebhook",
* tags={"subscriptions"}, * tags={"Webhooks"},
* summary="Deletes a subscription", * summary="Deletes a Webhook",
* description="Handles the deletion of a subscription by id", * description="Handles the deletion of a Webhook by id",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
@ -377,7 +391,7 @@ class SubscriptionController extends BaseController
* @OA\Parameter( * @OA\Parameter(
* name="id", * name="id",
* in="path", * in="path",
* description="The Subscription Hashed ID", * description="The Webhook Hashed ID",
* example="D2J234DFA", * example="D2J234DFA",
* required=true, * required=true,
* @OA\Schema( * @OA\Schema(
@ -406,26 +420,26 @@ class SubscriptionController extends BaseController
* ) * )
* *
*/ */
public function destroy(DestroySubscriptionRequest $request, Subscription $subscription) public function destroy(DestroyWebhookRequest $request, Webhook $webhook)
{ {
//may not need these destroy routes as we are using actions to 'archive/delete' //may not need these destroy routes as we are using actions to 'archive/delete'
$subscription->delete(); $webhook->delete();
return $this->itemResponse($subscription); return $this->itemResponse($webhook);
} }
/** /**
* Perform bulk actions on the list view * Perform bulk actions on the list view
* *
* @param BulkSubscriptionRequest $request * @param BulkWebhookRequest $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
* *
* *
* @OA\Post( * @OA\Post(
* path="/api/v1/subscriptions/bulk", * path="/api/v1/webhooks/bulk",
* operationId="bulkSubscriptions", * operationId="bulkWebhooks",
* tags={"subscriptions"}, * tags={"webhooks"},
* summary="Performs bulk actions on an array of subscriptions", * summary="Performs bulk actions on an array of Webhooks",
* description="", * description="",
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
@ -448,11 +462,11 @@ class SubscriptionController extends BaseController
* ), * ),
* @OA\Response( * @OA\Response(
* response=200, * response=200,
* description="The Subscription User response", * description="The Webhook User response",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @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-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
* @OA\JsonContent(ref="#/components/schemas/Subscription"), * @OA\JsonContent(ref="#/components/schemas/Webhook"),
* ), * ),
* @OA\Response( * @OA\Response(
* response=422, * response=422,
@ -471,129 +485,18 @@ class SubscriptionController extends BaseController
$action = request()->input('action'); $action = request()->input('action');
$ids = request()->input('ids'); $ids = request()->input('ids');
$subscriptions = Subscription::withTrashed()->find($this->transformKeys($ids));
$subscriptions->each(function ($subscription, $key) use ($action) { $webhooks = Webhook::withTrashed()->find($this->transformKeys($ids));
if (auth()->user()->can('edit', $subscription)) {
$this->base_repo->{$action}($subscription);
$webhooks->each(function ($webhook, $key) use ($action) {
if (auth()->user()->can('edit', $webhook)) {
$this->base_repo->{$action}($webhook);
} }
}); });
return $this->listResponse(Subscription::withTrashed()->whereIn('id', $this->transformKeys($ids))); return $this->listResponse(Webhook::withTrashed()->whereIn('id', $this->transformKeys($ids)));
} }
/**
* Store a newly created resource in storage.
*
* @OA\Post(
* path="/api/v1/hooks",
* operationId="storeHook",
* tags={"hooks"},
* summary="Adds a hook",
* description="Adds a hooks to a company",
* @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 hooks 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/Subscription"),
* ),
* @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 subscribe(StoreSubscriptionRequest $request)
{
$event_id = $request->input('event_id');
$target_url = $request->input('target_url');
if (!in_array($event_id, Subscription::$valid_events)) {
return response()->json("Invalid event", 400);
}
$subscription = new Subscription;
$subscription->company_id = auth()->user()->company()->id;
$subscription->user_id = auth()->user()->id;
$subscription->event_id = $event_id;
$subscription->target_url = $target_url;
$subscription->save();
if (!$subscription->id) {
return response()->json('Failed to create subscription', 400);
}
return $this->itemResponse($subscription);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*
*
* @OA\Delete(
* path="/api/v1/hooks/{subscription_id}",
* operationId="deleteHook",
* tags={"hooks"},
* summary="Deletes a hook",
* description="Handles the deletion of a hook 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="subscription_id",
* in="path",
* description="The Subscription 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 unsubscribe(DestroySubscriptionRequest $request, Subscription $subscription)
{
$subscription->delete();
return $this->itemResponse($subscription);
}
} }

View File

@ -1,12 +1,12 @@
<?php <?php
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Utils\Traits\BulkOptions; use App\Utils\Traits\BulkOptions;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use App\Models\Vendor; use App\Models\Vendor;
class BulkSubscriptionRequest extends FormRequest class BulkWebhookRequest extends FormRequest
{ {
use BulkOptions; use BulkOptions;

View File

@ -9,12 +9,12 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Models\Vendor; use App\Models\Vendor;
class ShowSubscriptionRequest extends Request class CreateWebhookRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.

View File

@ -9,11 +9,11 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
class DestroySubscriptionRequest extends Request class DestroyWebhookRequest extends Request
{ {
/** /**

View File

@ -9,12 +9,12 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Models\Vendor; use App\Models\Vendor;
class EditSubscriptionRequest extends Request class EditWebhookRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.

View File

@ -9,12 +9,12 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Models\Vendor; use App\Models\Vendor;
class CreateSubscriptionRequest extends Request class ShowWebhookRequest extends Request
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.

View File

@ -9,11 +9,11 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
class StoreSubscriptionRequest extends Request class StoreWebhookRequest extends Request
{ {
/** /**

View File

@ -9,14 +9,14 @@
* @license https://opensource.org/licenses/AAL * @license https://opensource.org/licenses/AAL
*/ */
namespace App\Http\Requests\Subscription; namespace App\Http\Requests\Webhook;
use App\Http\Requests\Request; use App\Http\Requests\Request;
use App\Utils\Traits\ChecksEntityStatus; use App\Utils\Traits\ChecksEntityStatus;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
class UpdateSubscriptionRequest extends Request class UpdateWebhookRequest extends Request
{ {
use MakesHash; use MakesHash;
use ChecksEntityStatus; use ChecksEntityStatus;

View File

@ -12,7 +12,7 @@ use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager; use League\Fractal\Manager;
use League\Fractal\Resource\Item; use League\Fractal\Resource\Item;
class SubscriptionHandler implements ShouldQueue class WebhookHandler implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@ -45,7 +45,7 @@ class SubscriptionHandler implements ShouldQueue
//info("i got past the check"); //info("i got past the check");
$subscriptions = Subscription::where('company_id', $this->entity->company_id) $subscriptions = Webhook::where('company_id', $this->entity->company_id)
->where('event_id', $this->event_id) ->where('event_id', $this->event_id)
->get(); ->get();

View File

@ -35,7 +35,7 @@ class Payment extends BaseModel
use Refundable; use Refundable;
const STATUS_PENDING = 1; const STATUS_PENDING = 1;
const STATUS_VOIDED = 2; const STATUS_CANCELLED = 2;
const STATUS_FAILED = 3; const STATUS_FAILED = 3;
const STATUS_COMPLETED = 4; const STATUS_COMPLETED = 4;
const STATUS_PARTIALLY_REFUNDED = 5; const STATUS_PARTIALLY_REFUNDED = 5;
@ -167,7 +167,7 @@ class Payment extends BaseModel
case self::STATUS_PENDING: case self::STATUS_PENDING:
return '<h6><span class="badge badge-secondary">'.ctrans('texts.payment_status_1').'</span></h6>'; return '<h6><span class="badge badge-secondary">'.ctrans('texts.payment_status_1').'</span></h6>';
break; break;
case self::STATUS_VOIDED: case self::STATUS_CANCELLED:
return '<h6><span class="badge badge-warning">'.ctrans('texts.payment_status_2').'</span></h6>'; return '<h6><span class="badge badge-warning">'.ctrans('texts.payment_status_2').'</span></h6>';
break; break;
case self::STATUS_FAILED: case self::STATUS_FAILED:
@ -248,7 +248,7 @@ class Payment extends BaseModel
public function isVoided() public function isVoided()
{ {
return $this->status_id == self::STATUS_VOIDED; return $this->status_id == self::STATUS_CANCELLED;
} }
public function isPartiallyRefunded() public function isPartiallyRefunded()
@ -274,7 +274,7 @@ class Payment extends BaseModel
} }
$this->refunded = $this->amount; $this->refunded = $this->amount;
$this->status_id = self::STATUS_VOIDED; $this->status_id = self::STATUS_CANCELLED;
$this->save(); $this->save();
event(new PaymentWasVoided($this)); event(new PaymentWasVoided($this));

View File

@ -14,7 +14,7 @@ namespace App\Models;
use App\Models\Filterable; use App\Models\Filterable;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
class Subscription extends BaseModel class Webhook extends BaseModel
{ {
use SoftDeletes; use SoftDeletes;
use Filterable; use Filterable;

View File

@ -12,9 +12,10 @@
namespace App\Observers; namespace App\Observers;
use App\Events\Client\ClientWasCreated; use App\Events\Client\ClientWasCreated;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Client; use App\Models\Client;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Webhook;
class ClientObserver class ClientObserver
{ {
@ -28,7 +29,7 @@ class ClientObserver
{ {
event(new ClientWasCreated($client, $client->company)); event(new ClientWasCreated($client, $client->company));
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_CLIENT, $client); WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client);
} }
/** /**
@ -39,7 +40,7 @@ class ClientObserver
*/ */
public function updated(Client $client) public function updated(Client $client)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_CLIENT, $client); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CLIENT, $client);
} }
/** /**
@ -50,7 +51,7 @@ class ClientObserver
*/ */
public function deleted(Client $client) public function deleted(Client $client)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_CLIENT, $client); WebhookHandler::dispatch(Webhook::EVENT_DELETE_CLIENT, $client);
} }

View File

@ -11,9 +11,10 @@
namespace App\Observers; namespace App\Observers;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Expense; use App\Models\Expense;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Webhook;
class ExpenseObserver class ExpenseObserver
{ {
@ -25,7 +26,7 @@ class ExpenseObserver
*/ */
public function created(Expense $expense) public function created(Expense $expense)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_EXPENSE, $expense); WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense);
} }
/** /**
@ -36,7 +37,7 @@ class ExpenseObserver
*/ */
public function updated(Expense $expense) public function updated(Expense $expense)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_EXPENSE, $expense); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense);
} }
/** /**
@ -47,7 +48,7 @@ class ExpenseObserver
*/ */
public function deleted(Expense $expense) public function deleted(Expense $expense)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_EXPENSE, $expense); WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense);
} }
/** /**

View File

@ -11,9 +11,10 @@
namespace App\Observers; namespace App\Observers;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Webhook;
class InvoiceObserver class InvoiceObserver
{ {
@ -25,7 +26,7 @@ class InvoiceObserver
*/ */
public function created(Invoice $invoice) public function created(Invoice $invoice)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_INVOICE, $invoice); WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice);
} }
/** /**
@ -36,7 +37,7 @@ class InvoiceObserver
*/ */
public function updated(Invoice $invoice) public function updated(Invoice $invoice)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_INVOICE, $invoice); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice);
} }
/** /**
@ -47,7 +48,7 @@ class InvoiceObserver
*/ */
public function deleted(Invoice $invoice) public function deleted(Invoice $invoice)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_INVOICE, $invoice); WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice);
} }
/** /**

View File

@ -12,9 +12,10 @@
namespace App\Observers; namespace App\Observers;
use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Payment; use App\Models\Payment;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Webhook;
class PaymentObserver class PaymentObserver
{ {
@ -26,7 +27,7 @@ class PaymentObserver
*/ */
public function created(Payment $payment) public function created(Payment $payment)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_PAYMENT, $payment); WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment);
} }
/** /**
@ -47,7 +48,7 @@ class PaymentObserver
*/ */
public function deleted(Payment $payment) public function deleted(Payment $payment)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_PAYMENT, $payment); WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment);
} }
/** /**

View File

@ -11,9 +11,10 @@
namespace App\Observers; namespace App\Observers;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Quote; use App\Models\Quote;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Webhook;
class QuoteObserver class QuoteObserver
{ {
@ -25,7 +26,7 @@ class QuoteObserver
*/ */
public function created(Quote $quote) public function created(Quote $quote)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_QUOTE, $quote); WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote);
} }
/** /**
@ -36,7 +37,7 @@ class QuoteObserver
*/ */
public function updated(Quote $quote) public function updated(Quote $quote)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_QUOTE, $quote); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote);
} }
/** /**
@ -47,7 +48,7 @@ class QuoteObserver
*/ */
public function deleted(Quote $quote) public function deleted(Quote $quote)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_QUOTE, $quote); WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote);
} }
/** /**

View File

@ -11,9 +11,10 @@
namespace App\Observers; namespace App\Observers;
use App\Jobs\Util\SubscriptionHandler; use App\Jobs\Util\WebhookHandler;
use App\Models\Subscription; use App\Models\Subscription;
use App\Models\Task; use App\Models\Task;
use App\Models\Webhook;
class TaskObserver class TaskObserver
{ {
@ -25,7 +26,7 @@ class TaskObserver
*/ */
public function created(Task $task) public function created(Task $task)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_CREATE_TASK, $task); WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task);
} }
/** /**
@ -36,7 +37,7 @@ class TaskObserver
*/ */
public function updated(Task $task) public function updated(Task $task)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_UPDATE_TASK, $task); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task);
} }
/** /**
@ -47,7 +48,7 @@ class TaskObserver
*/ */
public function deleted(Task $task) public function deleted(Task $task)
{ {
SubscriptionHandler::dispatch(Subscription::EVENT_DELETE_TASK, $task); WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task);
} }
/** /**

View File

@ -131,7 +131,7 @@ class PayPalExpressPaymentDriver extends BasePaymentDriver
$transaction_reference = $response->getTransactionReference() ?: $request->input('token'); $transaction_reference = $response->getTransactionReference() ?: $request->input('token');
if ($response->isCancelled()) { if ($response->isCancelled()) {
return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_voided')); return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_cancelled'));
} elseif ($response->isSuccessful()) { } elseif ($response->isSuccessful()) {
SystemLogger::dispatch( SystemLogger::dispatch(
[ [

View File

@ -102,6 +102,6 @@ class SOFORT
public function processUnsuccessfulPayment($state) public function processUnsuccessfulPayment($state)
{ {
return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_voided')); return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_cancelled'));
} }
} }

View File

@ -12,9 +12,9 @@
namespace App\Policies; namespace App\Policies;
/** /**
* Class SubscriptionPolicy * Class WebhookPolicy
* @package App\Policies * @package App\Policies
*/ */
class SubscriptionPolicy extends EntityPolicy class WebhookPolicy extends EntityPolicy
{ {
} }

View File

@ -28,10 +28,10 @@ use App\Models\Product;
use App\Models\Quote; use App\Models\Quote;
use App\Models\RecurringInvoice; use App\Models\RecurringInvoice;
use App\Models\RecurringQuote; use App\Models\RecurringQuote;
use App\Models\Subscription;
use App\Models\TaxRate; use App\Models\TaxRate;
use App\Models\User; use App\Models\User;
use App\Models\Vendor; use App\Models\Vendor;
use App\Models\Webhook;
use App\Policies\ActivityPolicy; use App\Policies\ActivityPolicy;
use App\Policies\ClientPolicy; use App\Policies\ClientPolicy;
use App\Policies\CompanyGatewayPolicy; use App\Policies\CompanyGatewayPolicy;
@ -49,10 +49,10 @@ use App\Policies\ProductPolicy;
use App\Policies\QuotePolicy; use App\Policies\QuotePolicy;
use App\Policies\RecurringInvoicePolicy; use App\Policies\RecurringInvoicePolicy;
use App\Policies\RecurringQuotePolicy; use App\Policies\RecurringQuotePolicy;
use App\Policies\SubscriptionPolicy;
use App\Policies\TaxRatePolicy; use App\Policies\TaxRatePolicy;
use App\Policies\UserPolicy; use App\Policies\UserPolicy;
use App\Policies\VendorPolicy; use App\Policies\VendorPolicy;
use App\Policies\WebhookPolicy;
use Auth; use Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Gate;
@ -82,7 +82,7 @@ class AuthServiceProvider extends ServiceProvider
Quote::class => QuotePolicy::class, Quote::class => QuotePolicy::class,
RecurringInvoice::class => RecurringInvoicePolicy::class, RecurringInvoice::class => RecurringInvoicePolicy::class,
RecurringQuote::class => RecurringQuotePolicy::class, RecurringQuote::class => RecurringQuotePolicy::class,
Subscription::class => SubscriptionPolicy::class, Webhook::class => WebhookPolicy::class,
TaxRate::class => TaxRatePolicy::class, TaxRate::class => TaxRatePolicy::class,
User::class => UserPolicy::class, User::class => UserPolicy::class,
Vendor::class => VendorPolicy::class, Vendor::class => VendorPolicy::class,

View File

@ -38,7 +38,7 @@ class DeletePayment
public function run() public function run()
{ {
return $this->setStatus(Payment::STATUS_VOIDED) //sets status of payment return $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment
->updateCreditables() //return the credits first ->updateCreditables() //return the credits first
->adjustInvoices() ->adjustInvoices()
->updateClient() ->updateClient()
@ -109,7 +109,7 @@ class DeletePayment
private function setStatus($status) private function setStatus($status)
{ {
$this->payment->status_id = Payment::STATUS_VOIDED; $this->payment->status_id = Payment::STATUS_CANCELLED;
return $this; return $this;
} }

View File

@ -46,6 +46,7 @@ class CompanyTokenTransformer extends EntityTransformer
'user_id' => $this->encodePrimaryKey($company_token->user_id), 'user_id' => $this->encodePrimaryKey($company_token->user_id),
'token' => $company_token->token, 'token' => $company_token->token,
'name' => $company_token->name ?: '', 'name' => $company_token->name ?: '',
'is_system' =>(bool)$company_token->is_system,
'updated_at' => (int)$company_token->updated_at, 'updated_at' => (int)$company_token->updated_at,
'archived_at' => (int)$company_token->deleted_at, 'archived_at' => (int)$company_token->deleted_at,
'created_at' => (int)$company_token->created_at, 'created_at' => (int)$company_token->created_at,

View File

@ -1,39 +0,0 @@
<?php
namespace App\Transformers;
use App\Models\Subscription;
use App\Utils\Traits\MakesHash;
class SubscriptionTransformer extends EntityTransformer
{
use MakesHash;
protected $defaultIncludes = [];
/**
* @var array
*/
protected $availableIncludes = [];
/**
* @param Activity $subscription
*
* @return array
*/
public function transform(Subscription $subscription)
{
return [
'id' => (string) $this->encodePrimaryKey($subscription->id),
'company_id' => (string) $this->encodePrimaryKey($subscription->company_id),
'user_id' => (string) $this->encodePrimaryKey($subscription->user_id),
'archived_at' => (int)$subscription->deleted_at,
'updated_at' => (int)$subscription->updated_at,
'created_at' => (int)$subscription->created_at,
'is_deleted' => (bool)$subscription->is_deleted,
'target_url' => $subscription->target_url ? (string) $subscription->target_url : '',
'event_id' => (string) $subscription->event_id,
'format' => (string) $subscription->format,
];
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Transformers;
use App\Models\Webhook;
use App\Utils\Traits\MakesHash;
class WebhookTransformer extends EntityTransformer
{
use MakesHash;
protected $defaultIncludes = [];
/**
* @var array
*/
protected $availableIncludes = [];
/**
* @param Activity $webhook
*
* @return array
*/
public function transform(Webhook $webhook)
{
return [
'id' => (string) $this->encodePrimaryKey($webhook->id),
'company_id' => (string) $this->encodePrimaryKey($webhook->company_id),
'user_id' => (string) $this->encodePrimaryKey($webhook->user_id),
'archived_at' => (int)$webhook->deleted_at,
'updated_at' => (int)$webhook->updated_at,
'created_at' => (int)$webhook->created_at,
'is_deleted' => (bool)$webhook->is_deleted,
'target_url' => $webhook->target_url ? (string) $webhook->target_url : '',
'event_id' => (string) $webhook->event_id,
'format' => (string) $webhook->format,
];
}
}

14
composer.lock generated
View File

@ -1089,20 +1089,6 @@
"sqlserver", "sqlserver",
"sqlsrv" "sqlsrv"
], ],
"funding": [
{
"url": "https://www.doctrine-project.org/sponsorship.html",
"type": "custom"
},
{
"url": "https://www.patreon.com/phpdoctrine",
"type": "patreon"
},
{
"url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdbal",
"type": "tidelift"
}
],
"time": "2020-04-20T17:19:26+00:00" "time": "2020-04-20T17:19:26+00:00"
}, },
{ {

View File

@ -13,9 +13,17 @@ class CompanyTooLargeAttribute extends Migration
*/ */
public function up() public function up()
{ {
Schema::table('companies', function (Blueprint $table) { Schema::table('companies', function (Blueprint $table) {
$table->boolean('is_large')->default(0); $table->boolean('is_large')->default(0);
}); });
Schema::table('company_tokens', function (Blueprint $table) {
$table->boolean('is_system')->default(0);
});
Schema::rename('subscriptions', 'webhooks');
} }
/** /**

View File

@ -3234,6 +3234,9 @@ return [
'enable_only_for_development' => 'Enable only for development', 'enable_only_for_development' => 'Enable only for development',
'test_pdf' => 'Test PDF', 'test_pdf' => 'Test PDF',
'status_cancelled' => 'Cancelled',
'checkout_authorize_label' => 'Checkout.com can be can saved as payment method for future use, once you complete your first transaction. Don\'t forget to check "Save card" during payment process.', 'checkout_authorize_label' => 'Checkout.com can be can saved as payment method for future use, once you complete your first transaction. Don\'t forget to check "Save card" during payment process.',
]; ];

View File

@ -135,8 +135,9 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a
/*Subscription and Webhook routes */ /*Subscription and Webhook routes */
Route::post('hooks', 'SubscriptionController@subscribe')->name('hooks.subscribe'); Route::post('hooks', 'SubscriptionController@subscribe')->name('hooks.subscribe');
Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe'); Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe');
Route::resource('subscriptions', 'SubscriptionController');
Route::post('subscriptions/bulk', 'SubscriptionController@bulk')->name('subscriptions.bulk'); Route::resource('webhooks', 'WebhookController');
Route::post('webhooks/bulk', 'WebhookController@bulk')->name('webhooks.bulk');
/*Company Ledger */ /*Company Ledger */
Route::get('company_ledger', 'CompanyLedgerController@index')->name('company_ledger.index'); Route::get('company_ledger', 'CompanyLedgerController@index')->name('company_ledger.index');

View File

@ -15,10 +15,10 @@ use Tests\TestCase;
/** /**
* @test * @test
* @covers App\Http\Controllers\SubscriptionController * @covers App\Http\Controllers\WebhookController
*/ */
class SubscriptionAPITest extends TestCase class WebhookAPITest extends TestCase
{ {
use MakesHash; use MakesHash;
use DatabaseTransactions; use DatabaseTransactions;
@ -41,17 +41,17 @@ class SubscriptionAPITest extends TestCase
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
} }
public function testSubscriptionGetRoute() public function testWebhookGetRoute()
{ {
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->get('/api/v1/subscriptions'); ])->get('/api/v1/webhooks');
$response->assertStatus(200); $response->assertStatus(200);
} }
public function testSubscriptionPostRoute() public function testWebhookPostRoute()
{ {
$data = [ $data = [
'target_url' => 'http://hook.com', 'target_url' => 'http://hook.com',
@ -62,7 +62,7 @@ class SubscriptionAPITest extends TestCase
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->post('/api/v1/subscriptions', $data); ])->post('/api/v1/webhooks', $data);
$response->assertStatus(200); $response->assertStatus(200);
@ -77,7 +77,7 @@ class SubscriptionAPITest extends TestCase
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->put('/api/v1/subscriptions/'.$arr['data']['id'], $data); ])->put('/api/v1/webhooks/'.$arr['data']['id'], $data);
$response->assertStatus(200); $response->assertStatus(200);
@ -88,7 +88,7 @@ class SubscriptionAPITest extends TestCase
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->delete('/api/v1/subscriptions/'.$arr['data']['id']); ])->delete('/api/v1/webhooks/'.$arr['data']['id']);
$arr = $response->json(); $arr = $response->json();
@ -103,7 +103,7 @@ class SubscriptionAPITest extends TestCase
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token 'X-API-TOKEN' => $this->token
])->post('/api/v1/subscriptions/bulk?action=restore', $data); ])->post('/api/v1/webhooks/bulk?action=restore', $data);
$arr = $response->json(); $arr = $response->json();
@ -113,7 +113,7 @@ class SubscriptionAPITest extends TestCase
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token 'X-API-TOKEN' => $this->token
])->post('/api/v1/subscriptions/bulk?action=delete', $data); ])->post('/api/v1/webhooks/bulk?action=delete', $data);
$arr = $response->json(); $arr = $response->json();