116 lines
2.6 KiB
PHP
116 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
use App\Classes\Payments\Ezypay;
|
|
use App\Models\{Account,AccoutBilling,Checkout,Payment};
|
|
|
|
class EzypayImport extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'payments:ezypay';
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
/*
|
|
DB::listen(function($query) {
|
|
$this->warn('- SQL:'.json_encode(['sql'=>$query->sql,'binding'=>$query->bindings]));
|
|
});
|
|
*/
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
// Load Direct Debit Details
|
|
$ab = AccoutBilling::whereIN('checkout_id',$cos)->where('checkout_data',$c->EzypayReferenceNumber)->first();
|
|
|
|
if (! $ab)
|
|
{
|
|
$this->warn(sprintf('Missing: [%s] %s %s',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname));
|
|
continue;
|
|
}
|
|
|
|
// Find the last payment logged
|
|
$last = Carbon::createFromTimestamp(Payment::whereIN('checkout_id',$cos)->where('account_id',$ab->service->account_id)->max('date_payment'));
|
|
|
|
$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.
|
|
if (! $p->Status == 'Success')
|
|
continue;
|
|
|
|
$pd = Carbon::createFromFormat('Y-m-d?H:i:s.u',$p->Date);
|
|
$lp = $ab->service->account->payments->last();
|
|
|
|
if ($lp AND (($pd == $lp->date_payment) OR ($p->Id == $lp->checkout_data)))
|
|
continue;
|
|
|
|
// New Payment
|
|
$po = new Payment;
|
|
$po->site_id = 1; // @todo
|
|
$po->date_payment = $pd;
|
|
$po->checkout_id = '999'; // @todo
|
|
$po->checkout_data = $p->Id;
|
|
$po->total_amt = $p->Amount;
|
|
$ab->service->account->payments()->save($po);
|
|
|
|
$this->info(sprintf('Recorded: [%s] %s %s (%s)',$c->EzypayReferenceNumber,$c->Firstname,$c->Surname,$po->id));
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|