2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-07-05 10:01:16 +02:00
|
|
|
use Input;
|
2015-03-17 02:30:56 +01:00
|
|
|
use Utils;
|
2015-04-08 20:19:58 +02:00
|
|
|
use Response;
|
|
|
|
use App\Models\Payment;
|
2015-07-05 10:01:16 +02:00
|
|
|
use App\Models\Invoice;
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class PaymentApiController extends Controller
|
|
|
|
{
|
|
|
|
protected $paymentRepo;
|
|
|
|
|
|
|
|
public function __construct(PaymentRepository $paymentRepo)
|
|
|
|
{
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
|
|
|
}
|
|
|
|
|
2015-09-07 11:07:55 +02:00
|
|
|
public function index($clientPublicId = false)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-07-05 10:01:16 +02:00
|
|
|
$payments = Payment::scope()
|
2015-09-07 11:07:55 +02:00
|
|
|
->with('client', 'contact', 'invitation', 'user', 'invoice');
|
|
|
|
|
|
|
|
if ($clientPublicId) {
|
|
|
|
$payments->whereHas('client', function($query) use ($clientPublicId) {
|
|
|
|
$query->where('public_id', '=', $clientPublicId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$payments = $payments->orderBy('created_at', 'desc')->get();
|
2015-07-02 22:21:29 +02:00
|
|
|
$payments = Utils::remapPublicIds($payments);
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$response = json_encode($payments, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders(count($payments));
|
|
|
|
|
|
|
|
return Response::make($response, 200, $headers);
|
|
|
|
}
|
|
|
|
|
2015-07-05 10:01:16 +02:00
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
$data = Input::all();
|
|
|
|
$error = false;
|
|
|
|
|
|
|
|
if (isset($data['invoice_id'])) {
|
|
|
|
$invoice = Invoice::scope($data['invoice_id'])->with('client')->first();
|
|
|
|
|
|
|
|
if ($invoice) {
|
|
|
|
$data['invoice'] = $invoice->public_id;
|
|
|
|
$data['client'] = $invoice->client->public_id;
|
|
|
|
} else {
|
|
|
|
$error = trans('validation.not_in', ['attribute' => 'invoice_id']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error = trans('validation.not_in', ['attribute' => 'invoice_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($data['transaction_reference'])) {
|
|
|
|
$data['transaction_reference'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$error) {
|
|
|
|
$payment = $this->paymentRepo->save(false, $data);
|
|
|
|
$payment = Payment::scope($payment->public_id)->with('client', 'contact', 'user', 'invoice')->first();
|
|
|
|
|
|
|
|
$payment = Utils::remapPublicIds([$payment]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = json_encode($error ?: $payment, JSON_PRETTY_PRINT);
|
|
|
|
$headers = Utils::getApiHeaders();
|
|
|
|
return Response::make($response, 200, $headers);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|