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($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); }
public function edit($publicId) { $payment = Payment::scope($publicId)->firstOrFail(); $payment->payment_date = Utils::fromSqlDate($payment->payment_date); $data = array('client' => null, 'invoice' => null, 'invoices' => Invoice::scope()->where('is_recurring', '=', false)->where('is_quote', '=', false)->with('client', 'invoice_status')->orderBy('invoice_number')->get(), 'payment' => $payment, 'method' => 'PUT', 'url' => 'payments/' . $publicId, 'title' => trans('texts.edit_payment'), 'paymentTypes' => Cache::get('paymentTypes'), 'clients' => Client::scope()->with('contacts')->orderBy('name')->get()); return View::make('payments.edit', $data); }
public function save($publicId = null, $input) { if ($publicId) { $payment = Payment::scope($publicId)->firstOrFail(); } else { $payment = Payment::createNew(); } $paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null; $payment->payment_type_id = $paymentTypeId; $payment->payment_date = Utils::toSqlDate($input['payment_date']); $payment->transaction_reference = trim($input['transaction_reference']); if (!$publicId) { $clientId = Client::getPrivateId($input['client']); $amount = Utils::parseFloat($input['amount']); if ($paymentTypeId == PAYMENT_TYPE_CREDIT) { $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get(); $applied = 0; foreach ($credits as $credit) { $applied += $credit->apply($amount); if ($applied >= $amount) { break; } } } $payment->client_id = $clientId; $payment->invoice_id = isset($input['invoice']) && $input['invoice'] != "-1" ? Invoice::getPrivateId($input['invoice']) : null; $payment->amount = $amount; } $payment->save(); return $payment; }
public function index() { $payments = Payment::scope()->orderBy('created_at', 'desc')->get(); $payments = Utils::remapPublicIds($payments->toArray()); $response = json_encode($payments, JSON_PRETTY_PRINT); $headers = Utils::getApiHeaders(count($payments)); return Response::make($response, 200, $headers); }
public function save($input, $payment = null) { $publicId = isset($input['public_id']) ? $input['public_id'] : false; if ($payment) { // do nothing } elseif ($publicId) { $payment = Payment::scope($publicId)->firstOrFail(); if (Utils::isNinjaDev()) { \Log::warning('Entity not set in payment repo save'); } } else { $payment = Payment::createNew(); } if ($payment->is_deleted) { return $payment; } $paymentTypeId = false; if (isset($input['payment_type_id'])) { $paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null; $payment->payment_type_id = $paymentTypeId; } if (isset($input['payment_date_sql'])) { $payment->payment_date = $input['payment_date_sql']; } elseif (isset($input['payment_date'])) { $payment->payment_date = Utils::toSqlDate($input['payment_date']); } else { $payment->payment_date = date('Y-m-d'); } if (isset($input['transaction_reference'])) { $payment->transaction_reference = trim($input['transaction_reference']); } if (!$publicId) { $clientId = $input['client_id']; $amount = Utils::parseFloat($input['amount']); if ($paymentTypeId == PAYMENT_TYPE_CREDIT) { $credits = Credit::scope()->where('client_id', '=', $clientId)->where('balance', '>', 0)->orderBy('created_at')->get(); $remaining = $amount; foreach ($credits as $credit) { $remaining -= $credit->apply($remaining); if (!$remaining) { break; } } } $payment->invoice_id = $input['invoice_id']; $payment->client_id = $clientId; $payment->amount = $amount; } $payment->save(); return $payment; }
/** * @SWG\Get( * path="/payments", * tags={"payment"}, * summary="List of payments", * @SWG\Response( * response=200, * description="A list with payments", * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Payment")) * ), * @SWG\Response( * response="default", * description="an ""unexpected"" error" * ) * ) */ public function index() { $paginator = Payment::scope(); $payments = Payment::scope()->with('client.contacts', 'invitation', 'user', 'invoice'); if ($clientPublicId = Input::get('client_id')) { $filter = function ($query) use($clientPublicId) { $query->where('public_id', '=', $clientPublicId); }; $payments->whereHas('client', $filter); $paginator->whereHas('client', $filter); } $payments = $payments->orderBy('created_at', 'desc')->paginate(); $paginator = $paginator->paginate(); $transformer = new PaymentTransformer(Auth::user()->account, Input::get('serializer')); $data = $this->createCollection($payments, $transformer, 'payments', $paginator); return $this->response($data); }
private function getData($request) { $account = Auth::user()->account; $data = ['account' => $account, 'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()), 'multiUser' => $account->users->count() > 1]; if ($request->input(ENTITY_CLIENT)) { $data['clients'] = Client::scope()->with('user', 'contacts', 'country')->withArchived()->get(); $data['contacts'] = Contact::scope()->with('user', 'client.contacts')->withTrashed()->get(); $data['credits'] = Credit::scope()->with('user', 'client.contacts')->get(); } if ($request->input(ENTITY_TASK)) { $data['tasks'] = Task::scope()->with('user', 'client.contacts')->withArchived()->get(); } if ($request->input(ENTITY_INVOICE)) { $data['invoices'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_quote', '=', false)->where('is_recurring', '=', false)->get(); $data['quotes'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_quote', '=', true)->where('is_recurring', '=', false)->get(); $data['recurringInvoices'] = Invoice::scope()->with('user', 'client.contacts', 'invoice_status', 'frequency')->withArchived()->where('is_quote', '=', false)->where('is_recurring', '=', true)->get(); } if ($request->input(ENTITY_PAYMENT)) { $data['payments'] = Payment::scope()->withArchived()->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')->get(); } if ($request->input(ENTITY_VENDOR)) { $data['clients'] = Vendor::scope()->with('user', 'vendorcontacts', 'country')->withArchived()->get(); $data['vendor_contacts'] = VendorContact::scope()->with('user', 'vendor.contacts')->withTrashed()->get(); /* $data['expenses'] = Credit::scope() ->with('user', 'client.contacts') ->get(); */ } return $data; }
private function export() { $output = fopen('php://output', 'w') or Utils::fatalError(); header('Content-Type:application/csv'); header('Content-Disposition:attachment;filename=export.csv'); $clients = Client::scope()->get(); Utils::exportData($output, $clients->toArray()); $contacts = Contact::scope()->get(); Utils::exportData($output, $contacts->toArray()); $invoices = Invoice::scope()->get(); Utils::exportData($output, $invoices->toArray()); $invoiceItems = InvoiceItem::scope()->get(); Utils::exportData($output, $invoiceItems->toArray()); $payments = Payment::scope()->get(); Utils::exportData($output, $payments->toArray()); $credits = Credit::scope()->get(); Utils::exportData($output, $credits->toArray()); fclose($output); exit; }
/** * @param $request * * @return array */ private function getData($request) { $account = Auth::user()->account; $data = ['account' => $account, 'title' => 'Invoice Ninja v' . NINJA_VERSION . ' - ' . $account->formatDateTime($account->getDateTime()), 'multiUser' => $account->users->count() > 1]; if ($request->input('include') === 'all' || $request->input('clients')) { $data['clients'] = Client::scope()->with('user', 'contacts', 'country')->withArchived()->get(); } if ($request->input('include') === 'all' || $request->input('contacts')) { $data['contacts'] = Contact::scope()->with('user', 'client.contacts')->withTrashed()->get(); } if ($request->input('include') === 'all' || $request->input('credits')) { $data['credits'] = Credit::scope()->with('user', 'client.contacts')->get(); } if ($request->input('include') === 'all' || $request->input('tasks')) { $data['tasks'] = Task::scope()->with('user', 'client.contacts')->withArchived()->get(); } if ($request->input('include') === 'all' || $request->input('invoices')) { $data['invoices'] = Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_recurring', '=', false)->get(); } if ($request->input('include') === 'all' || $request->input('quotes')) { $data['quotes'] = Invoice::scope()->invoiceType(INVOICE_TYPE_QUOTE)->with('user', 'client.contacts', 'invoice_status')->withArchived()->where('is_recurring', '=', false)->get(); } if ($request->input('include') === 'all' || $request->input('recurring')) { $data['recurringInvoices'] = Invoice::scope()->invoiceType(INVOICE_TYPE_STANDARD)->with('user', 'client.contacts', 'invoice_status', 'frequency')->withArchived()->where('is_recurring', '=', true)->get(); } if ($request->input('include') === 'all' || $request->input('payments')) { $data['payments'] = Payment::scope()->withArchived()->with('user', 'client.contacts', 'payment_type', 'invoice', 'account_gateway.gateway')->get(); } if ($request->input('include') === 'all' || $request->input('vendors')) { $data['vendors'] = Vendor::scope()->with('user', 'vendor_contacts', 'country')->withArchived()->get(); } if ($request->input('include') === 'all' || $request->input('vendor_contacts')) { $data['vendor_contacts'] = VendorContact::scope()->with('user', 'vendor.vendor_contacts')->withTrashed()->get(); } return $data; }
/** * @SWG\Post( * path="/payments", * summary="Create a payment", * tags={"payment"}, * @SWG\Parameter( * in="body", * name="body", * @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" * ) * ) */ 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) { return $error; } $payment = $this->paymentRepo->save($data); $payment = Payment::scope($payment->public_id)->with('client', 'contact', 'user', 'invoice')->first(); $transformer = new PaymentTransformer(Auth::user()->account, Input::get('serializer')); $data = $this->createItem($payment, $transformer, 'payment'); return $this->response($data); }
/** * @SWG\Delete( * path="/payments/{payment_id}", * summary="Delete a payment", * tags={"payment"}, * @SWG\Parameter( * in="body", * name="body", * @SWG\Schema(ref="#/definitions/Payment") * ), * @SWG\Response( * response=200, * description="Delete payment", * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Payment")) * ), * @SWG\Response( * response="default", * description="an ""unexpected"" error" * ) * ) */ public function destroy($publicId) { $payment = Payment::scope($publicId)->withTrashed()->first(); $invoiceId = $payment->invoice->public_id; $this->paymentRepo->delete($payment); $invoice = Invoice::scope($invoiceId)->with('client', 'invoice_items', 'invitations')->with(['payments' => function ($query) { $query->withTrashed(); }])->first(); $transformer = new InvoiceTransformer(\Auth::user()->account, Input::get('serializer')); $data = $this->createItem($invoice, $transformer, 'invoice'); return $this->response($data); }
/** * @param $startDate * @param $endDate * @param $isExport * @return array */ private function generatePaymentReport($startDate, $endDate, $isExport) { $columns = ['client', 'invoice_number', 'invoice_date', 'amount', 'payment_date', 'paid', 'method']; $account = Auth::user()->account; $displayData = []; $reportTotals = []; $payments = Payment::scope()->withArchived()->excludeFailed()->whereHas('client', function ($query) { $query->where('is_deleted', '=', false); })->whereHas('invoice', function ($query) { $query->where('is_deleted', '=', false); })->with('client.contacts', 'invoice', 'payment_type', 'account_gateway.gateway')->where('payment_date', '>=', $startDate)->where('payment_date', '<=', $endDate); foreach ($payments->get() as $payment) { $invoice = $payment->invoice; $client = $payment->client; $displayData[] = [$isExport ? $client->getDisplayName() : $client->present()->link, $isExport ? $invoice->invoice_number : $invoice->present()->link, $invoice->present()->invoice_date, $account->formatMoney($invoice->amount, $client), $payment->present()->payment_date, $account->formatMoney($payment->getCompletedAmount(), $client), $payment->present()->method]; $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'amount', $invoice->amount); $reportTotals = $this->addToTotals($reportTotals, $client->currency_id, 'paid', $payment->getCompletedAmount()); } return ['columns' => $columns, 'displayData' => $displayData, 'reportTotals' => $reportTotals]; }
/** * @SWG\Get( * path="/payments", * tags={"payment"}, * summary="List of payments", * @SWG\Response( * response=200, * description="A list with payments", * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Payment")) * ), * @SWG\Response( * response="default", * description="an ""unexpected"" error" * ) * ) */ public function index() { $payments = Payment::scope()->withTrashed()->with(['invoice'])->orderBy('created_at', 'desc'); return $this->listResponse($payments); }
public function handleWebHook($input) { $eventId = array_get($input, 'id'); $eventType = array_get($input, 'type'); $accountGateway = $this->accountGateway; $accountId = $accountGateway->account_id; if (!$eventId) { throw new Exception('Missing event id'); } if (!$eventType) { throw new Exception('Missing event type'); } $supportedEvents = ['charge.failed', 'charge.succeeded', 'charge.refunded', 'customer.source.updated', 'customer.source.deleted', 'customer.bank_account.deleted']; if (!in_array($eventType, $supportedEvents)) { return ['message' => 'Ignoring event']; } // Fetch the event directly from Stripe for security $eventDetails = $this->makeStripeCall('GET', 'events/' . $eventId); if (is_string($eventDetails) || !$eventDetails) { return false; } if ($eventType != $eventDetails['type']) { return false; } if (!$eventDetails['pending_webhooks']) { return false; } if ($eventType == 'charge.failed' || $eventType == 'charge.succeeded' || $eventType == 'charge.refunded') { $charge = $eventDetails['data']['object']; $transactionRef = $charge['id']; $payment = Payment::scope(false, $accountId)->where('transaction_reference', '=', $transactionRef)->first(); if (!$payment) { return false; } if ($eventType == 'charge.failed') { if (!$payment->isFailed()) { $payment->markFailed($charge['failure_message']); $userMailer = app('App\\Ninja\\Mailers\\UserMailer'); $userMailer->sendNotification($payment->user, $payment->invoice, 'payment_failed', $payment); } } elseif ($eventType == 'charge.succeeded') { $payment->markComplete(); } elseif ($eventType == 'charge.refunded') { $payment->recordRefund($charge['amount_refunded'] / 100 - $payment->refunded); } } elseif ($eventType == 'customer.source.updated' || $eventType == 'customer.source.deleted' || $eventType == 'customer.bank_account.deleted') { $source = $eventDetails['data']['object']; $sourceRef = $source['id']; $paymentMethod = PaymentMethod::scope(false, $accountId)->where('source_reference', '=', $sourceRef)->first(); if (!$paymentMethod) { return false; } if ($eventType == 'customer.source.deleted' || $eventType == 'customer.bank_account.deleted') { $paymentMethod->delete(); } elseif ($eventType == 'customer.source.updated') { //$this->paymentService->convertPaymentMethodFromStripe($source, null, $paymentMethod)->save(); } } return 'Processed successfully'; }
public function handleWebHook($input) { $accountGateway = $this->accountGateway; $accountId = $accountGateway->account_id; foreach (array_keys($input) as $key) { if ('_id' == substr($key, -3)) { $objectType = substr($key, 0, -3); $objectId = $input[$key]; break; } } if (!isset($objectType)) { throw new Exception('Could not find object id parameter'); } if ($objectType == 'credit_card') { $paymentMethod = PaymentMethod::scope(false, $accountId)->where('source_reference', '=', $objectId)->first(); if (!$paymentMethod) { throw new Exception('Unknown payment method'); } $wepay = Utils::setupWePay($accountGateway); $source = $wepay->request('credit_card', ['client_id' => WEPAY_CLIENT_ID, 'client_secret' => WEPAY_CLIENT_SECRET, 'credit_card_id' => intval($objectId)]); if ($source->state == 'deleted') { $paymentMethod->delete(); } else { //$this->paymentService->convertPaymentMethodFromWePay($source, null, $paymentMethod)->save(); } return 'Processed successfully'; } elseif ($objectType == 'account') { $config = $accountGateway->getConfig(); if ($config->accountId != $objectId) { throw new Exception('Unknown account'); } $wepay = Utils::setupWePay($accountGateway); $wepayAccount = $wepay->request('account', ['account_id' => intval($objectId)]); if ($wepayAccount->state == 'deleted') { $accountGateway->delete(); } else { $config->state = $wepayAccount->state; $accountGateway->setConfig($config); $accountGateway->save(); } return ['message' => 'Processed successfully']; } elseif ($objectType == 'checkout') { $payment = Payment::scope(false, $accountId)->where('transaction_reference', '=', $objectId)->first(); if (!$payment) { throw new Exception('Unknown payment'); } $wepay = Utils::setupWePay($accountGateway); $checkout = $wepay->request('checkout', ['checkout_id' => intval($objectId)]); if ($checkout->state == 'refunded') { $payment->recordRefund(); } elseif (!empty($checkout->refund) && !empty($checkout->refund->amount_refunded) && $checkout->refund->amount_refunded - $payment->refunded > 0) { $payment->recordRefund($checkout->refund->amount_refunded - $payment->refunded); } if ($checkout->state == 'captured') { $payment->markComplete(); } elseif ($checkout->state == 'cancelled') { $payment->markCancelled(); } elseif ($checkout->state == 'failed') { $payment->markFailed(); } return 'Processed successfully'; } else { return 'Ignoring event'; } }