Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.0 fix order validation trial users #17

Open
wants to merge 4 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Controllers/OrderFormJsonController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {

Expand All @@ -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']);
}
Expand Down
59 changes: 59 additions & 0 deletions src/Exceptions/RedirectNeededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Railroad\Ecommerce\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;

class RedirectNeededException extends Exception
{
protected $urlRedirect;
protected $redirectMessageToUser;
protected $messageTitleText;
protected $buttonText;

/**
* NotAllowedException constructor.
*
* @param $urlRedirect
* @param $redirectMessageToUser
* @param $messageTitleText
* @param $buttonText
*/
public function __construct(
$urlRedirect,
$redirectMessageToUser,
$messageTitleText,
$buttonText
)
{
parent::__construct(
'Redirect-with-message required with message: "' . $redirectMessageToUser . '".'
);

$this->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;
}
}
10 changes: 8 additions & 2 deletions src/Services/OrderFormService.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,7 +109,6 @@ public function __construct(
*/
public function processOrderFormSubmit(OrderFormSubmitRequest $request): array
{

try {
// setup the cart
$cart = $request->getCart();
Expand Down Expand Up @@ -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 [
Expand Down
49 changes: 38 additions & 11 deletions src/Services/OrderValidationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
);
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/Services/ResponseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down