addr1)) { $url .= '&first_name=' . urlencode($caddress->get_first_name()) . '&last_name=' . urlencode($caddress->get_last_name()) . '&address1=' . urlencode($caddress->addr1) . '&city=' . urlencode($caddress->city) . '&country=' . urlencode($caddress->country) . '&address_override=1'; if (!empty($caddress->addr2)) { $url .= '&address2=' . urlencode($caddress->addr2); } if (!empty($caddress->state)) { $url .= '&state=' . urlencode($caddress->state); } if (!empty($caddress->postcode)) { $url .= '&zip=' . urlencode($caddress->postcode); } } } $total_items = 0.0; foreach ($items as $key => $item) { $total_items += $item->quantity; } $total_price = 0.0; $count = 1; foreach ($items as $key => $item) { $itemname = urlencode(yak_get_title($item->id, $item->cat_id)); $price = yak_calc_price($item->id, $item->cat_id); $price = yak_calc_discount_price($price, $promo); $total_price += $price; if ($item->quantity > 0) { $url = $url . '&item_name_' . $count . '=' . urlencode($item->name) . '&amount_' . $count . '=' . $price . '&quantity_' . $count . '=' . $item->quantity; $count += 1; } } $shippingcost = yak_calc_discount_shipping($shippingcost, $promo); $total_price += $shippingcost; if ($total_price == 0.0) { $url = yak_get_url(yak_get_option(PAYPAL_RETURN_URL, ''), true) . '&order_id=' . $order_id; yak_insert_orderlog($order_id, 'Total order cost is 0, not submitting to PayPal'); yak_check_order($order_id); } else { $url = $url . '&custom=' . $order_id . '&handling_cart=' . $shippingcost; error_log("paypal redirect url = $url"); } return $url; } } if (!function_exists('yak_redirect_paypal_pro')) { /** * Use PayPal Payments Pro to handle payment. * * @see yak_redirect_page for description of parameters */ function yak_redirect_paypal_pro($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email) { global $wpdb, $cards; $registry =& Registry::getInstance(); $order_table =& $registry->get('order_table'); $cc = $_SESSION['cc']; $value = yak_order_value(false, $order_id); $caddress = yak_get_address('billing', false); $arr = split("[\n\r\t ]+", $caddress->recipient); $firstname = $arr[0]; $lastname = $arr[1]; $card_detail = $cards[strtolower($cc['type'])]; $params_array = array( 'USER' => yak_get_option(PAYPAL_API_USERNAME, ''), 'PWD' => yak_get_option(PAYPAL_API_PASSWORD, ''), 'SIGNATURE' => yak_get_option(PAYPAL_API_SIGNATURE, ''), 'VERSION' => '3.2', 'METHOD' => 'DoDirectPayment', 'PAYMENTACTION' => 'Sale', 'IPADDRESS' => $_SERVER['REMOTE_ADDR'], 'CREDITCARDTYPE' => $card_detail['paypal-name'], 'ACCT' => $cc['number'], 'EXPDATE' => str_replace('/', '', $cc['expiry']), 'CVV2' => $cc['security_code'], 'AMT' => $value, 'INVNUM' => $order_id, 'STREET' => $caddress->addr1, 'CITY' => $caddress->city, 'STATE' => $caddress->get_state_or_region(), 'COUNTRYCODE' => $caddress->country, 'ZIP' => $caddress->postcode, 'FIRSTNAME' => $firstname, 'LASTNAME' => $lastname ); $params = yak_encode_params($params_array); if ($payment_type == PAYPAL_PRO_LIVE) { $url = PAYPAL_PRO_URL; } else { $url = PAYPAL_PRO_SANDBOX_URL; } if ($value > 0.0) { $response = yak_do_http($url, '', $params); $param_array = yak_decode_params($response); $rtn = ''; if ($param_array['ACK'] == 'Success' || $param_array['ACK'] == 'SuccessWithWarning') { $wpdb->query("update $order_table set funds_received = $value where id = $order_id"); if ($param_array['ACK']) { yak_insert_orderlog($order_id, 'PayPal Pro transaction was approved'); $wpdb->query("update $order_table set funds_received = $value where id = $order_id"); } else { yak_insert_orderlog($order_id, 'PayPal Pro transaction was approved with warning(s) -- please check your PayPal account (manual intervention will be required)'); } $rtn = yak_redirect_page($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email); } else { $_SESSION['error_message'] = $param_array['L_LONGMESSAGE0']; yak_insert_orderlog($order_id, 'PayPal Pro transaction has failed'); $wpdb->query("update $order_table set status = '" . ERROR . "' where id = $order_id"); $rtn = yak_get_option(PAYPAL_ERROR_URL, ''); } $response = ''; foreach ($param_array as $key=>$value) { $response .= "$key = $value "; } yak_insert_orderlog($order_id, 'response received from PayPal Pro was: ' . $response); } else { // no order value -- just redirect to the success page $rtn = yak_redirect_page($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email); yak_insert_orderlog($order_id, 'Total order cost is 0, not submitting to PayPal Payments Pro'); } yak_check_order($order_id); return $rtn; } } if (!function_exists('yak_redirect_authorizenet')) { /** * Use Authorize.net to submit credit card transaction details. Pass result to redirect page if successful. * * @see yak_redirect_page for description of parameters */ function yak_redirect_authorizenet($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email) { global $wpdb; $registry =& Registry::getInstance(); $order_table =& $registry->get('order_table'); $cc = $_SESSION['cc']; $value = yak_order_value(false, $order_id); // Get Billing Address $baddress = yak_get_address('billing', false); // Get First and Last name $arr = split("[\n\r\t ]+", $baddress->recipient); $firstname = $arr[0]; $lastname = $arr[1]; // Get Shipping Address $saddress = yak_get_address('shipping', false); // Get First and Last name for shipping address $arr2 = split("[\n\r\t ]+", $saddress->recipient); $s_firstname = $arr2[0]; $s_lastname = $arr2[1]; // Create Customer ID $cust_id = rand(1000000000, 9999999999); $params_array = array( 'x_login' => yak_get_option(AUTHORIZE_NET_LOGIN_ID), 'x_tran_key' => yak_get_option(AUTHORIZE_NET_TRANS_KEY), 'x_version' => '3.1', 'x_delim_char' => '|', 'x_delim_data' => 'TRUE', 'x_method' => 'CC', 'x_relay_response' => 'FALSE', 'x_type' => 'AUTH_CAPTURE', 'x_amount' => number_format($value, 2, '.', ''), 'x_card_num' => $cc['number'], 'x_exp_date' => $cc['expiry'], 'x_card_code' => $cc['security_code'], 'x_trans_id' => $order_id, 'x_description' => get_bloginfo('description') . ' Products', 'c_cust_id' => $cust_id, 'x_country' => $baddress->country, 'x_zip' => $baddress->postcode, 'x_city' => $baddress->city, 'x_phone' => $baddress->phone, 'x_address' => $baddress->addr1, 'x_state' => $baddress->get_state_or_region(), 'x_email' => $baddress->email, 'x_first_name' => $firstname, 'x_last_name' => $lastname, 'x_ship_to_country' => $saddress->country, 'x_ship_to_zip' => $saddress->postcode, 'x_ship_to_city' => $saddress->city, 'x_ship_to_phone' => $saddress->phone, 'x_ship_to_address' => $saddress->addr1, 'x_ship_to_state' => $saddress->get_state_or_region(), 'x_ship_to_first_name' => $s_firstname, 'x_ship_to_last_name' => $s_lastname ); $payment_types = yak_get_option(PAYMENT_TYPES, null); if ($payment_types[$payment_type] == AUTHORIZE_NET_TEST) { $params_array['x_test_request'] = 'TRUE'; } $params = yak_encode_params($params_array); if ($payment_type == AUTHORIZE_NET) { $url = AUTHORIZE_NET_URL; } else { $url = AUTHORIZE_NET_TEST_URL; } if ($value > 0.0) { $response = yak_do_http($url, '', $params); $split = explode('|', $response); $rtn = ""; if ($split[0] == 1) { $wpdb->query("update $order_table set funds_received = $value where id = $order_id"); yak_insert_orderlog($order_id, 'Authorize.net transaction was approved'); $rtn = yak_redirect_page($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email); } else if ($split[0] == 2) { $_SESSION['error_message'] = __('The transaction has been declined', 'yak'); $wpdb->query("update $order_table set status = '" . ERROR . "' where id = $order_id"); yak_insert_orderlog($order_id, 'Authorize.net transaction was declined'); $rtn = yak_get_option(AUTHORIZE_NET_ERROR_PAGE, ''); } else if ($split[0] == 3) { $err = $split[3]; $wpdb->query("update $order_table set status = '" . ERROR . "' where id = $order_id"); yak_insert_orderlog($order_id, 'An error occurred processing the Authorize.net transaction: ' . $err); $_SESSION['error_message'] = $err; $rtn = yak_get_option(AUTHORIZE_NET_ERROR_PAGE, ''); } else { yak_insert_orderlog($order_id, 'Authorize.net transaction is held for review -- please resolve manually'); $rtn = yak_redirect_page($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email); } yak_insert_orderlog($order_id, 'response received from Authorize.net was: ' . $response); } else { // no order value -- just redirect to the success page $rtn = yak_redirect_page($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email); yak_insert_orderlog($order_id, 'Total order cost is 0, not submitting to Authorize.net'); } yak_check_order($order_id); return $rtn; } } if (!function_exists('yak_paypal_ipn')) { /** * Function to handle a PayPal Instant Payment Notification. */ function yak_paypal_ipn() { global $order_table, $order_log_table, $wpdb; $order_id = $_REQUEST['custom']; if (empty($order_id)) { echo "ERROR: Missing parameter 'custom'"; return; } // paypal requires all parameters to be sent back for verification $params = 'cmd=_notify-validate'; $msg = ''; foreach ( $_REQUEST as $key => $value ) { $params = $params . '&' . $key . '=' . urlencode(yak_fix_escaping($value)); if ($msg != '') { $msg = $msg . ', '; } $msg = $msg . $key . '=' . $value; } $payment_gross = $_REQUEST['mc_gross']; if (!isset($payment_gross) || $payment_gross == '') { $payment_gross = $_REQUEST['payment_gross']; } $row = $wpdb->get_row("select funds_received from $order_table where id = $order_id"); if ($row->funds_received > 0) { // we've already processed this return; } $business = $_REQUEST['business']; if ($business != yak_get_option(PAYPAL_ACCOUNT, '')) { echo "ERROR: paypal business mismatch: $business doesn't match expected value"; error_log("paypal business mismatch: $business doesn't match $msg"); yak_insert_orderlog($order_id, "business '$business' does not match [ $msg ]"); return; } $payment_types = yak_get_option(PAYMENT_TYPES_CASE_INSENSITIVE, null); // choose the right paypal url based on what's set in the payment types array if (in_array(PAYPAL_SANDBOX, $payment_types)) { $url = parse_url(PAYPAL_SANDBOX_URL); } else { $url = parse_url(PAYPAL_URL); } // call paypal to verify $response = yak_do_http($url['scheme'] . '://' . $url['host'], $url['path'], $params); error_log("IPN order_id=$order_id payment_gross=$payment_gross -- params=$params -- response=$response"); if (!(strpos($response, 'VERIFIED') === false)) { $wpdb->query("update $order_table set funds_received = $payment_gross where id = $order_id"); send_confirmation_email($order_id); } else { yak_insert_orderlog($order_id, "PayPal response not verified for order $order_id. response: [$response] data: [ $msg ]"); } yak_check_order($order_id); } } if (!function_exists('yak_paypal_pdt')) { /** * Function to handle a PayPal Payment Data Transfer */ function yak_paypal_pdt() { global $order_table, $order_log_table, $wpdb; // only process if payment notification is set to PDT (otherwise just return true) // this means the pdt function can be used on the success page whether or not pdt is actually // used for payment notification if (yak_get_option(PAYPAL_PAYMENT_NOTIFICATION, '') == PAYPAL_PDT) { $params = 'cmd=_notify-synch&tx=' . $_GET['tx'] . '&at=' . yak_get_option(PAYPAL_IDENTITY_TOKEN, ''); $payment_types = yak_get_option(PAYMENT_TYPES_CASE_INSENSITIVE, null); error_log("YAK PDT url " . $payment_types['paypal']); error_log("YAK PARAMS " . $params); $url = parse_url($payment_types['paypal']); $response = yak_do_http($url['scheme'] . '://' . $url['host'], $url['path'], $params, null, 'GET'); if (!(strpos($response, 'SUCCESS') === false)) { $payment_gross = yak_get_value($response, 'mc_gross='); $order_id = yak_get_value($response, 'custom='); yak_cleanup_after_order(); $wpdb->query("update $order_table set funds_received = $payment_gross where id = $order_id"); yak_check_order($order_id); send_confirmation_email($order_id); return true; } else { $order_id = null; if (isset($_GET['order_id'])) { $order_id = $_GET['order_id']; } else if (isset($_GET['cm'])) { $order_id = $_GET['cm']; } yak_insert_orderlog($order_id, "PayPal response not successful for id $order_id [ $response ]"); return false; } } else { return true; } } } if (!function_exists('yak_redirect_google')) { /** * Redirect to Google to handle payment. * * @see yak_redirect_page for description of parameters */ function yak_redirect_google($payment_type, $order_id, $items, $shippingcost, $promo, $send_conf_email) { $payment_types = yak_get_option(PAYMENT_TYPES, null); $ptypeval = $payment_types[$payment_type]; $merchant_id = yak_get_option(GOOGLE_MERCHANT_ID); $merchant_key = yak_get_option(GOOGLE_MERCHANT_KEY); $ccy = yak_get_option(CURRENCY_CODE, 'USD'); if ($ptypeval == GOOGLE_SANDBOX) { $server_type = 'sandbox'; } else { $server_type = 'production'; } $cart = new GoogleCart($merchant_id, $merchant_key, $server_type, $ccy); $shipping_method = yak_get_option(GOOGLE_SHIPPING_METHOD, null); $total_items = 0.0; foreach ($items as $key => $item) { $total_items += $item->quantity; } foreach ($items as $key => $item) { $price = yak_calc_price($item->id, $item->cat_id); if ($promo != null) { if ($promo->promo_type == 'pricing_perc') { $price -= ($promo->value / 100.0) * $price; } else if ($promo->promo_type == 'pricing_val') { $price -= ($promo->value / $total_items) * $item->quantity; } } $item = new GoogleItem(yak_get_title($item->id, $item->cat_id), null, $item->quantity, $price); $cart->AddItem($item); } if ($promo != null) { if ($promo->promo_type == 'shipping_perc') { $shippingcost -= ($promo->value / 100.0) * $shippingcost; } else if ($promo->promo_type == 'shipping_val') { $shippingcost -= $promo->value; } } $shipping = new GoogleFlatRateShipping($shipping_method, $shippingcost); $shippingfilter = new GoogleShippingFilters(); $shippingfilter->SetAllowedWorldArea(true); $shipping->AddShippingRestrictions($shippingfilter); $cart->AddShipping($shipping); $order_num = yak_get_order_num($order_id); $prvdata = new MerchantPrivateData(array('order-id' => $order_id, 'order-num' => $order_num)); $cart->SetMerchantPrivateData($prvdata); //$cart->SetEditCartUrl(yak_get_url(yak_get_option(GOOGLE_EDIT_CART_URL, ''))); //$cart->SetContinueShoppingUrl(yak_get_url(yak_get_option(GOOGLE_CONTINUE_URL, ''))); error_log("xml is " . $cart->GetXml()); yak_cleanup_after_order(); list($status, $error) = $cart->CheckoutServer2Server(); error_log("status=$status error=$error"); echo $error; } } if (!function_exists('send_confirmation_email')) { /** * Send a confirmation email for an order. * * @param $order_id the id of the order */ function send_confirmation_email($order_id) { global $wpdb, $order_table, $order_detail_table; $conf_email = yak_get_option(CONFIRMATION_EMAIL_ADDRESS, ''); if (!isset($conf_email) || $conf_email == '') { return; } $mail = yak_get_option(CONFIRMATION_MESSAGE, ''); $subject = yak_get_option(CONFIRMATION_SUBJECT, __('Order Confirmation', 'yak')); $result = $wpdb->get_row("select * from $order_table where id = $order_id"); $payment_type = $result->payment_type; $address = $result->address; $split = split("\n", $address); $email = $split[0]; $name = $split[1]; $address = implode("\n", array_slice($split, 1)); $shipping = $result->shipping_cost; $results = $wpdb->get_results("select * from $order_detail_table where id = $order_id"); $totalprice = 0; $totalquantity = 0; if (yak_str_contains($mail, '[order_detail]')) { $detail = __('Title', 'yak') . ": %s\n" . __('Quantity', 'yak') . ": %d\n" . __('Price', 'yak') . ": %s\n" . __('Total' , 'yak') . ": %s\n\n"; $detailmsg = ''; foreach ($results as $result) { $price = $result->price; $qty = $result->quantity; $totalprice += ($price * $qty); $itemprice = $price * $qty; $totalquantity += $qty; $detailmsg = $detailmsg . sprintf($detail, $result->itemname, $qty, yak_format_money($price, false), yak_format_money($itemprice, false)); } $detailmsg = $detailmsg . __('Shipping costs', 'yak') . ' ' . yak_format_money($shipping, true) . "\n"; $mail = str_replace('[order_detail]', $detailmsg, $mail); } if (yak_str_contains($mail, '[html_order_detail]')) { $detail = "
| " . __('Title', 'yak') . " | \n" . "" . __('Quantity', 'yak') . " | \n" . "" . __('Price', 'yak') . " | \n" . "" . __('Total', 'yak') . " | \n" . "
|---|
" . __('Shipping costs', 'yak') . ' ' . yak_format_money($shipping, true) . "
\n"; $mail = str_replace('[html_order_detail]', $detailmsg, $mail); } $totalprice += $shipping; $order_num = yak_get_order_num($order_id); $mail = str_replace('[order_id]', $order_num, $mail); $mail = str_replace('[order_cost]', yak_format_money($totalprice, true), $mail); $mail = str_replace('[payment_type]', $payment_type, $mail); $mail = str_replace('[shipping_address]', $address, $mail); $mail = str_replace('[html_shipping_address]', str_replace("\n", "