Skip to content

Commit 68e71eb

Browse files
committed
Completed code
1 parent c95f20d commit 68e71eb

File tree

14 files changed

+520
-5
lines changed

14 files changed

+520
-5
lines changed

src/ShopifyApp/Requests/AuthShop.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function rules()
4949
'code' => 'nullable|string',
5050
'hmac' => 'nullable|string',
5151
'timestamp' => 'nullable|numeric',
52+
'protocol' => 'nullable|string',
5253
];
5354
}
5455

src/ShopifyApp/Services/AuthShopHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function buildAuthUrl()
7979
}
8080

8181
/**
82-
* Determines if the request HMAC is veridied.
82+
* Determines if the request HMAC is verified.
8383
*
8484
* @param array $request The request parameters.
8585
*
@@ -113,7 +113,7 @@ public function storeAccessToken(string $code)
113113
/**
114114
* Dispatches the jobs that happen after authentication.
115115
*
116-
* @return void
116+
* @return boolean
117117
*/
118118
public function dispatchJobs()
119119
{
@@ -124,6 +124,8 @@ public function dispatchJobs()
124124
$this->dispatchWebhooks();
125125
$this->dispatchScripttags();
126126
$this->dispatchAfterAuthenticate();
127+
128+
return true;
127129
}
128130

129131
/**
@@ -159,6 +161,9 @@ public function dispatchScripttags()
159161
*/
160162
public function dispatchAfterAuthenticate()
161163
{
164+
// Grab the jobs config
165+
$jobsConfig = Config::get('shopify-app.after_authenticate_job');
166+
162167
/**
163168
* Fires the job.
164169
*

src/ShopifyApp/Services/UsageCharge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function activate()
9191
public function save()
9292
{
9393
if (!$this->response) {
94-
throw Exception('No activation response was recieved.');
94+
throw new Exception('No activation response was recieved.');
9595
}
9696

9797
// Get the plan charge

src/ShopifyApp/ShopifyApp.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(Application $app)
4141
/**
4242
* Gets/sets the current shop.
4343
*
44-
* @param string $shopDomain
44+
* @param string|null $shopDomain
4545
*
4646
* @return \OhMyBrew\ShopifyApp\Models\Shop
4747
*/
@@ -121,6 +121,7 @@ public function createHmac(array $opts)
121121
$data = $opts['data'];
122122
$raw = $opts['raw'] ?? false;
123123
$buildQuery = $opts['buildQuery'] ?? false;
124+
$buildQueryWithJoin = $opts['buildQueryWithJoin'] ?? false;
124125
$encode = $opts['encode'] ?? false;
125126
$secret = $opts['secret'] ?? Config::get('shopify-app.api_secret');
126127

@@ -131,7 +132,7 @@ public function createHmac(array $opts)
131132
foreach ($data as $key => $value) {
132133
$queryCompiled[] = "{$key}=".(is_array($value) ? implode($value, ',') : $value);
133134
}
134-
$data = implode($queryCompiled, '');
135+
$data = implode($queryCompiled, ($buildQueryWithJoin ? '&' : ''));
135136
}
136137

137138
// Create the hmac all based on the secret

src/ShopifyApp/resources/database/factories/ChargeFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
'type' => Charge::CHARGE_USAGE,
2929
]);
3030

31+
$factory->state(Charge::class, 'type_credit', [
32+
'type' => Charge::CHARGE_CREDIT,
33+
]);
34+
3135
$factory->state(Charge::class, 'trial', function ($faker) {
3236
$days = $faker->numberBetween(7, 14);
3337

tests/Middleware/BillableMiddlewareTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testEnabledBillingWithPaidShop()
3939
'plan_id' => $plan->id,
4040
'shop_id' => $shop->id,
4141
]);
42+
4243
Config::set('shopify-app.billing_enabled', true);
4344
Session::put('shopify_domain', $shop->shopify_domain);
4445

