2015-09-10 19:50:09 +02:00
|
|
|
<?php namespace App\Services;
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
use Utils;
|
2016-03-16 00:08:00 +01:00
|
|
|
use Auth;
|
2015-09-10 19:50:09 +02:00
|
|
|
use URL;
|
|
|
|
use DateTime;
|
|
|
|
use Event;
|
2016-04-29 23:50:21 +02:00
|
|
|
use Cache;
|
2015-09-10 19:50:09 +02:00
|
|
|
use Omnipay;
|
|
|
|
use Session;
|
|
|
|
use CreditCard;
|
2016-05-13 15:30:22 +02:00
|
|
|
use WePay;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Models\Payment;
|
2016-05-11 00:46:32 +02:00
|
|
|
use App\Models\PaymentMethod;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Country;
|
2016-03-16 03:07:11 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
2016-05-26 21:22:09 +02:00
|
|
|
use App\Models\Activity;
|
2016-05-06 23:05:42 +02:00
|
|
|
use App\Models\AccountGateway;
|
2016-04-29 23:50:21 +02:00
|
|
|
use App\Http\Controllers\PaymentController;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Models\AccountGatewayToken;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-10-28 20:22:07 +01:00
|
|
|
use App\Services\BaseService;
|
|
|
|
use App\Events\PaymentWasCreated;
|
2016-05-23 18:52:20 +02:00
|
|
|
use App\Ninja\Datatables\PaymentDatatable;
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
class PaymentService extends BaseService
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $datatableService;
|
2016-05-11 00:46:32 +02:00
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
public function __construct(PaymentRepository $paymentRepo, AccountRepository $accountRepo, DatatableService $datatableService)
|
2015-09-10 19:50:09 +02:00
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
$this->datatableService = $datatableService;
|
2015-10-28 20:22:07 +01:00
|
|
|
$this->paymentRepo = $paymentRepo;
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
protected function getRepo()
|
|
|
|
{
|
|
|
|
return $this->paymentRepo;
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
public function autoBillInvoice($invoice)
|
|
|
|
{
|
|
|
|
$client = $invoice->client;
|
2016-06-20 16:14:43 +02:00
|
|
|
$account = $client->account;
|
2015-09-10 19:50:09 +02:00
|
|
|
$invitation = $invoice->invitations->first();
|
2016-05-15 04:38:09 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
if ( ! $invitation) {
|
2016-05-15 04:38:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
$paymentDriver = $account->paymentDriver($invitation, GATEWAY_TYPE_TOKEN);
|
|
|
|
$customer = $paymentDriver->customer();
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
if ( ! $customer) {
|
2015-09-10 19:50:09 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2016-06-20 16:14:43 +02:00
|
|
|
return $paymentDriver->completeOnsitePurchase(false, $paymentMethod);
|
|
|
|
|
|
|
|
/*
|
2016-05-13 15:30:22 +02:00
|
|
|
if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
|
|
|
|
$details['transaction_id'] = 'autobill_'.$invoice->id;
|
2016-04-27 01:59:52 +02:00
|
|
|
}
|
2016-06-20 16:14:43 +02:00
|
|
|
*/
|
2016-05-25 17:07:20 +02:00
|
|
|
}
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
public function getDatatable($clientPublicId, $search)
|
|
|
|
{
|
2016-05-23 18:52:20 +02:00
|
|
|
$datatable = new PaymentDatatable( ! $clientPublicId, $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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-23 22:40:19 +02:00
|
|
|
public function bulk($ids, $action, $params = array())
|
|
|
|
{
|
|
|
|
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-06-20 16:14:43 +02:00
|
|
|
$accountGateway = $payment->account_gateway;
|
|
|
|
$paymentDriver = $accountGateway->paymentDriver();
|
|
|
|
if ($paymentDriver->refundPayment($payment, $amount)) {
|
2016-05-06 23:05:42 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 18:52:20 +02:00
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
}
|