diff --git a/VERSION.txt b/VERSION.txt index 7d908497ecd1..2baca668b6f3 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.0.43 \ No newline at end of file +5.0.44 \ No newline at end of file diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index d08840e86663..ecf65659aa07 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -83,8 +83,6 @@ class PaymentController extends Controller $gateway = CompanyGateway::find($request->input('company_gateway_id')); - //refactor from here! - /** * find invoices * @@ -95,11 +93,13 @@ class PaymentController extends Controller $invoices = Invoice::whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->get(); /* pop non payable invoice from the $payable_invoices array */ + $payable_invoices = $payable_invoices->filter(function ($payable_invoice) use ($invoices) { return $invoices->where('hashed_id', $payable_invoice['invoice_id'])->first()->isPayable(); }); /*return early if no invoices*/ + if ($payable_invoices->count() == 0) { return redirect() ->route('client.invoices.index') @@ -108,23 +108,34 @@ class PaymentController extends Controller $settings = auth()->user()->client->getMergedSettings(); - /*iterate through invoices and add gateway fees and other payment metadata*/ - $payable_invoices = $payable_invoices->map(function ($payable_invoice) use ($invoices, $settings) { - $payable_invoice['amount'] = Number::parseFloat($payable_invoice['amount']); + // nlog($settings); + + /* This loop checks for under / over payments and returns the user if a check fails */ + + foreach($payable_invoices as $payable_invoice) + { + + /*Match the payable invoice to the Model Invoice*/ $invoice = $invoices->first(function ($inv) use ($payable_invoice) { return $payable_invoice['invoice_id'] == $inv->hashed_id; }); - // Check if company supports over & under payments. - // In case it doesn't this is where process should stop. + /* + * Check if company supports over & under payments. + * Determine the payable amount and the max payable. ie either partial or invoice balance + */ $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision); - $invoice_balance = Number::roundValue($invoice->balance, auth()->user()->client->currency()->precision); + $invoice_balance = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), auth()->user()->client->currency()->precision); + + /*If we don't allow under/over payments force the payable amount - prevents inspect element adjustments in JS*/ if ($settings->client_portal_allow_under_payment == false && $settings->client_portal_allow_over_payment == false) { $payable_invoice['amount'] = Number::roundValue(($invoice->partial > 0 ? $invoice->partial : $invoice->balance), auth()->user()->client->currency()->precision); - } // We don't allow either of these, reset the amount to default invoice (to prevent inspect element payments). + } + + /* If we DO allow under payments check the minimum amount is present else return */ if ($settings->client_portal_allow_under_payment) { if ($payable_invoice['amount'] < $settings->client_portal_under_payment_minimum) { @@ -133,23 +144,38 @@ class PaymentController extends Controller ->with('message', ctrans('texts.minimum_required_payment', ['amount' => $settings->client_portal_under_payment_minimum])); } } else { - $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision); - $invoice_balance = Number::roundValue($invoice->balance, auth()->user()->client->currency()->precision); + /*Double check!!*/ if ($payable_amount < $invoice_balance) { return redirect() ->route('client.invoices.index') ->with('message', ctrans('texts.under_payments_disabled')); } - } // Make sure 'amount' from form is not lower than 'amount' from invoice. + } - if ($settings->client_portal_allow_over_payment == false) { + /* If we don't allow over payments and the amount exceeds the balance */ + + if (!$settings->client_portal_allow_over_payment) { if ($payable_amount > $invoice_balance) { return redirect() ->route('client.invoices.index') ->with('message', ctrans('texts.over_payments_disabled')); } - } // Make sure 'amount' from form is not higher than 'amount' from invoice. + } + + } + + /*Iterate through invoices and add gateway fees and other payment metadata*/ + + $payable_invoices = $payable_invoices->map(function ($payable_invoice) use ($invoices, $settings) { + $payable_invoice['amount'] = Number::parseFloat($payable_invoice['amount']); + + $invoice = $invoices->first(function ($inv) use ($payable_invoice) { + return $payable_invoice['invoice_id'] == $inv->hashed_id; + }); + + $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision); + $invoice_balance = Number::roundValue($invoice->balance, auth()->user()->client->currency()->precision); $payable_invoice['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format()); $payable_invoice['invoice_number'] = $invoice->number; @@ -196,7 +222,7 @@ class PaymentController extends Controller $payment_hash = new PaymentHash; $payment_hash->hash = Str::random(128); - $payment_hash->data = ['invoices' => $payable_invoices->toArray()]; + $payment_hash->data = ['invoices' => $payable_invoices->toArray() , 'credits' => $credit_totals]; $payment_hash->fee_total = $fee_totals; $payment_hash->fee_invoice_id = $first_invoice->id; $payment_hash->save(); @@ -231,6 +257,7 @@ class PaymentController extends Controller public function response(PaymentResponseRequest $request) { + $gateway = CompanyGateway::findOrFail($request->input('company_gateway_id')); $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first(); @@ -265,35 +292,7 @@ class PaymentController extends Controller $payment_hash->save(); } - /* Iterate through the invoices and apply credits to them */ - collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment, $payment_hash) { - $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id)); - $amount = $payable_invoice->amount; - - $credits = $payment_hash->fee_invoice - ->client - ->service() - ->getCredits(); - - foreach ($credits as $credit) { - //starting invoice balance - $invoice_balance = $invoice->balance; - - //credit payment applied - $credit->service()->applyPayment($invoice, $amount, $payment); - - //amount paid from invoice calculated - $remaining_balance = ($invoice_balance - $invoice->fresh()->balance); - - //reduce the amount to be paid on the invoice from the NEXT credit - $amount -= $remaining_balance; - - //break if the invoice is no longer PAYABLE OR there is no more amount to be applied - if (!$invoice->isPayable() || (int)$amount == 0) { - break; - } - } - }); + $payment = $payment->service()->applyCredits($payment_hash)->save(); return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 6e7919b7e046..770d90914363 100644 --- a/app/Http/Requests/Client/UpdateClientRequest.php +++ b/app/Http/Requests/Client/UpdateClientRequest.php @@ -90,6 +90,11 @@ class UpdateClientRequest extends Request $input['group_settings_id'] = $this->decodePrimaryKey($input['group_settings_id']); } + /* If the user removes the currency we must always set the default */ + if (array_key_exists('settings', $input) && ! array_key_exists('currency_id', $input['settings'])) { + $input['settings']['currency_id'] = (string) auth()->user()->company()->settings->currency_id; + } + $input = $this->decodePrimaryKeys($input); if (array_key_exists('settings', $input)) { diff --git a/app/Http/Requests/Company/UpdateCompanyRequest.php b/app/Http/Requests/Company/UpdateCompanyRequest.php index 505711533c31..cca073724822 100644 --- a/app/Http/Requests/Company/UpdateCompanyRequest.php +++ b/app/Http/Requests/Company/UpdateCompanyRequest.php @@ -57,7 +57,7 @@ class UpdateCompanyRequest extends Request protected function prepareForValidation() { $input = $this->all(); - +// nlog($input); if (array_key_exists('settings', $input)) { $input['settings'] = $this->filterSaveableSettings($input['settings']); } diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index 264884b429c3..2c1a9b0ed7bb 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -27,6 +27,11 @@ class PaymentHash extends Model return $this->data->invoices; } + public function credits_total() + { + return isset($this->data->credits) ? $this->data->credits : 0; + } + public function payment() { return $this->belongsTo(Payment::class)->withTrashed(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 983b3f5c7643..8366c4f5f627 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -213,6 +213,9 @@ class BaseDriver extends AbstractPaymentDriver $this->attachInvoices($payment, $this->payment_hash); + if($this->payment_hash->credits_total() > 0) + $payment = $payment->service()->applyCredits($this->payment_hash)->save(); + $payment->service()->updateInvoicePayment($this->payment_hash); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 59b972e307d8..085797d2a04c 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -158,10 +158,15 @@ class PayPalExpressPaymentDriver extends BaseDriver } if (!$response->isSuccessful()) { - PaymentFailureMailer::dispatch($this->client, $response->getMessage(), $this->client->company, $response['PAYMENTINFO_0_AMT']); + + $data = $response->getData(); + + nlog($data); + + PaymentFailureMailer::dispatch($this->client, $response->getMessage(), $this->client->company, $this->payment_hash->data->amount); $message = [ - 'server_response' => $response->getMessage(), + 'server_response' => $data['L_LONGMESSAGE0'], 'data' => $this->payment_hash->data, ]; diff --git a/app/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 954a904b68aa..c0c4f0acbfdd 100644 --- a/app/Services/Payment/PaymentService.php +++ b/app/Services/Payment/PaymentService.php @@ -15,9 +15,12 @@ use App\Factory\PaymentFactory; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; +use App\Utils\Traits\MakesHash; class PaymentService { + use MakesHash; + private $payment; public function __construct($payment) @@ -97,6 +100,43 @@ class PaymentService return $this; } + public function applyCredits($payment_hash) + { + /* Iterate through the invoices and apply credits to them */ + collect($payment_hash->invoices())->each(function ($payable_invoice) use ($payment_hash) { + + $invoice = Invoice::find($this->decodePrimaryKey($payable_invoice->invoice_id)); + + $amount = $payable_invoice->amount; + + $credits = $payment_hash->fee_invoice + ->client + ->service() + ->getCredits(); + + foreach ($credits as $credit) { + //starting invoice balance + $invoice_balance = $invoice->balance; + + //credit payment applied + $credit->service()->applyPayment($invoice, $amount, $this->payment); + + //amount paid from invoice calculated + $remaining_balance = ($invoice_balance - $invoice->fresh()->balance); + + //reduce the amount to be paid on the invoice from the NEXT credit + $amount -= $remaining_balance; + + //break if the invoice is no longer PAYABLE OR there is no more amount to be applied + if (!$invoice->isPayable() || (int)$amount == 0) { + break; + } + } + }); + + return $this; + } + public function save() { $this->payment->save(); diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index f67de4a7490f..9df0b53c066a 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -39,6 +39,7 @@ class UpdateInvoicePayment $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get(); collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) { + $invoice = $invoices->first(function ($inv) use ($paid_invoice) { return $paid_invoice->invoice_id == $inv->hashed_id; }); @@ -49,6 +50,11 @@ class UpdateInvoicePayment $paid_amount = $paid_invoice->amount; } + /* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */ + if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance) + $paid_amount = $invoice->balance; + + /* Updates the company ledger */ $this->payment ->ledger() ->updatePaymentBalance($paid_amount * -1); diff --git a/config/ninja.php b/config/ninja.php index a062f95a174c..635a08a21f46 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -13,7 +13,7 @@ return [ 'require_https' => env('REQUIRE_HTTPS', true), 'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_domain' => env('APP_DOMAIN', ''), - 'app_version' => '5.0.43', + 'app_version' => '5.0.44', 'minimum_client_version' => '5.0.16', 'terms_version' => '1.0.1', 'api_secret' => env('API_SECRET', false), diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 1c850c61c839..aeaf5a0a8e03 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3362,4 +3362,5 @@ return [ 'currency_albanian_lek' => 'Albanian Lek', 'endless' => 'Endless', + 'minimum_payment' => 'Minimum Payment', ];