tests/Models/ChargeModelTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,22 @@ public function testRemainingTrialDaysFromCancel()
203203
'status' => 'cancelled',
204204
'shop_id' => $shop->id,
205205
]);
206+
$charge_3 = factory(Charge::class)->states('type_recurring')->create([
207+
'cancelled_on' => Carbon::today()->subDays(1),
208+
'status' => 'cancelled',
209+
'shop_id' => $shop->id,
210+
]);
211+
$charge_4 = factory(Charge::class)->states('type_recurring')->create([
212+
'trial_days' => 5,
213+
'trial_ends_on' => Carbon::today()->subDays(1),
214+
'status' => 'active',
215+
'shop_id' => $shop->id,
216+
]);
206217

207218
$this->assertEquals(2, $charge->remainingTrialDaysFromCancel());
208219
$this->assertEquals(0, $charge_2->remainingTrialDaysFromCancel());
220+
$this->assertNull($charge_3->remainingTrialDaysFromCancel());
221+
$this->assertEquals(0, $charge_4->remainingTrialDaysFromCancel());
209222
}
210223

211224
public function testRetreieve()
@@ -214,15 +227,52 @@ public function testRetreieve()
214227
Config::set('shopify-app.api_class', new ApiStub());
215228
ApiStub::stubResponses([
216229
'get_application_charge',
230+
'get_recurring_application_charge_activate',
231+
'get_application_credit'
217232
]);
218233

219234
$shop = factory(Shop::class)->create();
220235
$charge = factory(Charge::class)->states('type_onetime')->create([
221236
'shop_id' => $shop->id,
222237
]);
238+
$charge_2 = factory(Charge::class)->states('type_recurring')->create([
239+
'shop_id' => $shop->id,
240+
]);
241+
$charge_3 = factory(Charge::class)->states('type_credit')->create([
242+
'shop_id' => $shop->id,
243+
]);
244+
223245
$result = $charge->retrieve();
246+
$result_2 = $charge_2->retrieve();
247+
$result_3 = $charge_3->retrieve();
224248

225249
// Assert we get an object
226250
$this->assertTrue(is_object($result));
251+
$this->assertTrue(is_object($result_2));
252+
$this->assertTrue(is_object($result_3));
253+
}
254+
255+
public function testCancel()
256+
{
257+
$shop = factory(Shop::class)->create();
258+
$charge = factory(Charge::class)->states('type_recurring')->create([
259+
'status' => Charge::STATUS_ACTIVE,
260+
'shop_id' => $shop->id,
261+
]);
262+
$charge->cancel();
263+
264+
$this->assertEquals(Charge::STATUS_CANCELLED, $charge->status);
265+
}
266+
267+
/**
268+
* @expectedException Exception
269+
*/
270+
public function testCancelError()
271+
{
272+
$shop = factory(Shop::class)->create();
273+
$charge = factory(Charge::class)->states('type_usage')->create([
274+
'shop_id' => $shop->id,
275+
]);
276+
$charge->cancel();
227277
}
228278
}

tests/Models/PlanModelTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ public function testReturnsChargesForPlan()
2828
public function testReturnsTypeAsString()
2929
{
3030
$plan = factory(Plan::class)->states('type_recurring')->create();
31+
$plan_2 = factory(Plan::class)->states('type_onetime')->create();
3132

3233
$this->assertEquals('recurring_application_charge', $plan->typeAsString());
3334
$this->assertEquals('recurring_application_charges', $plan->typeAsString(true));
35+
$this->assertEquals('application_charge', $plan_2->typeAsString());
36+
$this->assertEquals('application_charges', $plan_2->typeAsString(true));
3437
}
3538

