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;
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 21:40:43 +02:00
|
|
|
* Class AccountGatewayToken.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-17 07:45:25 +10:00
|
|
|
class AccountGatewayToken extends Eloquent
|
|
|
|
|
{
|
2015-03-31 12:38:24 +03:00
|
|
|
use SoftDeletes;
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2015-03-31 12:38:24 +03:00
|
|
|
protected $dates = ['deleted_at'];
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2015-03-17 07:45:25 +10:00
|
|
|
public $timestamps = true;
|
2016-05-10 18:46:32 -04:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2016-06-20 17:14:43 +03:00
|
|
|
protected $casts = [];
|
2016-05-10 18:46:32 -04:00
|
|
|
|
2017-08-31 15:55:15 +03: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-10 18:46:32 -04: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 17:14:43 +03:00
|
|
|
public function account_gateway()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('App\Models\AccountGateway');
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 15:55:15 +03: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-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
|
|
|
|
2017-08-31 15:55:15 +03:00
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getEntityType()
|
|
|
|
|
{
|
|
|
|
|
return ENTITY_CUSTOMER;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @param $query
|
|
|
|
|
* @param $clientId
|
|
|
|
|
* @param $accountGatewayId
|
2017-01-30 21:40:43 +02:00
|
|
|
*
|
2016-07-03 18:11:58 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2016-06-20 17:14:43 +03:00
|
|
|
public function gatewayName()
|
|
|
|
|
{
|
|
|
|
|
return $this->account_gateway->gateway->name;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|