1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 12:42:36 +01:00
invoiceninja/app/Models/AccountGatewayToken.php

134 lines
3.2 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-04-13 21:43:51 +02:00
use Eloquent;
2015-03-31 11:38:24 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
/**
2017-01-30 20:40:43 +01:00
* Class AccountGatewayToken.
*/
2015-03-16 22:45:25 +01:00
class AccountGatewayToken extends Eloquent
{
2015-03-31 11:38:24 +02:00
use SoftDeletes;
/**
* @var array
*/
2015-03-31 11:38:24 +02:00
protected $dates = ['deleted_at'];
/**
* @var bool
*/
2015-03-16 22:45:25 +01:00
public $timestamps = true;
2016-05-11 00:46:32 +02:00
/**
* @var array
*/
2016-06-20 16:14:43 +02:00
protected $casts = [];
2016-05-11 00:46:32 +02:00
/**
* @var array
*/
protected $fillable = [
'contact_id',
'account_gateway_id',
'client_id',
'token',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2016-05-11 00:46:32 +02:00
public function payment_methods()
{
return $this->hasMany('App\Models\PaymentMethod');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2016-06-20 16:14:43 +02:00
public function account_gateway()
{
return $this->belongsTo('App\Models\AccountGateway');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function contact()
{
return $this->belongsTo('App\Models\Contact');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
2016-05-11 00:46:32 +02:00
public function default_payment_method()
{
return $this->hasOne('App\Models\PaymentMethod', 'id', 'default_payment_method_id');
}
2016-06-20 16:14:43 +02:00
/**
* @return mixed
*/
public function getEntityType()
{
return ENTITY_CUSTOMER;
}
/**
* @return mixed
*/
2016-06-20 16:14:43 +02:00
public function autoBillLater()
{
2016-07-20 09:19:39 +02:00
if ($this->default_payment_method) {
return $this->default_payment_method->requiresDelayedAutoBill();
}
return false;
2016-06-20 16:14:43 +02:00
}
/**
* @param $query
* @param $clientId
* @param $accountGatewayId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-06-20 16:14:43 +02:00
public function scopeClientAndGateway($query, $clientId, $accountGatewayId)
{
$query->where('client_id', '=', $clientId)
->where('account_gateway_id', '=', $accountGatewayId);
return $query;
}
/**
* @return mixed
*/
2016-06-20 16:14:43 +02:00
public function gatewayName()
{
return $this->account_gateway->gateway->name;
}
/**
* @return bool|string
*/
2016-06-20 16:14:43 +02:00
public function gatewayLink()
{
$accountGateway = $this->account_gateway;
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
return "https://dashboard.stripe.com/customers/{$this->token}";
} elseif ($accountGateway->gateway_id == GATEWAY_BRAINTREE) {
2016-10-07 07:22:19 +02:00
$merchantId = $accountGateway->getConfigField('merchantId');
$testMode = $accountGateway->getConfigField('testMode');
2016-06-20 16:14:43 +02:00
return $testMode ? "https://sandbox.braintreegateway.com/merchants/{$merchantId}/customers/{$this->token}" : "https://www.braintreegateway.com/merchants/{$merchantId}/customers/{$this->token}";
2017-08-21 21:10:50 +02:00
} elseif ($accountGateway->gateway_id == GATEWAY_GOCARDLESS) {
$testMode = $accountGateway->getConfigField('testMode');
return $testMode ? "https://manage-sandbox.gocardless.com/customers/{$this->token}" : "https://manage.gocardless.com/customers/{$this->token}";
2016-06-20 16:14:43 +02:00
} else {
return false;
}
}
}