function fflcommerce_checkout($atts) { if (!defined('FFLCOMMERCE_CHECKOUT')) { define('FFLCOMMERCE_CHECKOUT', true); } $non_js_checkout = isset($_POST['update_totals']) && $_POST['update_totals'] ? true : false; $result = fflcommerce_cart::check_cart_item_stock(); if (is_wp_error($result)) { fflcommerce::add_error($result->get_error_message()); } if (!fflcommerce::has_errors() && $non_js_checkout) { fflcommerce::add_message(__('The order totals have been updated. Please confirm your order by pressing the Place Order button at the bottom of the page.', 'fflcommerce')); } fflcommerce::show_messages(); fflcommerce_get_template('checkout/form.php', false); }
function fflcommerce_cart($atts) { unset(fflcommerce_session::instance()->selected_rate_id); // Process Discount Codes if (isset($_POST['apply_coupon']) && $_POST['apply_coupon'] && fflcommerce::verify_nonce('cart')) { $coupon_code = sanitize_title($_POST['coupon_code']); fflcommerce_cart::add_discount($coupon_code); } elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && fflcommerce::verify_nonce('cart')) { // Update Shipping unset(fflcommerce_session::instance()->chosen_shipping_method_id); $country = $_POST['calc_shipping_country']; $state = $_POST['calc_shipping_state']; $postcode = $_POST['calc_shipping_postcode']; if ($postcode && !fflcommerce_validation::is_postcode($postcode, $country)) { fflcommerce::add_error(__('Please enter a valid postcode/ZIP.', 'fflcommerce')); $postcode = ''; } elseif ($postcode) { $postcode = fflcommerce_validation::format_postcode($postcode, $country); } if ($country) { // Update customer location fflcommerce_customer::set_location($country, $state, $postcode); fflcommerce_customer::set_shipping_location($country, $state, $postcode); fflcommerce::add_message(__('Shipping costs updated.', 'fflcommerce')); } else { fflcommerce_customer::set_shipping_location('', '', ''); fflcommerce::add_message(__('Shipping costs updated.', 'fflcommerce')); } } elseif (isset($_POST['shipping_rates'])) { $rates_params = explode(":", $_POST['shipping_rates']); $available_methods = fflcommerce_shipping::get_available_shipping_methods(); $shipping_method = $available_methods[$rates_params[0]]; if ($rates_params[1] != null) { fflcommerce_session::instance()->selected_rate_id = $rates_params[1]; } $shipping_method->choose(); // chooses the method selected by user. } // Re-Calc prices. This needs to happen every time the cart page is loaded and after checking post results. fflcommerce_cart::calculate_totals(); $result = fflcommerce_cart::check_cart_item_stock(); if (is_wp_error($result)) { fflcommerce::add_error($result->get_error_message()); } fflcommerce_render('shortcode/cart', array('cart' => fflcommerce_cart::get_cart(), 'coupons' => fflcommerce_cart::get_coupons())); }
function fflcommerce_pay_action() { if (!is_fflcommerce_single_page(FFLCOMMERCE_PAY)) { return; } if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) { // Pay for existing order $order_key = urldecode($_GET['order']); $order_id = (int) $_GET['order_id']; $order = new fflcommerce_order($order_id); if ($order->id == $order_id && $order->order_key == $order_key && $order->status == 'pending') { // Set customer location to order location if ($order->billing_country) { fflcommerce_customer::set_country($order->billing_country); } if ($order->billing_state) { fflcommerce_customer::set_state($order->billing_state); } if ($order->billing_postcode) { fflcommerce_customer::set_postcode($order->billing_postcode); } // Pay form was posted - process payment if (isset($_POST['pay']) && fflcommerce::verify_nonce('pay')) { // Update payment method if ($order->order_total > 0) { $payment_method = fflcommerce_clean($_POST['payment_method']); $data = (array) maybe_unserialize(get_post_meta($order_id, 'order_data', true)); $data['payment_method'] = $payment_method; update_post_meta($order_id, 'order_data', $data); $available_gateways = fflcommerce_payment_gateways::get_available_payment_gateways(); $result = $available_gateways[$payment_method]->process_payment($order_id); // Redirect to success/confirmation/payment page if ($result['result'] == 'success') { wp_safe_redirect($result['redirect']); exit; } } else { // No payment was required for order $order->payment_complete(); // filter redirect page $checkout_redirect = apply_filters('fflcommerce_get_checkout_redirect_page_id', fflcommerce_get_page_id('thanks')); wp_safe_redirect(get_permalink($checkout_redirect)); exit; } } } elseif ($order->status != 'pending') { fflcommerce::add_error(__('Your order has already been paid for. Please contact us if you need assistance.', 'fflcommerce')); } else { fflcommerce::add_error(__('Invalid order.', 'fflcommerce')); } } else { // Pay for order after checkout step if (isset($_GET['order'])) { $order_id = $_GET['order']; } else { $order_id = 0; } if (isset($_GET['key'])) { $order_key = $_GET['key']; } else { $order_key = ''; } if ($order_id > 0) { $order = new fflcommerce_order($order_id); if ($order->order_key != $order_key || $order->status != 'pending') { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } } else { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } } }
exit; } if ($isViewOrder) { if (!isset($_GET['order'])) { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } $order = new fflcommerce_order($_GET['order']); if ($order->user_id != get_current_user_id()) { wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id('myaccount')))); exit; } } if ($isChangePassword) { $user_id = get_current_user_id(); if ($_POST && $user_id > 0 && fflcommerce::verify_nonce('change_password')) { if ($_POST['password-1'] && $_POST['password-2']) { if ($_POST['password-1'] == $_POST['password-2']) { wp_update_user(array('ID' => $user_id, 'user_pass' => $_POST['password-1'])); fflcommerce::add_message(__('Password changed successfully.', 'fflcommerce')); wp_safe_redirect(apply_filters('fflcommerce_get_myaccount_page_id', get_permalink(fflcommerce_get_page_id(FFLCOMMERCE_MY_ACCOUNT)))); exit; } else { fflcommerce::add_error(__('Passwords do not match.', 'fflcommerce')); } } else { fflcommerce::add_error(__('Please enter your password.', 'fflcommerce')); } } } });
private function create_user_account() { $reg_errors = new WP_Error(); do_action('register_post', $this->posted['billing_email'], $this->posted['billing_email'], $reg_errors); if ($reg_errors->get_error_code()) { fflcommerce::add_error($reg_errors->get_error_message()); return 0; } $user_pass = $this->posted['account_password']; $user_id = wp_create_user($this->posted['account_username'], $user_pass, $this->posted['billing_email']); if (!$user_id) { fflcommerce::add_error(sprintf(__('<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !', 'fflcommerce'), self::get_options()->get('fflcommerce_email'))); return 0; } wp_update_user(array('ID' => $user_id, 'role' => 'customer', 'first_name' => $this->posted['billing_first_name'], 'last_name' => $this->posted['billing_last_name'])); do_action('fflcommerce_created_customer', $user_id); // send the user a confirmation and their login details if (apply_filters('fflcommerce_new_user_notification', true, $user_id, $user_pass)) { wp_new_user_notification($user_id, $user_pass); } wp_set_auth_cookie($user_id, true, is_ssl()); return $user_id; }
/** * Applies a coupon code * * @param string $coupon_code The code to apply * @return bool True if the coupon is applied, false if it does not exist or cannot be applied */ public static function add_discount($coupon_code) { if (!self::is_valid_coupon($coupon_code)) { return false; } // Check for other individual_use coupons before adding this coupon. foreach (self::get_coupons() as $code) { $current = JS_Coupons::get_coupon($code); if ($current['individual_use']) { fflcommerce::add_error(__("There is already an 'individual use' coupon on the Cart. No other coupons can be added until it is removed.", 'fflcommerce')); return false; } } $coupon = JS_Coupons::get_coupon($coupon_code); // Remove other coupons if this one is individual_use. if ($coupon['individual_use']) { if (!empty(self::$applied_coupons)) { fflcommerce::add_error(__("This is an 'individual use' coupon. All other discount coupons have been removed.", 'fflcommerce')); self::$applied_coupons = array(); } } // check if coupon is already applied and only add a new coupon if (!self::has_discount($coupon_code) && !empty($_POST['coupon_code'])) { self::$applied_coupons[] = $coupon_code; } fflcommerce_session::instance()->coupons = self::$applied_coupons; fflcommerce::add_message(__('Discount coupon applied successfully.', 'fflcommerce')); return true; }
function fflcommerce_cancel_order() { if (isset($_GET['cancel_order']) && isset($_GET['order']) && isset($_GET['order_id'])) { $order_key = urldecode($_GET['order']); $order_id = (int) $_GET['order_id']; $order = new fflcommerce_order($order_id); if ($order->id == $order_id && $order->order_key == $order_key && $order->status == 'pending' && fflcommerce::verify_nonce('cancel_order')) { // Cancel the order + restore stock $order->cancel_order(__('Order cancelled by customer.', 'fflcommerce')); // Message fflcommerce::add_message(__('Your order was cancelled.', 'fflcommerce')); } elseif ($order->status != 'pending') { fflcommerce::add_error(__('Your order is no longer pending and could not be cancelled. Please contact us if you need assistance.', 'fflcommerce')); } else { fflcommerce::add_error(__('Invalid order.', 'fflcommerce')); } wp_safe_redirect(fflcommerce_cart::get_cart_url()); exit; } }
/** * Check a nonce and sets FFL Commerce error in case it is invalid * To fail silently, set the error_message to an empty string * * @param string $action then nonce action * @return bool */ public static function verify_nonce($action) { $name = '_n'; $action = 'fflcommerce-' . $action; $request = array_merge($_GET, $_POST); if (!wp_verify_nonce($request[$name], $action)) { fflcommerce::add_error(__('Action failed. Please refresh the page and retry.', 'fflcommerce')); return false; } return true; }
/** * Generate the futurepay payment iframe */ protected function call_futurepay($order_id) { // Get the order $order = new fflcommerce_order($order_id); $data = array('gmid' => $this->gmid, 'reference' => $order_id . '-' . uniqid(), 'email' => $order->billing_email, 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address_line_1' => $order->billing_address_1, 'address_line_2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'country' => $order->billing_country, 'zip' => $order->billing_postcode, 'phone' => $order->billing_phone, 'shipping_address_line_1' => $order->shipping_address_1, 'shipping_address_line_2' => $order->shipping_address_2, 'shipping_city' => $order->shipping_city, 'shipping_state' => $order->shipping_state, 'shipping_country' => $order->shipping_country, 'shipping_zip' => $order->shipping_postcode, 'shipping_date' => date('Y/m/d g:i:s')); // for FFL Commerce 1.0, FuturePay doesn't allow negative prices (or 0.00 ) which affects discounts // with FuturePay doing the calcs, so we will bundle all products into ONE line item with // a quantity of ONE and send it that way using the final order total after shipping // and discounts are applied // all product titles will be comma delimited with their quantities $item_names = array(); if (sizeof($order->items) > 0) { foreach ($order->items as $item) { $_product = $order->get_product_from_item($item); $title = $_product->get_title(); // if variation, insert variation details into product title if ($_product instanceof fflcommerce_product_variation) { $title .= ' (' . fflcommerce_get_formatted_variation($_product, $item['variation'], true) . ')'; } $item_names[] = $item['qty'] . ' x ' . $title; } } // now add the one line item to the necessary product field arrays $data['sku'][] = "Products"; $data['price'][] = $order->order_total; // futurepay only needs final order amount $data['tax_amount'][] = 0; $data['description'][] = sprintf(__('Order %s', 'fflcommerce'), $order->get_order_number()) . ' = ' . implode(', ', $item_names); $data['quantity'][] = 1; try { $response = wp_remote_post(self::$request_url . 'merchant-request-order-token', array('body' => http_build_query($data), 'sslverify' => false)); // Convert error to exception if (is_wp_error($response)) { if (class_exists('WP_Exception') && $response instanceof WP_Exception) { throw $response; } else { fflcommerce_log($response->get_error_message()); throw new Exception($response->get_error_message()); } } // Fetch the body from the result, any errors should be caught before proceeding $response = trim(wp_remote_retrieve_body($response)); // we need something to validate the response. Valid transactions begin with 'FPTK' if (!strstr($response, 'FPTK')) { $error_message = isset(self::$futurepay_errorcodes[$response]) ? self::$futurepay_errorcodes[$response] : __('An unknown error has occured with code = ', 'fflcommerce') . $response; $order->add_order_note(sprintf(__('FUTUREPAY: %s', 'fflcommerce'), $error_message)); fflcommerce::add_error(sprintf(__('FUTUREPAY: %s. Please try again or select another gateway for your Order.', 'fflcommerce'), $error_message)); wp_safe_redirect(get_permalink(fflcommerce_get_page_id('checkout'))); exit; } /** * If we're good to go, haul in FuturePay's javascript and display the payment form * so that the customer can enter his ID and password */ echo '<div id="futurepay"></div>'; echo '<script src="' . self::$request_url . 'cart-integration/' . $response . '"></script>'; echo '<script type="text/javascript"> /*<![CDATA[*/ jQuery(window).load( function() { FP.CartIntegration(); // Need to replace form html jQuery("#futurepay").html(FP.CartIntegration.getFormContent()); FP.CartIntegration.displayFuturePay(); }); function FuturePayResponseHandler(response) { if (response.error) { // TODO: we need something better than this alert(response.code + " " + response.message); } else { window.location.replace("./?futurepay="+response.transaction_id); } } /*]]>*/ </script>'; echo '<input type="button" class="button alt" name="place_order" id="place_order" value="Place Order" onclick="FP.CartIntegration.placeOrder();" />'; } catch (Exception $e) { echo '<div class="fflcommerce_error">' . $e->getMessage() . '</div>'; fflcommerce_log('FUTUREPAY ERROR: ' . $e->getMessage()); } }