From 68eb2a119313ddced470e965df40adc881ab306d Mon Sep 17 00:00:00 2001 From: Usman Shaukat Date: Tue, 6 Sep 2022 16:01:37 +0500 Subject: [PATCH] Update BillingController.php (#1181) --- src/Traits/BillingController.php | 34 ++++++++++++++------------ tests/Traits/BillingControllerTest.php | 19 ++++++++++++++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/Traits/BillingController.php b/src/Traits/BillingController.php index 6cae42de..5b07e29d 100644 --- a/src/Traits/BillingController.php +++ b/src/Traits/BillingController.php @@ -27,18 +27,18 @@ trait BillingController /** * Redirects to billing screen for Shopify. * - * @param Request $request The request object. - * @param ShopQuery $shopQuery The shop querier. - * @param GetPlanUrl $getPlanUrl The action for getting the plan URL. - * @param int|null $plan The plan's ID, if provided in route. + * @param Request $request The request object. + * @param ShopQuery $shopQuery The shop querier. + * @param GetPlanUrl $getPlanUrl The action for getting the plan URL. + * @param int|null $plan The plan's ID, if provided in route. * * @return ViewView */ public function index( - Request $request, - ShopQuery $shopQuery, + Request $request, + ShopQuery $shopQuery, GetPlanUrl $getPlanUrl, - ?int $plan = null + ?int $plan = null ): ViewView { // Get the shop $shop = $shopQuery->getByDomain(ShopDomain::fromNative($request->get('shop'))); @@ -59,22 +59,26 @@ public function index( /** * Processes the response from the customer. * - * @param int $plan The plan's ID. - * @param Request $request The HTTP request object. - * @param ShopQuery $shopQuery The shop querier. + * @param int $plan The plan's ID. + * @param Request $request The HTTP request object. + * @param ShopQuery $shopQuery The shop querier. * @param ActivatePlan $activatePlan The action for activating the plan for a shop. * * @return RedirectResponse */ public function process( - int $plan, - Request $request, - ShopQuery $shopQuery, + int $plan, + Request $request, + ShopQuery $shopQuery, ActivatePlan $activatePlan ): RedirectResponse { // Get the shop $shop = $shopQuery->getByDomain(ShopDomain::fromNative($request->query('shop'))); - + if (!$request->has('charge_id')) { + return Redirect::route(Util::getShopifyConfig('route_names.home'), [ + 'shop' => $shop->getDomain()->toNative(), + ]); + } // Activate the plan and save $result = $activatePlan( $shop->getId(), @@ -94,7 +98,7 @@ public function process( /** * Allows for setting a usage charge. * - * @param StoreUsageCharge $request The verified request. + * @param StoreUsageCharge $request The verified request. * @param ActivateUsageCharge $activateUsageCharge The action for activating a usage charge. * * @return RedirectResponse diff --git a/tests/Traits/BillingControllerTest.php b/tests/Traits/BillingControllerTest.php index 3b0b94b4..eef18804 100644 --- a/tests/Traits/BillingControllerTest.php +++ b/tests/Traits/BillingControllerTest.php @@ -130,4 +130,23 @@ public function testUsageChargeSuccess(): void $response->assertRedirect('http://localhost'); $response->assertSessionHas('success'); } + + public function testReturnToSettingScreenNoPlan() + { + // Set up a shop + $shop = factory($this->model)->create([ + 'plan_id' => null, + ]); + //Log in + $this->auth->login($shop); + $url = 'https://example-app.com/billing/process/9999?shop='.$shop->name; + // Try to go to bill without a charge id which happens when you cancel the charge + $response = $this->call( + 'get', + $url, + ['shop' => $shop->name] + ); + //Confirm we get sent back to the homepage of the app + $response->assertRedirect('https://example-app.com?shop='.$shop->name); + } }