2019-08-22 00:34:20 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-08-22 00:34:20 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-08-22 00:34:20 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
use App\Models\Client;
|
2019-08-22 00:34:20 +02:00
|
|
|
use App\Models\Company;
|
2019-08-22 03:58:42 +02:00
|
|
|
use App\Models\Gateway;
|
|
|
|
use App\Models\GatewayType;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\PaymentDrivers\BasePaymentDriver;
|
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;
|
2020-06-04 23:21:30 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2019-08-22 00:34:20 +02:00
|
|
|
|
2019-09-08 12:39:13 +02:00
|
|
|
class CompanyGateway extends BaseModel
|
2019-08-22 00:34:20 +02:00
|
|
|
{
|
2020-06-04 23:21:30 +02:00
|
|
|
use SoftDeletes;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-10-15 02:37:16 +02:00
|
|
|
public const GATEWAY_CREDIT = 10000000;
|
|
|
|
|
2019-11-11 13:21:19 +01:00
|
|
|
protected $casts = [
|
|
|
|
'fees_and_limits' => 'object',
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
|
|
|
];
|
|
|
|
|
2019-10-02 05:00:51 +02:00
|
|
|
protected $fillable = [
|
2019-10-03 03:13:25 +02:00
|
|
|
'gateway_key',
|
|
|
|
'accepted_credit_cards',
|
|
|
|
'require_cvv',
|
2020-11-03 21:19:59 +01:00
|
|
|
'require_billing_address',
|
|
|
|
'require_shipping_address',
|
|
|
|
'require_client_name',
|
|
|
|
'require_zip',
|
|
|
|
'require_client_phone',
|
|
|
|
'require_contact_name',
|
2019-10-03 03:13:25 +02:00
|
|
|
'update_details',
|
|
|
|
'config',
|
2019-11-11 13:21:19 +01:00
|
|
|
'fees_and_limits',
|
2019-12-17 11:57:15 +01:00
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
2020-08-19 08:11:57 +02:00
|
|
|
'token_billing',
|
2020-08-24 23:09:27 +02:00
|
|
|
'label',
|
2019-10-02 05:00:51 +02:00
|
|
|
];
|
2019-10-03 03:13:25 +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'],
|
|
|
|
];
|
|
|
|
|
2019-11-11 13:21:19 +01:00
|
|
|
// public function getFeesAndLimitsAttribute()
|
|
|
|
// {
|
|
|
|
// return json_decode($this->attributes['fees_and_limits']);
|
|
|
|
// }
|
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 00:34:20 +02:00
|
|
|
public function company()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(Company::class);
|
2019-08-22 00:34:20 +02:00
|
|
|
}
|
2019-08-22 03:58:42 +02:00
|
|
|
|
|
|
|
public function gateway()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(Gateway::class, 'gateway_key', 'key');
|
2019-08-22 03:58:42 +02:00
|
|
|
}
|
|
|
|
|
2019-09-18 14:43:37 +02:00
|
|
|
public function getTypeAlias($gateway_type_id)
|
2019-08-22 03:58:42 +02:00
|
|
|
{
|
2020-10-13 12:32:15 +02:00
|
|
|
return GatewayType::getAlias($gateway_type_id);
|
2019-08-22 03:58:42 +02:00
|
|
|
}
|
2019-09-05 14:42:26 +02:00
|
|
|
|
|
|
|
/* This is the public entry point into the payment superclass */
|
2019-09-16 06:59:59 +02:00
|
|
|
public function driver(Client $client)
|
2019-09-05 14:42:26 +02:00
|
|
|
{
|
|
|
|
$class = static::driver_class();
|
|
|
|
|
2019-09-16 06:59:59 +02:00
|
|
|
return new $class($this, $client);
|
2019-09-05 14:42:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function driver_class()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$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 {
|
2020-10-28 11:10:49 +01:00
|
|
|
return BasePaymentDriver::class;
|
2019-09-05 14:42:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-28 03:06:46 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the current test mode of the gateway.
|
|
|
|
*
|
|
|
|
* @return bool whether the gateway is in testmode or not.
|
2020-08-28 03:06:46 +02:00
|
|
|
*/
|
|
|
|
public function isTestMode() :bool
|
2020-08-27 23:34:15 +02:00
|
|
|
{
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->gateway->provider == 'Stripe' && strpos($config->publishableKey, 'test')) {
|
2020-08-27 23:34:15 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-27 23:34:15 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($config && property_exists($config, 'testMode') && $config->testMode) {
|
2020-08-27 23:34:15 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-27 23:34:15 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-09-14 14:34:05 +02:00
|
|
|
/**
|
|
|
|
* Get Publishable Key
|
2020-09-06 11:38:10 +02:00
|
|
|
* Only works for STRIPE and PAYMILL.
|
2019-09-14 14:34:05 +02:00
|
|
|
* @return string The Publishable key
|
|
|
|
*/
|
|
|
|
public function getPublishableKey() :string
|
|
|
|
{
|
2020-03-22 21:45:16 +01:00
|
|
|
return $this->getConfigField('publishableKey');
|
2019-09-14 14:34:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 06:58:07 +02:00
|
|
|
public function getFeesAndLimits($gateway_type_id)
|
2020-08-26 00:10:49 +02:00
|
|
|
{
|
2020-10-12 06:10:34 +02:00
|
|
|
if (is_null($this->fees_and_limits) || empty($this->fees_and_limits)) {
|
2020-08-26 00:10:49 +02:00
|
|
|
return false;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-26 00:10:49 +02:00
|
|
|
|
2020-10-12 06:58:07 +02:00
|
|
|
return $this->fees_and_limits->{$gateway_type_id};
|
2020-08-26 00:10:49 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-09 05:27:16 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Returns the formatted fee amount for the gateway.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param float $amount The payment amount
|
|
|
|
* @param Client $client The client object
|
|
|
|
* @param int $gateway_type_id
|
2019-09-09 05:27:16 +02:00
|
|
|
* @return string The fee amount formatted in the client currency
|
|
|
|
*/
|
2020-10-12 11:38:55 +02:00
|
|
|
public function calcGatewayFeeLabel($amount, Client $client, $gateway_type_id = GatewayType::CREDIT_CARD) :string
|
2019-09-09 05:27:16 +02:00
|
|
|
{
|
2019-09-09 06:54:39 +02:00
|
|
|
$label = '';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->feesEnabled()) {
|
2019-09-09 06:54:39 +02:00
|
|
|
return $label;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-09-09 06:54:39 +02:00
|
|
|
|
2020-10-12 06:58:07 +02:00
|
|
|
$fee = $this->calcGatewayFee($amount, $gateway_type_id);
|
2019-09-09 05:27:16 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($fee > 0) {
|
2019-09-11 07:32:47 +02:00
|
|
|
$fee = Number::formatMoney(round($fee, 2), $client);
|
2020-09-06 11:38:10 +02:00
|
|
|
$label = ' - '.$fee.' '.ctrans('texts.fee');
|
2019-09-09 06:54:39 +02:00
|
|
|
}
|
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
|
|
|
|
2020-10-12 11:38:55 +02:00
|
|
|
public function calcGatewayFee($amount, $include_taxes = false, $gateway_type_id = GatewayType::CREDIT_CARD)
|
2019-09-09 06:54:39 +02:00
|
|
|
{
|
2020-10-12 11:38:55 +02:00
|
|
|
|
2020-10-12 06:58:07 +02:00
|
|
|
$fees_and_limits = $this->getFeesAndLimits($gateway_type_id);
|
2020-08-17 05:03:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $fees_and_limits) {
|
2020-08-26 00:10:49 +02:00
|
|
|
return 0;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-17 05:03:21 +02:00
|
|
|
|
2019-09-09 06:54:39 +02:00
|
|
|
$fee = 0;
|
|
|
|
|
2020-08-17 05:03:21 +02:00
|
|
|
if ($fees_and_limits->fee_amount) {
|
|
|
|
$fee += $fees_and_limits->fee_amount;
|
|
|
|
info("fee after adding fee amount = {$fee}");
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-17 05:03:21 +02:00
|
|
|
if ($fees_and_limits->fee_percent) {
|
2020-10-10 12:57:28 +02:00
|
|
|
$fee += round(($amount * $fees_and_limits->fee_percent / 100),2);
|
2020-08-17 05:03:21 +02:00
|
|
|
info("fee after adding fee percent = {$fee}");
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-08-17 05:03:21 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
/* Cap fee if we have to here. */
|
|
|
|
if ($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap)) {
|
2020-08-19 00:33:58 +02:00
|
|
|
$fee = $fees_and_limits->fee_cap;
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-19 00:33:58 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
$pre_tax_fee = $fee;
|
|
|
|
|
|
|
|
/**/
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($include_taxes) {
|
2020-08-31 04:00:43 +02:00
|
|
|
if ($fees_and_limits->fee_tax_rate1) {
|
2020-10-10 12:57:28 +02:00
|
|
|
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100),2);
|
2020-08-31 04:00:43 +02:00
|
|
|
info("fee after adding fee tax 1 = {$fee}");
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
if ($fees_and_limits->fee_tax_rate2) {
|
2020-10-10 12:57:28 +02:00
|
|
|
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100),2);
|
2020-08-31 04:00:43 +02:00
|
|
|
info("fee after adding fee tax 2 = {$fee}");
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-31 04:00:43 +02:00
|
|
|
if ($fees_and_limits->fee_tax_rate3) {
|
2020-10-10 12:57:28 +02:00
|
|
|
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100),2);
|
2020-08-31 04:00:43 +02:00
|
|
|
info("fee after adding fee tax 3 = {$fee}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 06:54:39 +02:00
|
|
|
return $fee;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:10:04 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* we need to average out the gateway fees across all the invoices
|
|
|
|
* so lets iterate.
|
|
|
|
*
|
|
|
|
* we MAY need to adjust the final fee to ensure our rounding makes sense!
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param $amount
|
|
|
|
* @param $invoice_count
|
|
|
|
* @return stdClass
|
2020-08-27 15:10:04 +02:00
|
|
|
*/
|
2020-11-01 06:09:09 +01:00
|
|
|
// public function calcGatewayFeeObject($amount, $invoice_count)
|
|
|
|
// {
|
|
|
|
// $total_gateway_fee = $this->calcGatewayFee($amount);
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fee_object = new stdClass;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fees_and_limits = $this->getFeesAndLimits();
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// if (! $fees_and_limits) {
|
|
|
|
// return $fee_object;
|
|
|
|
// }
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fee_component_amount = $fees_and_limits->fee_amount ?: 0;
|
|
|
|
// $fee_component_percent = $fees_and_limits->fee_percent ? ($amount * $fees_and_limits->fee_percent / 100) : 0;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $combined_fee_component = $fee_component_amount + $fee_component_percent;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fee_component_tax_name1 = $fees_and_limits->fee_tax_name1 ?: '';
|
|
|
|
// $fee_component_tax_rate1 = $fees_and_limits->fee_tax_rate1 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate1 / 100) : 0;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fee_component_tax_name2 = $fees_and_limits->fee_tax_name2 ?: '';
|
|
|
|
// $fee_component_tax_rate2 = $fees_and_limits->fee_tax_rate2 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate2 / 100) : 0;
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-11-01 06:09:09 +01:00
|
|
|
// $fee_component_tax_name3 = $fees_and_limits->fee_tax_name3 ?: '';
|
|
|
|
// $fee_component_tax_rate3 = $fees_and_limits->fee_tax_rate3 ? ($combined_fee_component * $fees_and_limits->fee_tax_rate3 / 100) : 0;
|
|
|
|
// }
|
2020-08-27 14:12:39 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function resolveRouteBinding($value, $field = NULL)
|
2019-11-12 05:41:02 +01:00
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
|
|
|
}
|
2019-08-22 00:34:20 +02:00
|
|
|
}
|