2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
use App\Http\Requests\PaymentRequest;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Http\Requests\CreatePaymentAPIRequest;
|
|
|
|
use App\Http\Requests\UpdatePaymentRequest;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Payment;
|
2016-02-03 11:06:49 +01:00
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
2017-01-30 20:40:43 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2017-10-24 12:16:46 +02:00
|
|
|
use App\Services\PaymentService;
|
2015-07-05 10:01:16 +02:00
|
|
|
use Input;
|
2015-04-08 20:19:58 +02:00
|
|
|
use Response;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
class PaymentApiController extends BaseAPIController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
protected $paymentRepo;
|
2017-10-24 12:16:46 +02:00
|
|
|
protected $paymentService;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
protected $entityType = ENTITY_PAYMENT;
|
|
|
|
|
2017-10-24 12:16:46 +02:00
|
|
|
public function __construct(PaymentRepository $paymentRepo, PaymentService $paymentService, ContactMailer $contactMailer)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-03-02 15:36:46 +01:00
|
|
|
parent::__construct();
|
2015-11-27 13:55:28 +01:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$this->paymentRepo = $paymentRepo;
|
2017-10-24 12:16:46 +02:00
|
|
|
$this->paymentService = $paymentService;
|
2016-02-03 11:06:49 +01:00
|
|
|
$this->contactMailer = $contactMailer;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/payments",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="List payments",
|
|
|
|
* operationId="listPayments",
|
2015-11-08 22:53:13 +01:00
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="A list of payments",
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2015-11-27 13:55:28 +01:00
|
|
|
public function index()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-07-05 10:01:16 +02:00
|
|
|
$payments = Payment::scope()
|
2016-05-01 22:55:13 +02:00
|
|
|
->withTrashed()
|
2016-05-03 22:02:29 +02:00
|
|
|
->with(['invoice'])
|
2019-01-30 11:45:46 +01:00
|
|
|
->orderBy('updated_at', 'desc');
|
2015-09-07 11:07:55 +02:00
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
return $this->listResponse($payments);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
/**
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Get(
|
|
|
|
* path="/payments/{payment_id}",
|
|
|
|
* summary="Retrieve a payment",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="getPayment",
|
|
|
|
* tags={"payment"},
|
2017-01-30 20:40:43 +01:00
|
|
|
* @SWG\Parameter(
|
2017-03-08 17:03:35 +01:00
|
|
|
* in="path",
|
|
|
|
* name="payment_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-01-30 20:40:43 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="A single payment",
|
2017-01-30 20:40:43 +01:00
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-08 17:03:35 +01:00
|
|
|
public function show(PaymentRequest $request)
|
2016-05-01 22:55:13 +02:00
|
|
|
{
|
2017-03-08 17:03:35 +01:00
|
|
|
return $this->itemResponse($request->entity());
|
2016-05-01 22:55:13 +02:00
|
|
|
}
|
|
|
|
|
2015-11-08 22:53:13 +01:00
|
|
|
/**
|
|
|
|
* @SWG\Post(
|
|
|
|
* path="/payments",
|
|
|
|
* summary="Create a payment",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="createPayment",
|
2015-11-08 22:53:13 +01:00
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="payment",
|
2015-11-08 22:53:13 +01:00
|
|
|
* @SWG\Schema(ref="#/definitions/Payment")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New payment",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2016-05-02 10:38:01 +02:00
|
|
|
public function store(CreatePaymentAPIRequest $request)
|
2015-07-05 10:01:16 +02:00
|
|
|
{
|
2017-01-13 08:02:22 +01:00
|
|
|
// check payment has been marked sent
|
|
|
|
$request->invoice->markSentIfUnsent();
|
|
|
|
|
2017-10-24 12:16:46 +02:00
|
|
|
$payment = $this->paymentService->save($request->input(), null, $request->invoice);
|
2015-11-27 13:55:28 +01:00
|
|
|
|
2016-02-03 11:06:49 +01:00
|
|
|
if (Input::get('email_receipt')) {
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
}
|
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
return $this->itemResponse($payment);
|
2015-07-05 10:01:16 +02:00
|
|
|
}
|
2016-02-07 11:24:21 +01:00
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
/**
|
2017-03-08 17:03:35 +01:00
|
|
|
* @SWG\Put(
|
2017-01-30 20:40:43 +01:00
|
|
|
* path="/payments/{payment_id}",
|
2017-03-08 17:03:35 +01:00
|
|
|
* summary="Update a payment",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="updatePayment",
|
2017-01-30 20:40:43 +01:00
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Parameter(
|
2017-03-08 17:03:35 +01:00
|
|
|
* in="path",
|
|
|
|
* name="payment_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Parameter(
|
2017-01-30 20:40:43 +01:00
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="payment",
|
2017-01-30 20:40:43 +01:00
|
|
|
* @SWG\Schema(ref="#/definitions/Payment")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="Updated payment",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
|
|
|
* @param mixed $publicId
|
|
|
|
*/
|
|
|
|
public function update(UpdatePaymentRequest $request, $publicId)
|
|
|
|
{
|
|
|
|
if ($request->action) {
|
|
|
|
return $this->handleAction($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = $request->input();
|
|
|
|
$data['public_id'] = $publicId;
|
|
|
|
$payment = $this->paymentRepo->save($data, $request->entity());
|
|
|
|
|
2018-09-04 19:50:08 +02:00
|
|
|
if (Input::get('email_receipt')) {
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
return $this->itemResponse($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Delete(
|
|
|
|
* path="/payments/{payment_id}",
|
|
|
|
* summary="Delete a payment",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="deletePayment",
|
2017-03-08 17:03:35 +01:00
|
|
|
* tags={"payment"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="payment_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Deleted payment",
|
2017-01-30 20:40:43 +01:00
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-09 11:11:45 +01:00
|
|
|
public function destroy(UpdatePaymentRequest $request)
|
2016-05-01 22:55:13 +02:00
|
|
|
{
|
2016-05-03 10:53:00 +02:00
|
|
|
$payment = $request->entity();
|
2017-01-13 08:02:22 +01:00
|
|
|
|
2017-03-08 17:03:35 +01:00
|
|
|
$this->paymentRepo->delete($payment);
|
2016-02-07 11:24:21 +01:00
|
|
|
|
2016-05-03 10:53:00 +02:00
|
|
|
return $this->itemResponse($payment);
|
2016-05-01 22:55:13 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|