From e31867243cdc09be7d388f19f4454bd483080551 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 10:39:37 +1100 Subject: [PATCH 1/6] Version bump --- VERSION.txt | 2 +- config/ninja.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 7d908497ec..2baca668b6 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/config/ninja.php b/config/ninja.php index a062f95a17..635a08a21f 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), From 6d366febd86bcac497583a899d32d6dbaa62609d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 13:26:40 +1100 Subject: [PATCH 2/6] Fixes for paypal driver --- app/PaymentDrivers/PayPalExpressPaymentDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 59b972e307..f9a6b02285 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -192,7 +192,7 @@ class PayPalExpressPaymentDriver extends BaseDriver 'cancelUrl' => $this->client->company->domain() . '/client/invoices', 'description' => implode(',', collect($this->payment_hash->data->invoices) ->map(function ($invoice) { - return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->invoice_number); + return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->number); })->toArray()), 'transactionId' => $this->payment_hash->hash . '-' . time(), 'ButtonSource' => 'InvoiceNinja_SP', From 3ee3f67c8c209b5252057e83ce7b28fdd93d71e8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 16:14:20 +1100 Subject: [PATCH 3/6] Fixes for over payments --- .../ClientPortal/PaymentController.php | 52 ++++++++++++++----- .../PayPalExpressPaymentDriver.php | 2 +- app/Services/Payment/UpdateInvoicePayment.php | 6 +++ resources/lang/en/texts.php | 1 + 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index d08840e866..86b41ec319 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,32 @@ 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']); + /* 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 +142,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; diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index f9a6b02285..59b972e307 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -192,7 +192,7 @@ class PayPalExpressPaymentDriver extends BaseDriver 'cancelUrl' => $this->client->company->domain() . '/client/invoices', 'description' => implode(',', collect($this->payment_hash->data->invoices) ->map(function ($invoice) { - return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->number); + return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->invoice_number); })->toArray()), 'transactionId' => $this->payment_hash->hash . '-' . time(), 'ButtonSource' => 'InvoiceNinja_SP', diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index f67de4a749..9df0b53c06 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/resources/lang/en/texts.php b/resources/lang/en/texts.php index 1c850c61c8..aeaf5a0a8e 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', ]; From 47f42b804d32926a6e807d065b659112895f3211 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 16:54:04 +1100 Subject: [PATCH 4/6] Fixes for partial credit payments" --- .../ClientPortal/PaymentController.php | 35 +++------------- app/Models/PaymentHash.php | 5 +++ app/PaymentDrivers/BaseDriver.php | 3 ++ app/Services/Payment/PaymentService.php | 40 +++++++++++++++++++ 4 files changed, 53 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 86b41ec319..a0d6d494da 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -108,6 +108,8 @@ class PaymentController extends Controller $settings = auth()->user()->client->getMergedSettings(); + //nlog($settings); + /* This loop checks for under / over payments and returns the user if a check fails */ foreach($payable_invoices as $payable_invoice) @@ -220,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(); @@ -255,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(); @@ -289,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/Models/PaymentHash.php b/app/Models/PaymentHash.php index 264884b429..2c1a9b0ed7 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 983b3f5c76..8366c4f5f6 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/Services/Payment/PaymentService.php b/app/Services/Payment/PaymentService.php index 954a904b68..c0c4f0acbf 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(); From 65afd70b3860307027d36366198f7f5e24968950 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 19:50:13 +1100 Subject: [PATCH 5/6] Always ensure the client has a currency set --- app/Http/Controllers/ClientPortal/PaymentController.php | 2 +- app/Http/Requests/Client/UpdateClientRequest.php | 5 +++++ app/Http/Requests/Company/UpdateCompanyRequest.php | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index a0d6d494da..b5f0b9c9d3 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -108,7 +108,7 @@ class PaymentController extends Controller $settings = auth()->user()->client->getMergedSettings(); - //nlog($settings); + nlog($settings); /* This loop checks for under / over payments and returns the user if a check fails */ diff --git a/app/Http/Requests/Client/UpdateClientRequest.php b/app/Http/Requests/Client/UpdateClientRequest.php index 6e7919b7e0..770d909143 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 505711533c..cca0737248 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']); } From 04d8bcccb185dcdceb86f7697fd694e48364aa80 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 6 Jan 2021 20:28:48 +1100 Subject: [PATCH 6/6] Fix for error handling in PayPal driver --- app/Http/Controllers/ClientPortal/PaymentController.php | 2 +- app/PaymentDrivers/PayPalExpressPaymentDriver.php | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index b5f0b9c9d3..ecf65659aa 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -108,7 +108,7 @@ class PaymentController extends Controller $settings = auth()->user()->client->getMergedSettings(); - nlog($settings); + // nlog($settings); /* This loop checks for under / over payments and returns the user if a check fails */ diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 59b972e307..085797d2a0 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, ];