1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Gateway.php

220 lines
5.1 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-03-25 20:56:31 +01:00
use Eloquent;
2015-03-26 07:24:02 +01:00
use Omnipay;
2015-12-02 14:26:06 +01:00
use Utils;
2015-03-25 20:56:31 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class Gateway.
*/
2015-03-16 22:45:25 +01:00
class Gateway extends Eloquent
{
/**
* @var bool
*/
2015-03-16 22:45:25 +01:00
public $timestamps = true;
2016-09-26 11:33:30 +02:00
protected $fillable = [
'provider',
'is_offsite',
'sort_order',
];
/**
* @var array
*/
2016-06-20 16:14:43 +02:00
public static $gatewayTypes = [
GATEWAY_TYPE_CREDIT_CARD,
GATEWAY_TYPE_BANK_TRANSFER,
GATEWAY_TYPE_PAYPAL,
GATEWAY_TYPE_BITCOIN,
GATEWAY_TYPE_DWOLLA,
GATEWAY_TYPE_TOKEN,
GATEWAY_TYPE_GOCARDLESS,
2016-06-20 16:14:43 +02:00
];
2016-06-09 09:56:22 +02:00
// these will appear in the primary gateway select
// the rest are shown when selecting 'more options'
/**
* @var array
*/
2016-06-09 09:56:22 +02:00
public static $preferred = [
GATEWAY_PAYPAL_EXPRESS,
GATEWAY_STRIPE,
2017-12-03 09:49:00 +01:00
GATEWAY_WEPAY,
2016-06-09 09:56:22 +02:00
GATEWAY_BRAINTREE,
2016-06-22 20:42:09 +02:00
GATEWAY_AUTHORIZE_NET,
GATEWAY_MOLLIE,
2017-08-21 18:08:49 +02:00
GATEWAY_GOCARDLESS,
2018-05-08 16:47:47 +02:00
GATEWAY_BITPAY,
GATEWAY_DWOLLA,
2018-04-13 12:50:38 +02:00
GATEWAY_CUSTOM1,
GATEWAY_CUSTOM2,
GATEWAY_CUSTOM3,
2016-06-09 09:56:22 +02:00
];
// allow adding these gateway if another gateway
// is already configured
/**
* @var array
*/
2016-06-09 09:56:22 +02:00
public static $alternate = [
GATEWAY_PAYPAL_EXPRESS,
GATEWAY_BITPAY,
GATEWAY_DWOLLA,
2018-04-13 12:50:38 +02:00
GATEWAY_CUSTOM1,
GATEWAY_CUSTOM2,
GATEWAY_CUSTOM3,
2016-06-09 09:56:22 +02:00
];
/**
* @var array
*/
2015-05-31 14:37:29 +02:00
public static $hiddenFields = [
// PayPal
'headerImageUrl',
'solutionType',
'landingPage',
'brandName',
'logoImageUrl',
'borderColor',
// Dwolla
2015-06-07 10:05:30 +02:00
'returnUrl',
2015-05-31 14:37:29 +02:00
];
/**
* @var array
*/
2015-05-31 14:37:29 +02:00
public static $optionalFields = [
// PayPal
'testMode',
'developerMode',
2015-06-03 19:55:48 +02:00
// Dwolla
'sandbox',
2017-10-24 15:17:34 +02:00
// Payfast
'pdtKey',
2015-05-31 14:37:29 +02:00
];
/**
* @return string
*/
2015-03-16 22:45:25 +01:00
public function getLogoUrl()
{
return '/images/gateways/logo_'.$this->provider.'.png';
}
/**
* @param $gatewayId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-11-09 20:24:22 +01:00
public function isGateway($gatewayId)
{
return $this->id == $gatewayId;
}
/**
* @param $type
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2015-12-02 14:26:06 +01:00
public static function getPaymentTypeName($type)
{
return Utils::toCamelCase(strtolower(str_replace('PAYMENT_TYPE_', '', $type)));
}
/**
* @param $gatewayIds
2017-01-30 20:40:43 +01:00
*
* @return int
*/
2016-06-09 09:56:22 +02:00
public static function hasStandardGateway($gatewayIds)
{
$diff = array_diff($gatewayIds, static::$alternate);
return count($diff);
}
/**
* @param $query
* @param $accountGatewaysIds
*/
2016-06-09 09:56:22 +02:00
public function scopePrimary($query, $accountGatewaysIds)
{
$query->where('payment_library_id', '=', 1)
2016-06-22 20:42:09 +02:00
->whereIn('id', static::$preferred)
->whereIn('id', $accountGatewaysIds);
2018-04-12 21:50:34 +02:00
if (! Utils::isNinja()) {
$query->where('id', '!=', GATEWAY_WEPAY);
}
2016-06-09 09:56:22 +02:00
}
/**
* @param $query
* @param $accountGatewaysIds
*/
2016-06-09 09:56:22 +02:00
public function scopeSecondary($query, $accountGatewaysIds)
{
2016-06-22 20:42:09 +02:00
$query->where('payment_library_id', '=', 1)
->whereNotIn('id', static::$preferred)
->whereIn('id', $accountGatewaysIds);
2016-06-09 09:56:22 +02:00
}
/**
* @return string|\Symfony\Component\Translation\TranslatorInterface
*/
2015-03-16 22:45:25 +01:00
public function getHelp()
{
$link = '';
2016-06-20 16:14:43 +02:00
if ($this->id == GATEWAY_AUTHORIZE_NET) {
2015-03-16 22:45:25 +01:00
$link = 'http://reseller.authorize.net/application/?id=5560364';
} elseif ($this->id == GATEWAY_PAYPAL_EXPRESS) {
$link = 'https://www.paypal.com/us/cgi-bin/webscr?cmd=_login-api-run';
} elseif ($this->id == GATEWAY_TWO_CHECKOUT) {
$link = 'https://www.2checkout.com/referral?r=2c37ac2298';
2015-04-17 13:57:17 +02:00
} elseif ($this->id == GATEWAY_BITPAY) {
$link = 'https://bitpay.com/dashboard/signup';
2015-05-31 14:37:29 +02:00
} elseif ($this->id == GATEWAY_DWOLLA) {
$link = 'https://www.dwolla.com/register';
2016-03-17 14:28:28 +01:00
} elseif ($this->id == GATEWAY_SAGE_PAY_DIRECT || $this->id == GATEWAY_SAGE_PAY_SERVER) {
$link = 'https://applications.sagepay.com/apply/2C02C252-0F8A-1B84-E10D-CF933EFCAA99';
2017-07-17 13:28:35 +02:00
} elseif ($this->id == GATEWAY_STRIPE) {
$link = 'https://dashboard.stripe.com/account/apikeys';
2017-12-03 09:49:00 +01:00
} elseif ($this->id == GATEWAY_WEPAY) {
$link = url('/gateways/create?wepay=true');
2015-03-16 22:45:25 +01:00
}
$key = 'texts.gateway_help_'.$this->id;
2016-07-04 21:26:41 +02:00
$str = trans($key, [
2017-12-03 09:49:00 +01:00
'link' => "<a href='$link' >Click here</a>",
2016-07-04 21:26:41 +02:00
'complete_link' => url('/complete'),
]);
2015-03-16 22:45:25 +01:00
return $key != $str ? $str : '';
}
/**
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function getFields()
{
2016-09-26 11:33:30 +02:00
if ($this->isCustom()) {
return [
'name' => '',
'text' => '',
];
} else {
return Omnipay::create($this->provider)->getDefaultParameters();
}
}
public function isCustom()
{
2018-04-13 12:50:38 +02:00
return in_array($this->id, [GATEWAY_CUSTOM1, GATEWAY_CUSTOM2, GATEWAY_CUSTOM3]);
2015-04-15 18:35:41 +02:00
}
2015-03-16 22:45:25 +01:00
}