1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/Ninja/CheckACHStatus.php

128 lines
3.4 KiB
PHP
Raw Normal View History

2023-09-11 04:01:05 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ninja;
use App\Libraries\MultiDB;
use App\Models\ClientGatewayToken;
2023-10-26 04:57:44 +02:00
use App\Models\Payment;
use Illuminate\Bus\Queueable;
2023-09-11 04:01:05 +02:00
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2023-09-11 04:01:05 +02:00
class CheckACHStatus implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
nlog("Checking ACH status");
ClientGatewayToken::query()
->where('created_at', '>', now()->subMonths(2))
->where('gateway_type_id', 2)
->whereHas('gateway', function ($q) {
$q->whereIn('gateway_key', ['d14dd26a37cecc30fdd65700bfb55b23','d14dd26a47cecc30fdd65700bfb67b34']);
})
->whereJsonContains('meta', ['state' => 'unauthorized'])
->cursor()
->each(function ($token) {
try {
$stripe = $token->gateway->driver($token->client)->init();
$pm = $stripe->getStripePaymentMethod($token->token);
if($pm) {
$meta = $token->meta;
$meta->state = 'authorized';
$token->meta = $meta;
$token->save();
}
} catch (\Exception $e) {
}
});
2023-10-10 03:20:16 +02:00
Payment::where('status_id', 1)
2023-10-26 04:57:44 +02:00
->whereHas('company_gateway', function ($q) {
2023-10-10 03:20:16 +02:00
$q->whereIn('gateway_key', ['d14dd26a47cecc30fdd65700bfb67b34', 'd14dd26a37cecc30fdd65700bfb55b23']);
})
->cursor()
->each(function ($p) {
try {
$stripe = $p->company_gateway->driver($p->client)->init();
} catch(\Exception $e) {
return;
}
$pi = false;
try {
$pi = $stripe->getPaymentIntent($p->transaction_reference);
} catch(\Exception $e) {
}
if(!$pi) {
try {
$pi = \Stripe\Charge::retrieve($p->transaction_reference, $stripe->stripe_connect_auth);
} catch(\Exception $e) {
return;
}
}
if($pi && $pi->status == 'succeeded') {
$p->status_id = Payment::STATUS_COMPLETED;
$p->saveQuietly();
} else {
if($pi) {
nlog("{$p->id} did not complete {$p->transaction_reference}");
} else {
nlog("did not find a payment intent {$p->transaction_reference}");
}
}
});
2023-09-11 04:01:05 +02:00
}
}
2023-10-26 04:57:44 +02:00
}