From ccd6a11c8ab960783509ed3f28d50d76719296b3 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 29 Jul 2021 13:18:39 +1000 Subject: [PATCH] Missed PaymentsImport in previous commit --- app/Jobs/PaymentsImport.php | 101 ++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 app/Jobs/PaymentsImport.php diff --git a/app/Jobs/PaymentsImport.php b/app/Jobs/PaymentsImport.php new file mode 100644 index 0000000..4c38070 --- /dev/null +++ b/app/Jobs/PaymentsImport.php @@ -0,0 +1,101 @@ +o = $o; + } + + public function handle() + { + Log::info(sprintf('%s:Importing Payment Date from [%s]',self::LOGKEY,get_class($this->o))); + + // Get our checkout IDs for this plugin + $cos = Checkout::where('plugin',config('services.ezypay.plugin'))->pluck('id'); + + foreach ($this->o->getCustomers() as $c) { + if ($c->BillingStatus == 'Inactive') { + Log::debug(sprintf('%s:Ignoring INACTIVE: [%s] %s %s',self::LOGKEY,$c->EzypayReferenceNumber,$c->Firstname,$c->Surname)); + + continue; + } + + // Load Account Details from ReferenceId + $ao = Account::where('site_id',(int)substr($c->ReferenceId,0,2)) + ->where('id',(int)substr($c->ReferenceId,2,4)) + ->first(); + + if (! $ao) { + Log::error(sprintf('%s:Missing: [%s] %s %s (%s)',self::LOGKEY,$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId)); + continue; + } + + // Find the last payment logged + $last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ao->id)->max('payment_date')); + + $o = $this->o->getDebits([ + 'customerId'=>$c->Id, + 'dateFrom'=>$last->format('Y-m-d'), + 'dateTo'=>$last->addQuarter()->format('Y-m-d'), + 'pageSize'=>100, + ]); + + // Load the payments + if ($o->count()) { + foreach ($o->reverse() as $p) { + $pd = Carbon::createFromTimeString($p->Date); + + // If not success, ignore it. + if ($p->Status != 'Success') { + Log::alert(sprintf('%s:Payment not successful: [%s] %s %s (%s) [%s]',self::LOGKEY,$pd->format('Y-m-d'),$ao->id,$p->Id,$p->Amount,$p->Status)); + continue; + } + + $lp = $ao->payments->last(); + + if ($lp AND (($pd == $lp->payment_date) OR ($p->Id == $lp->checkout_data))) { + Log::alert(sprintf('%s:Payment Already Recorded: [%s] %s %s (%s)',self::LOGKEY,$pd->format('Y-m-d'),$ao->id,$p->Id,$p->Amount)); + continue; + } + + // New Payment + $po = new Payment; + $po->site_id = 1; // @todo + $po->payment_date = $pd; + $po->checkout_id = '999'; // @todo + $po->checkout_data = $p->Id; + $po->total_amt = $p->Amount; + $ao->payments()->save($po); + + Log::info(sprintf('%s:Recorded: Payment for [%s] %s %s (%s) on %s',self::LOGKEY,$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id,$pd)); + } + } + } + } +} \ No newline at end of file