/** * update_shipping method, updates the shipping * @access public */ function update_shipping($method, $option) { global $wpdb, $wpsc_shipping_modules; if (!empty($method)) { $this->selected_shipping_method = $method; } if (!empty($option)) { $this->selected_shipping_option = $option; } $this->get_shipping_quotes(); // reapply coupon in case it's free shipping if ($this->coupons_name) { $coupon = new wpsc_coupons($this->coupons_name); if ($coupon->is_free_shipping()) { $this->apply_coupons($coupon->calculate_discount(), $this->coupons_name); } } }
function CallShortcutExpressCheckout($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL) { global $wpdb; $nvpstr = ''; $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType; $nvpstr = $nvpstr . "&RETURNURL=" . $returnURL; $nvpstr = $nvpstr . "&CANCELURL=" . $cancelURL; $nvpstr = $nvpstr . "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType; $data = array(); if (!isset($this->cart_data['shipping_address']['first_name']) && !isset($this->cart_data['shipping_address']['last_name'])) { $this->cart_data['shipping_address']['first_name'] = $this->cart_data['billing_address']['first_name']; $this->cart_data['shipping_address']['last_name'] = $this->cart_data['billing_address']['last_name']; } if ($this->cart_data['shipping_address']['country'] == 'UK') { $this->cart_data['shipping_address']['country'] = 'GB'; } $data += array('PAYMENTREQUEST_0_SHIPTONAME' => $this->cart_data['shipping_address']['first_name'] . ' ' . $this->cart_data['shipping_address']['last_name'], 'PAYMENTREQUEST_0_SHIPTOSTREET' => $this->cart_data['shipping_address']['address'], 'PAYMENTREQUEST_0_SHIPTOCITY' => $this->cart_data['shipping_address']['city'], 'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => $this->cart_data['shipping_address']['country'], 'PAYMENTREQUEST_0_SHIPTOZIP' => $this->cart_data['shipping_address']['post_code']); if (!empty($this->cart_data['shipping_address']['state'])) { $data += array('PAYMENTREQUEST_0_SHIPTOSTATE' => $this->cart_data['shipping_address']['state']); } $i = 0; $item_total = 0; $tax_total = 0; $shipping_total = 0; $is_free_shipping = false; if ($this->cart_data['has_discounts'] && (double) $this->cart_data['cart_discount_value'] > 0) { $coupon = new wpsc_coupons($this->cart_data['cart_discount_coupon']); $is_free_shipping = $coupon->is_free_shipping(); } foreach ($this->cart_items as $cart_item) { $data["L_PAYMENTREQUEST_0_NAME{$i}"] = urlencode(apply_filters('the_title', $cart_item['name'])); $data["L_PAYMENTREQUEST_0_AMT{$i}"] = $this->convert($cart_item['price']); $data["L_PAYMENTREQUEST_0_NUMBER{$i}"] = $i; $data["L_PAYMENTREQUEST_0_QTY{$i}"] = $cart_item['quantity']; $item_total += $this->convert($cart_item['price']) * $cart_item['quantity']; $shipping_total += $cart_item['shipping']; $i++; } //if we have a discount then include a negative amount with that discount // in php 0.00 = true so we will change that here if ($this->cart_data['cart_discount_value'] == 0.0) { $this->cart_data['cart_discount_value'] = 0; } $discount_value = $this->convert($this->cart_data['cart_discount_value']); if ($this->cart_data['cart_discount_value'] && !$is_free_shipping) { // if item total < discount amount, leave at least 0.01 unit in item total, then subtract // 0.01 from shipping as well if (!$is_free_shipping && $discount_value >= $item_total) { $discount_value = $item_total - 0.01; $shipping_total -= 0.01; } $item_total -= $discount_value; $data["L_PAYMENTREQUEST_0_NAME{$i}"] = "Discount / Coupon"; $data["L_PAYMENTREQUEST_0_AMT{$i}"] = -$discount_value; $data["L_PAYMENTREQUEST_0_NUMBER{$i}"] = $i; $data["L_PAYMENTREQUEST_0_QTY{$i}"] = 1; } $data["PAYMENTREQUEST_0_ITEMAMT"] = $this->format_price($item_total); if ($discount_value && $is_free_shipping) { $data["PAYMENTREQUEST_0_SHIPPINGAMT"] = 0; } else { $data["PAYMENTREQUEST_0_SHIPPINGAMT"] = $this->convert($this->cart_data['base_shipping'] + $shipping_total); } $total = $data["PAYMENTREQUEST_0_ITEMAMT"] + $data["PAYMENTREQUEST_0_SHIPPINGAMT"]; if (!wpsc_tax_isincluded()) { $data["PAYMENTREQUEST_0_TAXAMT"] = $this->convert($this->cart_data['cart_tax']); $total += $data["PAYMENTREQUEST_0_TAXAMT"]; } // adjust total amount in case we had to round up after converting currency // or discount calculation if ($total != $paymentAmount) { $paymentAmount = $total; } $data["PAYMENTREQUEST_0_AMT"] = $paymentAmount; if (count($data) >= 4) { $temp_data = array(); foreach ($data as $key => $value) { $temp_data[] = $key . "=" . $value; } $nvpstr = $nvpstr . "&" . implode("&", $temp_data); } wpsc_update_customer_meta('paypal_express_currency_code_type', $currencyCodeType); wpsc_update_customer_meta('paypal_express_payment_type', $paymentType); $resArray = paypal_hash_call("SetExpressCheckout", $nvpstr); $ack = strtoupper($resArray["ACK"]); if ($ack == "SUCCESS") { $token = urldecode($resArray["TOKEN"]); wpsc_update_customer_meta('paypal_express_token', $token); } return $resArray; }
/** * @description: wpec_taxes_calculate_total - takes into account all tax logic * settings and returns the calculated total tax. * Expects wpsc_cart to be set. * * @param: void * @return: array containing total tax and rate if applicable * */ function wpec_taxes_calculate_total() { //get the cart - NOTE: billing country is selected_country and shipping country is delivery_country global $wpsc_cart; //initialize return variable $returnable = array('total' => 0, 'rate' => 0); //check if tax is enabled if ($this->wpec_taxes->wpec_taxes_get_enabled()) { //run tax logic and calculate tax if ($this->wpec_taxes_run_logic()) { //get selected country code $wpec_selected_country = $this->wpec_taxes_retrieve_selected_country(); //set tax region $region = $this->wpec_taxes_retrieve_region(); //get the rate for the country and region if set $tax_rate = $this->wpec_taxes->wpec_taxes_get_rate($wpec_selected_country, $region); //start the total_tax off at 0 $total_tax = 0; foreach ($wpsc_cart->cart_items as $cart_item) { //if the tax is inclusive calculate vat if ($this->wpec_taxes_isincluded()) { //run wpec_taxes_calculate_included_tax $taxes = $this->wpec_taxes_calculate_included_tax($cart_item); $total_tax += $taxes['tax']; } else { //run wpec_taxes_calculate_excluded_tax $taxes = $this->wpec_taxes_calculate_excluded_tax($cart_item, $tax_rate); $total_tax += $taxes['tax']; } // if } // foreach $free_shipping = false; $coupon_num = wpsc_get_customer_meta('coupon'); if ($coupon_num) { $coupons = new wpsc_coupons($coupon_num); $free_shipping = $coupons->is_free_shipping(); } // minus coupon tax if we are using coupons, but make sure the coupon is not a free shipping coupon /* Iterative note: In a future implementation, we'll allow for coupons to either apply to taxes, or not */ /* The default logic to date has been that they do, which is generally improper, and there's a logic bug here as well */ /* @link: https://github.com/wp-e-commerce/WP-e-Commerce/issues/170 */ if ($wpsc_cart->coupons_amount > 0 && !$free_shipping) { if ($this->wpec_taxes_isincluded()) { $coupon_tax = $this->wpec_taxes_calculate_tax($wpsc_cart->coupons_amount, $tax_rate['rate'], false); } else { $coupon_tax = $this->wpec_taxes_calculate_tax($wpsc_cart->coupons_amount, $tax_rate['rate']); } /* Only subtract if coupons apply to tax. Likely in 4.0 */ /* $total_tax -= $coupon_tax; */ } //add shipping tax if set if ($tax_rate['shipping'] && !$free_shipping) { if ($this->wpec_taxes_isincluded()) { $total_tax += $this->wpec_taxes_calculate_tax($wpsc_cart->calculate_total_shipping(), $tax_rate['rate'], false); } else { $total_tax += $this->wpec_taxes_calculate_tax($wpsc_cart->calculate_total_shipping(), $tax_rate['rate']); } } // if $returnable = array('total' => $total_tax); if (!$this->wpec_taxes_isincluded() && isset($tax_rate['rate'])) { $returnable['rate'] = $tax_rate['rate']; } // if } // if } //if return $returnable; }
/** * update_shipping method, updates the shipping * @access public */ function update_shipping($method, $option) { global $wpdb, $wpsc_shipping_modules; $this->selected_shipping_method = $method; $this->shipping_quotes = $wpsc_shipping_modules[$method]->getQuote(); $this->selected_shipping_option = $option; foreach ($this->cart_items as $key => $cart_item) { $this->cart_items[$key]->calculate_shipping(); } $this->clear_cache(); $this->get_shipping_option(); // reapply coupon in case it's free shipping if ($this->coupons_name) { $coupon = new wpsc_coupons($this->coupons_name); if ($coupon->is_free_shipping()) { $this->apply_coupons($coupon->calculate_discount(), $this->coupons_name); } } }
/** * construct value array method, converts the data gathered by the base class code to something acceptable to the gateway * @access private * @param boolean $aggregate Whether to aggregate the cart data or not. Defaults to false. * @return array $paypal_vars The paypal vars */ function _construct_value_array($aggregate = false) { global $wpdb, $wpsc_cart; $paypal_vars = array(); $add_tax = !wpsc_tax_isincluded(); $buy_now = defined('WPSC_PAYPAL_BUY_NOW') && WPSC_PAYPAL_BUY_NOW; $return_url = add_query_arg('sessionid', $this->cart_data['session_id'], $this->cart_data['transaction_results_url']); if ($buy_now) { $return_url = add_query_arg('wpsc_buy_now_return', 1, $return_url); } // Store settings to be sent to paypal $paypal_vars += array('business' => get_option('paypal_multiple_business'), 'return' => $return_url, 'cancel_return' => $this->cart_data['transaction_results_url'], 'rm' => '2', 'currency_code' => $this->get_paypal_currency_code(), 'lc' => $this->cart_data['store_currency'], 'no_note' => '1', 'charset' => 'utf-8'); // IPN data if (get_option('paypal_ipn') == 1) { $notify_url = $this->cart_data['notification_url']; $notify_url = add_query_arg('gateway', 'wpsc_merchant_paypal_standard', $notify_url); $notify_url = apply_filters('wpsc_paypal_standard_notify_url', $notify_url); $paypal_vars += array('notify_url' => $notify_url); } // Customer details $paypal_vars += array('email' => $this->cart_data['email_address'], 'first_name' => $this->cart_data['billing_address']['first_name'], 'last_name' => $this->cart_data['billing_address']['last_name'], 'address1' => $this->cart_data['billing_address']['address'], 'city' => $this->cart_data['billing_address']['city'], 'state' => isset($this->cart_data['billing_address']['state']) ? $this->cart_data['billing_address']['state'] : '', 'zip' => $this->cart_data['billing_address']['post_code'], 'country' => isset($this->cart_data['billing_address']['country']) ? $this->cart_data['billing_address']['country'] : ''); // Shipping if ((bool) get_option('paypal_ship') && !$buy_now) { $paypal_vars += array('address_override' => get_option('address_override'), 'no_shipping' => '0'); if ($paypal_vars['country'] == 'UK') { $paypal_vars['country'] = 'GB'; } } else { $paypal_vars += array('no_shipping' => '1'); } // Order settings to be sent to paypal $paypal_vars += array('invoice' => $this->cart_data['session_id']); if ($buy_now) { $paypal_vars['custom'] = 'buy_now'; } // Two cases: // - We're dealing with a subscription // - We're dealing with a normal cart if ($this->cart_data['is_subscription']) { $paypal_vars += array('cmd' => '_xclick-subscriptions'); $reprocessed_cart_data['shopping_cart'] = array('is_used' => false, 'price' => 0, 'length' => 1, 'unit' => 'd', 'times_to_rebill' => 1); $reprocessed_cart_data['subscription'] = array('is_used' => false, 'price' => 0, 'length' => 1, 'unit' => 'D', 'times_to_rebill' => 1); foreach ($this->cart_items as $cart_row) { if ($cart_row['is_recurring']) { $reprocessed_cart_data['subscription']['is_used'] = true; $reprocessed_cart_data['subscription']['price'] = $this->convert($cart_row['price']); $reprocessed_cart_data['subscription']['length'] = $cart_row['recurring_data']['rebill_interval']['length']; $reprocessed_cart_data['subscription']['unit'] = strtoupper($cart_row['recurring_data']['rebill_interval']['unit']); $reprocessed_cart_data['subscription']['times_to_rebill'] = $cart_row['recurring_data']['times_to_rebill']; } else { $item_cost = ($cart_row['price'] + $cart_row['shipping'] + $cart_row['tax']) * $cart_row['quantity']; if ($item_cost > 0) { $reprocessed_cart_data['shopping_cart']['price'] += $item_cost; $reprocessed_cart_data['shopping_cart']['is_used'] = true; } } $paypal_vars += array('item_name' => apply_filters('the_title', $cart_row['name']), 'src' => '1'); // This can be false, we don't need to have additional items in the cart/ if ($reprocessed_cart_data['shopping_cart']['is_used']) { $paypal_vars += array("a1" => $this->convert($reprocessed_cart_data['shopping_cart']['price']), "p1" => $reprocessed_cart_data['shopping_cart']['length'], "t1" => $reprocessed_cart_data['shopping_cart']['unit']); } // We need at least one subscription product, // If this is not true, something is rather wrong. if ($reprocessed_cart_data['subscription']['is_used']) { $paypal_vars += array("a3" => $this->convert($reprocessed_cart_data['subscription']['price']), "p3" => $reprocessed_cart_data['subscription']['length'], "t3" => $reprocessed_cart_data['subscription']['unit']); // If the srt value for the number of times to rebill is not greater than 1, // paypal won't accept the transaction. if ($reprocessed_cart_data['subscription']['times_to_rebill'] > 1) { $paypal_vars += array('srt' => $reprocessed_cart_data['subscription']['times_to_rebill']); } } } // end foreach cart item } else { if ($buy_now) { $paypal_vars['cmd'] = '_xclick'; } else { $paypal_vars += array('upload' => '1', 'cmd' => '_ext-enter', 'redirect_cmd' => '_cart'); } $free_shipping = false; $coupon = wpsc_get_customer_meta('coupon'); if ($coupon) { $coupon = new wpsc_coupons($coupon); $free_shipping = $coupon->is_free_shipping(); } if ($this->cart_data['has_discounts'] && $free_shipping) { $handling = 0; } else { $handling = $this->cart_data['base_shipping']; } $tax_total = 0; if ($add_tax) { $tax_total = $this->cart_data['cart_tax']; } // Set base shipping $paypal_vars += array('handling_cart' => $this->convert($handling)); // Stick the cart item values together here $i = 1; if (!$buy_now) { if (!$aggregate) { foreach ($this->cart_items as $cart_row) { $item_number = get_post_meta($cart_row['product_id'], '_wpsc_sku', true); if (!$item_number) { $item_number = $cart_row['product_id']; } $paypal_vars += array("item_name_{$i}" => apply_filters('the_title', $cart_row['name']), "amount_{$i}" => $this->convert($cart_row['price']), "quantity_{$i}" => $cart_row['quantity'], "item_number_{$i}" => $item_number); if (!$free_shipping) { $paypal_vars += array("shipping_{$i}" => $this->convert($cart_row['shipping'] / $cart_row['quantity']), "shipping2_{$i}" => $this->convert($cart_row['shipping'] / $cart_row['quantity']), "handling_{$i}" => ''); } if ($add_tax && !empty($cart_row['tax'])) { $tax_total += $cart_row['tax']; } ++$i; } if ($this->cart_data['has_discounts'] && !$free_shipping) { $paypal_vars['discount_amount_cart'] = $this->convert($this->cart_data['cart_discount_value']); $subtotal = $wpsc_cart->calculate_subtotal(); if ($this->cart_data['cart_discount_value'] >= $wpsc_cart->calculate_subtotal()) { $paypal_vars['discount_amount_cart'] = $this->convert($subtotal) - 0.01; if (!empty($paypal_vars['handling_cart'])) { $paypal_vars['handling_cart'] -= 0.01; } } } } else { $paypal_vars['item_name_' . $i] = __("Your Shopping Cart", 'wp-e-commerce'); $paypal_vars['amount_' . $i] = $this->convert($this->cart_data['total_price']) - $this->convert($this->cart_data['base_shipping']); $paypal_vars['quantity_' . $i] = 1; $paypal_vars['shipping_' . $i] = 0; $paypal_vars['shipping2_' . $i] = 0; $paypal_vars['handling_' . $i] = 0; } $paypal_vars['tax_cart'] = $aggregate ? 0 : $this->convert($tax_total); } else { $cart_row = $this->cart_items[0]; $item_number = get_post_meta($cart_row['product_id'], '_wpsc_sku', true); $paypal_vars += array('item_name' => apply_filters('the_title', $cart_row['name']), 'item_number' => $item_number, 'amount' => $this->convert($cart_row['price']), 'quantity' => $cart_row['quantity'], 'handling' => $this->convert($handling)); } } $paypal_vars = apply_filters('wpsc_paypal_standard_post_data', $paypal_vars); $paypal_vars['bn'] = 'WPeC_Cart_WPS'; return $paypal_vars; }