1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #4639 from turbo124/v5-develop

Fixes for error handling in PayPal driver.
This commit is contained in:
David Bomba 2021-01-06 20:29:17 +11:00 committed by GitHub
commit 278117bebf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 113 additions and 49 deletions

View File

@ -1 +1 @@
5.0.43
5.0.44

View File

@ -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)]);
}

View File

@ -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)) {

View File

@ -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']);
}

View File

@ -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();

View File

@ -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()));

View File

@ -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,
];

View File

@ -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();

View File

@ -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);

View File

@ -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),

View File

@ -3362,4 +3362,5 @@ return [
'currency_albanian_lek' => 'Albanian Lek',
'endless' => 'Endless',
'minimum_payment' => 'Minimum Payment',
];