1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Models/AccountGateway.php

216 lines
4.5 KiB
PHP
Raw Normal View History

2015-03-18 00:39:03 +01:00
<?php namespace App\Models;
2015-03-16 22:45:25 +01:00
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
/**
* 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'];
/**
* @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
* @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\\';
2016-06-22 20:42:09 +02:00
$class = $folder . $provider . 'PaymentDriver';
2016-06-20 16:14:43 +02:00
$class = str_replace('_', '', $class);
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
}
/**
* @param bool $invitation
* @param bool $gatewayType
* @return mixed
*/
2016-06-22 20:42:09 +02:00
public function paymentDriver($invitation = false, $gatewayType = false)
{
$class = static::paymentDriverClass($this->gateway->provider);
return new $class($this, $invitation, $gatewayType);
}
/**
* @param $gatewayId
* @return bool
*/
2015-11-01 19:21:11 +01:00
public function isGateway($gatewayId)
{
return $this->gateway_id == $gatewayId;
}
2015-11-01 19:21:11 +01:00
/**
* @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
* @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
*/
2015-11-29 21:13:50 +01:00
public function getPublishableStripeKey()
{
if ( ! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('publishableKey');
}
/**
* @return bool
*/
public function getAchEnabled()
{
return !empty($this->getConfigField('enableAch'));
}
/**
* @return bool
*/
2016-05-12 17:09:07 +02:00
public function getPayPalEnabled()
2016-05-07 04:33:03 +02:00
{
return !empty($this->getConfigField('enablePayPal'));
}
/**
* @return bool|mixed
*/
public function getPlaidSecret()
{
if ( ! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidSecret');
}
/**
* @return bool|mixed
*/
public function getPlaidClientId()
{
if ( ! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidClientId');
}
/**
* @return bool|mixed
*/
public function getPlaidPublicKey()
{
if ( ! $this->isGateway(GATEWAY_STRIPE)) {
return false;
}
return $this->getConfigField('plaidPublicKey');
}
/**
* @return bool
*/
public function getPlaidEnabled()
{
return !empty($this->getPlaidClientId()) && $this->getAchEnabled();
}
/**
* @return null|string
*/
public function getPlaidEnvironment()
{
if (!$this->getPlaidClientId()) {
return null;
}
$stripe_key = $this->getPublishableStripeKey();
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
2016-06-23 15:15:52 +02: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
}
2015-03-16 22:45:25 +01:00
}