Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom models #22

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/cashierconnect.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
<?php

use Lanos\CashierConnect\Models\ConnectSubscriptionItem;
use Lanos\CashierConnect\Models\ConnectMapping;
use Lanos\CashierConnect\Models\ConnectSubscription;
use Lanos\CashierConnect\Models\ConnectCustomer;
use Lanos\CashierConnect\Models\TestModel;


return [

'models' => [
'connect_subscription_item' => ConnectSubscriptionItem::class,
'connect_subscription' => ConnectSubscription::class,
'connect_mapping' => ConnectMapping::class,
'connect_customer' => ConnectCustomer::class,
'test_model' => TestModel::class,
],

'webhook' => [
'secret' => env('CONNECT_WEBHOOK_SECRET'),
'tolerance' => env('CONNECT_WEBHOOK_TOLERANCE', 300)
Expand Down
2 changes: 1 addition & 1 deletion src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function establishTransferCurrency($providedCurrency = null){
return $this->defaultCurrency;
}

return config('cashierconnect.currency');
return config('cashierconnect.models.currency');

}

Expand Down
7 changes: 3 additions & 4 deletions src/Concerns/ManageCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Lanos\CashierConnect\Concerns;

use Illuminate\Database\Eloquent\Model;
use Lanos\CashierConnect\Models\ConnectCustomer;
use Lanos\CashierConnect\Exceptions\AccountAlreadyExistsException;
use Lanos\CashierConnect\Exceptions\AccountNotFoundException;
use Lanos\CashierConnect\Models\ConnectMapping;
use Lanos\CashierConnect\Contracts\ConnectMappingContract;
use Stripe\Customer;
use Stripe\Exception\ApiErrorException;

Expand All @@ -18,7 +17,7 @@ trait ManageCustomer
*/
public function stripeCustomerMapping()
{
return $this->belongsTo(ConnectCustomer::class, $this->primaryKey, $this->getLocalIDField())->where('model', '=', get_class($this));
return $this->belongsTo(config('cashierconnect.models.connect_customer'), $this->primaryKey, $this->getLocalIDField())->where('model', '=', get_class($this));
}

/**
Expand Down Expand Up @@ -89,7 +88,7 @@ public function createStripeCustomer($connectedAccount, array $customerData = []
*/
private function retrieveHostConnectedAccount(): Model{

$connectedAccount = ConnectMapping::where([
$connectedAccount = config('cashierconnect.models.connect_mapping')::where([
['stripe_account_id', '=', $this->stripeAccountId()]
])->first();

Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/ManagesAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Lanos\CashierConnect\Exceptions\AccountAlreadyExistsException;
use Lanos\CashierConnect\Exceptions\AccountNotFoundException;
use Lanos\CashierConnect\Models\ConnectMapping;
use Lanos\CashierConnect\Contracts\ConnectMappingContract;
use Stripe\Account;
use Stripe\Exception\ApiErrorException;

Expand All @@ -22,12 +22,12 @@ trait ManagesAccount
*/
public function stripeAccountMapping()
{
return $this->belongsTo(ConnectMapping::class, $this->primaryKey, $this->getLocalIDField())->where('model', '=', get_class($this));
return $this->belongsTo(config('cashierconnect.models.connect_mapping'), $this->primaryKey, $this->getLocalIDField())->where('model', '=', get_class($this));
}

/**
* Updates and returns the updated requirements against the stripe API for the mapping
* @return ConnectMapping
* @return ConnectMappingContract (ConnectMapping)
*/
public function updateStripeStatus(){

Expand Down
1 change: 0 additions & 1 deletion src/Concerns/ManagesApplePayDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Lanos\CashierConnect\Exceptions\AccountAlreadyExistsException;
use Lanos\CashierConnect\Exceptions\AccountNotFoundException;
use Lanos\CashierConnect\Models\ConnectMapping;
use Stripe\Account;
use Stripe\ApplePayDomain;
use Stripe\Exception\ApiErrorException;
Expand Down
8 changes: 3 additions & 5 deletions src/Concerns/ManagesConnectSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Illuminate\Support\Facades\Date;
use Lanos\CashierConnect\Exceptions\AccountNotFoundException;
use Illuminate\Support\Str;
use Lanos\CashierConnect\Models\ConnectSubscription;
use Lanos\CashierConnect\Models\ConnectSubscriptionItem;
use Stripe\Balance;
use Stripe\Charge;
use Stripe\Exception\ApiErrorException;
Expand Down Expand Up @@ -57,7 +55,7 @@ public function createDirectSubscription($customer, $price, $quantity = 1, $data

// TODO REWRITE TO USE RELATIONAL CREATION
// GENERATE DATABASE RECORD FOR SUBSCRIPTION
$ConnectSubscriptionRecord = ConnectSubscription::create([
$ConnectSubscriptionRecord = config('cashierconnect.models.connect_subscription')::create([
"name" => $name,
"stripe_id" => $subscription->id,
"stripe_status" => $subscription->status,
Expand All @@ -68,7 +66,7 @@ public function createDirectSubscription($customer, $price, $quantity = 1, $data
]);

// TODO REWRITE TO USE RELATIONAL CREATION
$ConnectSubscriptionItemRecord = ConnectSubscriptionItem::create([
$ConnectSubscriptionItemRecord = config('cashierconnect.models.connect_subscription_item')::create([
"connected_subscription_id" => $ConnectSubscriptionRecord->id,
"stripe_id" => $subscription->items->data[0]->id,
"connected_product" => $subscription->items->data[0]->price->product,
Expand Down Expand Up @@ -109,7 +107,7 @@ private function handleConnectedCustomer($customer)
// IT IS A CUSTOMER TRAIT MODEL
$traits = class_uses($customer);

if (!in_array('Lanos\CashierConnect\ConnectCustomer', $traits)) {
if (!in_array(config('cashierconnect.models.connect_customer'), $traits)) {
throw new Exception('The '.class_basename($customer).' model does not have the connect ConnectCustomer trait.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Console/ConnectWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle()
$webhookEndpoints = Cashier::stripe()->webhookEndpoints;

$endpoint = $webhookEndpoints->create([
'enabled_events' => config('cashierconnect.events'),
'enabled_events' => config('cashierconnect.models.events'),
'url' => $this->option('url') ?? route('stripeConnect.webhook'),
'api_version' => $this->option('api-version') ?? Cashier::STRIPE_VERSION,
'connect' => true
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/ConnectCustomerContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Lanos\CashierConnect\Contracts;

/**
* Stripe account.
*
* @package Lanos\CashierConnect\Contracts
*/
interface ConnectCustomerContract
{

}
14 changes: 14 additions & 0 deletions src/Contracts/ConnectMappingContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Lanos\CashierConnect\Contracts;

/**
* Stripe account.
*
* @package Lanos\CashierConnect\Contracts
*/
interface ConnectMappingContract
{

}
14 changes: 14 additions & 0 deletions src/Contracts/ConnectSubscriptionContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Lanos\CashierConnect\Contracts;

/**
* Stripe account.
*
* @package Lanos\CashierConnect\Contracts
*/
interface ConnectSubscriptionContract
{

}
14 changes: 14 additions & 0 deletions src/Contracts/ConnectSubscriptionItemContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php


namespace Lanos\CashierConnect\Contracts;

/**
* Stripe account.
*
* @package Lanos\CashierConnect\Contracts
*/
interface ConnectSubscriptionItemContract
{

}
4 changes: 2 additions & 2 deletions src/Http/Middleware/VerifyConnectWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function handle($request, Closure $next)
WebhookSignature::verifyHeader(
$request->getContent(),
$request->header('Stripe-Signature'),
config('cashierconnect.webhook.secret'),
config('cashierconnect.webhook.tolerance')
config('cashierconnect.models.webhook.secret'),
config('cashierconnect.models.webhook.tolerance')
);
} catch (SignatureVerificationException $exception) {
throw new AccessDeniedHttpException($exception->getMessage(), $exception);
Expand Down
3 changes: 2 additions & 1 deletion src/Models/ConnectCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Lanos\CashierConnect\Models;

use Illuminate\Database\Eloquent\Model;
use Lanos\CashierConnect\Contracts\ConnectCustomerContract;

class ConnectCustomer extends Model
class ConnectCustomer extends Model implements ConnectCustomerContract
{

protected $primaryKey = null;
Expand Down
5 changes: 3 additions & 2 deletions src/Models/ConnectMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Lanos\CashierConnect\Models;

use Illuminate\Database\Eloquent\Model;
use Lanos\CashierConnect\Contracts\ConnectMappingContract;

class ConnectMapping extends Model
class ConnectMapping extends Model implements ConnectMappingContract
{

protected $primaryKey = null;
Expand All @@ -22,7 +23,7 @@ class ConnectMapping extends Model
protected $table = 'stripe_connect_mappings';

public function subscriptions(){
return $this->hasMany(ConnectSubscription::class, 'stripe_account_id', 'stripe_account_id');
return $this->hasMany(config('cashierconnect.models.connect_subscription'), 'stripe_account_id', 'stripe_account_id');
}

}
5 changes: 3 additions & 2 deletions src/Models/ConnectSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Database\Eloquent\Model;
use Lanos\CashierConnect\StripeEntity;
use Lanos\CashierConnect\Contracts\ConnectSubscriptionContract;
use Stripe\Exception\ApiErrorException;
use Stripe\Subscription;

class ConnectSubscription extends Model
class ConnectSubscription extends Model implements ConnectSubscriptionContract
{

use StripeEntity;
Expand All @@ -16,7 +17,7 @@ class ConnectSubscription extends Model
protected $table = 'connected_subscriptions';

public function items(){
return $this->hasMany(ConnectSubscriptionItem::class, 'connected_subscription_id', 'id');
return $this->hasMany(config('cashierconnect.models.connect_subscription_item'), 'connected_subscription_id', 'id');
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Models/ConnectSubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace Lanos\CashierConnect\Models;

use Illuminate\Database\Eloquent\Model;
use Lanos\CashierConnect\Contracts\ConnectSubscriptionContract;

class ConnectSubscriptionItem extends Model
class ConnectSubscriptionItem extends Model implements ConnectSubscriptionContract
{

protected $guarded = [];
protected $table = 'connected_subscription_items';

public function subscription(){
return $this->belongsTo(ConnectSubscription::class, 'connected_subscription_id', 'id');
return $this->belongsTo(config('cashierconnect.models.connect_subscription'), 'connected_subscription_id', 'id');
}

}