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

Commit

Permalink
Merge pull request #22 from ohmybrew/usage-support
Browse files Browse the repository at this point in the history
Usage support built in to the plan init
  • Loading branch information
gnikyt authored Apr 25, 2018
2 parents 67ebbe9 + eb17d51 commit 4e0ad00
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/ShopifyApp/Libraries/BillingPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public function __construct(Shop $shop, string $chargeType = 'recurring')
*
* @param array $plan The plan details.
* $plan = [
* 'name' => (string) Plan name.
* 'price' => (float) Plan price. Required.
* 'test' => (boolean) Test mode or not.
* 'trial_days' => (int) Plan trial period in days.
* 'return_url' => (string) URL to handle response for acceptance or decline or billing. Required.
* 'name' => (string) Plan name.
* 'price' => (float) Plan price. Required.
* 'test' => (boolean) Test mode or not.
* 'trial_days' => (int) Plan trial period in days.
* 'return_url' => (string) URL to handle response for acceptance or decline or billing. Required.
* 'capped_amount' => (float) Capped price if using UsageCharge API.
* 'terms' => (string) Terms for the usage. Required if using capped_amount.
* ]
*
* @return $this
Expand Down Expand Up @@ -143,19 +145,26 @@ public function getConfirmationUrl()
throw new Exception('Plan details are missing for confirmation URL request.');
}

// Build the charge array
$chargeDetails = [
'test' => isset($this->details['test']) ? $this->details['test'] : false,
'trial_days' => isset($this->details['trial_days']) ? $this->details['trial_days'] : 0,
'name' => $this->details['name'],
'price' => $this->details['price'],
'return_url' => $this->details['return_url'],
];

// Handle capped amounts for UsageCharge API
if (isset($this->details['capped_amount'])) {
$chargeDetails['capped_amount'] = $this->details['capped_amount'];
$chargeDetails['terms'] = $this->details['terms'];
}

// Begin the charge request
$charge = $this->shop->api()->request(
'POST',
"/admin/{$this->chargeType}s.json",
[
"{$this->chargeType}" => [
'test' => isset($this->details['test']) ? $this->details['test'] : false,
'trial_days' => isset($this->details['trial_days']) ? $this->details['trial_days'] : 0,
'name' => $this->details['name'],
'price' => $this->details['price'],
'return_url' => $this->details['return_url'],
],
]
["{$this->chargeType}" => $chargeDetails]
)->body->{$this->chargeType};

return $charge->confirmation_url;
Expand Down
10 changes: 9 additions & 1 deletion src/ShopifyApp/Traits/BillingControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ public function process()
*/
protected function planDetails()
{
return [
$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')),
];

// Handle capped amounts for UsageCharge API
if (config('shopify-app.billing_capped_amount')) {
$plan['capped_amount'] = config('shopify-app.billing_capped_amount');
$plan['terms'] = config('shopify-app.billing_terms');
}

return $plan;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/ShopifyApp/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@

'billing_redirect' => env('SHOPIFY_BILLING_REDIRECT', '/billing/process'),

/*
|--------------------------------------------------------------------------
| Billing Capped Amount
|--------------------------------------------------------------------------
|
| The capped price for charging a customer when using the UsageCharge API.
|
*/

'billing_capped_amount' => env('SHOPIFY_BILLING_CAPPED_AMOUNT'),

/*
|--------------------------------------------------------------------------
| Billing Terms
|--------------------------------------------------------------------------
|
| Terms for the usage. Required if using capped amount.
|
*/

'billing_terms' => env('SHOPIFY_BILLING_TERMS'),

/*
|--------------------------------------------------------------------------
| Shopify Webhooks
Expand Down
23 changes: 23 additions & 0 deletions tests/Controllers/BillingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,30 @@ public function testReturnsBasePlanDetails()
'test' => config('shopify-app.billing_test'),
'trial_days' => config('shopify-app.billing_trial_days'),
'return_url' => url(config('shopify-app.billing_redirect')),
],
$method->invoke($controller, 'planDetails')
);
}

public function testReturnsBasePlanDetailsWithUsage()
{
config(['shopify-app.billing_capped_amount' => 100.00]);
config(['shopify-app.billing_terms' => '$1 for 100 emails.']);

$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' => config('shopify-app.billing_trial_days'),
'capped_amount' => config('shopify-app.billing_capped_amount'),
'terms' => config('shopify-app.billing_terms'),
'return_url' => url(config('shopify-app.billing_redirect')),
],
$method->invoke($controller, 'planDetails')
);
Expand Down
14 changes: 14 additions & 0 deletions tests/Libraries/BillingPlanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public function testShouldReturnConfirmationUrl()
);
}

public function testShouldReturnConfirmationUrlWhenUsageIsEnabled()
{
$plan = array_merge($this->plan, [
'capped_amount' => 100.00,
'terms' => '$1 for 500 emails',
]);
$url = (new BillingPlan($this->shop))->setDetails($plan)->getConfirmationUrl();

$this->assertEquals(
'https://example.myshopify.com/admin/charges/1029266947/confirm_recurring_application_charge?signature=BAhpBANeWT0%3D--64de8739eb1e63a8f848382bb757b20343eb414f',
$url
);
}

/**
* @expectedException \Exception
* @expectedExceptionMessage Plan details are missing for confirmation URL request.
Expand Down

0 comments on commit 4e0ad00

Please sign in to comment.