From e342c02063f2cdd7e73c4862c608a674fb707216 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Sep 2020 07:13:00 +1000 Subject: [PATCH 1/6] fixes for quote activities --- app/Listeners/Activity/QuoteUpdatedActivity.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/app/Listeners/Activity/QuoteUpdatedActivity.php b/app/Listeners/Activity/QuoteUpdatedActivity.php index 9f3e4025a..418e5bc53 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); - // } } } From 2bd858159227da44263df6405655ed985b11e7f2 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Sep 2020 09:28:37 +1000 Subject: [PATCH 2/6] Fixes for payments with gateway fees --- app/Models/Payment.php | 1 + app/PaymentDrivers/BaseDriver.php | 17 +++++--------- app/PaymentDrivers/BasePaymentDriver.php | 22 ++++++++----------- app/PaymentDrivers/Stripe/CreditCard.php | 10 ++++++++- app/Services/Payment/UpdateInvoicePayment.php | 22 ++++++++++--------- resources/views/setup/index.blade.php | 4 ++-- 6 files changed, 39 insertions(+), 37 deletions(-) diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 38353eddb..76b1641db 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/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index f95b97e9c..8572ed559 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 81fd86fe9..fb499b6b1 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 b00552e87..48ae2cc50 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -169,6 +169,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 +195,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/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 914a277ea..ba5fa7ea8 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/resources/views/setup/index.blade.php b/resources/views/setup/index.blade.php index 04f410757..39c61bcdf 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']}}

From 3d8ddb23cf6448da504e377eef3f6fa05828e462 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Sep 2020 12:32:36 +1000 Subject: [PATCH 3/6] fix for types --- app/Services/Invoice/InvoiceService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index a3d729a51..2bb79b2d4 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,7 +208,7 @@ 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(); From 455e9a8e1d72303c82a75f0ace988c038db45a92 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 2 Sep 2020 08:11:59 +1000 Subject: [PATCH 4/6] fix for company gateways if properties not set --- .../StoreCompanyGatewayRequest.php | 18 ++++++++++++++++++ .../UpdateCompanyGatewayRequest.php | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php index 47513d4f4..6d138eba2 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 @@ -43,6 +44,22 @@ class StoreCompanyGatewayRequest extends Request { $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'])) + { + 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 cf7107b80..15c3e5e5a 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']) && $input['gateway_key']) + { + $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'])) { From 2f478158e9ba8eae15dbf432a7a4e0d750429065 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 2 Sep 2020 11:11:01 +1000 Subject: [PATCH 5/6] Fixes for company gateways --- app/Http/Controllers/InvoiceController.php | 4 +++ .../StoreCompanyGatewayRequest.php | 4 +-- .../UpdateCompanyGatewayRequest.php | 2 +- app/Services/Invoice/InvoiceService.php | 27 +++++++++++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index ae280aedd..2e5aedba9 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 6d138eba2..050bcd6a9 100644 --- a/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/StoreCompanyGatewayRequest.php @@ -43,12 +43,12 @@ 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'])) + if(isset($input['config']) && is_object(json_decode($input['config']))) { foreach(json_decode($input['config']) as $key => $value) { diff --git a/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php index 15c3e5e5a..4c9974658 100644 --- a/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php +++ b/app/Http/Requests/CompanyGateway/UpdateCompanyGatewayRequest.php @@ -46,7 +46,7 @@ class UpdateCompanyGatewayRequest extends Request $input = $this->all(); /*Force gateway properties */ - if(isset($input['config']) && $input['gateway_key']) + 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); diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 2bb79b2d4..40471e2e2 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -215,6 +215,7 @@ class InvoiceService 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; + } + /** From a8bbb7035fa89ff41d2ecc207bbe9b571facc162 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 2 Sep 2020 19:36:32 +1000 Subject: [PATCH 6/6] Fixes for random data seeder --- .../Authorize/AuthorizeCreditCard.php | 14 ++++++++++++-- .../Authorize/AuthorizePaymentMethod.php | 2 +- app/PaymentDrivers/Stripe/CreditCard.php | 1 + database/seeds/RandomDataSeeder.php | 11 ++++++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index ffaee62e7..4c2997ae6 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 96d072994..c6dc4ef9c 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/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 48ae2cc50..d726dcf1b 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(); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index c6ec55d47..79affea2b 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); }