/** * Validate the checkout */ public function validate_checkout() { if (fflcommerce_cart::is_empty()) { fflcommerce::add_error(sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage →</a>', 'fflcommerce'), home_url())); } // Process Discount Codes if (!empty($_POST['coupon_code'])) { $coupon = sanitize_title($_POST['coupon_code']); fflcommerce_cart::add_discount($coupon); } foreach (fflcommerce_cart::get_coupons() as $coupon) { fflcommerce_cart::is_valid_coupon($coupon); } // Checkout fields $this->posted['shipping_method'] = ''; $this->posted['shipping_service'] = ''; if (isset($_POST['shipping_method'])) { $shipping_method = fflcommerce_clean($_POST['shipping_method']); $shipping_data = explode(':', $shipping_method); $this->posted['shipping_method'] = $shipping_data[0]; $this->posted['shipping_service'] = $shipping_data[1]; } $this->posted['shiptobilling'] = isset($_POST['shiptobilling']) ? fflcommerce_clean($_POST['shiptobilling']) : ''; $this->posted['payment_method'] = isset($_POST['payment_method']) ? fflcommerce_clean($_POST['payment_method']) : ''; $this->posted['order_comments'] = isset($_POST['order_comments']) ? fflcommerce_clean($_POST['order_comments']) : ''; $this->posted['terms'] = isset($_POST['terms']) ? fflcommerce_clean($_POST['terms']) : ''; $this->posted['create_account'] = isset($_POST['create_account']) ? fflcommerce_clean($_POST['create_account']) : ''; $this->posted['account_username'] = isset($_POST['account_username']) ? fflcommerce_clean($_POST['account_username']) : ''; $this->posted['account_password'] = isset($_POST['account_password']) ? fflcommerce_clean($_POST['account_password']) : ''; $this->posted['account_password_2'] = isset($_POST['account_password_2']) ? fflcommerce_clean($_POST['account_password_2']) : ''; if (fflcommerce_cart::get_total(false) == 0) { $this->posted['payment_method'] = 'no_payment'; } // establish customer billing and shipping locations if (fflcommerce_cart::ship_to_billing_address_only()) { $this->posted['shiptobilling'] = 'true'; } $country = isset($_POST['billing_country']) ? fflcommerce_clean($_POST['billing_country']) : ''; $state = isset($_POST['billing_state']) ? fflcommerce_clean($_POST['billing_state']) : ''; $allowed_countries = FFLCommerce_Base::get_options()->get('fflcommerce_allowed_countries'); if ($allowed_countries === 'specific') { $specific_countries = FFLCommerce_Base::get_options()->get('fflcommerce_specific_allowed_countries'); if (!in_array($country, $specific_countries)) { fflcommerce::add_error(__('Invalid billing country.', 'fflcommerce')); return; } } if (fflcommerce_countries::country_has_states($country)) { $states = fflcommerce_countries::get_states($country); if (!in_array($state, array_keys($states))) { fflcommerce::add_error(__('Invalid billing state.', 'fflcommerce')); return; } } $postcode = isset($_POST['billing_postcode']) ? fflcommerce_clean($_POST['billing_postcode']) : ''; $ship_to_billing = FFLCommerce_Base::get_options()->get('fflcommerce_ship_to_billing_address_only') == 'yes'; fflcommerce_customer::set_location($country, $state, $postcode); if (FFLCommerce_Base::get_options()->get('fflcommerce_calc_shipping') == 'yes') { if ($ship_to_billing || !empty($_POST['shiptobilling'])) { fflcommerce_customer::set_shipping_location($country, $state, $postcode); } else { $country = isset($_POST['shipping_country']) ? fflcommerce_clean($_POST['shipping_country']) : ''; $state = isset($_POST['shipping_state']) ? fflcommerce_clean($_POST['shipping_state']) : ''; $postcode = isset($_POST['shipping_postcode']) ? fflcommerce_clean($_POST['shipping_postcode']) : ''; if ($allowed_countries === 'specific') { $specific_countries = FFLCommerce_Base::get_options()->get('fflcommerce_specific_allowed_countries'); if (!in_array($country, $specific_countries)) { fflcommerce::add_error(__('Invalid shipping country.', 'fflcommerce')); return; } } if (fflcommerce_countries::country_has_states($country)) { $states = fflcommerce_countries::get_states($country); if (!in_array($state, array_keys($states))) { fflcommerce::add_error(__('Invalid shipping state.', 'fflcommerce')); return; } } fflcommerce_customer::set_shipping_location($country, $state, $postcode); } } // Billing Information foreach ($this->billing_fields as $field) { $field = apply_filters('fflcommerce_billing_field', $field); $this->posted[$field['name']] = isset($_POST[$field['name']]) ? fflcommerce_clean($_POST[$field['name']]) : ''; // Format if (isset($field['format'])) { switch ($field['format']) { case 'postcode': $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']])); break; } } // Required if ($field['name'] == 'billing_state' && fflcommerce_customer::has_valid_shipping_state()) { $field['required'] = false; } if (isset($field['required']) && $field['required'] && empty($this->posted[$field['name']])) { fflcommerce::add_error($field['label'] . __(' (billing) is a required field.', 'fflcommerce')); } if ($field['name'] == 'billing_euvatno') { $vatno = isset($this->posted['billing_euvatno']) ? $this->posted['billing_euvatno'] : ''; $vatno = str_replace(' ', '', $vatno); $country = fflcommerce_tax::get_customer_country(); // strip any country code from the beginning of the number if (strpos($vatno, $country) === 0) { $vatno = substr($vatno, strlen($country)); } if ($vatno != '') { $url = 'http://isvat.appspot.com/' . $country . '/' . $vatno . '/'; $httpRequest = curl_init(); curl_setopt($httpRequest, CURLOPT_FAILONERROR, true); curl_setopt($httpRequest, CURLOPT_RETURNTRANSFER, true); curl_setopt($httpRequest, CURLOPT_HEADER, false); curl_setopt($httpRequest, CURLOPT_URL, $url); $result = curl_exec($httpRequest); curl_close($httpRequest); if ($result === 'false') { fflcommerce_log('EU VAT validation error with URL: ' . $url); fflcommerce::add_error($field['label'] . __(' (billing) is not a valid VAT Number. Leave it blank to disable VAT validation. (VAT may be charged depending on your location)', 'fflcommerce')); } else { $this->valid_euvatno = fflcommerce_countries::get_base_country() != fflcommerce_tax::get_customer_country() && fflcommerce_countries::is_eu_country(fflcommerce_tax::get_customer_country()); } } } // Validation if (isset($field['validate']) && !empty($this->posted[$field['name']])) { switch ($field['validate']) { case 'phone': if (!fflcommerce_validation::is_phone($this->posted[$field['name']])) { fflcommerce::add_error($field['label'] . __(' (billing) is not a valid number.', 'fflcommerce')); } break; case 'email': if (!fflcommerce_validation::is_email($this->posted[$field['name']])) { fflcommerce::add_error($field['label'] . __(' (billing) is not a valid email address.', 'fflcommerce')); } break; case 'postcode': if (!fflcommerce_validation::is_postcode($this->posted[$field['name']], $_POST['billing_country'])) { fflcommerce::add_error($field['label'] . __(' (billing) is not a valid postcode/ZIP.', 'fflcommerce')); } else { $this->posted[$field['name']] = fflcommerce_validation::format_postcode($this->posted[$field['name']], $_POST['billing_country']); } break; } } } // Shipping Information if (fflcommerce_shipping::is_enabled() && !fflcommerce_cart::ship_to_billing_address_only() && empty($this->posted['shiptobilling'])) { foreach ($this->shipping_fields as $field) { $field = apply_filters('fflcommerce_shipping_field', $field); if (isset($_POST[$field['name']])) { $this->posted[$field['name']] = fflcommerce_clean($_POST[$field['name']]); } else { $this->posted[$field['name']] = ''; } // Format if (isset($field['format'])) { switch ($field['format']) { case 'postcode': $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']])); break; } } // Required if ($field['name'] == 'shipping_state' && fflcommerce_customer::has_valid_shipping_state()) { $field['required'] = false; } if (isset($field['required']) && $field['required'] && empty($this->posted[$field['name']])) { fflcommerce::add_error($field['label'] . __(' (shipping) is a required field.', 'fflcommerce')); } // Validation if (isset($field['validate']) && !empty($this->posted[$field['name']])) { switch ($field['validate']) { case 'postcode': if (!fflcommerce_validation::is_postcode($this->posted[$field['name']], $country)) { fflcommerce::add_error($field['label'] . __(' (shipping) is not a valid postcode/ZIP.', 'fflcommerce')); } else { $this->posted[$field['name']] = fflcommerce_validation::format_postcode($this->posted[$field['name']], $country); } break; } } } } if ($this->must_register && empty($this->posted['create_account'])) { fflcommerce::add_error(__('Sorry, you must agree to creating an account', 'fflcommerce')); } if ($this->must_register || empty($user_id) && $this->posted['create_account']) { if (!$this->show_signup) { fflcommerce::add_error(__('Sorry, the shop owner has disabled guest purchases.', 'fflcommerce')); } if (empty($this->posted['account_username'])) { fflcommerce::add_error(__('Please enter an account username.', 'fflcommerce')); } if (empty($this->posted['account_password'])) { fflcommerce::add_error(__('Please enter an account password.', 'fflcommerce')); } if ($this->posted['account_password_2'] !== $this->posted['account_password']) { fflcommerce::add_error(__('Passwords do not match.', 'fflcommerce')); } // Check the username if (!validate_username($this->posted['account_username'])) { fflcommerce::add_error(__('Invalid email/username.', 'fflcommerce')); } elseif (username_exists($this->posted['account_username'])) { fflcommerce::add_error(__('An account is already registered with that username. Please choose another.', 'fflcommerce')); } // Check the e-mail address if (email_exists($this->posted['billing_email'])) { fflcommerce::add_error(__('An account is already registered with your email address. Please login.', 'fflcommerce')); } } // Terms if (!isset($_POST['update_totals']) && empty($this->posted['terms']) && fflcommerce_get_page_id('terms') > 0) { fflcommerce::add_error(__('You must accept our Terms & Conditions.', 'fflcommerce')); } if (fflcommerce_cart::needs_shipping()) { // Shipping Method $available_methods = fflcommerce_shipping::get_available_shipping_methods(); if (!isset($available_methods[$this->posted['shipping_method']])) { fflcommerce::add_error(__('Invalid shipping method.', 'fflcommerce')); } } }
?> </tfoot> <?php do_action('fflcommerce_shop_table_cart'); ?> </table> </form> <div class="cart-collaterals"> <?php do_action('cart-collaterals'); ?> <div class="cart_totals"> <?php // Hide totals if customer has set location and there are no methods going there $available_methods = fflcommerce_shipping::get_available_shipping_methods(); if ($available_methods || !fflcommerce_customer::get_shipping_country() || !fflcommerce_shipping::is_enabled()) { do_action('fflcommerce_before_cart_totals'); ?> <h2><?php _e('Cart Totals', 'fflcommerce'); ?> </h2> <div class="cart_totals_table"> <table cellspacing="0" cellpadding="0"> <tbody> <tr> <?php $price_label = fflcommerce_cart::show_retail_price() ? __('Retail Price', 'fflcommerce') : __('Subtotal', 'fflcommerce'); ?> <th class="cart-row-subtotal-title"><?php echo $price_label;
/** * Generate the paypal button link * * @param int $order_id * @return string */ public function generate_paypal_form($order_id) { $order = new fflcommerce_order($order_id); if ($this->testmode == 'yes') { $url = $this->testurl . '?test_ipn=1&'; } else { $url = $this->liveurl . '?'; } if (in_array($order->billing_country, array('US', 'CA'))) { $order->billing_phone = str_replace(array('(', '-', ' ', ')'), '', $order->billing_phone); $phone_args = array('night_phone_a' => substr($order->billing_phone, 0, 3), 'night_phone_b' => substr($order->billing_phone, 3, 3), 'night_phone_c' => substr($order->billing_phone, 6, 4), 'day_phone_a' => substr($order->billing_phone, 0, 3), 'day_phone_b' => substr($order->billing_phone, 3, 3), 'day_phone_c' => substr($order->billing_phone, 6, 4)); } else { $phone_args = array('night_phone_b' => $order->billing_phone, 'day_phone_b' => $order->billing_phone); } // filter redirect page $checkout_redirect = apply_filters('fflcommerce_get_checkout_redirect_page_id', fflcommerce_get_page_id('thanks')); $paypal_args = array_merge(array('cmd' => '_cart', 'business' => $this->testmode == 'yes' ? $this->testemail : $this->email, 'no_note' => 1, 'currency_code' => FFLCommerce_Base::get_options()->get('fflcommerce_currency'), 'charset' => 'UTF-8', 'rm' => 2, 'upload' => 1, 'return' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink($checkout_redirect))), 'cancel_return' => $order->get_cancel_order_url(), 'custom' => $order_id, 'notify_url' => $this->notify_url, 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address1' => $order->billing_address_1, 'address2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zip' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'invoice' => $order->get_order_number(), 'amount' => number_format((double) $order->order_total, $this->decimals), 'bn' => 'FFLCommerce_SP'), $phone_args); if ($this->send_shipping == 'yes') { $paypal_args['no_shipping'] = 1; $paypal_args['address_override'] = 1; $paypal_args['first_name'] = $order->shipping_first_name; $paypal_args['last_name'] = $order->shipping_last_name; $paypal_args['address1'] = $order->shipping_address_1; $paypal_args['address2'] = $order->shipping_address_2; $paypal_args['city'] = $order->shipping_city; $paypal_args['state'] = $order->shipping_state; $paypal_args['zip'] = $order->shipping_postcode; $paypal_args['country'] = $order->shipping_country; // PayPal counts Puerto Rico as a US Territory, won't allow payment without it if ($paypal_args['country'] == 'PR') { $paypal_args['country'] = 'US'; $paypal_args['state'] = 'PR'; } } else { $paypal_args['no_shipping'] = 1; $paypal_args['address_override'] = 0; } // If prices include tax, send the whole order as a single item if (FFLCommerce_Base::get_options()->get('fflcommerce_prices_include_tax') == 'yes') { // Discount $paypal_args['discount_amount_cart'] = number_format((double) $order->order_discount, $this->decimals); // Don't pass items - PayPal breaks tax due to catalog prices include tax. // PayPal has no option for tax inclusive pricing. // Pass 1 item for the order items overall $item_names = array(); 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[] = $title . ' x ' . $item['qty']; } $paypal_args['item_name_1'] = sprintf(__('Order %s', 'fflcommerce'), $order->get_order_number()) . ' - ' . implode(', ', $item_names); $paypal_args['quantity_1'] = 1; $paypal_args['amount_1'] = number_format($order->order_total - $order->order_shipping - $order->order_shipping_tax + $order->order_discount, $this->decimals, '.', ''); if ($order->order_shipping + $order->order_shipping_tax > 0) { $paypal_args['item_name_2'] = __('Shipping cost', 'fflcommerce'); $paypal_args['quantity_2'] = '1'; $paypal_args['amount_2'] = number_format($order->order_shipping + $order->order_shipping_tax, $this->decimals, '.', ''); } } else { // Cart Contents $item_loop = 0; foreach ($order->items as $item) { $_product = $order->get_product_from_item($item); if ($_product->exists() && $item['qty']) { $item_loop++; $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) . ')'; } $paypal_args['item_name_' . $item_loop] = $title; $paypal_args['quantity_' . $item_loop] = $item['qty']; $paypal_args['amount_' . $item_loop] = number_format(apply_filters('fflcommerce_paypal_adjust_item_price', $item['cost'], $item, 10, 2), $this->decimals); //Apparently, Paypal did not like "28.4525" as the amount. Changing that to "28.45" fixed the issue. } } // Shipping Cost if (fflcommerce_shipping::is_enabled() && $order->order_shipping > 0) { $item_loop++; $paypal_args['item_name_' . $item_loop] = __('Shipping cost', 'fflcommerce'); $paypal_args['quantity_' . $item_loop] = '1'; $paypal_args['amount_' . $item_loop] = number_format((double) $order->order_shipping, $this->decimals); } $paypal_args['tax'] = $order->get_total_tax(false, false); // no currency sign or pricing options for separators $paypal_args['tax_cart'] = $order->get_total_tax(false, false); // no currency sign or pricing options for separators $paypal_args['discount_amount_cart'] = $order->order_discount; if ($this->force_payment == 'yes') { $sum = 0; for ($i = 1; $i < $item_loop; $i++) { $sum += $paypal_args['amount_' . $i]; } $item_loop++; if ($sum == 0 || $order->order_discount && $sum - $order->order_discount == 0) { $paypal_args['item_name_' . $item_loop] = __('Force payment on free', 'fflcommerce'); $paypal_args['quantity_' . $item_loop] = '1'; $paypal_args['amount_' . $item_loop] = 0.01; // force payment } } } $paypal_args = apply_filters('fflcommerce_paypal_args', $paypal_args); return fflcommerce_render_result('gateways/paypal', array('url' => $url, 'fields' => $paypal_args)); }
/** looks through the cart to see if shipping is actually required */ public static function needs_shipping() { if (!fflcommerce_shipping::is_enabled() || !is_array(self::$cart_contents)) { return false; } foreach (self::$cart_contents as $values) { /** @var fflcommerce_product $_product */ $_product = $values['data']; if ($_product->requires_shipping()) { return true; } } return false; }