src/Controller/RCS/AuthController.php line 680

Open in your IDE?
  1. <?php
  2. namespace App\Controller\RCS;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use App\Entity\Purchase;
  9. use App\Entity\PurchaseItem;
  10. use App\Entity\User;
  11. use App\Entity\Contact;
  12. use App\Entity\Coupon;
  13. use App\Entity\PurchaseNote;
  14. use App\Entity\Product;
  15. use App\Utils\ContentHelper;
  16. use App\Utils\UserHelper;
  17. use App\Utils\ContactHelper;
  18. use App\Utils\EmailHelper;
  19. use App\Utils\OrderHelper;
  20. use App\Utils\LeadHelper;
  21. use App\Security\WordPressPasswordEncoder;
  22. use App\Exception\ResourceNotFoundException;
  23. use Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider;
  24. use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
  25. use Symfony\Component\HttpFoundation\Cookie;
  26. class AuthController extends DefaultController
  27. {
  28.     /**
  29.      * @Route("/sign-in",
  30.      *    name="sign-in-post",
  31.      *    defaults={"message": ""},
  32.      *  methods={"POST"}
  33.      *    )
  34.      */
  35.     public function signInPost(
  36.         Request $request,
  37.         UserHelper $userHelper,
  38.         EmailHelper $emailHelper,
  39.         DoctrineTokenProvider $doctrineTokenProvider,
  40.         $message ""
  41.     
  42.     {
  43.         $username $request->get("_username");
  44.         $password $request->get("_password");
  45.         if(!$emailHelper->checkCaptcha($request->get("g-recaptcha-response"))) {
  46.             $this->addFlash("warning""The reCAPTCHA was invalid. Please try again.");
  47.             $message = array (
  48.                 "type" => "warning",
  49.                 "text" => "The reCAPTCHA was invalid. Please try again.",
  50.             );
  51.             return $this->defaultRender("forms/sign-in.html.twig", [
  52.                 "message" => $message,
  53.             ]);
  54.         }
  55.         $session $request->getSession();
  56.         $nRoute $session->get("non_sign_in_route");
  57.         $path = !empty($nRoute["path"]) ? $nRoute["path"] : "/";
  58.         $result $userHelper->signIn(
  59.             $username
  60.             $password,
  61.             //$this->get("security.token_storage"),
  62.             //$this->get("event_dispatcher")
  63.         );
  64.         if(is_array($result) && $result["status"] == 2) {
  65.             return new RedirectResponse("/mfa/{$result['uid']}/{$result['token']}");
  66.         }
  67.         elseif (is_array($result) && $result["status"] == 1) {
  68.             $this->addFlash("success""You have successfully logged in.");
  69.             $response = new RedirectResponse($path);
  70.             $response $this->setRememberMe($request$response$doctrineTokenProvider$username);
  71.         }
  72.         else {            
  73.             $message = array (
  74.                 "type" => "warning",
  75.                 "text" => "Invalid credentials provided.",
  76.             );
  77.             return $this->defaultRender("forms/sign-in.html.twig", [
  78.                 "message" => $message,
  79.             ]);
  80.         }
  81.         
  82.         return $response;
  83.     }
  84.     
  85.     /**
  86.      * @Route("/sign-in",
  87.      *    name="sign-in",
  88.      *    defaults={"message": ""}
  89.      *    )
  90.      */
  91.     public function signIn(
  92.         Request $request,
  93.         AuthenticationUtils $authUtils,
  94.         WordPressPasswordEncoder $wpEncoder,
  95.         $message ""
  96.     ) {
  97.         //just testing: $message = $wpEncoder->hash("M0byd1ck771!"); 
  98.         //$message = null;
  99.         $message $request->query->get("message") ?? "";
  100.         
  101.         $error $authUtils->getLastAuthenticationError();
  102.         $lastUsername $authUtils->getLastUsername();
  103.         if ($error) {
  104.             $message = array (
  105.                 "type" => "warning",
  106.                 "text" => "Invalid credentials.",
  107.             );
  108.         }
  109.         
  110.         
  111.         return $this->defaultRender("forms/sign-in.html.twig", [
  112.             "last_username" => $lastUsername,
  113.             // "error" => $error,
  114.             "message" => $message,
  115.         ]);
  116.     }
  117.     
  118.     /**
  119.      * @Route("/forgot-password", name="forgot-password")
  120.      */
  121.     public function forgotPassword (
  122.         Request $request,
  123.         UserHelper $userHelper,
  124.         ContactHelper $contactHelper,
  125.         EmailHelper $emailHelper
  126.     ) {        
  127.         // should we rate limit ??
  128.         $username $request->request->get("username");
  129.         $message = array ();
  130.         
  131.         if ($username) {
  132.             $rsp $userHelper->forgotPassword($username);
  133.             $message = array(
  134.                 "type" => $rsp["status"] ? "success" "warning",
  135.                 "text" => $rsp["message"],
  136.             );
  137.         }
  138.         
  139.         return $this->defaultRender("forms/forgot-password.html.twig", [
  140.             "message" => $message,
  141.         ]);
  142.     }
  143.     
  144.     /**
  145.      * @Route("/forgot-username", name="forgot-username", methods={"POST"})
  146.      */
  147.     public function forgotUsername (
  148.         Request $request,
  149.         UserHelper $userHelper
  150.     ) {
  151.         $email $request->get("email");
  152.         $rsp $userHelper->forgotUsername($email);
  153.         $msg = array ();
  154.         
  155.         if ($rsp["status"]) {
  156.             $msg = array (
  157.                 "type" => "success",
  158.                 "text" => $rsp["message"],
  159.             );
  160.         } 
  161.         
  162.         else {
  163.             sleep (3);
  164.             $msg = array (
  165.                 "type" => "warning",
  166.                 "text" => $rsp["message"],
  167.             );
  168.         }
  169.         
  170.         return $this->defaultRender("forms/forgot-username.html.twig", [
  171.             "message" => $msg
  172.         ]);
  173.         
  174.         /*
  175.         try {
  176.             $user = $userHelper->getUserByEmail($email);
  177.             $username = $user->getUsername();
  178.             
  179.             return $this->defaultRender("forms/sign-in.html.twig", [
  180.                 "message" => [
  181.                     "type" => "success",
  182.                     "text" => "The username linked to that account is <strong>{$username}</strong>.",
  183.                 ]
  184.             ]);
  185.             
  186.         } catch (ResourceNotFoundException $e) {
  187.             sleep(3);
  188.             return $this->defaultRender("forms/forgot-username.html.twig", [
  189.                 "message" => [
  190.                     "type" => "warning",
  191.                     "text" => "That e-mail address was not found in the system.",
  192.                 ]
  193.             ]);
  194.         } 
  195.         */    
  196.     }
  197.     
  198.     /**
  199.      * @Route("/forgot-username", name="forgot-username-form")
  200.      */
  201.     public function viewForgotUsername (
  202.     
  203.     ) {
  204.         
  205.         return $this->defaultRender("forms/forgot-username.html.twig", [
  206.             "message" => array ()
  207.         ]);
  208.     }
  209.     
  210.     /**
  211.      * @Route("/rcs-reset-password/{id}/{token}", name="reset-password-form", defaults={"message": ""})
  212.      */
  213.     public function viewResetPassword (
  214.         UserHelper $userHelper,
  215.         Request $request,
  216.         $id
  217.         $token,
  218.         $message ""
  219.     ) {
  220.         
  221.         try {
  222.             $user $userHelper->getUserById($id);
  223.             
  224.             $message $request->query->get("message") ?? "";
  225.             // if token does not match 
  226.             if ($token != $user->getResetPwToken()) {
  227.                 return $this->defaultRender("errors/404.html.twig");
  228.             }
  229.             
  230.             return $this->defaultRender("forms/reset-password.html.twig", [
  231.                 "user" => $user,
  232.                 "id" => $id,
  233.                 "token" => $token,
  234.                 "message" => $message
  235.             ]);
  236.         } catch (ResourceNotFoundException $e) {
  237.             
  238.         }
  239.     }
  240.     
  241.     /**
  242.      * @Route("/rcs-reset-password", name="reset-password", methods={"POST"})
  243.      */
  244.     public function resetPassword (
  245.         Request $request,
  246.         WordPressPasswordEncoder $wpEncoder,
  247.         UserHelper $userHelper
  248.     ) {
  249.         
  250.         $id $request->get("id");
  251.         $token $request->get("token");
  252.         $password1 $request->get("password1");
  253.         $password2 $request->get("password2");
  254.         
  255.         $rsp $userHelper->resetPassword(
  256.             $id,
  257.             $token,
  258.             $password1,
  259.             $password2
  260.         );
  261.         
  262.         $msg = array ();
  263.         
  264.         if ($rsp["status"]) {
  265.             $msg = array (
  266.                 "type" => "success",
  267.                 "text" => $rsp["message"],
  268.             );
  269.             return $this->redirectToRoute("sign-in", [
  270.                 "message" => $msg
  271.             ]);
  272.         } 
  273.         
  274.         else {
  275.             $msg = array (
  276.                 "type" => "warning",
  277.                 "text" => $rsp["message"],
  278.             );
  279.             return $this->redirectToRoute("reset-password-form", [
  280.                "id" => $id,
  281.                "token" => $token,
  282.                "message" => $msg
  283.             ]);
  284.         }
  285.         
  286.     }
  287.      
  288.     /* *
  289.      * @ R o u t e ("/rcs-sign-up", name="sign-up", methods={"POST"})
  290.      * /
  291.     public function signUp (
  292.         Request $request,
  293.         UserHelper $userHelper,
  294.         EmailHelper $emailHelper
  295.     ) {
  296.         
  297.         $username = $request->get("username");
  298.         $email = $request->get("email");
  299.         
  300.         if($emailHelper->checkCaptcha($request->get("g-recaptcha-response"))) {
  301.             
  302.             $password1 = $request->get("password1");
  303.             $password2 = $request->get("password2");
  304.             
  305.             $rsp = $userHelper->signUp(
  306.                 $username,
  307.                 $email,
  308.                 $password1,
  309.                 $password2
  310.             );
  311.             
  312.             if ($rsp["status"]) {
  313.                 return $this->defaultRender("forms/sign-in.html.twig", [
  314.                     "message" => $rsp["message"],
  315.                 ]);
  316.             }
  317.             
  318.             return $this->defaultRender("forms/sign-up.html.twig", [
  319.                 "username" => $username,
  320.                 "email" => $email,
  321.                 "message" => $rsp["message"],
  322.             ]);
  323.         }
  324.         else {
  325.             return $this->defaultRender("forms/sign-up.html.twig", [
  326.                 "username" => $username,
  327.                 "email" => $email,
  328.                 "message" => [
  329.                     "type" => "warning",
  330.                     "text" => "The reCAPTCHA was invalid. Please try again.",
  331.                 ],
  332.             ]);
  333.         }
  334.     }
  335.     */
  336.     
  337.     /* *
  338.      * @ R o u t e("/rcs-sign-up", name="sign-up-form")
  339.      * /
  340.     public function viewSignUp (
  341.     
  342.     ) {
  343.         
  344.         return $this->defaultRender("forms/sign-up.html.twig", [
  345.             "username" => "",
  346.             "email" => "",
  347.             "message" => array (
  348.             
  349.             )
  350.         ]);
  351.     }
  352.     */
  353.     
  354.     /**
  355.      * @Route("/sign-up", name="new-sign-up", methods={"POST"})
  356.      */
  357.     public function newSignUp (
  358.         Request $request,
  359.         UserHelper $userHelper,
  360.         EmailHelper $emailHelper,
  361.         LeadHelper $leadHelper,
  362.         ContentHelper $contentHelper
  363.     ) {
  364.         
  365.         if(!$emailHelper->checkCaptcha($request->get("g-recaptcha-response"))) {
  366.             
  367.             $lead $request->get("lead");
  368.             
  369.             $firstname array_key_exists("firstname"$lead) ? $lead["firstname"] : "";
  370.             $lastname array_key_exists("lastname"$lead) ? $lead["lastname"] : "";
  371.             $email array_key_exists("email"$lead) ? $lead["email"] : "";
  372.             $username array_key_exists("username"$lead) ? $lead["username"] : "";
  373.             $address1 array_key_exists("address1"$lead) ? $lead["address1"] : "";
  374.             $address2 array_key_exists("address2"$lead) ? $lead["address2"] : "";
  375.             $city array_key_exists("city"$lead) ? $lead["city"] : "";
  376.             $state array_key_exists("state"$lead) ? $lead["state"] : "";
  377.             $zip array_key_exists("zip"$lead) ? $lead["zip"] : "";
  378.             $company array_key_exists("company"$lead) ? $lead["company"] : "";
  379.             $phone array_key_exists("phone"$lead) ? $lead["phone"] : "";
  380.             
  381.             //$this->addFlash("warning", "The reCAPTCHA was invalid. Please try again.");
  382.             return $this->defaultRender("forms/new-sign-up.html.twig", [
  383.                 "username" => $username,
  384.                 "email" => $email,
  385.                 "firstname" => $firstname,
  386.                 "lastname" => $lastname,
  387.                 "address1" => $address1,
  388.                 "address2" => $address2,
  389.                 "city" => $city,
  390.                 "state" => $state,
  391.                 "zip" => $zip,
  392.                 "company" => $company,
  393.                 "phone" => $phone,
  394.                 "message" => [
  395.                     "type" => "warning",
  396.                     "text" => "The reCAPTCHA was invalid. Please try again.",
  397.                 ]
  398.             ]);
  399.         }
  400.         
  401.         
  402.         $siteSignup $request->get("site-signup");
  403.         $enewsSignup $request->get("enews-signup");
  404.         //$enewsMetalSignup = $request->get("enews-metal-signup");
  405.         
  406.         $enewsSignupSuccess false//store if they signed up for the enews
  407.         $enewsMetalSignupSuccess false//store if they signed up for metal enews
  408.         $siteSignupSuccess false//store if they signed up for the site
  409.         
  410.         
  411.         if($siteSignup) {
  412.             //Handle the form for signing up to the site.
  413.             $lead $request->get("lead");
  414.             
  415.             $username $lead["username"];
  416.             $email $lead["email"];
  417.             $email trim($email);
  418.             $firstname $lead["firstname"];
  419.             $lastname $lead["lastname"];
  420.             
  421.             $address1 $lead["address1"];
  422.             $address2 $lead["address2"];
  423.             $city $lead["city"];
  424.             $state $lead["state"];
  425.             $zip $lead["zip"];
  426.             $company $lead["company"];
  427.             $phone $lead["phone"];
  428.             
  429.             $password1 $request->get("password1");
  430.             $password2 $request->get("password2");
  431.             
  432.             $rsp $userHelper->signUp(
  433.                 $firstname,
  434.                 $lastname,
  435.                 $username,
  436.                 $email,
  437.                 $password1,
  438.                 $password2
  439.             );
  440.             
  441.             if ($rsp["status"]) { //success
  442.                 $siteSignupSuccess true;
  443.                 //return new RedirectResponse('/welcome-activation');
  444.             }
  445.             else {
  446.                 
  447.                 //$this->addFlash("warning", $rsp["message"]);
  448.                 return $this->defaultRender("forms/new-sign-up.html.twig", [
  449.                     "username" => $username,
  450.                     "email" => $email,
  451.                     "firstname" => $firstname,
  452.                     "lastname" => $lastname,
  453.                     "address1" => $address1,
  454.                     "address2" => $address2,
  455.                     "city" => $city,
  456.                     "state" => $state,
  457.                     "zip" => $zip,
  458.                     "company" => $company,
  459.                     "phone" => $phone,
  460.                     "message" => $rsp["message"],
  461.                 ]);
  462.                 
  463.             }
  464.             
  465.         }
  466.         
  467.         
  468.         
  469.         if ($enewsSignup || ($siteSignup && $siteSignupSuccess && ($request->get("enews") || $request->get("metal-enews") || $request->get("coatings-enews")))) {
  470.             //Handle the form for signing up for just enews-signup
  471.             $lead $request->get("lead");
  472.             
  473.             $firstname $lead["firstname"];
  474.             $lastname $lead["lastname"];
  475.             $email $lead["email"];
  476.             $email trim($email);
  477.             $company $lead["company"];
  478.             $contractor_type $lead["contractor_type"];
  479.             $country = ($lead["country"] == "Other" $lead["country_other"] : $lead["country"]);
  480.             $business_type $lead["business_type"];
  481.             
  482.             if(empty($email) || filter_var($emailFILTER_VALIDATE_EMAIL) === false){
  483.                 $this->addFlash("warning""Please enter a valid email address.");
  484.                 return new RedirectResponse($request->headers->get('referer'));
  485.             }            
  486.             if(empty($firstname)){
  487.                 $this->addFlash("warning""Please enter your first name");
  488.                 return new RedirectResponse($request->headers->get('referer'));
  489.             }
  490.             if(empty($lastname)){
  491.                 $this->addFlash("warning""Please enter your last name");
  492.                 return new RedirectResponse($request->headers->get('referer'));
  493.             }
  494.             if(empty($company)){
  495.                 $this->addFlash("warning""Please enter your company");
  496.                 return new RedirectResponse($request->headers->get('referer'));
  497.             }
  498.             
  499.             try {
  500.                 //add to mailchimp list
  501.                 
  502.                 // MailChimp API credentials
  503.                 $apiKey "27ce559264a47aa3487224e1c95424bc-us10";
  504.                 $listID "3efdbc4139";
  505.                 
  506.                 // Determine wir wim or both
  507.                 $keys = [];
  508.                 if ($request->get("enews")) {
  509.                     array_push($keys'WIR');
  510.                 }
  511.                 if ($request->get("metal-enews")) {
  512.                     array_push($keys'WIM');
  513.                 }
  514.                 if ($request->get("coatings-enews")) {
  515.                     array_push($keys'WIC');
  516.                 }
  517.                 // MailChimp API URL
  518.                 $memberID md5(strtolower($email));
  519.                 $dataCenter substr($apiKey,strpos($apiKey,'-')+1);
  520.                 $url 'https://' $dataCenter '.api.mailchimp.com/3.0/lists/' $listID '/members/' $memberID;
  521.                 
  522.                 // member information
  523.                 $json json_encode([
  524.                     'email_address' => $email,
  525.                     'status'        => 'subscribed',
  526.                     'merge_fields'  => [
  527.                         'FNAME'     => $firstname,
  528.                         'LNAME'     => $lastname,
  529.                         'MMERGE3'    => $company,
  530.                         'MMERGE4'    => $contractor_type,
  531.                         'MMERGE5'    => $business_type
  532.                     ],
  533.                     'tags' => $keys
  534.                 ]);
  535.                 
  536.                 $ch curl_init($url);
  537.                 curl_setopt($chCURLOPT_USERPWD'user:' $apiKey);
  538.                 curl_setopt($chCURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  539.                 curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  540.                 curl_setopt($chCURLOPT_TIMEOUT10);
  541.                 curl_setopt($chCURLOPT_CUSTOMREQUEST'PUT');
  542.                 curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
  543.                 curl_setopt($chCURLOPT_POSTFIELDS$json);
  544.                 $result curl_exec($ch);
  545.                 $httpCode curl_getinfo($chCURLINFO_HTTP_CODE);
  546.                 curl_close($ch);
  547.                 
  548.                 if ($httpCode != 200) {
  549.                     $httpErrorCode $httpCode;
  550.                 }
  551.                 
  552.                 // end mailchimp
  553.                 
  554.                 if ($request->get("enews")) {
  555.                     $emailHelper->sendEmail(
  556.                         ["rcs@rooferscoffeeshop.com"],
  557.                         "New submission from the \"Week in Roofing\" sign-up form.",
  558.                         [
  559.                             ["p" => "New submission from the \"Week in Roofing\" sign-up form."],
  560.                             ["table" => [
  561.                                 "First Name" => $firstname,
  562.                                 "Last Name" => $lastname,
  563.                                 "Email Address" => $email,
  564.                                 "Company" => $company,
  565.                                 "Country" => $country,
  566.                                 "Type of Work" => $contractor_type,
  567.                                 "Type of Business" => $business_type,
  568.                             ]],
  569.                         ]
  570.                     );
  571.                 }
  572.                 if ($request->get("metal-enews")) {
  573.                     $emailHelper->sendEmail(
  574.                         ["rcs@rooferscoffeeshop.com"],
  575.                         "New submission from the \"Week in Metal\" sign-up form.",
  576.                         [
  577.                             ["p" => "New submission from the \"Week in Metal\" sign-up form."],
  578.                             ["table" => [
  579.                                 "First Name" => $firstname,
  580.                                 "Last Name" => $lastname,
  581.                                 "Email Address" => $email,
  582.                                 "Company" => $company,
  583.                                 "Country" => $country,
  584.                                 "Type of Work" => $contractor_type,
  585.                                 "Type of Business" => $business_type,
  586.                             ]],
  587.                         ]
  588.                     );
  589.                 }
  590.                 if ($request->get("coatings-enews")) {
  591.                     $emailHelper->sendEmail(
  592.                         ["rcs@rooferscoffeeshop.com"],
  593.                         "New submission from the \"Week in Coatings\" sign-up form.",
  594.                         [
  595.                             ["p" => "New submission from the \"Week in Coatings\" sign-up form."],
  596.                             ["table" => [
  597.                                 "First Name" => $firstname,
  598.                                 "Last Name" => $lastname,
  599.                                 "Email Address" => $email,
  600.                                 "Company" => $company,
  601.                                 "Country" => $country,
  602.                                 "Type of Work" => $contractor_type,
  603.                                 "Type of Business" => $business_type,
  604.                             ]],
  605.                         ]
  606.                     );
  607.                 }
  608.                 // store the lead
  609.                 $content null;
  610.                 
  611.                 $lead_custom $request->get("lead_custom");
  612.                 $lead $leadHelper->saveLead($lead$content$request$lead_custom);
  613.                 
  614.                 if(isset($httpErrorCode)) {
  615.                     $lead->setComments("MailChimp http code: {$httpErrorCode}");
  616.                 }
  617.                 
  618.                 $em $this->getDoctrine()->getManager();
  619.                 $em->persist($lead);
  620.                 $em->flush();
  621.                 
  622.                 if(isset($httpErrorCode)) {
  623.                     $this->addFlash("warning""An error occurred while attempting to add you to our mailing list. Please try again later.");
  624.                     return new RedirectResponse($request->headers->get('referer'));
  625.                 }
  626.                 
  627.                 //$this->addFlash("success", "Thank you for signing up for the RCS Week in Review.");
  628.                 //return new RedirectResponse($request->headers->get('referer'));
  629.                 $enewsSignupSuccess true;
  630.                 
  631.             }
  632.             
  633.             catch (\Exception $e) {
  634.                 $this->addFlash("warning"$e->getMessage());
  635.                 return new RedirectResponse($request->headers->get('referer'));
  636.             }
  637.             
  638.         }
  639.         else {
  640.             $lead $request->get("lead");
  641.             $lead_custom $request->get("lead_custom");
  642.             $lead $leadHelper->saveLead($leadnull$request$lead_custom);
  643.             $em $this->getDoctrine()->getManager();
  644.             $em->persist($lead);
  645.             $em->flush();
  646.         }
  647.         
  648.         if($siteSignupSuccess) {
  649.             return new RedirectResponse('/welcome-activation');
  650.         }
  651.         
  652.         if($enewsSignupSuccess) {
  653.             // $this->addFlash("success", "Thank you for signing up for the RCS Week in Roofing.");
  654.             return new RedirectResponse('/thank-you-wir');
  655.         }
  656.         
  657.     }
  658.     
  659.     /**
  660.      * @Route("/sign-up", name="new-sign-up-form")
  661.      */
  662.     public function viewNewSignUp (
  663.     
  664.     ) {
  665.         
  666.         return $this->defaultRender("forms/new-sign-up.html.twig", [
  667.             "firstname" => "",
  668.             "lastname" => "",
  669.             "username" => "",
  670.             "email" => "",
  671.             "address1" => "",
  672.             "address2" => "",
  673.             "city" => "",
  674.             "state" => "",
  675.             "zip" => "",
  676.             "company" => "",
  677.             "message" => array (
  678.             
  679.             ),
  680.             "meta" => array (
  681.                "breadcrumbs" => [
  682.                    [
  683.                        "title" => "Sign Up",
  684.                        "href" => "/sign-up",
  685.                    ]
  686.                ]
  687.            )
  688.         ]);
  689.     }
  690.     
  691.     
  692.     
  693.     
  694.     
  695.     
  696.     
  697.     
  698.     /**
  699.      * @Route("/activate-account", name="activate", methods={"POST"})
  700.      */
  701.     public function activateAccount (
  702.         Request $request,
  703.         UserHelper $userHelper,
  704.         ContentHelper $contentHelper
  705.     ) {
  706.         
  707.         $id $request->get("id");
  708.         $token $request->get("token");
  709.         $rclubProdId $request->get("rclubProdId");
  710.         $rsp $userHelper->activateAccount($id$token);
  711.         
  712.         if ($rsp["status"]) {
  713.             
  714.             //redirect to purchase page if rclubProdId is set
  715.             if($rclubProdId) {
  716.                 $user $userHelper->getUserById($id);
  717.                 if ($user) {
  718.             
  719.                     // use the current cart or create one
  720.                    $purchase $user->getActivePurchase();
  721.                    if (!$purchase) {
  722.                        $purchase = new Purchase();
  723.                    }
  724.                    
  725.                    //Remove all items since we're going straight to checkout
  726.                    foreach($purchase->getItems() as $item) {
  727.                        //$product = $item->getProduct();
  728.                        //if($product->getType() == Product::PRODUCT_TYPE_MEMBERSHIP) {
  729.                            $purchase->removeItem($item);
  730.                        //}
  731.                    }
  732.                    
  733.                    $newItem = new PurchaseItem();
  734.                    
  735.                    $newItem->setProduct($contentHelper->getProductById($rclubProdId));
  736.                    $newItem->setType(PurchaseItem::TYPE_MEMBERSHIP);
  737.                    $newItem->setPurchase($purchase);
  738.                    
  739.                    // add to the current Item
  740.                    $purchase->setCurrentItem($newItem);
  741.                    $purchase->setUser($user);
  742.                    $purchase->setDiscount(0);
  743.                    
  744.                    $manager $this->getDoctrine()->getManager();
  745.                    $manager->persist($purchase);
  746.                    $manager->flush();
  747.                    
  748.                    $this->addFlash("success""You have successfully activated your account! Please complete your purchase below to join the R-Club.");
  749.                    return $this->redirectToRoute("purchase", [
  750.                    
  751.                    ]);
  752.         
  753.                }
  754.             }
  755.             /*return $this->defaultRender("/", [
  756.                 "message" => array (
  757.                     "type" => "success",
  758.                     "text" => $rsp["message"],
  759.                 )
  760.             ]);*/
  761.             
  762.             //$this->addFlash("success", $rsp["message"]);
  763.             //return $this->redirect("/");
  764.             return $this->redirect("/welcome-to-rcs");
  765.             
  766.             //$this->addFlash("success", $rsp["message"]);
  767.             //return $this->redirect('http://staging.rooferscoffeeshop.com/post-a-classified-ad/2');
  768.         
  769.         
  770.         else {
  771.             return $this->defaultRender("forms/send-activation.html.twig", [
  772.                 "type" => "warning",
  773.                 "text" => $rsp["message"],
  774.             ]);
  775.         }
  776.         
  777.         
  778.         /*
  779.         try {
  780.             $id = $request->get("id");
  781.             $token = $request->get("token");
  782.             
  783.             $user = $userHelper->getUserById($id);
  784.             $expires = $user->getActivationExpires();
  785.             
  786.             if ($user->getActivationToken() == $token && 
  787.                 new \DateTime("now") < $expires
  788.             ) {
  789.                 $user->setActivationToken(null);
  790.                 $user->setActivationExpires(null);
  791.                 // $user->setIsActive(1);
  792.                 $user->setStatus(1);
  793.                 
  794.                 $em = $this->getDoctrine()->getManager();
  795.                 $em->persist($user);
  796.                 $em->flush();
  797.                 $em->clear();
  798.                 
  799.                 return $this->defaultRender("forms/sign-in.html.twig", [
  800.                     "message" => array (
  801.                         "type" => "success",
  802.                         "text" => "The account has been activated. You may now sign in.",
  803.                     ),
  804.                 ]);
  805.             }
  806.         } 
  807.         
  808.         catch (ResourceNotFoundException $e) {
  809.             // ... ignore
  810.         }
  811.         
  812.         return $this->defaultRender("forms/send-activation.html.twig", [
  813.             "message" => array (
  814.                 "type" => "warning",
  815.                 "text" => "The provided token was not found in the system. It is possible that it has expired. If the token has expired, you will need to sign up again.",
  816.             ),
  817.         ]);
  818.         */
  819.         
  820.     }
  821.     
  822.     /**
  823.      * @Route("/activate-account/{id}/{token}/{rclubProdId}", name="view-activation-form")
  824.      */
  825.     public function viewActivateAccount (
  826.         UserHelper $userHelper,
  827.         $id "",
  828.         $token "",
  829.         $rclubProdId ""
  830.     ) {
  831.         try {
  832.             $user $userHelper->getUserById($id);
  833.             $expires $user->getActivationExpires();
  834.             if ($token == $user->getActivationToken() && 
  835.                 new \DateTime("now") < $expires 
  836.             ) {
  837.                 return $this->defaultRender("forms/activate.html.twig", [
  838.                     "username" => $user->getUsername(),
  839.                     "id" => $id,
  840.                     "token" => $token,
  841.                     "rclubProdId" => $rclubProdId,
  842.                 ]);
  843.             }
  844.         } catch (ResourceNotFoundException $e) {
  845.             // ... ignore
  846.         }
  847.         
  848.         return $this->defaultRender("forms/send-activation.html.twig", [
  849.             "message" => array (
  850.                 "type" => "warning",
  851.                 "text" => "The provided token was not found in the system. It is possible that it has expired. If the token has expired, you will need to sign up again or re-send the activation link.",
  852.             ),
  853.         ]);
  854.     }
  855.     
  856.     /**
  857.      * @Route("/send-activation", name="send-activation", methods={"POST"})
  858.      */
  859.     public function sendActivation (
  860.         Request $request,
  861.         UserHelper $userHelper,
  862.         EmailHelper $emailHelper
  863.     ) {
  864.         
  865.         $email $request->get("email");
  866.         $resp $userHelper->sendActivation($email);
  867.                  
  868.         if ($resp["status"]) {
  869.             return $this->defaultRender("forms/send-activation.html.twig", [
  870.                 "message" => array (
  871.                     "type" => "success",
  872.                     "text" => $resp["message"],
  873.                 )
  874.             ]);
  875.         }
  876.         
  877.         return $this->defaultRender("forms/send-activation.html.twig", [
  878.             "message" => array (
  879.                 "type" => "warning",
  880.                 "text" => $resp["message"],
  881.             )
  882.         ]);
  883.         
  884.         /*
  885.         try {
  886.             $email = $request->get("email");
  887.             $user = $userHelper->getUserByEmail($email);
  888.             $uid = $user->getId();
  889.             $token = $user->getActivationToken();
  890.             $expires = $user->getActivationExpires();
  891.             
  892.             $pooled = $emailHelper->sendEmail($email, "Account Activation Link", [
  893.                 ["p" => "Hello {$user->getFullName()},"],
  894.                 ["p" => "You recently requested to receive an activation link for your RoofersCoffeeShop account. Click the button below to activate it."],
  895.                 ["button" => [
  896.                     "text" => "Activate Account",
  897.                     "href" => "https://rooferscoffeeshop.com/activate-account/{$uid}/{$token}",
  898.                 ]],
  899.                 ["p" => "If you did not request a password reset, please ignore this email or reply to let us know. This password reset link is only valid for the next 2 hours."],
  900.             ]);
  901.             
  902.             if ($pooled) {
  903.                 return $this->defaultRender("forms/send-activation.html.twig", [
  904.                     "message" => array (
  905.                         "type" => "success",
  906.                         "text" => "The activation link has been sent to that e-mail address.",
  907.                     )
  908.                 ]);
  909.             } 
  910.             
  911.             return $this->defaultRender("forms/send-activation.html.twig", [
  912.                 "message" => array (
  913.                     "type" => "warning",
  914.                     "text" => "An error occurred while trying to send a system e-mail. If this error continues to occur please contact support.",
  915.                 )
  916.             ]);
  917.             
  918.         } catch (ResourceNotFoundException $e) {
  919.             
  920.         }
  921.         
  922.         return $this->defaultRender("forms/send-activation.html.twig", [
  923.             "message" => array (
  924.                 "type" => "warning",
  925.                 "text" => "No account found matching that criteria.",
  926.             )
  927.         ]);
  928.         */
  929.         
  930.     }
  931.     
  932.     /**
  933.      * @Route("/send-activation", name="send-activation-form")
  934.      */
  935.     public function viewSendActivation (
  936.         UserHelper $userHelper
  937.     ) {
  938.         return $this->defaultRender("forms/send-activation.html.twig", [
  939.             "message" => array (),
  940.         ]);
  941.     }
  942.     /**
  943.      * @Route("/mfa/{id}/{token}", name="view-mfa-form")
  944.      */
  945.     public function viewMfa (
  946.         UserHelper $userHelper,
  947.         $id "",
  948.         $token ""
  949.     ) {
  950.         try {
  951.             $user $userHelper->getUserById($id);
  952.             $expires $user->getMfaTokenExpires();
  953.             if ($token == $user->getMfaToken() && 
  954.                 new \DateTime("now") < $expires 
  955.             ) {
  956.                 return $this->defaultRender("forms/mfa.html.twig", [
  957.                     "username" => $user->getUsername(),
  958.                     "id" => $id,
  959.                     "token" => $token,
  960.                 ]);
  961.             }
  962.         } catch (ResourceNotFoundException $e) {
  963.             // ... ignore
  964.         }
  965.         
  966.         return $this->defaultRender("forms/sign-in.html.twig", [
  967.             "message" => array (
  968.                 "type" => "warning",
  969.                 "text" => "The provided token was not found in the system. It is possible that it has expired. Try signing in again.",
  970.             ),
  971.         ]);
  972.     }
  973.     /**
  974.      * @Route("/mfasignin", name="mfasignin", methods={"POST"})
  975.      */
  976.     public function mfaSignIn (
  977.         Request $request,
  978.         UserHelper $userHelper,
  979.         DoctrineTokenProvider $doctrineTokenProvider
  980.     ) {
  981.         
  982.         $id $request->get("id");
  983.         $token $request->get("token");
  984.         $code $request->get("code");
  985.         $rsp $userHelper->signInMfa($id$token$code);
  986.         
  987.         if ($rsp["status"]) {
  988.             
  989.             /*return $this->defaultRender("/", [
  990.                 "message" => array (
  991.                     "type" => "success",
  992.                     "text" => $rsp["message"],
  993.                 )
  994.             ]);*/
  995.             $session $request->getSession();
  996.             $nRoute $session->get("non_sign_in_route");
  997.             $path = !empty($nRoute["path"]) ? $nRoute["path"] : "/";
  998.             $user $userHelper->getUserById($id);
  999.             $username $user->getUsername();
  1000.             
  1001.             $this->addFlash("success""You have successfully logged in.");
  1002.             $response = new RedirectResponse($path);
  1003.             $response $this->setRememberMe($request$response$doctrineTokenProvider$username);
  1004.             return $response;
  1005.             
  1006.             //return $this->redirect("/");
  1007.             
  1008.             //$this->addFlash("success", $rsp["message"]);
  1009.             //return $this->redirect('http://staging.rooferscoffeeshop.com/post-a-classified-ad/2');
  1010.         
  1011.         
  1012.         else {
  1013.             return $this->defaultRender("forms/mfa.html.twig", [
  1014.                 //"username" => $user->getUsername(),
  1015.                 "id" => $id,
  1016.                 "token" => $token,
  1017.                 "message" => array (
  1018.                     "type" => "warning",
  1019.                     "text" => $rsp["message"],
  1020.                 )
  1021.             ]);
  1022.         }
  1023.         
  1024.     }
  1025.     public function setRememberMe(
  1026.         $request,
  1027.         $response,
  1028.         $doctrineTokenProvider,
  1029.         $username
  1030.     ) {
  1031.         $series base64_encode(random_bytes(64));
  1032.         $tokenValue base64_encode(random_bytes(64));
  1033.         $user $this->user;
  1034.         $doctrineTokenProvider->createNewToken(
  1035.             new PersistentToken(
  1036.                 User::class,
  1037.                 //$user->getUsername(),
  1038.                 $username,
  1039.                 $series,
  1040.                 $tokenValue,
  1041.                 new \DateTime()
  1042.             )
  1043.         );
  1044.         
  1045.         $options = [
  1046.             'name' => 'REMEMBERME',
  1047.             'lifetime' => 604800,
  1048.             'path' => '/',
  1049.             //'domain' => 'staging.rooferscoffeeshop.com',
  1050.             'domain' => null,
  1051.             'secure' => true,
  1052.             'httponly' => true,
  1053.             'samesite' => 'strict',
  1054.         ];
  1055.         //samesite = 
  1056.         //public const SAMESITE_NONE = 'none';
  1057.         //public const SAMESITE_LAX = 'lax';
  1058.         //public const SAMESITE_STRICT = 'strict';
  1059.         $cookieParts = [$series$tokenValue];
  1060.         
  1061.         foreach ($cookieParts as $cookiePart) {
  1062.             if (str_contains($cookiePart":")) {
  1063.                 throw new \InvalidArgumentException(sprintf('$cookieParts should not contain the cookie delimiter "%s".'":"));
  1064.             }
  1065.         }
  1066.         $encodedCookie base64_encode(implode(":"$cookieParts));
  1067.         //$response = new Response();
  1068.         $response->headers->setCookie(
  1069.             new Cookie(
  1070.                 $options['name'],
  1071.                 //$this->encodeCookie([$series, $tokenValue]),
  1072.                 $encodedCookie,
  1073.                 time() + $options['lifetime'],
  1074.                 $options['path'],
  1075.                 $options['domain'],
  1076.                 $options['secure'] ?? $request->isSecure(),
  1077.                 $options['httponly'],
  1078.                 false,
  1079.                 $options['samesite']
  1080.             )
  1081.         );
  1082.         return $response;
  1083.     }
  1084.     
  1085.     /**
  1086.      * @Route("/account", methods={"POST"}, name="post-account")
  1087.      */
  1088.     public function updateAccount (
  1089.         Request $request,
  1090.         UserHelper $userHelper
  1091.     ) {
  1092.         // if we have a current user
  1093.         if ($this->user) {
  1094.             // grab the fields
  1095.             $data = [];
  1096.             
  1097.             if ($request->get("firstname")) {
  1098.                 $data["firstname"] = $request->get("firstname");
  1099.             }
  1100.             
  1101.             if ($request->get("lastname")) {
  1102.                 $data["lastname"] = $request->get("lastname");
  1103.             }
  1104.             
  1105.             if ($request->get("displayname")) {
  1106.                 $data["displayname"] = $request->get("displayname");
  1107.             }
  1108.             
  1109.             if ($request->get("username")) {
  1110.                 $data["username"] = $request->get("username");
  1111.             }
  1112.             
  1113.             if ($request->get("email")) {
  1114.                 $data["email"] = $request->get("email");
  1115.             }
  1116.             
  1117.             if ($request->get("password1")) {
  1118.                 $data["password1"] = $request->get("password1");
  1119.                 $data["password2"] = $request->get("password2");
  1120.                 $data["password3"] = $request->get("password3");
  1121.             }
  1122.             
  1123.             // user avatar
  1124.             if ($request->files->get("avatar")) {
  1125.                 $data["avatar"] = $request->files->get("avatar");
  1126.                 $mime =  $data["avatar"]->getMimeType();
  1127.                 if(!in_array($mime, ["image/jpg""image/jpeg""image/png""image/gif"])) {
  1128.                     $this->addFlash("danger""Only PNG, JPG, and GIF file types are allowed.");
  1129.                     return new RedirectResponse($request->headers->get('referer'));
  1130.                 }
  1131.             }
  1132.             
  1133.             else if ($request->get("remove_avatar")) {
  1134.                 $data["remove_avatar"] = true;
  1135.             }
  1136.             
  1137.             //Update notifications 
  1138.             $data["usermeta"] = [];
  1139.             $notifications $request->get("notifications");
  1140.             if(empty($notifications)) {
  1141.                 $notifications = [];
  1142.             }
  1143.             $data["usermeta"]["member_notification_forum"] = in_array("forum"$notifications);
  1144.             $data["usermeta"]["member_notification_page"] = in_array("page"$notifications);
  1145.             $data["usermeta"]["member_notification_classifieds"] = in_array("classifieds"$notifications);
  1146.             
  1147.             
  1148.             $rsp $userHelper->updateAccount(
  1149.                 $this->user->getId(),
  1150.                 $data
  1151.             );
  1152.             $this->addFlash($rsp["type"], $rsp["message"]);
  1153.         }
  1154.         
  1155.         return $this->redirectToRoute("account");
  1156.     }
  1157.     
  1158.     
  1159.     /**
  1160.      * @Route("/account/profile", methods={"POST"}, name="post-account-profile")
  1161.      */
  1162.     public function updateAccountProfile (
  1163.         Request $request,
  1164.         ContactHelper $contactHelper,
  1165.         EmailHelper $emailHelper
  1166.     ) {
  1167.         
  1168.         $user $this->user;
  1169.         // if we have a current user
  1170.         if($user) {
  1171.             
  1172.             $contact $user->getContact();
  1173.             if(!$contact) {
  1174.                 $contact = new Contact();
  1175.                 $user->setContact($contact);
  1176.                 $entityManager $this->getDoctrine()->getManager();
  1177.                 $entityManager->persist($user);
  1178.                 $entityManager->flush();
  1179.             }
  1180.             
  1181.             $contactHelper->updateContact($contact->getId(), $request->request->all());
  1182.             $this->addFlash("success""Profile updated.");
  1183.             
  1184.             $emailHelper->sendEmail(
  1185.                 ["rcs@rooferscoffeeshop.com"],
  1186.                 "Profile Updated - {$user->getUsername()}",
  1187.                 [
  1188.                     ["p" => "{$user->getUsername()} has updated their account's profile information."],
  1189.                     ["p" => "Visit the User Dashboard to view these changes."],
  1190.                     ["button" => [
  1191.                         "text" => "User Dashboard",
  1192.                         "href" => "https://www.rooferscoffeeshop.com/user-dashboard",
  1193.                     ]],
  1194.                 ]
  1195.             );
  1196.             
  1197.         }
  1198.         
  1199.         return $this->redirectToRoute("account", [
  1200.            "slug" => "profile",
  1201.         ]);
  1202.     }
  1203.     
  1204.     /**
  1205.      * @Route("/account/content/{type}", name="account-content-filter")
  1206.      */
  1207.     public function viewAccountContentType (
  1208.         Request $request,
  1209.         UserHelper $userHelper,
  1210.         $type ""
  1211.     ) {
  1212.         if (!$this->user) {
  1213.             return $this->redirect("/sign-in");
  1214.         }
  1215.         $user $this->user;
  1216.         
  1217.         return $this->defaultRender("forms/user-account.html.twig", [
  1218.             "user" => $user,
  1219.             "slug" => "content",
  1220.             "type" => $type,
  1221.             
  1222.             "meta" => [
  1223.                 "breadcrumbs" => [
  1224.                     [
  1225.                         "title" => "Your Account",
  1226.                         "href" => "/account",
  1227.                     ]
  1228.                 ]
  1229.             ]
  1230.         ]);
  1231.         
  1232.         
  1233.     }
  1234.     
  1235.     /**
  1236.      * @Route("/account/membership/cancel", methods={"POST"}, name="post-account-membership-cancel")
  1237.      */
  1238.     public function cancelAccountMembership (
  1239.         Request $request,
  1240.         UserHelper $userHelper,
  1241.         OrderHelper $orderHelper,
  1242.         EmailHelper $emailHelper
  1243.     ) {
  1244.         if (!$this->user) {
  1245.             return $this->redirect("/sign-in");
  1246.         }
  1247.         $user $this->user;
  1248.         
  1249.         $manager $this->getDoctrine()->getManager();
  1250.         $item $manager->getRepository(PurchaseItem::class)
  1251.             ->findOneBy([
  1252.                "id" => $request->get("item_id"),
  1253.             ]);
  1254.         
  1255.         $item->setStatus(PurchaseItem::STATUS_ACTIVE);
  1256.         $manager->persist($item);
  1257.         $manager->flush();
  1258.         
  1259.         //send email
  1260.         $message = [];
  1261.         $message[] = ["p" => "Your R-Club membership has been cancelled and will not auto-renew."];
  1262.         $message[] = ["a" => ["href" => "rooferscoffeeshop.com/account""text" => "Go to your account page to turn auto-renew back on or update your settings."]];
  1263.         
  1264.         $emailHelper->sendEmail (
  1265.             [$user->getEmail()],
  1266.             "R-Club Auto-Renew Cancelled",
  1267.             $message
  1268.         );
  1269.         
  1270.         $this->addFlash("success""Your membership has been cancelled and will not auto-renew.");
  1271.         return $this->redirectToRoute("account", [
  1272.            "slug" => "membership",
  1273.         ]);
  1274.     }
  1275.     
  1276.     /**
  1277.      * @Route("/account/membership/continue", methods={"POST"}, name="post-account-membership-continue")
  1278.      */
  1279.     public function continueAccountMembership (
  1280.         Request $request,
  1281.         UserHelper $userHelper,
  1282.         OrderHelper $orderHelper,
  1283.         EmailHelper $emailHelper
  1284.     ) {
  1285.         if (!$this->user) {
  1286.             return $this->redirect("/sign-in");
  1287.         }
  1288.         $user $this->user;
  1289.         
  1290.         $manager $this->getDoctrine()->getManager();
  1291.         $item $manager->getRepository(PurchaseItem::class)
  1292.             ->findOneBy([
  1293.                "id" => $request->get("item_id"),
  1294.             ]);
  1295.         
  1296.         $item->setStatus($item->getPaymentFailed() ? PurchaseItem::STATUS_ACTIVE_RENEWING_FINAL PurchaseItem::STATUS_ACTIVE_RENEWING);
  1297.         $manager->persist($item);
  1298.         $manager->flush();
  1299.         
  1300.         //send email
  1301.         $message = [];
  1302.         $message[] = ["p" => "Thank you for re-activating your R-Club membership."];
  1303.         $message[] = ["a" => ["href" => "rooferscoffeeshop.com/account""text" => "Go to your account page to view your renewal information."]];
  1304.         
  1305.         $emailHelper->sendEmail (
  1306.             [$user->getEmail()],
  1307.             "R-Club Auto-Renew Activated",
  1308.             $message
  1309.         );
  1310.         
  1311.         $this->addFlash("success""Your membership has been re-activated and will now automatically renew.");
  1312.         return $this->redirectToRoute("account", [
  1313.            "slug" => "membership",
  1314.         ]);
  1315.     }
  1316.     
  1317.     /**
  1318.      * @Route("/account/membership", methods={"POST"}, name="post-account-membership")
  1319.      */
  1320.     public function updateAccountMembership (
  1321.         Request $request,
  1322.         UserHelper $userHelper,
  1323.         OrderHelper $orderHelper
  1324.     ) {
  1325.         
  1326.         if (!$this->user) {
  1327.             return $this->redirect("/sign-in");
  1328.         }
  1329.         $user $this->user;
  1330.         
  1331.         $payment $request->get("payment");
  1332.         $card $request->get("card");
  1333.         
  1334.         //expiration date
  1335.         $card["card_expires"] = $card["card_expires_month"].$card["card_expires_year"];
  1336.         
  1337.         if (!$payment || !isset($payment["terms"])) {
  1338.             $this->addFlash("warning""You must accept the terms and conditions.");
  1339.             return $this->redirectToRoute("account", [
  1340.                "slug" => "membership",
  1341.             ]);
  1342.         }
  1343.         
  1344.         $result $orderHelper->generateToken(
  1345.            $payment["firstname"],
  1346.            $payment["lastname"],
  1347.            $payment["company"],
  1348.            $payment["address1"],
  1349.            $payment["address2"],
  1350.            $payment["city"],
  1351.            $payment["state"],
  1352.            $payment["zip"],
  1353.            $payment["country"],
  1354.            $payment["email"],
  1355.            $payment["phone"],
  1356.            $_SERVER["REMOTE_ADDR"],
  1357.            $card["card_number"],
  1358.            $card["card_expires"],
  1359.            $card["card_csc"]
  1360.         );
  1361.         
  1362.         if ($result["result"] == "APPROVAL" || $result["result"] == "APPROVED") {
  1363.             
  1364.             $last4 $card["card_number"];
  1365.             $last4 preg_replace("/[^0-9]/"''$last4);
  1366.             $last4 substr($last4, -4);
  1367.             
  1368.             $user->setUsermetum("has_payment_info""1");
  1369.             $user->setUsermetum("payment_first_name"$payment["firstname"]);
  1370.             $user->setUsermetum("payment_last_name"$payment["lastname"]);
  1371.             $user->setUsermetum("payment_company"$payment["company"]);
  1372.             $user->setUsermetum("payment_address_line_1"$payment["address1"]);
  1373.             $user->setUsermetum("payment_address_line_2"$payment["address2"]);
  1374.             $user->setUsermetum("payment_city"$payment["city"]);
  1375.             $user->setUsermetum("payment_state_province"$payment["state"]);
  1376.             $user->setUsermetum("payment_postal_code"$payment["zip"]);
  1377.             $user->setUsermetum("payment_country"$payment["country"]);
  1378.             $user->setUsermetum("payment_email"$payment["email"]);
  1379.             $user->setUsermetum("payment_phone"$payment["phone"]);
  1380.             $user->setUsermetum("payment_last4"$last4);
  1381.             $user->setUsermetum("payment_exp"$card["card_expires"]);
  1382.             //$user->setUsermetum("payment_cvv2", $card["card_csc"]);
  1383.             $user->setUsermetum("payment_token"$result["token"]);
  1384.             $user->setUsermetum("payment_token_response"$result["token_response"]);
  1385.             
  1386.             $manager $this->getDoctrine()->getManager();
  1387.             $manager->persist($user);
  1388.             $manager->flush();
  1389.             
  1390.             $this->addFlash("success""Membership settings updated.");
  1391.             return $this->redirectToRoute("account", [
  1392.                //"slug" => "membership",
  1393.             ]);
  1394.         }
  1395.         else {
  1396.             $this->addFlash("warning""Their was an error while updating your payment information. Response: {$result["result"]}");
  1397.             return $this->redirectToRoute("account", [
  1398.                "slug" => "membership",
  1399.             ]);
  1400.         }
  1401.         
  1402.         
  1403.         
  1404.     }
  1405.     
  1406.     /**
  1407.      * @Route("/account/{slug}/{id}", 
  1408.      *      name="account",
  1409.      *      defaults={"slug": "", "id": ""}
  1410.      * )
  1411.      */
  1412.     public function viewAccount (
  1413.         UserHelper $userHelper,
  1414.         Request $request,
  1415.         $slug "",
  1416.         $id ""
  1417.     ) {
  1418.         if (!$this->user) {
  1419.             return $this->redirect("/sign-in");
  1420.         }
  1421.         $user $this->user;
  1422.         $manager $this->getDoctrine()->getManager();
  1423.         $purchases $user->getPurchases();
  1424.         $purchase null;
  1425.         
  1426.         if (!$slug) {
  1427.             $slug "account";
  1428.         }
  1429.         
  1430.         if ($slug == "order" && $id) {
  1431.             // show the order details
  1432.             $purchase $manager->getRepository(Purchase::class)
  1433.                 ->findOneBy([
  1434.                     "id" => $id
  1435.                 ]);
  1436.             
  1437.             if (!$purchase || $purchase->getUser()->getId() != $user->getId()) {
  1438.                 $purchase null;
  1439.             }
  1440.         }
  1441.         
  1442.         $customers $user->getCompanies();
  1443.         
  1444.         $membership_item "";
  1445.         $membership_product "";
  1446.         $will_renew false;
  1447.         //Get the current purchase item used to activate the user's membership
  1448.         if($user->isMember()) {
  1449.             $now = new \DateTime();
  1450.             foreach($purchases as $p) {
  1451.                 $items $p->getItems();
  1452.                 foreach($items as $item) {
  1453.                     if(($item->getStatus() >= PurchaseItem::STATUS_ACTIVE) && ($item->getType() == PurchaseItem::TYPE_MEMBERSHIP) && ($item->getExpiresAt() > $now)) {
  1454.                         $membership_item $item;
  1455.                         $membership_product $item->getProduct();
  1456.                         $will_renew = ($item->getStatus() >= PurchaseItem::STATUS_ACTIVE_RENEWING true false);
  1457.                     }
  1458.                 }
  1459.             }
  1460.         }
  1461.         
  1462.         $payment false;
  1463.         if($slug == "membership") {
  1464.             if($user->getUserMetaValueByKey("has_payment_info")) {
  1465.                 $payment = [];
  1466.                 $payment["firstname"] = $user->getUserMetaValueByKey("payment_first_name");
  1467.                 $payment["lastname"] = $user->getUserMetaValueByKey("payment_last_name");
  1468.                 $payment["company"] = $user->getUserMetaValueByKey("payment_company");
  1469.                 $payment["address1"] = $user->getUserMetaValueByKey("payment_address_line_1");
  1470.                 $payment["address2"] = $user->getUserMetaValueByKey("payment_address_line_2");
  1471.                 $payment["city"] = $user->getUserMetaValueByKey("payment_city");
  1472.                 $payment["state"] = $user->getUserMetaValueByKey("payment_state_province");
  1473.                 $payment["zip"] = $user->getUserMetaValueByKey("payment_postal_code");
  1474.                 $payment["country"] = $user->getUserMetaValueByKey("payment_country");
  1475.                 $payment["email"] = $user->getUserMetaValueByKey("payment_email");
  1476.                 $payment["phone"] = $user->getUserMetaValueByKey("payment_phone");
  1477.             }
  1478.         }
  1479.         
  1480.         return $this->defaultRender("forms/user-account.html.twig", [
  1481.             "user" => $user,
  1482.             "purchases" => $purchases,
  1483.             "purchase" => $purchase,
  1484.             "slug" => $slug,
  1485.             "customers" => $customers,
  1486.             "membership_item" => $membership_item,
  1487.             "membership_product" => $membership_product,
  1488.             "will_renew" => $will_renew,
  1489.             "payment" => $payment,
  1490.             
  1491.             "meta" => [
  1492.                 "breadcrumbs" => [
  1493.                     [
  1494.                         "title" => "Your Account",
  1495.                         "href" => "/account",
  1496.                     ]
  1497.                 ]
  1498.             ]
  1499.         ]);
  1500.     }
  1501.     
  1502.     /**
  1503.      * @Route("/admin-account/membership/{id}", methods={"GET"}, name="admin-account-membership")
  1504.      */
  1505.     public function adminAccountMembership(
  1506.         Request $request,
  1507.         UserHelper $userHelper,
  1508.         $id ""
  1509.     ) {
  1510.         
  1511.         //Admin check
  1512.         $access false;
  1513.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  1514.             $user $this->getUser();
  1515.             if($user) {
  1516.                 if($user->isAdmin()) {
  1517.                     $access true;
  1518.                 }
  1519.             }
  1520.         }
  1521.         
  1522.         if(!$access) {
  1523.             $this->addFlash("warning""This page is only accessible to site administrators.");
  1524.             return $this->redirectToRoute("/");
  1525.         }
  1526.         
  1527.         $member "";
  1528.         if($id) {
  1529.             $member $userHelper->getUserById($id);
  1530.         }
  1531.         $manager $this->getDoctrine()->getManager();
  1532.         $purchases $member->getPurchases();
  1533.         $purchase null;
  1534.         
  1535.         $membership_item "";
  1536.         $membership_product "";
  1537.         $will_renew false;
  1538.         $last_membership_item "";
  1539.         $last_membership_product "";
  1540.         //Get the current purchase item used to activate the user's membership
  1541.         if($member->isMember()) {
  1542.             $now = new \DateTime();
  1543.             foreach($purchases as $p) {
  1544.                 $items $p->getItems();
  1545.                 foreach($items as $item) {
  1546.                     if(($item->getStatus() >= PurchaseItem::STATUS_ACTIVE) && ($item->getType() == PurchaseItem::TYPE_MEMBERSHIP) && ($item->getExpiresAt() > $now)) {
  1547.                         $membership_item $item;
  1548.                         $membership_product $item->getProduct();
  1549.                         $will_renew = ($item->getStatus() >= PurchaseItem::STATUS_ACTIVE_RENEWING true false);
  1550.                     }
  1551.                 }
  1552.             }
  1553.         }
  1554.         else {
  1555.             //Get the most recent purchase item used to activate the user's membership
  1556.             $now = new \DateTime();
  1557.             foreach($purchases as $p) {
  1558.                 if(!$last_membership_item) {
  1559.                     $items $p->getItems();
  1560.                     foreach($items as $item) {
  1561.                         if(($item->getStatus() == PurchaseItem::STATUS_INACTIVE) && ($item->getType() == PurchaseItem::TYPE_MEMBERSHIP) && ($item->getExpiresAt() < $now)) {
  1562.                             $last_membership_item $item;
  1563.                             $last_membership_product $item->getProduct();
  1564.                             break;
  1565.                         }
  1566.                     }
  1567.                 }
  1568.             }
  1569.         }
  1570.         
  1571.         $payment false;
  1572.         if($member->getUserMetaValueByKey("has_payment_info")) {
  1573.             $payment = [];
  1574.             $payment["firstname"] = $member->getUserMetaValueByKey("payment_first_name");
  1575.             $payment["lastname"] = $member->getUserMetaValueByKey("payment_last_name");
  1576.             $payment["company"] = $member->getUserMetaValueByKey("payment_company");
  1577.             $payment["address1"] = $member->getUserMetaValueByKey("payment_address_line_1");
  1578.             $payment["address2"] = $member->getUserMetaValueByKey("payment_address_line_2");
  1579.             $payment["city"] = $member->getUserMetaValueByKey("payment_city");
  1580.             $payment["state"] = $member->getUserMetaValueByKey("payment_state_province");
  1581.             $payment["zip"] = $member->getUserMetaValueByKey("payment_postal_code");
  1582.             $payment["country"] = $member->getUserMetaValueByKey("payment_country");
  1583.             $payment["email"] = $member->getUserMetaValueByKey("payment_email");
  1584.             $payment["phone"] = $member->getUserMetaValueByKey("payment_phone");
  1585.         }
  1586.         
  1587.         return $this->defaultRender("forms/account/admin-membership.html.twig", [
  1588.             "id" => $id,
  1589.             "member" => $member,
  1590.             "purchases" => $purchases,
  1591.             "purchase" => $purchase,
  1592.             "membership_item" => $membership_item,
  1593.             "membership_product" => $membership_product,
  1594.             "last_membership_item" => $last_membership_item,
  1595.             "last_membership_product" => $last_membership_product,
  1596.             "will_renew" => $will_renew,
  1597.             "payment" => $payment,
  1598.         ]);
  1599.     }
  1600.     
  1601.     /**
  1602.      * @Route("/admin-account/membership-cancel", methods={"POST"}, name="post-admin-account-membership-cancel")
  1603.      */
  1604.     public function adminCancelAccountMembership (
  1605.         Request $request,
  1606.         UserHelper $userHelper,
  1607.         OrderHelper $orderHelper
  1608.     ) {
  1609.         //Admin check
  1610.         $access false;
  1611.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  1612.             $admin $this->getUser();
  1613.             if($admin) {
  1614.                 if($admin->isAdmin()) {
  1615.                     $access true;
  1616.                 }
  1617.             }
  1618.         }
  1619.         
  1620.         if(!$access) {
  1621.             $this->addFlash("warning""This page is only accessible to site administrators.");
  1622.             return $this->redirectToRoute("/");
  1623.         }
  1624.         $user_id $request->get("user_id");
  1625.         $user $userHelper->getUserById($user_id);
  1626.         
  1627.         $manager $this->getDoctrine()->getManager();
  1628.         $item $manager->getRepository(PurchaseItem::class)
  1629.             ->findOneBy([
  1630.                "id" => $request->get("item_id"),
  1631.             ]);
  1632.         
  1633.         $item->setStatus(PurchaseItem::STATUS_ACTIVE);
  1634.         $manager->persist($item);
  1635.         $manager->flush();
  1636.         
  1637.         $this->addFlash("success""Membership has been cancelled and will not auto-renew.");
  1638.         return $this->redirectToRoute("admin-account-membership", [
  1639.            "id" => $user_id,
  1640.         ]);
  1641.     }
  1642.     
  1643.     /**
  1644.      * @Route("/admin-account/membership-continue", methods={"POST"}, name="post-admin-account-membership-continue")
  1645.      */
  1646.     public function adminContinueAccountMembership (
  1647.         Request $request,
  1648.         UserHelper $userHelper,
  1649.         OrderHelper $orderHelper
  1650.     ) {
  1651.         //Admin check
  1652.         $access false;
  1653.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  1654.             $admin $this->getUser();
  1655.             if($admin) {
  1656.                 if($admin->isAdmin()) {
  1657.                     $access true;
  1658.                 }
  1659.             }
  1660.         }
  1661.         
  1662.         if(!$access) {
  1663.             $this->addFlash("warning""This page is only accessible to site administrators.");
  1664.             return $this->redirectToRoute("/");
  1665.         }
  1666.         $user_id $request->get("user_id");
  1667.         $user $userHelper->getUserById($user_id);
  1668.         
  1669.         $manager $this->getDoctrine()->getManager();
  1670.         $item $manager->getRepository(PurchaseItem::class)
  1671.             ->findOneBy([
  1672.                "id" => $request->get("item_id"),
  1673.             ]);
  1674.         
  1675.         $item->setStatus($item->getPaymentFailed() ? PurchaseItem::STATUS_ACTIVE_RENEWING_FINAL PurchaseItem::STATUS_ACTIVE_RENEWING);
  1676.         $manager->persist($item);
  1677.         $manager->flush();
  1678.         
  1679.         $this->addFlash("success""Membership has been re-activated and will now automatically renew.");
  1680.         return $this->redirectToRoute("admin-account-membership", [
  1681.            "id" => $user_id,
  1682.         ]);
  1683.     }
  1684.     
  1685.     /**
  1686.      * @Route("/admin-account/membership-retry", methods={"POST"}, name="post-admin-account-membership-retry")
  1687.      */
  1688.     public function adminRetryAccountMembership (
  1689.         Request $request,
  1690.         UserHelper $userHelper,
  1691.         OrderHelper $orderHelper,
  1692.         EmailHelper $emailHelper
  1693.     ) {
  1694.         //Admin check
  1695.         $access false;
  1696.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  1697.             $admin $this->getUser();
  1698.             if($admin) {
  1699.                 if($admin->isAdmin()) {
  1700.                     $access true;
  1701.                 }
  1702.             }
  1703.         }
  1704.         
  1705.         if(!$access) {
  1706.             $this->addFlash("warning""This page is only accessible to site administrators.");
  1707.             return $this->redirectToRoute("/");
  1708.         }
  1709.         $user_id $request->get("user_id");
  1710.         $user $userHelper->getUserById($user_id);
  1711.         
  1712.         $entityManager $this->getDoctrine()->getManager();
  1713.         $item $entityManager->getRepository(PurchaseItem::class)
  1714.             ->findOneBy([
  1715.                "id" => $request->get("item_id"),
  1716.             ]);
  1717.         
  1718.         /*
  1719.         $item->setStatus($item->getPaymentFailed() ? PurchaseItem::STATUS_ACTIVE_RENEWING_FINAL : PurchaseItem::STATUS_ACTIVE_RENEWING);
  1720.         $entityManager->persist($item);
  1721.         $entityManager->flush();
  1722.         */
  1723.         
  1724.         /* Modified copy of the OrderHelper's "updateExpiredPurchaseItems" function */
  1725.         //TODO: move this to the OrderHelper so code isn't repeated
  1726.         $currentStatus PurchaseItem::STATUS_ACTIVE_RENEWING;
  1727.                 
  1728.         //Do this first: in case there's an error, it won't keep trying to renew
  1729.         $item->setStatus(PurchaseItem::STATUS_INACTIVE);
  1730.         //$item->setExpiresAt(null); //might be useful to see when it expired
  1731.         $entityManager->persist($item);
  1732.         $entityManager->flush();
  1733.         
  1734.         $purchase_completed false;
  1735.         $product $item->getProduct();
  1736.         $purchase $item->getPurchase();
  1737.         if($purchase) {
  1738.             $user $purchase->getUser();
  1739.             if($product && $user) {
  1740.                 if($user->getUserMetaValueByKey("has_payment_info")) {
  1741.                     
  1742.                     $new_purchase = new Purchase();
  1743.                     
  1744.                     $newItem = new PurchaseItem();
  1745.                     
  1746.                     $newItem->setProduct($item->getProduct());
  1747.                     $newItem->setType($item->getType());
  1748.                     $newItem->setPurchase($new_purchase);
  1749.                     
  1750.                     $new_purchase->setStatus(Purchase::STATUS_FAILED); //default to failed (we don't ever want it 'active')
  1751.                     $new_purchase->setCurrentItem($newItem);
  1752.                     $new_purchase->setUser($user);
  1753.                     $new_purchase->setDiscount(0);
  1754.                     
  1755.                     //apply any coupons
  1756.                     $old_coupons $purchase->getCoupons();
  1757.                     foreach($old_coupons as $coupon) {
  1758.                         if($coupon->getProductType() == $product->getType()
  1759.                             && ($coupon->getRecurringType() == Coupon::RECURRING_UNLIMITED
  1760.                             || ($coupon->getRecurringType() == Coupon::RECURRING_SET_MONTHS && $coupon->getMonthsRecurring() > $item->getMonthsRenewed()))
  1761.                         ) {
  1762.                             $new_purchase->addCoupon($coupon);
  1763.                         }
  1764.                     }
  1765.                     
  1766.                     //renew the purchase item
  1767.                     $result $orderHelper->placeOrder(
  1768.                         $user->getUserMetaValueByKey("payment_first_name"),
  1769.                         $user->getUserMetaValueByKey("payment_last_name"),
  1770.                         $user->getUserMetaValueByKey("payment_company"),
  1771.                         $user->getUserMetaValueByKey("payment_address_line_1"),
  1772.                         $user->getUserMetaValueByKey("payment_address_line_2"),
  1773.                         $user->getUserMetaValueByKey("payment_city"),
  1774.                         $user->getUserMetaValueByKey("payment_state_province"),
  1775.                         $user->getUserMetaValueByKey("payment_postal_code"),
  1776.                         $user->getUserMetaValueByKey("payment_country"),
  1777.                         $user->getUserMetaValueByKey("payment_email"),
  1778.                         $user->getUserMetaValueByKey("payment_phone"),
  1779.                         ""//$_SERVER["REMOTE_ADDR"],
  1780.                         $new_purchase->getInvoiceNumber(),
  1781.                         $new_purchase->getTotalUsd(true),
  1782.                         $new_purchase->getTaxUsd(true),
  1783.                         ""//no card number
  1784.                         ""//no expiration date
  1785.                         ""//$user->getUserMetaValueByKey("payment_cvv2"),
  1786.                         false,
  1787.                         $user->getUserMetaValueByKey("payment_token")
  1788.                     );
  1789.                     
  1790.                     
  1791.                     $new_purchase->setFirstname($user->getUserMetaValueByKey("payment_first_name"));
  1792.                     $new_purchase->setLastname($user->getUserMetaValueByKey("payment_last_name"));
  1793.                     $new_purchase->setCompany($user->getUserMetaValueByKey("payment_company"));
  1794.                     $new_purchase->setAddressLine1($user->getUserMetaValueByKey("payment_address_line_1"));
  1795.                     $new_purchase->setAddressLine2($user->getUserMetaValueByKey("payment_address_line_2"));
  1796.                     $new_purchase->setCity($user->getUserMetaValueByKey("payment_city"));
  1797.                     $new_purchase->setStateProvince($user->getUserMetaValueByKey("payment_state_province"));
  1798.                     $new_purchase->setPostalCode($user->getUserMetaValueByKey("payment_postal_code"));
  1799.                     $new_purchase->setCountry($user->getUserMetaValueByKey("payment_country"));
  1800.                     $new_purchase->setEmail($user->getUserMetaValueByKey("payment_email"));
  1801.                     $new_purchase->setPhone($user->getUserMetaValueByKey("payment_phone"));
  1802.                     //$new_purchase->setLast4("");
  1803.                     //$new_purchase->setUserIP($_SERVER["REMOTE_ADDR"]);
  1804.                     //$new_purchase->setUserAgent($_SERVER["HTTP_USER_AGENT"]);
  1805.                     $new_purchase->setRawResponse(json_encode([
  1806.                         "body" => $result["raw"],
  1807.                         "request" => $result["request"],
  1808.                     ]));
  1809.                     
  1810.                     $purchaseNote = new PurchaseNote();
  1811.                     $purchaseNote->setAuthorName("RCS Payment Gateway");
  1812.                     $purchaseNote->setAuthorEmail("admin@rooferscoffeeshop.com");
  1813.                     if($result["result"] == "DECLINED 82") {
  1814.                         $result["result"] = "INVALID CAM\CVV";
  1815.                     }
  1816.                     
  1817.                     if (/*true || */$result["result"] == "APPROVAL" || $result["result"] == "APPROVED") {
  1818.                         
  1819.                         $purchaseNote->setDescription("Automatic payment attempt was successful. Response Code: {$result["code"]}. Response Message: {$result["result"]}");
  1820.                         $purchaseNote->setHiddenDescription(json_encode($result));
  1821.                         $new_purchase->addPurchaseNote($purchaseNote);
  1822.                         
  1823.                         // mark purchase items as processed...
  1824.                         $new_purchase->setStatus(Purchase::STATUS_PROCESSED); //< this updates all expiration dates based on product purchased...
  1825.                         
  1826.                         if($product->getPaymentRate() == Product::RENEW_YEARLY) {
  1827.                             $newItem->setMonthsRenewed($item->getMonthsRenewed() + 12);
  1828.                         }
  1829.                         else {
  1830.                             $newItem->setMonthsRenewed($item->getMonthsRenewed() + 1);
  1831.                         }
  1832.                         
  1833.                         $purchase_completed true;
  1834.                     }else{
  1835.                         $purchaseNote->setDescription("An error occurred while attempting to renew your purchase. Response Code: {$result["code"]}. Response Message: {$result["result"]}");
  1836.                         $purchaseNote->setHiddenDescription(json_encode($result));
  1837.                         $new_purchase->addPurchaseNote($purchaseNote);
  1838.                         
  1839.                         // mark purchase items as failed...
  1840.                         $new_purchase->setStatus(Purchase::STATUS_FAILED); //< this updates all expiration dates based on product purchased...
  1841.                     }
  1842.                     
  1843.                     $entityManager->persist($new_purchase);
  1844.                     $entityManager->flush();
  1845.                     
  1846.                 } 
  1847.                 else{
  1848.                     $purchaseNote = new PurchaseNote();
  1849.                     $purchaseNote->setAuthorName("RCS Payment Gateway");
  1850.                     $purchaseNote->setAuthorEmail("admin@rooferscoffeeshop.com");
  1851.                     $purchaseNote->setDescription("No Payment Information On Record.");
  1852.                     $purchase->addPurchaseNote($purchaseNote);
  1853.                     $purchase->setStatus(Purchase::STATUS_FAILED);
  1854.                 }
  1855.             }
  1856.         }
  1857.         
  1858.         if($purchase_completed) {
  1859.             //send purchase email
  1860.             $message = [];
  1861.             $message[] = ["p" => "Your R-Club membership has been successfully renewed for {$new_purchase->getTotalUsd()}."];
  1862.             $message[] = ["a" => ["href" => "rooferscoffeeshop.com/account""text" => "Go to your account page to update your membership settings at any time."]];
  1863.             
  1864.             $this->addFlash("success""Payment successful. Membership has been re-activated and will now automatically renew.");
  1865.             return $this->redirectToRoute("admin-account-membership", [
  1866.                 "id" => $user_id,
  1867.             ]);
  1868.         }
  1869.         else {
  1870.             
  1871.             $user->setMember(0);
  1872.             $entityManager->persist($user);
  1873.             $entityManager->flush();
  1874.             
  1875.             $this->addFlash("danger""An error occurred when attempting to renew this user's membership. Response Code: {$result["code"]}. Response Message: {$result["result"]}");
  1876.             return $this->redirectToRoute("admin-account-membership", [
  1877.                 "id" => $user_id,
  1878.             ]);
  1879.         }
  1880.         
  1881.     
  1882.         
  1883.         
  1884.         
  1885.     }
  1886.     /**
  1887.      * @Route("/admin-account/membership", methods={"POST"}, name="post-admin-account-membership")
  1888.      */
  1889.     public function adminUpdateAccountMembership (
  1890.         Request $request,
  1891.         UserHelper $userHelper,
  1892.         OrderHelper $orderHelper
  1893.     ) {
  1894.         //Admin check
  1895.         $access false;
  1896.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  1897.             $admin $this->getUser();
  1898.             if($admin) {
  1899.                 if($admin->isAdmin()) {
  1900.                     $access true;
  1901.                 }
  1902.             }
  1903.         }
  1904.         
  1905.         if(!$access) {
  1906.             $this->addFlash("warning""This page is only accessible to site administrators.");
  1907.             return $this->redirectToRoute("/");
  1908.         }
  1909.         
  1910.         $user_id $request->get("user_id");
  1911.         $user $userHelper->getUserById($user_id);
  1912.         
  1913.         $payment $request->get("payment");
  1914.         $card $request->get("card");
  1915.         
  1916.         //expiration date
  1917.         $card["card_expires"] = $card["card_expires_month"].$card["card_expires_year"];
  1918.         
  1919.         if (!$payment || !isset($payment["terms"])) {
  1920.             $this->addFlash("warning""You must accept the terms and conditions.");
  1921.             return $this->redirectToRoute("admin-account-membership", [
  1922.                 "id" => $user_id,
  1923.              ]);
  1924.         }
  1925.         
  1926.         $result $orderHelper->generateToken(
  1927.            $payment["firstname"],
  1928.            $payment["lastname"],
  1929.            $payment["company"],
  1930.            $payment["address1"],
  1931.            $payment["address2"],
  1932.            $payment["city"],
  1933.            $payment["state"],
  1934.            $payment["zip"],
  1935.            $payment["country"],
  1936.            $payment["email"],
  1937.            $payment["phone"],
  1938.            $_SERVER["REMOTE_ADDR"],
  1939.            $card["card_number"],
  1940.            $card["card_expires"],
  1941.            $card["card_csc"]
  1942.         );
  1943.         
  1944.         if ($result["result"] == "APPROVAL" || $result["result"] == "APPROVED") {
  1945.             
  1946.             $last4 $card["card_number"];
  1947.             $last4 preg_replace("/[^0-9]/"''$last4);
  1948.             $last4 substr($last4, -4);
  1949.             
  1950.             $user->setUsermetum("has_payment_info""1");
  1951.             $user->setUsermetum("payment_first_name"$payment["firstname"]);
  1952.             $user->setUsermetum("payment_last_name"$payment["lastname"]);
  1953.             $user->setUsermetum("payment_company"$payment["company"]);
  1954.             $user->setUsermetum("payment_address_line_1"$payment["address1"]);
  1955.             $user->setUsermetum("payment_address_line_2"$payment["address2"]);
  1956.             $user->setUsermetum("payment_city"$payment["city"]);
  1957.             $user->setUsermetum("payment_state_province"$payment["state"]);
  1958.             $user->setUsermetum("payment_postal_code"$payment["zip"]);
  1959.             $user->setUsermetum("payment_country"$payment["country"]);
  1960.             $user->setUsermetum("payment_email"$payment["email"]);
  1961.             $user->setUsermetum("payment_phone"$payment["phone"]);
  1962.             $user->setUsermetum("payment_last4"$last4);
  1963.             $user->setUsermetum("payment_exp"$card["card_expires"]);
  1964.             //$user->setUsermetum("payment_cvv2", $card["card_csc"]);
  1965.             $user->setUsermetum("payment_token"$result["token"]);
  1966.             $user->setUsermetum("payment_token_response"$result["token_response"]);
  1967.             
  1968.             $manager $this->getDoctrine()->getManager();
  1969.             $manager->persist($user);
  1970.             $manager->flush();
  1971.             
  1972.             $this->addFlash("success""Membership settings updated.");
  1973.             return $this->redirectToRoute("admin-account-membership", [
  1974.                 "id" => $user_id,
  1975.              ]);
  1976.         }
  1977.         else {
  1978.             $this->addFlash("warning""Their was an error while updating your payment information. Response: {$result["result"]}");
  1979.             return $this->redirectToRoute("admin-account-membership", [
  1980.                 "id" => $user_id,
  1981.              ]);
  1982.         }
  1983.         
  1984.         
  1985.         
  1986.     }
  1987.     
  1988. }