public function add()
 {
     if (!isset($_POST['card_number'])) {
         $this->redirect_to();
     }
     $paymentMethod = new Payment_Method();
     $paymentMethod->assignProperties($_POST);
     $paymentMethod->runValidations();
     if ($_POST['include_new_address'] == "1") {
         require_once '../app/models/Address.php';
         $address = new Address();
         $address->assignProperties($_POST);
         $address->runValidations();
         // A valid payment method will still have one error - fk_payment_method_address  will be missing.
         if (count($paymentMethod->errorsList) == 1 && isset($paymentMethod->errorsList['fk_payment_method_address'])) {
             $addressId = $address->savePreparedStatementToDb('address', $address->properties);
         }
     } else {
         $addressId = $_POST['addressId'];
     }
     $paymentMethod->properties['fk_payment_method_address'] = $addressId;
     $paymentMethodId = $paymentMethod->savePreparedStatementToDb('payment_method', $paymentMethod->properties);
     $_SESSION['paymentMethodId'] = $paymentMethodId;
     $_SESSION['payment_method'] = $paymentMethod;
     if (isset($_SESSION['address'])) {
         $_SESSION['address'] = $address;
     }
     $this->redirect_to($_POST['redirect']);
 }
 public function paymentMethod()
 {
     require_once '../app/models/Payment_method.php';
     require_once '../app/models/Address.php';
     if (isset($_POST['paymentMethodId']) && Checkout_helper::confirmCardOwnership($_POST['paymentMethodId'])) {
         $_SESSION['checkout']['properties']['paymentMethod'] = $_POST['paymentMethodId'];
         $this->redirect_to('checkout/confirm');
         break;
     }
     if (isset($_SESSION['payment_method'])) {
         $paymentMethod = $_SESSION['payment_method'];
         if (count($paymentMethod->errorsList) == 0) {
             $_SESSION['checkout']['properties']['paymentMethod'] = $_SESSION['paymentMethodId'];
             unset($_SESSION['paymentMethodId']);
             unset($_SESSION['payment_method']);
             $this->redirect_to('checkout/confirm');
             break;
         }
     } else {
         $paymentMethod = new Payment_Method();
         unset($_SESSION['payment_method']);
         unset($_SESSION['paymentMethodId']);
     }
     if (isset($_SESSION['address'])) {
         $address = $_SESSION['address'];
     } else {
         $address = new Address();
     }
     $addressList = $address->findByUserId($_SESSION['user_id']);
     $paymentList = $paymentMethod->findByUserId($_SESSION['user_id']);
     $addressAttributes = ['full_name', 'address_line_1', 'address_line_2', 'city', 'county', 'postcode', 'country', 'phone_number'];
     $view = new View('checkout/payment_method', ['header' => false, 'footer' => false]);
     $view->set_title('Payment Method');
     $view->pass_data('payment_method', $paymentMethod);
     $view->pass_data('paymentList', $paymentList);
     $view->pass_data('redirect', 'checkout/paymentmethod');
     $view->pass_data('addressAttributes', $addressAttributes);
     $view->pass_data('address', $address);
     $view->pass_data('addressList', $addressList);
     $view->load_page();
     unset($_SESSION['address']);
     unset($_SESSION['payment_method']);
 }