Merge pull request #4026 from turbo124/v2

Company gateway / Bug fixes
This commit is contained in:
David Bomba 2020-09-02 19:49:44 +10:00 committed by GitHub
commit 34d244ff8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 134 additions and 58 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -76,6 +76,7 @@ class Payment extends BaseModel
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'is_deleted' => 'bool',
'meta' => 'object',
];
protected $with = [

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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;
}
/**

View File

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

View File

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

View File

@ -34,13 +34,13 @@
@include('setup._issues')
@else
@if(!$check['npm_status'])
@if(isset($check['npm_status']) && !$check['npm_status'])
<div class="alert alert-success mt-4">
<p>NPM Version => {{$check['npm_status']}}</p>
</div>
@endif
@if(!$check['node_status'])
@if(isset($check['node_status']) && !$check['node_status'])
<div class="alert alert-success mt-4">
<p>Node Version => {{$check['node_status']}}</p>
</div>