Skip to content

Commit b75986d

Browse files
authored
Merge pull request #1 from kurakin-oleksandr/feature_pass_host_param
2 parents bbd5ce7 + 2be5d4a commit b75986d

File tree

8 files changed

+33
-19
lines changed

8 files changed

+33
-19
lines changed

src/Actions/ActivatePlan.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ public function __construct(
9999
* @param ShopId $shopId The shop ID.
100100
* @param PlanId $planId The plan to use.
101101
* @param ChargeReference $chargeRef The charge ID from Shopify.
102+
* @param string $host
102103
*
103104
* @return ChargeId
104105
*/
105-
public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $chargeRef): ChargeId
106+
public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $chargeRef, string $host): ChargeId
106107
{
107108
// Get the shop
108109
$shop = $this->shopQuery->getById($shopId);
@@ -136,7 +137,7 @@ public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $charge
136137
$transfer->billingOn = null;
137138
$transfer->trialEndsOn = null;
138139
}
139-
$transfer->planDetails = $this->chargeHelper->details($plan, $shop);
140+
$transfer->planDetails = $this->chargeHelper->details($plan, $shop, $host);
140141

141142
// Create the charge
142143
$charge = $this->chargeCommand->make($transfer);

src/Actions/GetPlanUrl.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public function __construct(ChargeHelper $chargeHelper, IPlanQuery $planQuery, I
5858
*
5959
* @param ShopId $shopId The shop ID.
6060
* @param NullablePlanId $planId The plan to present.
61+
* @param string $host
6162
*
6263
* @return string
6364
*/
64-
public function __invoke(ShopId $shopId, NullablePlanId $planId): string
65+
public function __invoke(ShopId $shopId, NullablePlanId $planId, string $host): string
6566
{
6667
// Get the shop
6768
$shop = $this->shopQuery->getById($shopId);
@@ -72,14 +73,14 @@ public function __invoke(ShopId $shopId, NullablePlanId $planId): string
7273
// Confirmation URL
7374
if ($plan->getInterval()->toNative() === ChargeInterval::ANNUAL()->toNative()) {
7475
$api = $shop->apiHelper()
75-
->createChargeGraphQL($this->chargeHelper->details($plan, $shop));
76+
->createChargeGraphQL($this->chargeHelper->details($plan, $shop, $host));
7677

7778
$confirmationUrl = $api['confirmationUrl'];
7879
} else {
7980
$api = $shop->apiHelper()
8081
->createCharge(
8182
ChargeType::fromNative($plan->getType()->toNative()),
82-
$this->chargeHelper->details($plan, $shop)
83+
$this->chargeHelper->details($plan, $shop, $host)
8384
);
8485

8586
$confirmationUrl = $api['confirmation_url'];

src/Services/ChargeHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,11 @@ public function chargeForPlan(PlanId $planId, IShopModel $shop): ?ChargeModel
247247
*
248248
* @param Plan $plan The plan.
249249
* @param IShopModel $shop The shop the plan is for.
250+
* @param string $host
250251
*
251252
* @return PlanDetailsTransfer
252253
*/
253-
public function details(Plan $plan, IShopModel $shop): PlanDetailsTransfer
254+
public function details(Plan $plan, IShopModel $shop, string $host): PlanDetailsTransfer
254255
{
255256
// Handle capped amounts for UsageCharge API
256257
$isCapped = isset($plan->capped_amount) && $plan->capped_amount > 0;
@@ -267,7 +268,7 @@ public function details(Plan $plan, IShopModel $shop): PlanDetailsTransfer
267268
$transfer->returnUrl = URL::secure(
268269
Util::getShopifyConfig('billing_redirect'),
269270
['plan' => $plan->getId()->toNative()]
270-
).'?'.http_build_query(['shop' => $shop->getDomain()->toNative()]);
271+
).'?'.http_build_query(['shop' => $shop->getDomain()->toNative(), 'host' => $host]);
271272

272273
return $transfer;
273274
}

src/Traits/BillingController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ public function index(
4545
): ViewView {
4646
// Get the shop
4747
$shop = $shopQuery->getByDomain(ShopDomain::fromNative($request->get('shop')));
48+
$host = $request->get('host');
4849

4950
// Get the plan URL for redirect
5051
$url = $getPlanUrl(
5152
$shop->getId(),
52-
NullablePlanId::fromNative($plan)
53+
NullablePlanId::fromNative($plan),
54+
$host
5355
);
5456

5557
// Do a fullpage redirect
@@ -77,24 +79,26 @@ public function process(
7779
): RedirectResponse {
7880
// Get the shop
7981
$shop = $shopQuery->getByDomain(ShopDomain::fromNative($request->query('shop')));
82+
$host = $request->query('host', base64_encode($shop->getDomain()->toNative().'/admin'));
8083
if (!$request->has('charge_id')) {
8184
return Redirect::route(Util::getShopifyConfig('route_names.home'), [
8285
'shop' => $shop->getDomain()->toNative(),
83-
'host' => base64_encode($shop->getDomain()->toNative().'/admin'),
86+
'host' => $host,
8487
]);
8588
}
8689
// Activate the plan and save
8790
$result = $activatePlan(
8891
$shop->getId(),
8992
PlanId::fromNative($plan),
90-
ChargeReference::fromNative((int) $request->query('charge_id'))
93+
ChargeReference::fromNative((int) $request->query('charge_id')),
94+
$host
9195
);
9296

9397
// Go to homepage of app
9498
return Redirect::route(Util::getShopifyConfig('route_names.home'), array_merge([
9599
'shop' => $shop->getDomain()->toNative(),
96100
], Util::useNativeAppBridge() ? [] : [
97-
'host' => base64_encode($shop->getDomain()->toNative().'/admin'),
101+
'host' => $host,
98102
'billing' => $result ? 'success' : 'failure',
99103
]))->with(
100104
$result ? 'success' : 'failure',

tests/Actions/ActivatePlanTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function testRunRecurring(): void
5151
$this->action,
5252
$shop->getId(),
5353
$plan->getId(),
54-
ChargeReference::fromNative(12345)
54+
ChargeReference::fromNative(12345),
55+
'decoded_host'
5556
);
5657

5758
$this->assertInstanceOf(ChargeId::class, $result);
@@ -83,7 +84,8 @@ public function testRunOnetime(): void
8384
$this->action,
8485
$shop->getId(),
8586
$plan->getId(),
86-
ChargeReference::fromNative(12345)
87+
ChargeReference::fromNative(12345),
88+
'decoded_host'
8789
);
8890

8991
$this->assertInstanceOf(ChargeId::class, $result);

tests/Actions/GetPlanUrlTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public function testRun30Days(): void
3838
$result = call_user_func(
3939
$this->action,
4040
$shop->getId(),
41-
NullablePlanId::fromNative(null)
41+
NullablePlanId::fromNative(null),
42+
'decoded_host'
4243
);
4344

4445
$this->assertNotEmpty($result);
@@ -59,7 +60,8 @@ public function testRunAnnual(): void
5960
$result = call_user_func(
6061
$this->action,
6162
$shop->getId(),
62-
NullablePlanId::fromNative(null)
63+
NullablePlanId::fromNative(null),
64+
'decoded_host'
6365
);
6466

6567
$this->assertNotEmpty($result);

tests/Services/ChargeHelperTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ public function testDetails(): void
136136
{
137137
// Seed (trial)
138138
$seed = $this->seedData();
139-
$result = $this->chargeHelper->details($seed['plan'], $seed['shop']);
139+
$result = $this->chargeHelper->details($seed['plan'], $seed['shop'], $seed['host']);
140140
$this->assertInstanceOf(PlanDetails::class, $result);
141141

142142
// Seed (no trial)
143143
$seed = $this->seedData([], ['trial_days' => 0]);
144-
$result = $this->chargeHelper->details($seed['plan'], $seed['shop']);
144+
$result = $this->chargeHelper->details($seed['plan'], $seed['shop'], $seed['host']);
145145
$this->assertInstanceOf(PlanDetails::class, $result);
146146
}
147147

@@ -157,7 +157,9 @@ public function testDetails2(): void
157157
'plan_id' => $plan->getId()->toNative(),
158158
]);
159159

160-
$result = $this->chargeHelper->details($plan, $shop);
160+
$host = 'decoded_host';
161+
162+
$result = $this->chargeHelper->details($plan, $shop, $host);
161163
$this->assertInstanceOf(PlanDetails::class, $result);
162164
}
163165

@@ -192,6 +194,7 @@ protected function seedData($extraCharge = [], $extraPlan = [], $type = 'onetime
192194
'plan' => $plan,
193195
'shop' => $shop,
194196
'charge' => $charge,
197+
'host' => 'decoded_host',
195198
];
196199
}
197200
}

tests/Traits/BillingControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testSendsShopToBillingScreen(): void
4444
factory(Util::getShopifyConfig('models.plan', Plan::class))->states('type_recurring', 'installable')->create();
4545

4646
// Run the call
47-
$response = $this->call('get', '/billing', ['shop' => $shop->getDomain()->toNative()]);
47+
$response = $this->call('get', '/billing', ['shop' => $shop->getDomain()->toNative(), 'host' => 'decoded_host']);
4848
$response->assertViewHas(
4949
'url',
5050
'https://example.myshopify.com/admin/charges/1029266947/confirm_recurring_application_charge?signature=BAhpBANeWT0%3D--64de8739eb1e63a8f848382bb757b20343eb414f'

0 commit comments

Comments
 (0)