1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00
invoiceninja/app/Models/AccountGateway.php

322 lines
6.7 KiB
PHP
Raw Permalink Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
use Utils;
use HTMLUtils;
2015-11-01 19:21:11 +01:00
use Crypt;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2016-06-20 16:14:43 +02:00
use Laracasts\Presenter\PresentableTrait;
2015-03-31 11:38:24 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class AccountGateway.
*/
2015-03-16 22:45:25 +01:00
class AccountGateway extends EntityModel
{
2015-03-31 11:38:24 +02:00
use SoftDeletes;
2016-06-20 16:14:43 +02:00
use PresentableTrait;
/**
* @var string
*/
2016-06-20 16:14:43 +02:00
protected $presenter = 'App\Ninja\Presenters\AccountGatewayPresenter';
/**
* @var array
*/
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
2018-04-24 15:20:05 +02:00
/**
* @var array
*/
protected $hidden = [
'config'
];
/**
* @return mixed
*/
2015-11-05 23:37:04 +01:00
public function getEntityType()
{
return ENTITY_ACCOUNT_GATEWAY;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-03-16 22:45:25 +01:00
public function gateway()
{
2015-03-31 11:38:24 +02:00
return $this->belongsTo('App\Models\Gateway');
2015-03-16 22:45:25 +01:00
}
/**
* @return array
*/
2015-03-16 22:45:25 +01:00
public function getCreditcardTypes()
{
$flags = unserialize(CREDIT_CARDS);
$arrayOfImages = [];
foreach ($flags as $card => $name) {
if (($this->accepted_credit_cards & $card) == $card) {
$arrayOfImages[] = ['source' => asset($name['card']), 'alt' => $name['text']];
}
}
return $arrayOfImages;
}
/**
* @param $provider
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2016-06-22 20:42:09 +02:00
public static function paymentDriverClass($provider)
2015-11-01 19:21:11 +01:00
{
$folder = 'App\\Ninja\\PaymentDrivers\\';
2017-08-21 18:08:49 +02:00
$provider = str_replace('\\', '', $provider);
2016-06-22 20:42:09 +02:00
$class = $folder . $provider . 'PaymentDriver';
2016-06-20 16:14:43 +02:00
$class = str_replace('_', '', $class);
2017-09-04 12:14:58 +02:00
2016-06-20 16:14:43 +02:00
if (class_exists($class)) {
2016-06-22 20:42:09 +02:00
return $class;
2016-06-20 16:14:43 +02:00
} else {
return $folder . 'BasePaymentDriver';
2016-06-20 16:14:43 +02:00
}
2015-03-16 22:45:25 +01:00
}
/**
2017-01-30 20:40:43 +01:00
* @param bool $invitation
2016-09-15 12:41:09 +02:00
* @param mixed $gatewayTypeId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-09-15 12:41:09 +02:00
public function paymentDriver($invitation = false, $gatewayTypeId = false)
2016-06-22 20:42:09 +02:00
{
$class = static::paymentDriverClass($this->gateway->provider);
2016-09-15 12:41:09 +02:00
return new $class($this, $invitation, $gatewayTypeId);
2016-06-22 20:42:09 +02:00
}
/**
* @param $gatewayId
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2015-11-01 19:21:11 +01:00
public function isGateway($gatewayId)
{
2018-03-07 16:23:25 +01:00
if (is_array($gatewayId)) {
foreach ($gatewayId as $id) {
if ($this->gateway_id == $id) {
return true;
}
}
return false;
} else {
return $this->gateway_id == $gatewayId;
}
}
2015-11-01 19:21:11 +01:00
2018-04-13 12:50:38 +02:00
public function isCustom()
{
return in_array($this->gateway_id, [GATEWAY_CUSTOM1, GATEWAY_CUSTOM2, GATEWAY_CUSTOM3]);
}
/**
* @param $config
*/
2015-11-01 19:21:11 +01:00
public function setConfig($config)
{
$this->config = Crypt::encrypt(json_encode($config));
}
/**
* @return mixed
*/
2015-11-01 19:21:11 +01:00
public function getConfig()
{
return json_decode(Crypt::decrypt($this->config));
}
2015-11-29 21:13:50 +01:00
/**
* @param $field
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-11-29 21:13:50 +01:00
public function getConfigField($field)
{
return object_get($this->getConfig(), $field, false);
2015-11-29 21:13:50 +01:00
}
/**
* @return bool|mixed
*/
2018-03-07 16:23:25 +01:00
public function getPublishableKey()
2015-11-29 21:13:50 +01:00
{
2018-03-07 16:23:25 +01:00
if (! $this->isGateway([GATEWAY_STRIPE, GATEWAY_PAYMILL])) {
2015-11-29 21:13:50 +01:00
return false;
}
return $this->getConfigField('publishableKey');
}
2017-11-27 15:50:06 +01:00
public function getAppleMerchantId()
{
if (! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('appleMerchantId');
}
/**
* @return bool
*/
public function getAchEnabled()
{
2017-01-30 20:40:43 +01:00
return ! empty($this->getConfigField('enableAch'));
}
2017-11-27 15:50:06 +01:00
/**
* @return bool
*/
public function getApplePayEnabled()
{
return ! empty($this->getConfigField('enableApplePay'));
}
2017-09-04 12:14:58 +02:00
/**
* @return bool
*/
public function getAlipayEnabled()
{
return ! empty($this->getConfigField('enableAlipay'));
}
2017-09-05 15:37:19 +02:00
/**
* @return bool
*/
public function getSofortEnabled()
{
return ! empty($this->getConfigField('enableSofort'));
}
2017-10-19 15:49:15 +02:00
/**
* @return bool
*/
public function getSepaEnabled()
{
return ! empty($this->getConfigField('enableSepa'));
}
/**
* @return bool
*/
public function getBitcoinEnabled()
{
return ! empty($this->getConfigField('enableBitcoin'));
}
/**
* @return bool
*/
2016-05-12 17:09:07 +02:00
public function getPayPalEnabled()
2016-05-07 04:33:03 +02:00
{
2017-01-30 20:40:43 +01:00
return ! empty($this->getConfigField('enablePayPal'));
2016-05-07 04:33:03 +02:00
}
/**
* @return bool|mixed
*/
public function getPlaidSecret()
{
2017-01-30 17:05:31 +01:00
if (! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidSecret');
}
/**
* @return bool|mixed
*/
public function getPlaidClientId()
{
2017-01-30 17:05:31 +01:00
if (! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidClientId');
}
/**
* @return bool|mixed
*/
public function getPlaidPublicKey()
{
2017-01-30 17:05:31 +01:00
if (! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidPublicKey');
}
/**
* @return bool
*/
public function getPlaidEnabled()
{
2017-01-30 20:40:43 +01:00
return ! empty($this->getPlaidClientId()) && $this->getAchEnabled();
}
/**
* @return null|string
*/
public function getPlaidEnvironment()
{
2017-01-30 20:40:43 +01:00
if (! $this->getPlaidClientId()) {
return null;
}
2018-03-07 16:23:25 +01:00
$stripe_key = $this->getPublishableKey();
2016-05-01 04:45:51 +02:00
return substr(trim($stripe_key), 0, 8) == 'pk_test_' ? 'tartan' : 'production';
}
2016-05-18 15:51:20 +02:00
/**
* @return string
*/
2016-05-18 15:51:20 +02:00
public function getWebhookUrl()
{
$account = $this->account ? $this->account : Account::find($this->account_id);
2016-06-21 18:10:22 +02:00
2017-01-30 17:05:31 +01:00
return \URL::to(env('WEBHOOK_PREFIX', '').'payment_hook/'.$account->account_key.'/'.$this->gateway_id.env('WEBHOOK_SUFFIX', ''));
2016-05-18 15:51:20 +02:00
}
2018-02-20 13:36:36 +01:00
public function isTestMode()
{
if ($this->isGateway(GATEWAY_STRIPE)) {
2018-03-07 16:23:25 +01:00
return strpos($this->getPublishableKey(), 'test') !== false;
2018-02-20 13:36:36 +01:00
} else {
return $this->getConfigField('testMode');
}
}
public function getCustomHtml($invitation)
{
$text = $this->getConfigField('text');
if ($text == strip_tags($text)) {
$text = nl2br($text);
}
if (Utils::isNinja()) {
$text = HTMLUtils::sanitizeHTML($text);
}
$templateService = app('App\Services\TemplateService');
$text = $templateService->processVariables($text, ['invitation' => $invitation]);
return $text;
}
2015-03-16 22:45:25 +01:00
}