2019-06-07 06:54:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2020-02-01 11:12:31 +00:00
|
|
|
use App\Classes\External\Payments\Ezypay;
|
2019-06-11 02:36:58 +00:00
|
|
|
use App\Models\{Account,Checkout,Payment};
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2020-02-01 11:12:31 +00:00
|
|
|
class PaymentsEzypayImport extends Command
|
2019-06-07 06:54:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-02-01 11:12:31 +00:00
|
|
|
protected $signature = 'payments:ezypay:import';
|
2019-06-07 06:54:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Retrieve payments from Ezypay';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$poo = new Ezypay();
|
|
|
|
|
|
|
|
// Get our checkout IDs for this plugin
|
|
|
|
$cos = Checkout::where('plugin',config('services.ezypay.plugin'))->pluck('id');
|
|
|
|
|
|
|
|
foreach ($poo->getCustomers() as $c)
|
|
|
|
{
|
|
|
|
if ($c->BillingStatus == 'Inactive')
|
|
|
|
{
|
|
|
|
$this->info(sprintf('Ignoring INACTIVE: [%s] %s %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:34:41 +00:00
|
|
|
// 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();
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2019-06-07 12:34:41 +00:00
|
|
|
if (! $ao)
|
2019-06-07 06:54:27 +00:00
|
|
|
{
|
2019-06-07 12:34:41 +00:00
|
|
|
$this->warn(sprintf('Missing: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$c->ReferenceId));
|
2019-06-07 06:54:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the last payment logged
|
2021-07-23 07:25:26 +00:00
|
|
|
$last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ao->id)->max('payment_date'));
|
2019-06-07 06:54:27 +00:00
|
|
|
|
|
|
|
$o = $poo->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)
|
|
|
|
{
|
|
|
|
// If not success, ignore it.
|
2019-06-07 12:34:41 +00:00
|
|
|
if ($p->Status != 'Success')
|
|
|
|
continue;
|
2019-06-07 06:54:27 +00:00
|
|
|
|
|
|
|
$pd = Carbon::createFromFormat('Y-m-d?H:i:s.u',$p->Date);
|
2019-06-07 12:34:41 +00:00
|
|
|
$lp = $ao->payments->last();
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2021-07-23 07:25:26 +00:00
|
|
|
if ($lp AND (($pd == $lp->payment_date) OR ($p->Id == $lp->checkout_data)))
|
2019-06-07 06:54:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// New Payment
|
|
|
|
$po = new Payment;
|
|
|
|
$po->site_id = 1; // @todo
|
2021-07-23 07:25:26 +00:00
|
|
|
$po->payment_date = $pd;
|
2019-06-07 06:54:27 +00:00
|
|
|
$po->checkout_id = '999'; // @todo
|
|
|
|
$po->checkout_data = $p->Id;
|
|
|
|
$po->total_amt = $p->Amount;
|
2019-06-07 12:34:41 +00:00
|
|
|
$ao->payments()->save($po);
|
2019-06-07 06:54:27 +00:00
|
|
|
|
2019-06-07 12:34:41 +00:00
|
|
|
$this->info(sprintf('Recorded: Payment for [%s] %s %s (%s) on %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id,$pd));
|
2019-06-07 06:54:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 13:47:37 +00:00
|
|
|
}
|