1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Services/PaymentService.php

172 lines
5.0 KiB
PHP
Raw Normal View History

<?php namespace App\Services;
use App\Models\Invoice;
2015-11-05 23:37:04 +01:00
use Utils;
2016-03-16 00:08:00 +01:00
use Auth;
use Exception;
use App\Models\Account;
use App\Models\Client;
2016-05-26 21:22:09 +02:00
use App\Models\Activity;
2015-10-28 20:22:07 +01:00
use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\AccountRepository;
2016-05-23 18:52:20 +02:00
use App\Ninja\Datatables\PaymentDatatable;
2015-10-28 20:22:07 +01:00
class PaymentService extends BaseService
{
/**
* PaymentService constructor.
*
* @param PaymentRepository $paymentRepo
* @param AccountRepository $accountRepo
* @param DatatableService $datatableService
*/
public function __construct(
PaymentRepository $paymentRepo,
AccountRepository $accountRepo,
DatatableService $datatableService
)
{
2015-11-05 23:37:04 +01:00
$this->datatableService = $datatableService;
2015-10-28 20:22:07 +01:00
$this->paymentRepo = $paymentRepo;
$this->accountRepo = $accountRepo;
}
/**
* @return PaymentRepository
*/
2015-10-28 20:22:07 +01:00
protected function getRepo()
{
return $this->paymentRepo;
}
/**
* @param Invoice $invoice
* @return bool
*/
public function autoBillInvoice(Invoice $invoice)
{
/** @var \App\Models\Client $client */
$client = $invoice->client;
/** @var \App\Models\Account $account */
2016-06-20 16:14:43 +02:00
$account = $client->account;
/** @var \App\Models\Invitation $invitation */
$invitation = $invoice->invitations->first();
2016-06-20 16:14:43 +02:00
if ( ! $invitation) {
return false;
}
2016-07-19 19:12:28 +02:00
if ($credits = $client->credits->sum('balance')) {
$balance = $invoice->balance;
$amount = min($credits, $balance);
$data = [
'payment_type_id' => PAYMENT_TYPE_CREDIT,
'invoice_id' => $invoice->id,
'client_id' => $client->id,
'amount' => $amount,
];
$payment = $this->paymentRepo->save($data);
if ($amount == $balance) {
return $payment;
}
}
2016-06-20 16:14:43 +02:00
$paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_TOKEN);
2016-07-19 19:15:03 +02:00
if ( ! $paymentDriver) {
return false;
}
2016-06-20 16:14:43 +02:00
$customer = $paymentDriver->customer();
2016-06-20 16:14:43 +02:00
if ( ! $customer) {
return false;
}
2016-06-20 16:14:43 +02:00
$paymentMethod = $customer->default_payment_method;
if ($paymentMethod->requiresDelayedAutoBill()) {
2016-05-26 21:22:09 +02:00
$invoiceDate = \DateTime::createFromFormat('Y-m-d', $invoice->invoice_date);
2016-05-25 04:49:06 +02:00
$minDueDate = clone $invoiceDate;
$minDueDate->modify('+10 days');
2016-05-26 21:22:09 +02:00
if (date_create() < $minDueDate) {
2016-05-25 04:49:06 +02:00
// Can't auto bill now
return false;
}
2016-05-26 21:22:09 +02:00
if ($invoice->partial > 0) {
// The amount would be different than the amount in the email
return false;
}
$firstUpdate = Activity::where('invoice_id', '=', $invoice->id)
2016-05-25 04:49:06 +02:00
->where('activity_type_id', '=', ACTIVITY_TYPE_UPDATE_INVOICE)
->first();
if ($firstUpdate) {
$backup = json_decode($firstUpdate->json_backup);
if ($backup->balance != $invoice->balance || $backup->due_date != $invoice->due_date) {
// It's changed since we sent the email can't bill now
return false;
}
}
2016-05-26 21:22:09 +02:00
if ($invoice->payments->count()) {
2016-05-25 04:49:06 +02:00
// ACH requirements are strict; don't auto bill this
return false;
}
}
try {
return $paymentDriver->completeOnsitePurchase(false, $paymentMethod);
} catch (Exception $exception) {
return false;
}
}
2015-11-05 23:37:04 +01:00
public function getDatatable($clientPublicId, $search)
{
$datatable = new PaymentDatatable(true, $clientPublicId);
2015-11-05 23:37:04 +01:00
$query = $this->paymentRepo->find($clientPublicId, $search);
2016-03-16 00:08:00 +01:00
if(!Utils::hasPermission('view_all')){
$query->where('payments.user_id', '=', Auth::user()->id);
}
2016-05-23 18:52:20 +02:00
return $this->datatableService->createDatatable($datatable, $query);
2015-11-05 23:37:04 +01:00
}
public function bulk($ids, $action, $params = [])
2016-04-23 22:40:19 +02:00
{
if ($action == 'refund') {
if ( ! $ids ) {
return 0;
}
2015-11-05 23:37:04 +01:00
2016-04-23 22:40:19 +02:00
$payments = $this->getRepo()->findByPublicIdsWithTrashed($ids);
2016-05-06 23:05:42 +02:00
$successful = 0;
2015-11-05 23:37:04 +01:00
2016-04-23 22:40:19 +02:00
foreach ($payments as $payment) {
2016-06-20 16:14:43 +02:00
if (Auth::user()->can('edit', $payment)) {
2016-05-06 23:05:42 +02:00
$amount = !empty($params['amount']) ? floatval($params['amount']) : null;
2016-10-06 07:44:19 +02:00
if ($accountGateway = $payment->account_gateway) {
$paymentDriver = $accountGateway->paymentDriver();
if ($paymentDriver->refundPayment($payment, $amount)) {
$successful++;
}
2016-05-17 17:32:17 +02:00
}
2016-04-23 22:40:19 +02:00
}
}
2016-05-06 23:05:42 +02:00
return $successful;
2016-04-23 22:40:19 +02:00
} else {
return parent::bulk($ids, $action);
}
}
}