public static function checkout($customer, $cart_info, $stripeToken) { $ufstore = UFStore::instance(); $shipping_info = array(); //Calculate new costs, all should be in pennies $cart_subtotal = UFStoreCart::getCartCost(); $cart_shipping = $cart_info['shipping_cost']; $cart_total = $cart_subtotal + $cart_shipping; //!!!Doublecheck our new calculations match the AJAX response //Run the amount on stripe if (UFStoreCartCheckout::stripePayment($cart_total, $stripeToken)) { $cart = UFStoreCart::getCart(); //Send email confirmation ob_start(); // start output buffer include dirname(__FILE__) . '/../templates/email.php'; $template = ob_get_contents(); // get contents of buffer ob_end_clean(); UFStoreCartCheckout::sendReceipt($customer['email'], $template); UFStoreCartCheckout::saveOrder($customer, $cart_info, $cart, $cart_subtotal, $cart_shipping, $cart_total); UFStoreCart::clearCart(); return array('cart' => $cart, 'customer' => $customer); } else { return false; } }
function checkout() { header('Content-Type: application/json'); $ufstore = UFStore::instance(); $errors = array(); //Super simple validation if (strlen($_POST['fullname']) <= 0) { array_push($errors, array('fullname' => 'Please enter your name')); } if (strlen($_POST['email']) <= 0) { array_push($errors, array('email' => 'Please enter your email address')); } if (strlen($_POST['address1']) <= 0) { array_push($errors, array('address1' => 'Please enter your address')); } if (strlen($_POST['city']) <= 0) { array_push($errors, array('city' => 'Please enter your city')); } if (strlen($_POST['country']) <= 0) { array_push($errors, array('country' => 'Please enter your country')); } if (strlen($_POST['state']) <= 0) { array_push($errors, array('state' => 'Please enter your state')); } if ($_POST['country'] == 'United States') { if (strlen($_POST['zipcode']) <= 0 || !is_numeric($_POST['zipcode']) || strlen($_POST['zipcode']) !== 5) { array_push($errors, array('zipcode' => 'Invalid Postal Code')); } } else { if (strlen($_POST['zipcode']) <= 0) { array_push($errors, array('zipcode' => 'Please enter your zipcode')); } } if (strlen($_POST['shipping_name']) <= 0 || strlen($_POST['shipping_cost']) <= 0) { array_push($errors, array('shippingError' => 'Please select a shiping option')); } if (strlen($_POST['stripeToken']) <= 0) { array_push($errors, array('stripeToken' => 'No Stripe Token')); } // if(UFStoreCart::getCartCount() <= 0){ array_push($errors, array('cart' => 'Shopping cart is empty')); } //Return Errors if (!empty($errors)) { http_response_code(400); echo json_encode(array('errors' => $errors)); die; } //This needs to be cleaned up //SANATIZE INPUT !!! $customer = array('name' => $_POST['fullname'], 'email' => $_POST['email'], 'address1' => $_POST['address1'], 'address2' => $_POST['address2'], 'city' => $_POST['city'], 'country' => $_POST['country'], 'state' => $_POST['state'], 'zipcode' => $_POST['zipcode']); $cart_info = array('cart_subtotal' => $_POST['cart_subtotal'], 'cart_shipping' => $_POST['cart_shipping'], 'cart_total' => $_POST['cart_total'], 'shipping_name' => $_POST['shipping_name'], 'shipping_cost' => $_POST['shipping_cost'], 'shipping_speed' => $_POST['shipping_speed']); $stripeToken = $_POST['stripeToken']; $result = UFStoreCartCheckout::checkout($customer, $cart_info, $stripeToken); echo json_encode($result); die; }