diff --git a/app/Http/Requests/Payment/StorePaymentRequest.php b/app/Http/Requests/Payment/StorePaymentRequest.php index 9f0ba912bd5f..2deffed782ec 100644 --- a/app/Http/Requests/Payment/StorePaymentRequest.php +++ b/app/Http/Requests/Payment/StorePaymentRequest.php @@ -20,6 +20,7 @@ use App\Http\ValidationRules\ValidCreditsPresentRule; use App\Http\ValidationRules\ValidPayableInvoicesRule; use App\Models\Payment; use App\Utils\Traits\MakesHash; +use Illuminate\Validation\Rule; class StorePaymentRequest extends Request { @@ -100,7 +101,8 @@ class StorePaymentRequest extends Request 'credits.*.credit_id' => new ValidCreditsRules($this->all()), 'credits.*.amount' => ['required', new CreditsSumRule($this->all())], 'invoices' => new ValidPayableInvoicesRule(), - 'number' => 'bail|nullable|unique:payments,number,'.$this->id.',id,company_id,'.$this->company_id, + 'number' => ['nullable', Rule::unique('payments')->where('company_id', auth()->user()->company()->id)], + ]; if ($this->input('documents') && is_array($this->input('documents'))) { diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 438fb6f85e03..b63f7a43e4d9 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -17,6 +17,8 @@ use App\Models\CompanyGateway; use App\Models\Invoice; use App\Models\Payment; use App\Services\AbstractService; +use App\Utils\Ninja; +use Illuminate\Support\Facades\App; class AddGatewayFee extends AbstractService { @@ -72,6 +74,10 @@ class AddGatewayFee extends AbstractService private function processGatewayFee($gateway_fee) { + App::forgetInstance('translator'); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->invoice->company->settings)); + $invoice_item = new InvoiceItem; $invoice_item->type_id = '3'; $invoice_item->product_key = ctrans('texts.surcharge'); @@ -98,6 +104,10 @@ class AddGatewayFee extends AbstractService private function processGatewayDiscount($gateway_fee) { + App::forgetInstance('translator'); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->invoice->company->settings)); + $invoice_item = new InvoiceItem; $invoice_item->type_id = '3'; $invoice_item->product_key = ctrans('texts.discount'); diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 1fe76bf7bfe8..dd926406300f 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -1480,5 +1480,49 @@ class PaymentTest extends TestCase $this->assertEquals(10, $this->invoice->fresh()->balance); $this->assertEquals(10, $this->invoice->fresh()->balance); } + + + public function testUniquePaymentNumbers() + { + $data = [ + 'amount' => $this->invoice->amount, + 'client_id' => $this->client->hashed_id, + 'date' => '2020/12/12', + 'number' => 'duplicate', + ]; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($message); + } + + $arr = $response->json(); + + $response->assertStatus(200); + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/payments', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($message); + } + + if($response) + $response->assertStatus(302); + + + + + } }