invoiceninja/app/Models/AccountGatewayToken.php

134 lines
3.2 KiB
PHP
Raw Permalink Normal View History

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