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;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class AccountGatewayToken.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class AccountGatewayToken extends Eloquent
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
use SoftDeletes;
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-31 11:38:24 +02:00
|
|
|
protected $dates = ['deleted_at'];
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public $timestamps = true;
|
2016-05-11 00:46:32 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-06-20 16:14:43 +02:00
|
|
|
protected $casts = [];
|
2016-05-11 00:46:32 +02:00
|
|
|
|
2017-08-31 14:55:15 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
|
|
|
'contact_id',
|
|
|
|
'account_gateway_id',
|
|
|
|
'client_id',
|
|
|
|
'token',
|
|
|
|
];
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
2016-05-11 00:46:32 +02:00
|
|
|
public function payment_methods()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\PaymentMethod');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2016-06-20 16:14:43 +02:00
|
|
|
public function account_gateway()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\AccountGateway');
|
|
|
|
}
|
|
|
|
|
2017-08-31 14:55:15 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
|
|
|
public function contact()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Contact');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2017-08-31 14:55:15 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_CUSTOMER;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $query
|
|
|
|
* @param $clientId
|
|
|
|
* @param $accountGatewayId
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02: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;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-06-20 16:14:43 +02:00
|
|
|
public function gatewayName()
|
|
|
|
{
|
|
|
|
return $this->account_gateway->gateway->name;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|