Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Change plan based on charge
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikyt committed Jun 27, 2018
1 parent f0d22fc commit bef44d6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 12 deletions.
34 changes: 27 additions & 7 deletions src/ShopifyApp/Traits/BillingControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
use OhMyBrew\ShopifyApp\Libraries\BillingPlan;
use OhMyBrew\ShopifyApp\Models\Shop;
use OhMyBrew\ShopifyApp\Models\Charge;

trait BillingControllerTrait
Expand All @@ -17,8 +18,9 @@ trait BillingControllerTrait
public function index()
{
// Get the confirmation URL
$plan = new BillingPlan(ShopifyApp::shop(), $this->chargeType());
$plan->setDetails($this->planDetails());
$shop = ShopifyApp::shop();
$plan = new BillingPlan($shop, $this->chargeType());
$plan->setDetails($this->planDetails($shop));

// Do a fullpage redirect
return view('shopify-app::billing.fullpage_redirect', [
Expand All @@ -43,7 +45,7 @@ public function process()
$status = $plan->getCharge()->status;

// Grab the plan detailed used
$planDetails = $this->planDetails();
$planDetails = $this->planDetails($shop);
unset($planDetails['return_url']);

// Create a charge (regardless of the status)
Expand Down Expand Up @@ -83,18 +85,22 @@ public function process()
}

/**
* Base plan to use for billing.
* Setup as a function so its patchable.
* Base plan to use for billing. Setup as a function so its patchable.
* Checks for cancelled charge within trial day limit, and issues
* a new trial days number depending on the result for shops who
* resinstall the app.
*
* @param object $shop The shop object.
*
* @return array
*/
protected function planDetails()
protected function planDetails(Shop $shop)
{
// Initial plan details
$plan = [
'name' => config('shopify-app.billing_plan'),
'price' => config('shopify-app.billing_price'),
'test' => config('shopify-app.billing_test'),
'trial_days' => config('shopify-app.billing_trial_days'),
'return_url' => url(config('shopify-app.billing_redirect')),
];

Expand All @@ -104,6 +110,20 @@ protected function planDetails()
$plan['terms'] = config('shopify-app.billing_terms');
}

// Grab the last charge for the shop (if any) to determine if this shop
// reinstalled the app so we can issue new trial days based on result
$lastCharge = $shop->charges()
->whereIn('type', [Charge::CHARGE_RECURRING, Charge::CHARGE_ONETIME])
->orderBy('created_at', 'desc')
->first();
if ($lastCharge && $lastCharge->isCancelled()) {
// Return the new trial days, could result in 0
$plan['trial_days'] = $lastCharge->remainingTrialDaysFromCancel();
} else {
// Set initial trial days fromc config
$plan['trial_days'] = config('shopify-app.billing_trial_days');
}

return $plan;
}

Expand Down
47 changes: 43 additions & 4 deletions tests/Controllers/BillingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace OhMyBrew\ShopifyApp\Test\Controllers;

use Carbon\Carbon;
use OhMyBrew\ShopifyApp\Controllers\BillingController;
use OhMyBrew\ShopifyApp\Models\Shop;
use OhMyBrew\ShopifyApp\Models\Charge;
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
use OhMyBrew\ShopifyApp\Test\TestCase;
use ReflectionMethod;
Expand All @@ -18,7 +20,8 @@ public function setUp()
config(['shopify-app.api_class' => new ApiStub()]);

// Base shop for all tests here
session(['shopify_domain' => 'example.myshopify.com']);
$this->shop = Shop::where('shopify_domain', 'example.myshopify.com')->first();
session(['shopify_domain' => $this->shop->shopify_domain]);
}

public function testSendsShopToBillingScreen()
Expand Down Expand Up @@ -69,7 +72,7 @@ public function testReturnsBasePlanDetails()
'trial_days' => config('shopify-app.billing_trial_days'),
'return_url' => url(config('shopify-app.billing_redirect')),
],
$method->invoke($controller, 'planDetails')
$method->invoke($controller, $this->shop)
);
}

Expand All @@ -93,7 +96,43 @@ public function testReturnsBasePlanDetailsWithUsage()
'terms' => config('shopify-app.billing_terms'),
'return_url' => url(config('shopify-app.billing_redirect')),
],
$method->invoke($controller, 'planDetails')
$method->invoke($controller, $this->shop)
);
}

public function testReturnsBasePlanDetailsChangedByCancelledCharge()
{
$shop = new Shop();
$shop->shopify_domain = 'test-cancelled-shop.myshopify.com';
$shop->save();

$charge = new Charge();
$charge->charge_id = 267921978;
$charge->test = false;
$charge->name = 'Base Plan Cancelled';
$charge->status = 'cancelled';
$charge->type = 1;
$charge->price = 25.00;
$charge->trial_days = 7;
$charge->trial_ends_on = Carbon::today()->addWeeks(1)->format('Y-m-d');
$charge->cancelled_on = Carbon::today()->addDays(2)->format('Y-m-d');
$charge->shop_id = $shop->id;
$charge->save();

$controller = new BillingController();
$method = new ReflectionMethod(BillingController::class, 'planDetails');
$method->setAccessible(true);

// Based on default config
$this->assertEquals(
[
'name' => config('shopify-app.billing_plan'),
'price' => config('shopify-app.billing_price'),
'test' => config('shopify-app.billing_test'),
'trial_days' => 5,
'return_url' => url(config('shopify-app.billing_redirect')),
],
$method->invoke($controller, $shop)
);
}

Expand All @@ -104,6 +143,6 @@ public function testReturnsBaseChargeType()
$method->setAccessible(true);

// Based on default config
$this->assertEquals(config('shopify-app.billing_type'), $method->invoke($controller, 'chargeType'));
$this->assertEquals(config('shopify-app.billing_type'), $method->invoke($controller));
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function createCharges()
$charge->trial_days = 7;
$charge->trial_ends_on = Carbon::today()->addWeeks(1)->format('Y-m-d');
$charge->cancelled_on = Carbon::today()->addDays(2)->format('Y-m-d');
$charge->shop_id = Shop::where('shopify_domain', 'example.myshopify.com')->first()->id;
$charge->shop_id = Shop::withTrashed()->where('shopify_domain', 'trashed-shop.myshopify.com')->first()->id;
$charge->save();
}
}

0 comments on commit bef44d6

Please sign in to comment.