Import payment methods for checkout

This commit is contained in:
David Bomba 2024-03-18 13:50:12 +11:00
parent 86fc7a0662
commit 37e2e772f9
3 changed files with 140 additions and 41 deletions

View File

@ -11,28 +11,29 @@
namespace App\Http\Controllers;
use App\Models\Client;
use App\Libraries\MultiDB;
use Illuminate\Http\Response;
use App\Models\CompanyGateway;
use App\Utils\Traits\MakesHash;
use App\DataMapper\FeesAndLimits;
use App\Jobs\Util\ApplePayDomain;
use Illuminate\Support\Facades\Cache;
use App\Factory\CompanyGatewayFactory;
use App\Filters\CompanyGatewayFilters;
use App\Repositories\CompanyRepository;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Transformers\CompanyGatewayTransformer;
use App\PaymentDrivers\Stripe\Jobs\StripeWebhook;
use App\PaymentDrivers\CheckoutCom\CheckoutSetupWebhook;
use App\Http\Requests\CompanyGateway\BulkCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\CreateCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\DestroyCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\EditCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\ShowCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\StoreCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\TestCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\StoreCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\CreateCompanyGatewayRequest;
use App\Http\Requests\CompanyGateway\UpdateCompanyGatewayRequest;
use App\Jobs\Util\ApplePayDomain;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\CompanyGateway;
use App\PaymentDrivers\CheckoutCom\CheckoutSetupWebhook;
use App\PaymentDrivers\Stripe\Jobs\StripeWebhook;
use App\Repositories\CompanyRepository;
use App\Transformers\CompanyGatewayTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Response;
use App\Http\Requests\CompanyGateway\DestroyCompanyGatewayRequest;
/**
* Class CompanyGatewayController.
@ -547,12 +548,17 @@ class CompanyGatewayController extends BaseController
public function importCustomers(TestCompanyGatewayRequest $request, CompanyGateway $company_gateway)
{
//Throttle here
// if (Cache::get("throttle_polling:import_customers:{$company_gateway->company->company_key}:{$company_gateway->hashed_id}"))
// return response()->json(['message' => ctrans('texts.import_started')], 200);
dispatch(function () use($company_gateway) {
MultiDB::setDb($company_gateway->company->db);
$company_gateway->driver()->importCustomers();
})->afterResponse();
Cache::put("throttle_polling:import_customers:{$company_gateway->company->company_key}:{$company_gateway->hashed_id}", true, 300);
return response()->json(['message' => ctrans('texts.import_started')], 200);
}

View File

@ -23,6 +23,7 @@ use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\ClientContact;
use App\Factory\ClientFactory;
use Illuminate\Support\Carbon;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Factory\ClientContactFactory;
@ -343,7 +344,17 @@ class BraintreePaymentDriver extends BaseDriver
->exists();
}
private function findContact(string $email) {
private function getToken(string $token, string $gateway_customer_reference)
{
return ClientGatewayToken::where('company_id', $this->company_gateway->company_id)
->where('gateway_customer_reference', $gateway_customer_reference)
->where('token', $token)
->first();
}
private function findClient(string $email) {
return ClientContact::where('company_id', $this->company_gateway->company_id)
->where('email', $email)
->first()->client ?? false;
@ -356,6 +367,9 @@ class BraintreePaymentDriver extends BaseDriver
foreach($cards as $card) {
if($this->getToken($card->token, $card->customerId) || Carbon::createFromDate($card->expirationYear, $card->expirationMonth, '1')->lt(now()))
continue;
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $card->expirationMonth;
$payment_meta->exp_year = (string) $card->expirationYear;
@ -468,19 +482,22 @@ class BraintreePaymentDriver extends BaseDriver
$customer = $this->find($c->id);
// nlog(count($customer->creditCards). " Exists for {$c->id}");
if(!$customer)
continue;
if(!$this->findTokens($c->id) && !($client = $this->findContact($customer->email))) {
$client = $this->findClient($customer->email);
if(!$this->findTokens($c->id) && !$client) {
//customer is not referenced in the system - create client
$client = $this->createNinjaClient($customer);
nlog("Creating new Client");
// nlog("Creating new Client");
}
$this->addClientCards($client, $customer->creditCards);
nlog("Adding Braintree Client: {$c->id} => {$client->id}");
// nlog("Adding Braintree Client: {$c->id} => {$client->id}");
}
}

View File

@ -12,37 +12,38 @@
namespace App\PaymentDrivers;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Http\Requests\Gateways\Checkout3ds\Checkout3dsRequest;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use Exception;
use App\Models\Company;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\SystemLog;
use Checkout\CheckoutSdk;
use Checkout\Environment;
use Checkout\Common\Phone;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\CheckoutCom\CheckoutWebhook;
use App\PaymentDrivers\CheckoutCom\CreditCard;
use App\PaymentDrivers\CheckoutCom\Utilities;
use App\Utils\Traits\SystemLogTrait;
use Illuminate\Support\Carbon;
use App\Jobs\Util\SystemLogger;
use App\Exceptions\PaymentFailed;
use App\Models\ClientGatewayToken;
use Checkout\CheckoutApiException;
use App\Utils\Traits\SystemLogTrait;
use Checkout\Payments\RefundRequest;
use Illuminate\Support\Facades\Auth;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\CheckoutSdk;
use Checkout\Common\Phone;
use Checkout\Customers\CustomerRequest;
use Checkout\Environment;
use Checkout\CheckoutAuthorizationException;
use App\PaymentDrivers\CheckoutCom\Utilities;
use Checkout\Payments\Request\PaymentRequest;
use App\PaymentDrivers\CheckoutCom\CreditCard;
use App\PaymentDrivers\CheckoutCom\CheckoutWebhook;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use Checkout\Payments\Request\Source\RequestIdSource;
use App\Http\Requests\Gateways\Checkout3ds\Checkout3dsRequest;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use Checkout\Payments\Previous\PaymentRequest as PreviousPaymentRequest;
use Checkout\Payments\Previous\Source\RequestIdSource as SourceRequestIdSource;
use Checkout\Payments\RefundRequest;
use Checkout\Payments\Request\PaymentRequest;
use Checkout\Payments\Request\Source\RequestIdSource;
use Exception;
use Illuminate\Support\Facades\Auth;
class CheckoutComPaymentDriver extends BaseDriver
{
@ -546,4 +547,79 @@ class CheckoutComPaymentDriver extends BaseDriver
}
return false;
}
private function getToken(string $token, $gateway_customer_reference)
{
return ClientGatewayToken::query()
->where('company_id', $this->company_gateway->company_id)
->where('gateway_customer_reference', $gateway_customer_reference)
->where('token', $token)
->first();
}
public function importCustomers()
{
$this->init();
$this->company_gateway
->company
->clients()
->cursor()
->each(function ($client){
nlog("1");
if(!str_contains($client->present()->email(), "@"))
return;
nlog("2");
nlog($client->present()->email());
try{
$customer = $this->gateway->getCustomersClient()->get($client->present()->email());
}
catch(\Exception $e) {
nlog("returning due to exception");
return;
}
nlog("3");
$this->client = $client;
nlog($customer['instruments']);
foreach($customer['instruments'] as $card)
{
if(
$card['type'] != 'card' ||
Carbon::createFromDate($card['expiry_year'], $card['expiry_month'], '1')->lt(now()) ||
$this->getToken($card['id'], $customer['id'])
)
continue;
nlog("4");
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $card['expiry_month'];
$payment_meta->exp_year = (string) $card['expiry_year'];
$payment_meta->brand = (string) $card['scheme'];
$payment_meta->last4 = (string) $card['last4'];
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $card['id'],
'payment_method_id' => GatewayType::CREDIT_CARD,
];
$this->storeGatewayToken($data, ['gateway_customer_reference' => $customer['id']]);
}
});
}
}