1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Models/CompanyGateway.php

464 lines
14 KiB
PHP
Raw Normal View History

2019-08-22 00:34:20 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-08-22 00:34:20 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-08-22 00:34:20 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-08-22 00:34:20 +02:00
*/
namespace App\Models;
2019-09-09 06:54:39 +02:00
use App\Utils\Number;
use Illuminate\Database\Eloquent\SoftDeletes;
2019-08-22 00:34:20 +02:00
2023-03-08 08:33:42 +01:00
/**
* App\Models\CompanyGateway
*
* @property int $id
* @property int $company_id
* @property int $user_id
* @property string $gateway_key
2023-08-01 14:36:04 +02:00
* @property bool $accepted_credit_cards
* @property bool $require_cvv
* @property bool $require_billing_address
* @property bool $require_shipping_address
* @property bool $update_details
* @property bool $is_deleted
2023-03-08 08:33:42 +01:00
* @property string $config
2023-08-01 15:01:48 +02:00
* @property object $fees_and_limits
2023-03-08 08:33:42 +01:00
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
* @property string|null $custom_value4
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $deleted_at
* @property string $token_billing
* @property string|null $label
2023-08-01 14:36:04 +02:00
* @property bool $require_client_name
* @property bool $require_postal_code
* @property bool $require_client_phone
* @property bool $require_contact_name
* @property bool $require_contact_email
* @property bool $require_custom_value1
* @property bool $require_custom_value2
* @property bool $require_custom_value3
* @property bool $require_custom_value4
2023-03-08 08:33:42 +01:00
* @property-read int|null $client_gateway_tokens_count
* @property-read \App\Models\Company $company
* @property-read \App\Models\Gateway $gateway
* @property-read mixed $hashed_id
2023-08-06 09:03:12 +02:00
* @method getConfigField(string $field)
2023-03-08 08:33:42 +01:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway filter(\App\Filters\QueryFilters $filters)
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway query()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|CompanyGateway withoutTrashed()
2023-03-16 05:20:38 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientGatewayToken> $client_gateway_tokens
2023-08-06 09:41:40 +02:00
* @method static CompanyGateway find($value)
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
*/
2019-09-08 12:39:13 +02:00
class CompanyGateway extends BaseModel
2019-08-22 00:34:20 +02:00
{
use SoftDeletes;
2022-01-03 02:35:31 +01:00
use Filterable;
2020-10-15 02:37:16 +02:00
public const GATEWAY_CREDIT = 10000000;
protected $casts = [
'fees_and_limits' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
2023-02-20 11:10:37 +01:00
protected $with = [
'gateway',
];
2019-10-02 05:00:51 +02:00
protected $fillable = [
'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_postal_code',
2020-11-03 21:19:59 +01:00
'require_client_phone',
'require_contact_name',
2022-10-11 22:53:29 +02:00
'require_contact_email',
'require_custom_value1',
'require_custom_value2',
'require_custom_value3',
'require_custom_value4',
'update_details',
'config',
'fees_and_limits',
'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-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'],
];
2021-07-04 00:22:33 +02:00
// const TYPE_PAYPAL = 300;
// const TYPE_STRIPE = 301;
// const TYPE_LEDGER = 302;
// const TYPE_FAILURE = 303;
// const TYPE_CHECKOUT = 304;
// const TYPE_AUTHORIZE = 305;
// const TYPE_CUSTOM = 306;
// const TYPE_BRAINTREE = 307;
// const TYPE_WEPAY = 309;
2021-07-20 13:26:24 +02:00
// const TYPE_PAYFAST = 310;
// const TYPE_PAYTRACE = 311;
// const TYPE_MOLLIE = 312;
2023-02-17 22:36:51 +01:00
// const TYPE_EWAY = 313;
2022-08-04 16:02:15 +02:00
// const TYPE_FORTE = 314;
2021-07-04 00:22:33 +02:00
public $gateway_consts = [
'38f2c48af60c7dd69e04248cbb24c36e' => 300,
'd14dd26a37cecc30fdd65700bfb55b23' => 301,
2021-07-20 13:26:24 +02:00
'd14dd26a47cecc30fdd65700bfb67b34' => 301,
'3758e7f7c6f4cecf0f4f348b9a00f456' => 304,
'3b6621f970ab18887c4f6dca78d3f8bb' => 305,
'54faab2ab6e3223dbe848b1686490baa' => 306,
2021-07-04 00:22:33 +02:00
'f7ec488676d310683fb51802d076d713' => 307,
2021-07-20 13:26:24 +02:00
'8fdeed552015b3c7b44ed6c8ebd9e992' => 309,
'd6814fc83f45d2935e7777071e629ef9' => 310,
'bbd736b3254b0aabed6ad7fda1298c88' => 311,
'1bd651fb213ca0c9d66ae3c336dc77e7' => 312,
'944c20175bbe6b9972c05bcfe294c2c7' => 313,
2022-08-04 16:02:15 +02:00
'kivcvjexxvdiyqtj3mju5d6yhpeht2xs' => 314,
2022-04-28 04:40:07 +02:00
'65faab2ab6e3223dbe848b1686490baz' => 320,
'b9886f9257f0c6ee7c302f1c74475f6c' => 321,
'hxd6gwg3ekb9tb3v9lptgx1mqyg69zu9' => 322,
];
2020-07-23 05:55:11 +02:00
protected $touches = [];
public function getEntityType()
{
return self::class;
}
public function system_logs()
{
return $this->company
->system_log_relation
->where('type_id', $this->gateway_consts[$this->gateway->key])
->take(50);
}
2019-08-22 00:34:20 +02:00
public function company()
{
return $this->belongsTo(Company::class);
2019-08-22 00:34:20 +02:00
}
public function client_gateway_tokens()
{
return $this->hasMany(ClientGatewayToken::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)
{
return GatewayType::getAlias($gateway_type_id);
}
2019-09-05 14:42:26 +02:00
/* This is the public entry point into the payment superclass */
public function driver(Client $client = null)
2019-09-05 14:42:26 +02:00
{
2023-07-25 12:24:33 +02:00
// $class = static::driver_class();
$class = self::driver_class();
2019-09-05 14:42:26 +02:00
2023-02-16 02:36:09 +01:00
if (!$class) {
return false;
2023-02-16 02:36:09 +01:00
}
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-05 14:42:26 +02:00
$class = str_replace('_', '', $class);
2023-02-16 02:36:09 +01:00
if (class_exists($class)) {
2019-09-05 14:42:26 +02:00
return $class;
2023-02-16 02:36:09 +01:00
}
2021-10-17 12:40:40 +02:00
return false;
// throw new \Exception("Payment Driver does not exist");
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
2023-07-25 12:24:33 +02:00
// public function feesEnabled()
// {
// return floatval($this->fee_amount) || floatval($this->fee_percent);
// }
2019-09-09 06:54:39 +02:00
2020-08-28 03:06:46 +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();
if ($this->gateway && $this->gateway->provider == 'Stripe' && property_exists($config, 'publishableKey') && strpos($config->publishableKey, 'test')) {
2020-08-27 23:34:15 +02:00
return true;
}
2020-08-27 23:34:15 +02:00
if ($config && property_exists($config, 'testMode') && $config->testMode) {
2020-08-27 23:34:15 +02:00
return true;
}
2020-08-27 23:34:15 +02:00
return false;
}
2019-09-14 14:34:05 +02:00
/**
* Get Publishable Key
* Only works for STRIPE and PAYMILL.
2019-09-14 14:34:05 +02:00
* @return string The Publishable key
*/
public function getPublishableKey() :string
{
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)
{
if (is_null($this->fees_and_limits) || empty($this->fees_and_limits) || !property_exists($this->fees_and_limits, $gateway_type_id)) {
return false;
}
2020-12-16 12:52:40 +01:00
if ($gateway_type_id == GatewayType::CUSTOM) {
2020-12-10 11:20:12 +01:00
$gateway_type_id = GatewayType::CREDIT_CARD;
2020-12-16 12:52:40 +01:00
}
2020-12-10 11:20:12 +01:00
2020-10-12 06:58:07 +02:00
return $this->fees_and_limits->{$gateway_type_id};
}
2019-09-09 05:27:16 +02:00
/**
* Returns the formatted fee amount for the gateway.
*
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
{
2021-04-23 07:26:58 +02:00
$label = ' ';
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
2023-02-16 02:36:09 +01:00
if ($fee > 0) {
2021-04-23 07:26:58 +02:00
$fees_and_limits = $this->fees_and_limits->{$gateway_type_id};
2023-02-16 02:36:09 +01:00
if (strlen($fees_and_limits->fee_percent) >=1) {
2021-04-23 07:26:58 +02:00
$label .= $fees_and_limits->fee_percent . '%';
2023-02-16 02:36:09 +01:00
}
2021-04-23 07:26:58 +02:00
2023-02-16 02:36:09 +01:00
if (strlen($fees_and_limits->fee_amount) >=1 && $fees_and_limits->fee_amount > 0) {
if (strlen($label) > 1) {
2021-04-23 07:26:58 +02:00
$label .= ' + ' . Number::formatMoney($fees_and_limits->fee_amount, $client);
2023-02-16 02:36:09 +01:00
} else {
2021-04-23 07:26:58 +02:00
$label .= Number::formatMoney($fees_and_limits->fee_amount, $client);
}
}
2019-09-09 06:54:39 +02:00
}
2019-09-09 05:27:16 +02:00
2021-04-23 07:26:58 +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, $gateway_type_id, $include_taxes = false)
2019-09-09 06:54:39 +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
if (! $fees_and_limits) {
2021-05-16 00:18:43 +02:00
return false;
}
2020-08-17 05:03:21 +02:00
2019-09-09 06:54:39 +02:00
$fee = 0;
2023-02-16 02:36:09 +01:00
if ($fees_and_limits->adjust_fee_percent) {
$adjusted_fee = 0;
2022-03-16 10:14:40 +01:00
2023-02-16 02:36:09 +01:00
if ($fees_and_limits->fee_amount) {
$adjusted_fee += $fees_and_limits->fee_amount + $amount;
} else {
$adjusted_fee = $amount;
}
2022-03-16 10:14:40 +01:00
2023-02-16 02:36:09 +01:00
if ($fees_and_limits->fee_percent) {
$divisor = 1 - ($fees_and_limits->fee_percent/100);
2022-03-16 10:14:40 +01:00
2023-02-16 02:36:09 +01:00
$gross_amount = round($adjusted_fee/$divisor, 2);
$fee = $gross_amount - $amount;
}
} else {
if ($fees_and_limits->fee_amount) {
$fee += $fees_and_limits->fee_amount;
}
2022-03-16 10:14:40 +01:00
2023-02-16 02:36:09 +01:00
if ($fees_and_limits->fee_percent) {
if ($fees_and_limits->fee_percent == 100) { //unusual edge case if the user wishes to charge a fee of 100% 09/01/2022
$fee += $amount;
} else {
$fee += round(($amount * $fees_and_limits->fee_percent / 100), 2);
2022-03-16 10:14:40 +01:00
}
2023-02-16 02:36:09 +01:00
//elseif ($fees_and_limits->adjust_fee_percent) {
// $fee += round(($amount / (1 - $fees_and_limits->fee_percent / 100) - $amount), 2);
//} else {
2022-03-16 11:40:11 +01:00
2023-02-16 02:36:09 +01:00
//}
}
2022-03-16 10:14:40 +01: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-08-19 00:33:58 +02:00
$pre_tax_fee = $fee;
/**/
if ($include_taxes) {
if ($fees_and_limits->fee_tax_rate1) {
2020-11-25 15:19:52 +01:00
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate1 / 100), 2);
// info("fee after adding fee tax 1 = {$fee}");
}
if ($fees_and_limits->fee_tax_rate2) {
2020-11-25 15:19:52 +01:00
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate2 / 100), 2);
// info("fee after adding fee tax 2 = {$fee}");
}
if ($fees_and_limits->fee_tax_rate3) {
2020-11-25 15:19:52 +01:00
$fee += round(($pre_tax_fee * $fees_and_limits->fee_tax_rate3 / 100), 2);
// info("fee after adding fee tax 3 = {$fee}");
}
}
2019-09-09 06:54:39 +02:00
return $fee;
}
2021-06-24 07:46:10 +02:00
public function webhookUrl()
{
return route('payment_webhook', ['company_key' => $this->company->company_key, 'company_gateway_id' => $this->hashed_id]);
}
2019-08-22 00:34:20 +02:00
}