diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index ae280aedd205..2e5aedba901a 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -717,6 +717,10 @@ class InvoiceController extends BaseController else $this->reminder_template = $invoice->calculateTemplate(); + //touch reminder1,2,3_sent + last_sent here if the email is a reminder. + + $invoice->service()->touchReminder($this->reminder_template)->save(); + $invoice->invitations->load('contact.client.country','invoice.client.country','invoice.company')->each(function ($invitation) use ($invoice) { $email_builder = (new InvoiceEmail())->build($invitation, $this->reminder_template); diff --git a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php index 47513d4f40f3..050bcd6a9de9 100644 --- a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php @@ -13,6 +13,7 @@ namespace App\Http\Requests\CompanyGateway; use App\Http\Requests\Request; use App\Http\ValidationRules\ValidCompanyGatewayFeesAndLimitsRule; +use App\Models\Gateway; use App\Utils\Traits\CompanyGatewayFeesAndLimitsSaver; class StoreCompanyGatewayRequest extends Request @@ -42,6 +43,22 @@ class StoreCompanyGatewayRequest extends Request protected function prepareForValidation() { $input = $this->all(); + $gateway = Gateway::where('key', $input['gateway_key'])->first(); + + $default_gateway_fields = json_decode($gateway->fields); + + /*Force gateway properties */ + if(isset($input['config']) && is_object(json_decode($input['config']))) + { + foreach(json_decode($input['config']) as $key => $value) { + + $default_gateway_fields->{$key} = $value; + + } + + $input['config'] = json_encode($default_gateway_fields); + } + if (isset($input['config'])) { $input['config'] = encrypt($input['config']); @@ -51,6 +68,7 @@ class StoreCompanyGatewayRequest extends Request $input['fees_and_limits'] = $this->cleanFeesAndLimits($input['fees_and_limits']); } + $this->replace($input); } } diff --git a/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php index cf7107b80ced..4c9974658312 100644 --- a/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php @@ -14,6 +14,7 @@ namespace App\Http\Requests\CompanyGateway; use App\Http\Requests\Request; use App\Http\ValidationRules\ValidCompanyGatewayFeesAndLimitsRule; use App\Models\Company; +use App\Models\Gateway; use App\Utils\Traits\CompanyGatewayFeesAndLimitsSaver; class UpdateCompanyGatewayRequest extends Request @@ -44,6 +45,21 @@ class UpdateCompanyGatewayRequest extends Request { $input = $this->all(); + /*Force gateway properties */ + if(isset($input['config']) && is_object(json_decode($input['config'])) && array_key_exists('gateway_key', $input)) + { + $gateway = Gateway::where('key', $input['gateway_key'])->first(); + $default_gateway_fields = json_decode($gateway->fields); + + foreach(json_decode($input['config']) as $key => $value) { + + $default_gateway_fields->{$key} = $value; + + } + + $input['config'] = json_encode($default_gateway_fields); + } + $input['config'] = encrypt($input['config']); if (isset($input['fees_and_limits'])) { diff --git a/app/Listeners/Activity/QuoteUpdatedActivity.php b/app/Listeners/Activity/QuoteUpdatedActivity.php index 9f3e4025aa33..418e5bc534b0 100644 --- a/app/Listeners/Activity/QuoteUpdatedActivity.php +++ b/app/Listeners/Activity/QuoteUpdatedActivity.php @@ -44,28 +44,17 @@ class QuoteUpdatedActivity implements ShouldQueue MultiDB::setDb($event->company->db); $quote = $event->quote; - - $invoices = $payment->invoices; $fields = new \stdClass; - $fields->payment_id = $quote->id; - $fields->client_id = $quote->client_id; - $fields->user_id = $quote->user_id; + $fields->quote_id = $quote->id; + $fields->client_id = $quote->client_id; + $fields->user_id = $quote->user_id; $fields->company_id = $quote->company_id; $fields->activity_type_id = Activity::UPDATE_QUOTE; $this->activity_repo->save($fields, $quote, $event->event_vars); - // foreach ($invoices as $invoice) { - // //todo we may need to add additional logic if in the future we apply payments to other entity Types, not just invoices - // $fields->invoice_id = $invoice->id; - // $this->activity_repo->save($fields, $invoice, $event->event_vars); - // } - - // if (count($invoices) == 0) { - // $this->activity_repo->save($fields, $payment, $event->event_vars); - // } } } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 38353eddb229..76b1641db904 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -76,6 +76,7 @@ class Payment extends BaseModel 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', 'is_deleted' => 'bool', + 'meta' => 'object', ]; protected $with = [ diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index ffaee62e700e..4c2997ae6bd2 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php @@ -18,6 +18,7 @@ use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Payment; +use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; use App\PaymentDrivers\AuthorizePaymentDriver; @@ -71,14 +72,14 @@ class AuthorizeCreditCard $gateway_customer_reference = $authorise_create_customer->create($data); - info($gateway_customer_reference); + //info($gateway_customer_reference); $authorise_payment_method = new AuthorizePaymentMethod($this->authorize); $payment_profile = $authorise_payment_method->addPaymentMethodToClient($gateway_customer_reference, $data); $payment_profile_id = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId(); - info($request->input('store_card')); + //info($request->input('store_card')); if($request->has('store_card') && $request->input('store_card') === 'true'){ $authorise_payment_method->payment_method = GatewayType::CREDIT_CARD; @@ -162,8 +163,17 @@ class AuthorizeCreditCard private function processSuccessfulResponse($data, $request) { + $payment_hash = PaymentHash::whereRaw("BINARY `hash`= ?", [$request->input('payment_hash')])->firstOrFail(); + + + + + $payment = $this->createPaymentRecord($data, $request->input('amount_with_fee')); + + + $this->authorize->attachInvoices($payment, $request->hashed_ids); $payment->service()->updateInvoicePayment(); diff --git a/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php b/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php index 96d072994941..c6dc4ef9ca3c 100644 --- a/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php +++ b/app/PaymentDrivers/Authorize/AuthorizePaymentMethod.php @@ -122,7 +122,7 @@ class AuthorizePaymentMethod public function createClientGatewayToken($payment_profile, $gateway_customer_reference) { - info(print_r($payment_profile,1)); + // info(print_r($payment_profile,1)); $client_gateway_token = new ClientGatewayToken(); $client_gateway_token->company_id = $this->authorize->client->company_id; diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index f95b97e9c449..8572ed559345 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -20,6 +20,7 @@ use App\Models\ClientGatewayToken; use App\Models\CompanyGateway; use App\Models\Invoice; use App\Models\Payment; +use App\Models\PaymentHash; use App\PaymentDrivers\AbstractPaymentDriver; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; @@ -110,25 +111,19 @@ class BaseDriver extends AbstractPaymentDriver * @param array $hashed_ids The array of invoice hashed_ids * @return Payment The payment object */ - public function attachInvoices(Payment $payment, $hashed_ids): Payment + + public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment { - $transformed = $this->transformKeys($hashed_ids); - $array = is_array($transformed) ? $transformed : [$transformed]; - - $invoices = Invoice::whereIn('id', $array) - ->whereClientId($this->client->id) - ->get(); + $paid_invoices = $payment_hash->invoices(); + $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get(); $payment->invoices()->sync($invoices); - $payment->save(); - - $payment->service()->applyNumber()->save(); $invoices->each(function ($invoice) use($payment){ event(new InvoiceWasPaid($invoice, $payment->company, Ninja::eventVars())); }); - return $payment; + return $payment->service()->applyNumber()->save(); } /** diff --git a/app/PaymentDrivers/BasePaymentDriver.php b/app/PaymentDrivers/BasePaymentDriver.php index 81fd86fe97b6..fb499b6b1659 100644 --- a/app/PaymentDrivers/BasePaymentDriver.php +++ b/app/PaymentDrivers/BasePaymentDriver.php @@ -20,6 +20,7 @@ use App\Models\CompanyGateway; use App\Models\GatewayType; use App\Models\Invoice; use App\Models\Payment; +use App\Models\PaymentHash; use App\Utils\Traits\MakesHash; use App\Utils\Traits\SystemLogTrait; use Illuminate\Support\Carbon; @@ -241,14 +242,13 @@ class BasePaymentDriver { $this->gateway(); - $response = $this->gateway - ->purchase($data) - ->setItems($items) - ->send(); + $response = $this->gateway + ->purchase($data) + ->setItems($items) + ->send(); return $response; - /* - $this->purchaseResponse = (array)$response->getData();*/ + } public function completePurchase($data) @@ -273,15 +273,11 @@ class BasePaymentDriver } - public function attachInvoices(Payment $payment, $hashed_ids): Payment + public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment { - $transformed = $this->transformKeys($hashed_ids); - $array = is_array($transformed) ? $transformed : [$transformed]; - - $invoices = Invoice::whereIn('id', $array) - ->whereClientId($this->client->id) - ->get(); + $paid_invoices = $payment_hash->invoices(); + $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get(); $payment->invoices()->sync($invoices); $payment->save(); diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index b00552e87a30..d726dcf1b774 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -126,6 +126,7 @@ class CreditCard 'payment_hash' => $payment_hash, ]; + /*Hydrate the invoices from the payment hash*/ $invoices = Invoice::whereIn('id', $this->stripe->transformKeys(array_column($payment_hash->invoices(), 'invoice_id'))) ->whereClientId($this->stripe->client->id) ->get(); @@ -169,6 +170,13 @@ class CreditCard 'type' => $payment_method_object['type'], ]; + $payment_meta = new \stdClass; + $payment_meta->exp_month = $payment_method_object['card']['exp_month']; + $payment_meta->exp_year = $payment_method_object['card']['exp_year']; + $payment_meta->brand = $payment_method_object['card']['brand']; + $payment_meta->last4 = $payment_method_object['card']['last4']; + $payment_meta->type = $payment_method_object['type']; + $payment_type = PaymentType::parseCardType($payment_method_object['card']['brand']); if ($state['save_card'] == true) { @@ -188,8 +196,9 @@ class CreditCard ]; $payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED); + $payment->meta = $payment_meta; - $this->stripe->attachInvoices($payment, $state['hashed_ids']); + $payment = $this->stripe->attachInvoices($payment, $state['payment_hash']); $payment->service()->updateInvoicePayment($state['payment_hash']); diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index a3d729a51b18..40471e2e26d2 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -192,8 +192,8 @@ class InvoiceService $this->invoice->line_items = collect($this->invoice->line_items) ->map(function ($item) { - if($item->type_id == 3) - $item->type_id = 4; + if($item->type_id == '3') + $item->type_id = '4'; return $item; @@ -208,13 +208,14 @@ class InvoiceService $this->invoice->line_items = collect($this->invoice->line_items) ->reject(function ($item) { - return $item->type_id == 3; + return $item->type_id == '3'; })->toArray(); return $this; } + /*Set partial value and due date to null*/ public function clearPartial() { $this->invoice->partial = null; @@ -223,6 +224,7 @@ class InvoiceService return $this; } + /*Update the partial amount of a invoice*/ public function updatePartial($amount) { $this->invoice->partial += $amount; @@ -230,6 +232,31 @@ class InvoiceService return $this; } + /*When a reminder is sent we want to touch the dates they were sent*/ + public function touchReminder(string $reminder_template) + { + switch ($reminder_template) { + case 'reminder1': + $this->invoice->reminder1_sent = now()->format('Y-m-d'); + $this->invoice->reminder_last_sent = now()->format('Y-m-d'); + break; + case 'reminder2': + $this->invoice->reminder2_sent = now()->format('Y-m-d'); + $this->invoice->reminder_last_sent = now()->format('Y-m-d'); + break; + case 'reminder3': + $this->invoice->reminder3_sent = now()->format('Y-m-d'); + $this->invoice->reminder_last_sent = now()->format('Y-m-d'); + break; + + default: + # code... + break; + } + + return $this; + } + /** diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 914a277ea259..ba5fa7ea88fb 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -38,9 +38,6 @@ class UpdateInvoicePayment public function run() { - // $invoices = $this->payment->invoices()->get(); - // $invoices_total = $invoices->sum('balance'); - $paid_invoices = $this->payment_hash->invoices(); $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get(); @@ -66,14 +63,19 @@ class UpdateInvoicePayment ->updatePaidToDate($paid_amount) ->save(); - /*i think to interact with this correct - we need to do this form $payment->invoice()->pivot*/ - // $invoice->pivot->amount = $paid_amount; - // $invoice->pivot->save(); + $pivot_invoice = $this->payment->invoices->first(function ($inv) use($paid_invoice){ + return $inv->hashed_id == $paid_invoice->invoice_id; + }); - $invoice->service() //caution what if we amount paid was less than partial - we wipe it! - ->clearPartial() - ->updateBalance($paid_amount*-1) - ->save(); + /*update paymentable record*/ + $pivot_invoice->pivot->amount = $paid_amount; + $pivot_invoice->save(); + + + $invoice->service() //caution what if we amount paid was less than partial - we wipe it! + ->clearPartial() + ->updateBalance($paid_amount*-1) + ->save(); }); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index c6ec55d476a2..79affea2bba8 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -19,6 +19,7 @@ use App\Models\GatewayType; use App\Models\GroupSetting; use App\Models\Invoice; use App\Models\Payment; +use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\Quote; use App\Models\User; @@ -30,6 +31,7 @@ use App\Utils\Ninja; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Str; class RandomDataSeeder extends Seeder { @@ -213,9 +215,16 @@ class RandomDataSeeder extends Seeder $payment->invoices()->save($invoice); + $payment_hash = new PaymentHash; + $payment_hash->hash = Str::random(128); + $payment_hash->data = [['invoice_id' => $invoice->hashed_id, 'amount' => $invoice->balance]]; + $payment_hash->fee_total = 0; + $payment_hash->fee_invoice_id = $invoice->id; + $payment_hash->save(); + event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); - $payment->service()->updateInvoicePayment(); + $payment->service()->updateInvoicePayment($payment_hash); // UpdateInvoicePayment::dispatchNow($payment, $payment->company); } diff --git a/resources/views/setup/index.blade.php b/resources/views/setup/index.blade.php index 04f4107579a5..39c61bcdfe0d 100644 --- a/resources/views/setup/index.blade.php +++ b/resources/views/setup/index.blade.php @@ -34,13 +34,13 @@ @include('setup._issues') @else - @if(!$check['npm_status']) + @if(isset($check['npm_status']) && !$check['npm_status'])

NPM Version => {{$check['npm_status']}}

@endif - @if(!$check['node_status']) + @if(isset($check['node_status']) && !$check['node_status'])

Node Version => {{$check['node_status']}}