diff --git a/src/Controllers/OrderFormJsonController.php b/src/Controllers/OrderFormJsonController.php old mode 100644 new mode 100755 index 9af73a8c..18cd6fa1 --- a/src/Controllers/OrderFormJsonController.php +++ b/src/Controllers/OrderFormJsonController.php @@ -5,6 +5,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller; use Railroad\Ecommerce\Exceptions\NotFoundException; +use Railroad\Ecommerce\Exceptions\RedirectNeededException; use Railroad\Ecommerce\Requests\OrderFormSubmitRequest; use Railroad\Ecommerce\Services\CartService; use Railroad\Ecommerce\Services\OrderFormService; @@ -94,8 +95,22 @@ public function submitOrder(OrderFormSubmitRequest $request) return ResponseService::order($result['order']) ->addMeta(['redirect' => config('ecommerce.order_form_post_purchase_redirect_path_without_brand') . $result['order']->getBrand()]); } - } - elseif (isset($result['errors'])) { + } elseif ((isset($result['redirect-with-message']) && $result['redirect-with-message'])) { + + /** @var $redirectNeededException RedirectNeededException */ + $redirectNeededException = $result['redirect-needed-exception']; + + return response()->json( + [ + 'modal-show-redirect-with-message' => true, + 'modal-header' => $redirectNeededException->getMessageTitleText(), + 'modal-message' => $redirectNeededException->getRedirectMessageToUser(), + 'modal-button-text' => $redirectNeededException->getButtonText(), + 'modal-button-url' => $redirectNeededException->getUrlRedirect() + ], + 422 + ); + } elseif (isset($result['errors'])) { $errors = []; foreach ($result['errors'] as $message) { @@ -111,8 +126,13 @@ public function submitOrder(OrderFormSubmitRequest $request) ], 404 ); - } - elseif ($result['redirect'] && !isset($result['errors'])) { + } elseif ($result['redirect-with-message'] && isset($result['redirect-message']) && isset($result['redirect-url'])) { + + return ResponseService::redirectWithMessage($result['redirect-url'], $result['redirect-message']) + ->addMeta(['redirect' => $result['redirect-url']]) + ->addMeta(['message' => $result['redirect-message']]); + + } elseif ($result['redirect'] && !isset($result['errors'])) { return ResponseService::redirect($result['redirect']); } diff --git a/src/Exceptions/RedirectNeededException.php b/src/Exceptions/RedirectNeededException.php new file mode 100755 index 00000000..3b85de52 --- /dev/null +++ b/src/Exceptions/RedirectNeededException.php @@ -0,0 +1,59 @@ +urlRedirect = $urlRedirect; + $this->redirectMessageToUser = $redirectMessageToUser; + $this->messageTitleText = $messageTitleText; + $this->buttonText = $buttonText; + } + + public function getUrlRedirect() + { + return $this->urlRedirect; + } + + public function getRedirectMessageToUser() + { + return $this->redirectMessageToUser; + } + + public function getMessageTitleText() + { + return $this->messageTitleText; + } + + public function getButtonText() + { + return $this->buttonText; + } +} diff --git a/src/Services/OrderFormService.php b/src/Services/OrderFormService.php old mode 100644 new mode 100755 index dadfb6e3..4daace52 --- a/src/Services/OrderFormService.php +++ b/src/Services/OrderFormService.php @@ -9,6 +9,7 @@ use Railroad\Ecommerce\Events\GiveContentAccess; use Railroad\Ecommerce\Exceptions\Cart\ProductOutOfStockException; use Railroad\Ecommerce\Exceptions\PaymentFailedException; +use Railroad\Ecommerce\Exceptions\RedirectNeededException; use Railroad\Ecommerce\Exceptions\StripeCardException; use Railroad\Ecommerce\Gateways\PayPalPaymentGateway; use Railroad\Ecommerce\Repositories\PaymentMethodRepository; @@ -108,7 +109,6 @@ public function __construct( */ public function processOrderFormSubmit(OrderFormSubmitRequest $request): array { - try { // setup the cart $cart = $request->getCart(); @@ -270,8 +270,14 @@ public function processOrderFormSubmit(OrderFormSubmitRequest $request): array $paymentMethod = $payment->getPaymentMethod(); } - } catch (PaymentFailedException $paymentFailedException) { + } catch (RedirectNeededException $redirectNeededException) { + + return [ + 'redirect-with-message' => true, + 'redirect-needed-exception' => $redirectNeededException, + ]; + } catch (PaymentFailedException $paymentFailedException) { $url = $request->get('redirect') ?? strtok(app('url')->previous(), '?'); return [ diff --git a/src/Services/OrderValidationService.php b/src/Services/OrderValidationService.php index cc516a5e..b0170fb1 100644 --- a/src/Services/OrderValidationService.php +++ b/src/Services/OrderValidationService.php @@ -6,6 +6,7 @@ use Railroad\Ecommerce\Entities\Structures\Cart; use Railroad\Ecommerce\Entities\Structures\Purchaser; use Railroad\Ecommerce\Exceptions\PaymentFailedException; +use Railroad\Ecommerce\Exceptions\RedirectNeededException; class OrderValidationService { @@ -40,31 +41,57 @@ public function __construct(UserProductService $userProductService, CartService public function validateOrder(Cart $cart, Purchaser $purchaser) { if (!empty($purchaser->getId())) { - // check if they have any trial membership user products for brand that were valid within the last 120 days + // check if they have any trial membership user products for that were valid within the last x days $this->userProductService->arrayCache->deleteAll(); $usersProducts = $this->userProductService->getAllUsersProducts($purchaser->getId()); - $hasTrialCreatedWithinPeriod = false; + $hasRecentTrial = false; foreach ($usersProducts as $userProduct) { - if ($userProduct->getProduct()->getBrand() == $purchaser->getBrand() && - strpos(strtolower($userProduct->getProduct()->getSku()), 'trial') !== false && - !empty($userProduct->getExpirationDate()) && - $userProduct->getExpirationDate() > Carbon::now()->subDays(120)) { - $hasTrialCreatedWithinPeriod = true; + + if(empty($userProduct->getExpirationDate())){ + continue; + } + + $productAlmostCertainlyTrial = + strpos(strtolower($userProduct->getProduct()->getSku()), 'trial') !== false; + + $expiryWithinTimeConstraint = $userProduct->getExpirationDate() > Carbon::now()->subDays(90); + + if ($productAlmostCertainlyTrial && $expiryWithinTimeConstraint) { + $hasRecentTrial = true; } } - if ($hasTrialCreatedWithinPeriod) { + if ($hasRecentTrial) { $this->cartService->setCart($cart); - // then check if they are trying to order a trial product, if so, reject it + // check if they are trying to order a trial product, if so, reject it foreach ($cart->getItems() as $cartItem) { if (strpos(strtolower($cartItem->getSku()), 'trial') !== false && + $this->cartService->getDueForInitialPayment() === 0) { - throw new PaymentFailedException( - 'This account is not eligible for a free trial period. Please choose a regular membership.' + + $urlForEvergreenSalesPage = 'https://www.' . $purchaser->getBrand() . '.com/lp'; + if(env('APP_ENV') === 'local'){ + $urlForEvergreenSalesPage = 'https://dev.' . $purchaser->getBrand() . '.com:8443/lp'; + } + + $redirectMessageToUser = 'It looks like you’ve started a trial with Musora in the last 90 ' . + 'days. Unfortunately, that means that your account is not eligible to start another ' . + 'trial at this time. Click below to check out a special offer and start your membership ' . + 'today!'; + + $messageTitleText = 'Something went wrong'; + + $buttonText = 'YOUR OFFER'; + + throw new RedirectNeededException( + $urlForEvergreenSalesPage, + $redirectMessageToUser, + $messageTitleText, + $buttonText ); } } diff --git a/src/Services/ResponseService.php b/src/Services/ResponseService.php index 5a470b67..a97aac7b 100644 --- a/src/Services/ResponseService.php +++ b/src/Services/ResponseService.php @@ -498,6 +498,25 @@ function () { )->addMeta(['redirect' => $url]); } + /** + * @param string $url + * + * @return Fractal + */ + public static function redirectWithMessage(string $url, string $message) + { + return fractal( + null, + function () { + return null; + }, + new JsonApiSerializer() + )->addMeta([ + 'redirect' => $url, + 'message' => $message + ]); + } + /** * @param array $cartItems * @param Address $billingAddress