2024-09-10 00:40:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\CBAPowerBoard;
|
|
|
|
|
|
|
|
|
2024-09-24 06:49:19 +02:00
|
|
|
use App\Models\GatewayType;
|
2024-09-10 00:40:40 +02:00
|
|
|
|
|
|
|
use App\PaymentDrivers\CBAPowerBoardPaymentDriver;
|
2024-09-24 06:49:19 +02:00
|
|
|
use App\PaymentDrivers\CBAPowerBoard\Models\Gateway;
|
|
|
|
use App\PaymentDrivers\CBAPowerBoard\Models\Gateways;
|
2024-09-10 00:40:40 +02:00
|
|
|
|
|
|
|
class Settings
|
|
|
|
{
|
|
|
|
protected const GATEWAY_CBA = 'MasterCard';
|
|
|
|
protected const GATEWAY_AFTERPAY = 'Afterpay';
|
|
|
|
protected const GATEWAY_PAYPAL = 'Paypal';
|
|
|
|
protected const GATEWAY_ZIP = 'Zipmoney';
|
|
|
|
|
|
|
|
public function __construct(public CBAPowerBoardPaymentDriver $powerboard)
|
|
|
|
{
|
|
|
|
}
|
2024-09-24 09:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the API response for the gateways
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getGateways(): mixed
|
2024-09-10 00:40:40 +02:00
|
|
|
{
|
|
|
|
$r = $this->powerboard->gatewayRequest('/v1/gateways', (\App\Enum\HttpVerb::GET)->value, [], []);
|
|
|
|
|
|
|
|
if($r->failed())
|
|
|
|
$r->throw();
|
|
|
|
|
|
|
|
return (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Gateway::class."[]", $r->object()->resource->data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-24 09:31:35 +02:00
|
|
|
/** We will need to have a process that updates this at intervals */
|
|
|
|
/**
|
|
|
|
* updateSettings from the API
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2024-09-10 00:40:40 +02:00
|
|
|
public function updateSettings():self
|
|
|
|
{
|
|
|
|
$gateways = $this->getGateways();
|
|
|
|
|
|
|
|
$settings = $this->powerboard->company_gateway->getSettings();
|
|
|
|
$settings->gateways = $gateways;
|
|
|
|
$this->powerboard->company_gateway->setSettings($settings);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2024-09-24 09:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* getSettings
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-09-10 00:40:40 +02:00
|
|
|
public function getSettings(): mixed
|
|
|
|
{
|
|
|
|
return $this->powerboard->company_gateway->getSettings();
|
|
|
|
}
|
2024-09-24 09:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entry point for getting the payment gateway configuration
|
|
|
|
*
|
|
|
|
* @param int $gateway_type_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-09-10 00:40:40 +02:00
|
|
|
public function getPaymentGatewayConfiguration(int $gateway_type_id): mixed
|
|
|
|
{
|
|
|
|
$type = self::GATEWAY_CBA;
|
|
|
|
|
|
|
|
match($gateway_type_id){
|
|
|
|
\App\Models\GatewayType::CREDIT_CARD => $type = self::GATEWAY_CBA,
|
|
|
|
default => $type = self::GATEWAY_CBA,
|
|
|
|
};
|
|
|
|
|
|
|
|
return $this->getGatewayByType($type);
|
|
|
|
}
|
2024-09-24 09:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the CBA gateway object for a given gateway type
|
|
|
|
*
|
|
|
|
* @param string $gateway_type_const
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2024-09-10 00:40:40 +02:00
|
|
|
private function getGatewayByType(string $gateway_type_const): mixed
|
|
|
|
{
|
|
|
|
$settings = $this->getSettings();
|
|
|
|
|
2024-09-24 09:20:00 +02:00
|
|
|
if(!property_exists($settings, 'gateways')){
|
2024-09-10 00:40:40 +02:00
|
|
|
$this->updateSettings();
|
|
|
|
$settings = $this->getSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
$gateways = (new \App\PaymentDrivers\CBAPowerBoard\Models\Parse())->encode(Gateway::class."[]", $settings->gateways);
|
|
|
|
|
2024-09-24 09:00:24 +02:00
|
|
|
if ($gateway_type_const == self::GATEWAY_CBA && strlen($this->powerboard->company_gateway->getConfigField('gatewayId') ?? '') > 1) {
|
2024-09-24 08:58:49 +02:00
|
|
|
|
|
|
|
return collect($gateways)->first(function (Gateway $gateway) {
|
|
|
|
return $gateway->_id == $this->powerboard->company_gateway->getConfigField('gatewayId');
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-10 00:40:40 +02:00
|
|
|
return collect($gateways)->first(function (Gateway $gateway) use ($gateway_type_const){
|
|
|
|
return $gateway->type == $gateway_type_const;
|
|
|
|
});
|
|
|
|
}
|
2024-09-24 09:31:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the CBA gateway ID for a given gateway type
|
|
|
|
*
|
|
|
|
* @param int $gateway_type_id
|
|
|
|
* @return string
|
|
|
|
*/
|
2024-09-10 00:40:40 +02:00
|
|
|
public function getGatewayId(int $gateway_type_id): string
|
|
|
|
{
|
2024-09-24 01:21:44 +02:00
|
|
|
|
2024-09-10 00:40:40 +02:00
|
|
|
$gateway = $this->getPaymentGatewayConfiguration($gateway_type_id);
|
|
|
|
|
|
|
|
return $gateway->_id;
|
|
|
|
}
|
|
|
|
}
|