1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Models/CompanyGateway.php

244 lines
5.6 KiB
PHP
Raw Normal View History

2019-08-22 00:34:20 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
use App\Models\Client;
2019-08-22 00:34:20 +02:00
use App\Models\Company;
use App\Models\Gateway;
use App\Models\GatewayType;
2019-09-09 06:54:39 +02:00
use App\Utils\Number;
2019-08-22 00:34:20 +02:00
use Illuminate\Database\Eloquent\Model;
2019-09-08 12:39:13 +02:00
class CompanyGateway extends BaseModel
2019-08-22 00:34:20 +02:00
{
2019-10-02 05:00:51 +02:00
protected $fillable = [
'gateway_key',
'accepted_credit_cards',
'require_cvv',
'show_address',
'show_shipping_address',
'update_details',
'config',
'priority_id',
'min_limit',
'max_limit',
'fee_amount',
'fee_percent',
'fee_tax_name1',
'fee_tax_name2',
'fee_tax_rate1',
'fee_tax_rate2',
'fee_cap',
'adjust_fee_percent',
2019-10-02 05:00:51 +02:00
];
2019-09-08 12:39:13 +02:00
public static $credit_cards = [
2019-08-22 00:34:20 +02:00
1 => ['card' => 'images/credit_cards/Test-Visa-Icon.png', 'text' => 'Visa'],
2 => ['card' => 'images/credit_cards/Test-MasterCard-Icon.png', 'text' => 'Master Card'],
4 => ['card' => 'images/credit_cards/Test-AmericanExpress-Icon.png', 'text' => 'American Express'],
8 => ['card' => 'images/credit_cards/Test-Diners-Icon.png', 'text' => 'Diners'],
16 => ['card' => 'images/credit_cards/Test-Discover-Icon.png', 'text' => 'Discover'],
];
public function company()
{
return $this->belongsTo(Company::class);
}
public function gateway()
{
return $this->belongsTo(Gateway::class, 'gateway_key', 'key');
}
2019-09-18 14:43:37 +02:00
public function getTypeAlias($gateway_type_id)
{
2019-09-18 14:43:37 +02:00
if($gateway_type_id == 'token')
$gateway_type_id = 1;
return GatewayType::find($gateway_type_id)->alias;
}
2019-09-05 14:42:26 +02:00
/* This is the public entry point into the payment superclass */
public function driver(Client $client)
2019-09-05 14:42:26 +02:00
{
$class = static::driver_class();
return new $class($this, $client);
2019-09-05 14:42:26 +02:00
}
private function driver_class()
{
$class = 'App\\PaymentDrivers\\' . $this->gateway->provider . 'PaymentDriver';
2019-09-14 14:34:05 +02:00
//$class = str_replace('\\', '', $class);
2019-09-05 14:42:26 +02:00
$class = str_replace('_', '', $class);
if (class_exists($class)) {
return $class;
} else {
return 'App\\PaymentDrivers\\BasePaymentDriver';
}
}
2019-09-14 14:34:05 +02:00
/**
* @param $config
*/
public function setConfig($config)
2019-09-08 12:39:13 +02:00
{
2019-09-14 14:34:05 +02:00
$this->config = encrypt(json_encode($config));
2019-09-08 12:39:13 +02:00
}
2019-09-14 14:34:05 +02:00
/**
* @return mixed
*/
public function getConfig()
2019-09-08 12:39:13 +02:00
{
2019-09-15 13:40:46 +02:00
//return decrypt($this->config);
2019-09-14 14:34:05 +02:00
return json_decode(decrypt($this->config));
2019-09-08 12:39:13 +02:00
}
2019-10-03 12:59:19 +02:00
public function getConfigTransformed()
{
return $this->config ? decrypt($this->config) : '';
}
2019-09-14 14:34:05 +02:00
/**
* @param $field
*
* @return mixed
*/
public function getConfigField($field)
{
return object_get($this->getConfig(), $field, false);
}
2019-09-05 14:42:26 +02:00
/**
* @return bool
*/
public function getAchEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_ach'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getApplePayEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_apple_pay'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getAlipayEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_alipay'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getSofortEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_sofort'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getSepaEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_sepa'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getBitcoinEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_bitcoin'));
2019-09-05 14:42:26 +02:00
}
/**
* @return bool
*/
public function getPayPalEnabled()
{
2019-09-16 04:05:30 +02:00
return ! empty($this->getConfigField('enable_pay_pal'));
2019-09-05 14:42:26 +02:00
}
2019-09-09 05:27:16 +02:00
2019-09-09 06:54:39 +02:00
public function feesEnabled()
{
return floatval($this->fee_amount) || floatval($this->fee_percent);
}
2019-09-14 14:34:05 +02:00
/**
* Get Publishable Key
* Only works for STRIPE and PAYMILL
* @return string The Publishable key
*/
public function getPublishableKey() :string
{
2019-09-16 04:05:30 +02:00
return $this->getConfigField('publishable_key');
2019-09-14 14:34:05 +02:00
}
2019-09-09 05:27:16 +02:00
/**
* Returns the formatted fee amount for the gateway
*
* @param float $amount The payment amount
* @param Client $client The client object
* @return string The fee amount formatted in the client currency
*/
2019-09-09 06:54:39 +02:00
public function calcGatewayFeeLabel($amount, Client $client) :string
2019-09-09 05:27:16 +02:00
{
2019-09-09 06:54:39 +02:00
$label = '';
if(!$this->feesEnabled())
return $label;
$fee = $this->calcGatewayFee($amount);
2019-09-09 05:27:16 +02:00
2019-09-09 06:54:39 +02:00
if($fee > 0 ){
2019-09-11 07:32:47 +02:00
$fee = Number::formatMoney(round($fee, 2), $client);
2019-09-09 06:54:39 +02:00
$label = ' - ' . $fee . ' ' . ctrans('texts.fee');
}
2019-09-09 05:27:16 +02:00
2019-09-09 06:54:39 +02:00
return $label;
2019-09-09 05:27:16 +02:00
}
2019-09-09 06:54:39 +02:00
public function calcGatewayFee($amount)
{
$fee = 0;
if ($this->fee_amount)
$fee += $this->fee_amount;
if ($this->fee_percent)
$fee += $amount * $this->fee_percent / 100;
$pre_tax_fee = $fee;
if ($this->fee_tax_rate1)
$fee += $pre_tax_fee * $this->fee_tax_rate1 / 100;
if ($this->fee_tax_rate2)
$fee += $pre_tax_fee * $this->fee_tax_rate2 / 100;
return $fee;
}
2019-08-22 00:34:20 +02:00
}