Skip to content

Commit

Permalink
Merge pull request #42 from jprodrigues70/master
Browse files Browse the repository at this point in the history
Extrai metodo createLocalSubscription a partir do metodo create
  • Loading branch information
gabrielpeixoto committed Mar 24, 2022
2 parents 7ba31bb + 8a0585c commit 5c4c933
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/SubscriptionBuilder.php
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use Iugu_Subscription as IuguSubscription;

class SubscriptionBuilder
{
Expand Down Expand Up @@ -174,8 +175,6 @@ public function add(array $options = [])
*/
public function create($token = null, array $options = [])
{
$iuguSubscriptionModelIdColumn = getenv('IUGU_SUBSCRIPTION_MODEL_ID_COLUMN') ?: config('services.iugu.subscription_model_id_column', 'iugu_id');
$iuguSubscriptionModelPlanColumn = getenv('IUGU_SUBSCRIPTION_MODEL_PLAN_COLUMN') ?: config('services.iugu.subscription_model_plan_column', 'iugu_plan');

$customer = $this->getIuguCustomer($token, $options);

Expand All @@ -186,6 +185,19 @@ public function create($token = null, array $options = [])

$subscriptionIugu = $this->user->createIuguSubscription($this->buildPayload($customer->id));

return $this->createLocalSubscription($subscriptionIugu);
}

/**
* Creates a local subscription from an Iugu subscription
*
* @param IuguSubscription $subscriptionIugu
* @return \Potelo\GuPayment\Subscription|boolean
*/
public function createLocalSubscription($subscriptionIugu)
{
$iuguSubscriptionModelIdColumn = getenv('IUGU_SUBSCRIPTION_MODEL_ID_COLUMN') ?: config('services.iugu.subscription_model_id_column', 'iugu_id');
$iuguSubscriptionModelPlanColumn = getenv('IUGU_SUBSCRIPTION_MODEL_PLAN_COLUMN') ?: config('services.iugu.subscription_model_plan_column', 'iugu_plan');
if (isset($subscriptionIugu->errors)) {
if (isset($subscriptionIugu->LR)) {
$this->lr = $subscriptionIugu->LR;
Expand Down Expand Up @@ -229,7 +241,7 @@ public function create($token = null, array $options = [])
*/
protected function getIuguCustomer($token = null, array $options = [])
{
if (! $this->user->getIuguUserId()) {
if (!$this->user->getIuguUserId()) {
$customer = $this->user->createAsIuguCustomer(
$token,
array_merge($options, array_filter(['coupon' => $this->coupon]))
Expand All @@ -238,7 +250,7 @@ protected function getIuguCustomer($token = null, array $options = [])
$customer = $this->user->asIuguCustomer();

if (!empty($options)) {
foreach($options as $key => $value){
foreach ($options as $key => $value) {
$customer->{$key} = $value;
}
$customer->save();
Expand Down Expand Up @@ -429,5 +441,4 @@ public function addSubItem($subItem)

return $this;
}

}

0 comments on commit 5c4c933

Please sign in to comment.