3639
public function testPlanHasTrial()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace OhMyBrew\ShopifyApp\Test\Requests;
4+
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
6+
use Illuminate\Support\Facades\Validator;
7+
use Illuminate\Support\Facades\Config;
8+
use OhMyBrew\ShopifyApp\Requests\AuthShop;
9+
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
10+
use OhMyBrew\ShopifyApp\Test\Stubs\ApiStub;
11+
12+
class AuthShopRequestTest extends TestCase
13+
{
14+
public function testFailsWithNoCode()
15+
{
16+
$validator = Validator::make(
17+
[
18+
'code' => '1234',
19+
],
20+
(new AuthShop())->rules()
21+
);
22+
23+
$this->assertTrue($validator->fails());
24+
}
25+
26+
public function testFailsWithInvalidHmac()
27+
{
28+
$data = [
29+
'shop' => 'test.myshopify.com',
30+
'code' => '1234',
31+
'timestamp' => time(),
32+
'protocol' => 'https',
33+
];
34+
$hmac = ShopifyApp::createHmac([
35+
'data' => $data,
36+
'buildQuery' => true,
37+
'buildQueryWithJoin' => true,
38+
]);
39+
40+
$data['shop'] = 'oops';
41+
42+
$authShop = new AuthShop([], $data);
43+
$validator = Validator::make(
44+
array_merge($data, ['hmac' => $hmac]),
45+
$authShop->rules()
46+
);
47+
$authShop->withValidator($validator);
48+
49+
$this->assertTrue($validator->fails());
50+
}
51+
52+
public function testPasses()
53+
{
54+
$data = [
55+
'shop' => 'test.myshopify.com',
56+
'timestamp' => time(),
57+
'protocol' => 'https',
58+
];
59+
$hmac = ShopifyApp::createHmac([
60+
'data' => $data,
61+
'buildQuery' => true,
62+
'buildQueryWithJoin' => true,
63+
]);
64+
$data['hmac'] = $hmac;
65+
66+
$authShop = new AuthShop([], $data);
67+
$validator = Validator::make(
68+
array_merge($data, ['hmac' => $hmac]),
69+
$authShop->rules()
70+
);
71+
$authShop->withValidator($validator);
72+
73+
$this->assertFalse($validator->fails());
74+
}
75+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace OhMyBrew\ShopifyApp\Test\Requests;
4+
5+
use OhMyBrew\ShopifyApp\Test\TestCase;
6+
use Illuminate\Support\Facades\Validator;
7+
use Illuminate\Support\Facades\Config;
8+
use OhMyBrew\ShopifyApp\Requests\StoreUsageCharge;
9+
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
10+
11+
class StoreUsageChargeRequestTest extends TestCase
12+
{
13+
public function testFailsWithNoCode()
14+
{
15+
$validator = Validator::make(
16+
[],
17+
(new StoreUsageCharge())->rules()
18+
);
19+
20+
$this->assertTrue($validator->fails());
21+
}
22+
23+
public function testFailsForInvalidSignature()
24+
{
25+
$data = [
26+
'price' => '1.00',
27+
'description' => 'Testing'
28+
];
29+
30+
$signature = ShopifyApp::createHmac(['data' => $data, 'buildQuery' => true]);
31+
$data['signature'] = $signature;
32+
$data['price'] = '2.00';
33+
34+
$storeUsage = new StoreUsageCharge([], $data);
35+
$validator = Validator::make($data, $storeUsage->rules());
36+
$storeUsage->withValidator($validator);
37+
38+
$this->assertTrue($validator->fails());
39+
}
40+
41+
public function testPasses()
42+
{
43+
$data = [
44+
'price' => '1.00',
45+
'description' => 'Testing'
46+
];
47+
$signature = ShopifyApp::createHmac(['data' => $data, 'buildQuery' => true]);
48+
$data['signature'] = $signature;
49+
50+
$storeUsage= new StoreUsageCharge([], $data);
51+
$validator = Validator::make($data, $storeUsage->rules());
52+
$storeUsage->withValidator($validator);
53+
54+
$this->assertFalse($validator->fails());
55+
}
56+
}

0 commit comments

Comments
 (0)