public function executepay() { if (empty(WC()->session->token) || empty(WC()->session->PayerID) || empty(WC()->session->paymentId)) { return; } $execution = new PaymentExecution(); $execution->setPayerId(WC()->session->PayerID); try { $payment = Payment::get(WC()->session->paymentId, $this->getAuth()); $payment->execute($execution, $this->getAuth()); $this->add_log(print_r($payment, true)); if ($payment->state == "approved") { //if state = approved continue.. global $wpdb; $this->log->add('paypal_plus', sprintf(__('Response: %s', 'paypal-for-woocommerce'), print_r($payment, true))); $order = new WC_Order(WC()->session->orderId); if ($this->billing_address == 'yes') { require_once "lib/NameParser.php"; $parser = new FullNameParser(); $split_name = $parser->split_full_name($payment->payer->payer_info->shipping_address->recipient_name); $shipping_first_name = $split_name['fname']; $shipping_last_name = $split_name['lname']; $full_name = $split_name['fullname']; update_post_meta(WC()->session->orderId, '_billing_first_name', $shipping_first_name); update_post_meta(WC()->session->orderId, '_billing_last_name', $shipping_last_name); update_post_meta(WC()->session->orderId, '_billing_full_name', $full_name); update_post_meta(WC()->session->orderId, '_billing_address_1', $payment->payer->payer_info->shipping_address->line1); update_post_meta(WC()->session->orderId, '_billing_address_2', $payment->payer->payer_info->shipping_address->line2); update_post_meta(WC()->session->orderId, '_billing_city', $payment->payer->payer_info->shipping_address->city); update_post_meta(WC()->session->orderId, '_billing_postcode', $payment->payer->payer_info->shipping_address->postal_code); update_post_meta(WC()->session->orderId, '_billing_country', $payment->payer->payer_info->shipping_address->country_code); update_post_meta(WC()->session->orderId, '_billing_state', $payment->payer->payer_info->shipping_address->state); } $order->add_order_note(__('PayPal Plus payment completed', 'paypal-for-woocommerce')); $order->payment_complete($payment->id); //add hook do_action('woocommerce_checkout_order_processed', WC()->session->orderId); wp_redirect($this->get_return_url($order)); } } catch (PayPal\Exception\PayPalConnectionException $ex) { wc_add_notice(__("Error processing checkout. Please try again. ", 'woocommerce'), 'error'); $this->add_log($ex->getData()); } catch (Exception $ex) { $this->add_log($ex->getMessage()); // Prints the Error Code wc_add_notice(__("Error processing checkout. Please try again.", 'woocommerce'), 'error'); } }
/** * PayPal Express Checkout * * Main action function that handles PPE actions: * 1. 'expresscheckout' - Initiates the Express Checkout process; called by the checkout button. * 2. 'revieworder' - Customer has reviewed the order. Saves shipping info to order. * 3. 'payaction' - Customer has pressed "Place Order" on the review page. */ function paypal_express_checkout($posted = null) { if (!empty($posted) || isset($_GET['pp_action']) && $_GET['pp_action'] == 'expresscheckout') { if (sizeof(WC()->cart->get_cart()) > 0) { // The customer has initiated the Express Checkout process with the button on the cart page if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } $this->add_log('Start Express Checkout'); /** * Check if the EC button used was the PayPal Credit button. * This $usePayPalCredit flag will be used to adjust the SEC request accordingly. */ if (isset($_GET['use_paypal_credit']) && 'true' == $_GET['use_paypal_credit']) { $usePayPalCredit = true; } else { $usePayPalCredit = false; } WC()->cart->calculate_totals(); //$paymentAmount = WC()->cart->get_total(); $paymentAmount = AngellEYE_Gateway_Paypal::number_format(WC()->cart->total); //Check if review order page is exist, otherwise re-create it on the fly $review_order_page_url = get_permalink(wc_get_page_id('review_order')); if (!$review_order_page_url) { $this->add_log(__('Review Order Page not found, re-create it. ', 'paypal-for-woocommerce')); include_once WC()->plugin_path() . '/includes/admin/wc-admin-functions.php'; $page_id = wc_create_page(esc_sql(_x('review-order', 'page_slug', 'woocommerce')), 'woocommerce_review_order_page_id', __('Checkout → Review Order', 'paypal-for-woocommerce'), '[woocommerce_review_order]', wc_get_page_id('checkout')); $review_order_page_url = get_permalink($page_id); } $returnURL = urlencode(add_query_arg('pp_action', 'revieworder', $review_order_page_url)); $cancelURL = isset($this->settings['cancel_page']) ? get_permalink($this->settings['cancel_page']) : WC()->cart->get_cart_url(); $cancelURL = apply_filters('angelleye_express_cancel_url', urlencode($cancelURL)); $resArray = $this->CallSetExpressCheckout($paymentAmount, $returnURL, $cancelURL, $usePayPalCredit, $posted); $ack = strtoupper($resArray["ACK"]); /** * I've replaced the original redirect URL's here with * what the PayPal class library returns so that options like * "skip details" will work correctly with PayPal's review pages. */ if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { $this->add_log('Redirecting to PayPal'); if (is_ajax()) { $result = array('redirect' => $resArray['REDIRECTURL'], 'result' => 'success'); echo json_encode($result); exit; } else { //$this->RedirectToPayPal( $resArray["TOKEN"] ); wp_redirect($resArray['REDIRECTURL']); exit; } } else { // Display a user friendly Error on the page and log details $ErrorCode = urldecode($resArray["L_ERRORCODE0"]); $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]); $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]); $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]); $this->add_log(__('SetExpressCheckout API call failed. ', 'paypal-for-woocommerce')); $this->add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); $this->add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); $this->add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); $this->add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); // Notice admin if has any issue from PayPal $message = ''; if ($this->error_email_notify) { $admin_email = get_option("admin_email"); $message .= __("SetExpressCheckout API call failed.", "paypal-for-woocommerce") . "\n\n"; $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; $message .= __('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode . "\n"; $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; $message .= __('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg . "\n"; $message .= __('User IP: ', 'paypal-for-woocommerce') . $this->get_user_ip() . "\n"; $error_email_notify_mes = apply_filters('ae_ppec_error_email_message', $message, $ErrorCode, $ErrorSeverityCode, $ErrorShortMsg, $ErrorLongMsg); $subject = "PayPal Express Checkout Error Notification"; $error_email_notify_subject = apply_filters('ae_ppec_error_email_subject', $subject); wp_mail($admin_email, $error_email_notify_subject, $error_email_notify_mes); } // Generate error message based on Error Display Type setting if ($this->error_display_type == 'detailed') { $sec_error_notice = $ErrorCode . ' - ' . $ErrorLongMsg; $error_display_type_message = sprintf(__($sec_error_notice, 'paypal-for-woocommerce')); } else { $error_display_type_message = sprintf(__('There was a problem paying with PayPal. Please try another method.', 'paypal-for-woocommerce')); } $error_display_type_message = apply_filters('ae_ppec_error_user_display_message', $error_display_type_message, $ErrorCode, $ErrorLongMsg); wc_add_notice($error_display_type_message, 'error'); if (!is_ajax()) { wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } else { return; } } } } elseif (isset($_GET['pp_action']) && $_GET['pp_action'] == 'revieworder') { // The customer has logged into PayPal and approved order. // Retrieve the shipping details and present the order for completion. if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } $this->add_log('Start Review Order'); if (isset($_GET['token'])) { $token = $_GET['token']; $this->set_session('TOKEN', $token); } if (isset($_GET['PayerID'])) { $payerID = $_GET['PayerID']; $this->set_session('PayerID', $payerID); } $this->add_log("...Token:" . $this->get_session('TOKEN')); $this->add_log("...PayerID: " . $this->get_session('PayerID')); //if empty TOKEN redirect to cart page if (empty(WC()->session->TOKEN)) { $ms = sprintf(__('Sorry, your session has expired. <a href=%s>Return to homepage →</a>', 'paypal-for-woocommerce'), '"' . home_url() . '"'); $ec_confirm_message = apply_filters('angelleye_ec_confirm_message', $ms); wc_add_notice($ec_confirm_message, "error"); wp_redirect(get_permalink(wc_get_page_id('cart'))); } else { $result = $this->CallGetShippingDetails($this->get_session('TOKEN')); if (!empty($result)) { $this->set_session('RESULT', serialize($result)); if (isset($result['SHIPTOCOUNTRYCODE'])) { /** * Check if shiptocountry is in the allowed countries list */ if (!array_key_exists($result['SHIPTOCOUNTRYCODE'], WC()->countries->get_allowed_countries())) { wc_add_notice(sprintf(__('We do not sell in your country, please try again with another address.', 'paypal-for-woocommerce')), 'error'); wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } WC()->customer->set_shipping_country($result['SHIPTOCOUNTRYCODE']); } if (isset($result['FIRSTNAME'])) { WC()->customer->firstname = $result['FIRSTNAME']; } if (isset($result['LASTNAME'])) { WC()->customer->lastname = $result['LASTNAME']; } if (isset($result['SHIPTONAME'])) { WC()->customer->shiptoname = $result['SHIPTONAME']; } if (isset($result['SHIPTOSTREET'])) { WC()->customer->set_address($result['SHIPTOSTREET']); } if (isset($result['SHIPTOSTREET2'])) { WC()->customer->set_address_2($result['SHIPTOSTREET2']); } if (isset($result['SHIPTOCITY'])) { WC()->customer->set_city($result['SHIPTOCITY']); } if (isset($result['SHIPTOCOUNTRYCODE'])) { WC()->customer->set_country($result['SHIPTOCOUNTRYCODE']); } if (isset($result['SHIPTOSTATE'])) { WC()->customer->set_state($this->get_state_code($result['SHIPTOCOUNTRYCODE'], $result['SHIPTOSTATE'])); } if (isset($result['SHIPTOZIP'])) { WC()->customer->set_postcode($result['SHIPTOZIP']); } if (isset($result['SHIPTOSTATE'])) { WC()->customer->set_shipping_state($this->get_state_code($result['SHIPTOCOUNTRYCODE'], $result['SHIPTOSTATE'])); } if (isset($result['SHIPTOZIP'])) { WC()->customer->set_shipping_postcode($result['SHIPTOZIP']); } /** * Save GECD data in sessions for use in DECP */ $this->set_session('company', isset($result['BUSINESS']) ? $result['BUSINESS'] : ''); $this->set_session('firstname', isset($result['FIRSTNAME']) ? $result['FIRSTNAME'] : ''); $this->set_session('lastname', isset($result['LASTNAME']) ? $result['LASTNAME'] : ''); $this->set_session('shiptoname', isset($result['SHIPTONAME']) ? $result['SHIPTONAME'] : ''); $this->set_session('shiptostreet', isset($result['SHIPTOSTREET']) ? $result['SHIPTOSTREET'] : ''); $this->set_session('shiptostreet2', isset($result['SHIPTOSTREET2']) ? $result['SHIPTOSTREET2'] : ''); $this->set_session('shiptocity', isset($result['SHIPTOCITY']) ? $result['SHIPTOCITY'] : ''); $this->set_session('shiptocountrycode', isset($result['SHIPTOCOUNTRYCODE']) ? $result['SHIPTOCOUNTRYCODE'] : ''); $this->set_session('shiptostate', isset($result['SHIPTOSTATE']) ? $result['SHIPTOSTATE'] : ''); $this->set_session('shiptozip', isset($result['SHIPTOZIP']) ? $result['SHIPTOZIP'] : ''); $this->set_session('payeremail', isset($result['EMAIL']) ? $result['EMAIL'] : ''); $this->set_session('giftmessage', isset($result['GIFTMESSAGE']) ? $result['GIFTMESSAGE'] : ''); $this->set_session('giftreceiptenable', isset($result['GIFTRECEIPTENABLE']) ? $result['GIFTRECEIPTENABLE'] : ''); $this->set_session('giftwrapname', isset($result['GIFTWRAPNAME']) ? $result['GIFTWRAPNAME'] : ''); $this->set_session('giftwrapamount', isset($result['GIFTWRAPAMOUNT']) ? $result['GIFTWRAPAMOUNT'] : ''); $this->set_session('customer_notes', isset($result['PAYMENTREQUEST_0_NOTETEXT']) ? $result['PAYMENTREQUEST_0_NOTETEXT'] : ''); $this->set_session('phonenum', isset($result['PHONENUM']) ? $result['PHONENUM'] : ''); WC()->cart->calculate_totals(); } else { $this->add_log("...ERROR: GetShippingDetails returned empty result"); } if ($this->skip_final_review == 'yes' && (get_option('woocommerce_enable_guest_checkout') === "yes" || apply_filters('woocommerce_enable_guest_checkout', get_option('woocommerce_enable_guest_checkout')) == "yes" || is_user_logged_in()) || is_user_logged_in() && isset(WC()->session->checkout_form)) { //check terms enable $checkout_form_data = maybe_unserialize(WC()->session->checkout_form); if (!(wc_get_page_id('terms') > 0 && apply_filters('woocommerce_checkout_show_terms', true) && empty($checkout_form_data['terms']))) { $url = add_query_arg(array('pp_action' => 'payaction')); wp_redirect($url); exit; } } if (isset($_POST['createaccount'])) { $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id()); if (empty($_POST['username'])) { wc_add_notice(__('Username is required', 'paypal-for-woocommerce'), 'error'); } elseif (username_exists($_POST['username'])) { wc_add_notice(__('This username is already registered.', 'paypal-for-woocommerce'), 'error'); } elseif (empty($_POST['email']) || !is_email($_POST['email'])) { wc_add_notice(__('Please provide a valid email address.', 'paypal-for-woocommerce'), 'error'); } elseif (empty($_POST['password']) || empty($_POST['repassword'])) { wc_add_notice(__('Password is required.', 'paypal-for-woocommerce'), 'error'); } elseif ($_POST['password'] != $_POST['repassword']) { wc_add_notice(__('Passwords do not match.', 'paypal-for-woocommerce'), 'error'); } elseif (get_user_by('email', $_POST['email']) != false) { wc_add_notice(__('This email address is already registered.', 'paypal-for-woocommerce'), 'error'); } else { $username = !empty($_POST['username']) ? $_POST['username'] : ''; $password = !empty($_POST['password']) ? $_POST['password'] : ''; $email = $_POST['email']; try { // Anti-spam trap if (!empty($_POST['email_2'])) { throw new Exception(__('Anti-spam field was filled in.', 'woocommerce')); wc_add_notice('<strong>' . __('Anti-spam field was filled in.', 'paypal-for-woocommerce') . ':</strong> ', 'error'); } $new_customer = wc_create_new_customer(sanitize_email($email), wc_clean($username), $password); if (is_wp_error($new_customer)) { wc_add_notice($new_customer->get_error_message(), 'error'); } if (apply_filters('paypal-for-woocommerce_registration_auth_new_customer', true, $new_customer)) { wc_set_customer_auth_cookie($new_customer); } $creds = array('user_login' => wc_clean($username), 'user_password' => $password, 'remember' => true); $user = wp_signon($creds, false); if (is_wp_error($user)) { wc_add_notice($user->get_error_message(), 'error'); } else { wp_set_current_user($user->ID); //Here is where we update the global user variables $secure_cookie = is_ssl() ? true : false; wp_set_auth_cookie($user->ID, true, $secure_cookie); } } catch (Exception $e) { wc_add_notice('<strong>' . __('Error', 'paypal-for-woocommerce') . ':</strong> ' . $e->getMessage(), 'error'); } $this->customer_id = $user->ID; // As we are now logged in, checkout will need to refresh to show logged in data WC()->session->set('reload_checkout', true); // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering WC()->cart->calculate_totals(); require_once PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/lib/NameParser.php'; $parser = new FullNameParser(); if (isset($result['SHIPTONAME']) && !empty($result['SHIPTONAME'])) { $split_name = $parser->split_full_name($result['SHIPTONAME']); $shipping_first_name = $split_name['fname']; $shipping_last_name = $split_name['lname']; $full_name = $split_name['fullname']; } // Add customer info from other billing fields if (isset($result)) { update_user_meta($this->customer_id, 'first_name', isset($result['FIRSTNAME']) ? $result['FIRSTNAME'] : ''); update_user_meta($this->customer_id, 'last_name', isset($result['LASTNAME']) ? $result['LASTNAME'] : ''); update_user_meta($this->customer_id, 'shipping_first_name', isset($shipping_first_name) ? $shipping_first_name : ''); update_user_meta($this->customer_id, 'shipping_last_name', isset($shipping_last_name) ? $shipping_first_name : ''); update_user_meta($this->customer_id, 'shipping_company', isset($result['BUSINESS']) ? $result['BUSINESS'] : ''); update_user_meta($this->customer_id, 'shipping_address_1', isset($result['SHIPTOSTREET']) ? $result['SHIPTOSTREET'] : ''); update_user_meta($this->customer_id, 'shipping_address_2', isset($result['SHIPTOSTREET2']) ? $result['SHIPTOSTREET2'] : ''); update_user_meta($this->customer_id, 'shipping_city', isset($result['SHIPTOCITY']) ? $result['SHIPTOCITY'] : ''); update_user_meta($this->customer_id, 'shipping_postcode', isset($result['SHIPTOZIP']) ? $result['SHIPTOZIP'] : ''); update_user_meta($this->customer_id, 'shipping_country', isset($result['SHIPTOCOUNTRYCODE']) ? $result['SHIPTOCOUNTRYCODE'] : ''); update_user_meta($this->customer_id, 'shipping_state', isset($result['SHIPTOSTATE']) ? $result['SHIPTOSTATE'] : ''); $user_submit_form = maybe_unserialize(WC()->session->checkout_form); if (isset($user_submit_form) && !empty($user_submit_form) && is_array($user_submit_form)) { update_user_meta($this->customer_id, 'billing_first_name', isset($user_submit_form['billing_first_name']) ? $user_submit_form['billing_first_name'] : $result['FIRSTNAME']); update_user_meta($this->customer_id, 'billing_last_name', isset($user_submit_form['billing_last_name']) ? $user_submit_form['billing_last_name'] : $result['LASTNAME']); update_user_meta($this->customer_id, 'billing_address_1', isset($user_submit_form['billing_address_1']) ? $user_submit_form['billing_address_1'] : $result['SHIPTOSTREET']); update_user_meta($this->customer_id, 'billing_address_2', isset($user_submit_form['billing_address_2']) ? $user_submit_form['billing_address_2'] : $result['SHIPTOSTREET2']); update_user_meta($this->customer_id, 'billing_city', isset($user_submit_form['billing_city']) ? $user_submit_form['billing_city'] : $result['SHIPTOCITY']); update_user_meta($this->customer_id, 'billing_postcode', isset($user_submit_form['billing_postcode']) ? $user_submit_form['billing_postcode'] : $result['SHIPTOZIP']); update_user_meta($this->customer_id, 'billing_country', isset($user_submit_form['billing_country']) ? $user_submit_form['billing_country'] : $result['SHIPTOCOUNTRYCODE']); update_user_meta($this->customer_id, 'billing_state', isset($user_submit_form['billing_state']) ? $user_submit_form['billing_state'] : $result['SHIPTOSTATE']); update_user_meta($this->customer_id, 'billing_phone', isset($user_submit_form['billing_phone']) ? $user_submit_form['billing_phone'] : $result['PHONENUM']); update_user_meta($this->customer_id, 'billing_email', isset($user_submit_form['billing_email']) ? $user_submit_form['billing_email'] : $result['EMAIL']); } else { update_user_meta($this->customer_id, 'billing_first_name', $result['FIRSTNAME']); update_user_meta($this->customer_id, 'billing_last_name', $result['LASTNAME']); update_user_meta($this->customer_id, 'billing_address_1', isset($result['SHIPTOSTREET']) ? $result['SHIPTOSTREET'] : ''); update_user_meta($this->customer_id, 'billing_address_2', isset($result['SHIPTOSTREET2']) ? $result['SHIPTOSTREET2'] : ''); update_user_meta($this->customer_id, 'billing_city', isset($result['SHIPTOCITY']) ? $result['SHIPTOCITY'] : ''); update_user_meta($this->customer_id, 'billing_postcode', isset($result['SHIPTOZIP']) ? $result['SHIPTOZIP'] : ''); update_user_meta($this->customer_id, 'billing_country', isset($result['SHIPTOCOUNTRYCODE']) ? $result['SHIPTOCOUNTRYCODE'] : ''); update_user_meta($this->customer_id, 'billing_state', isset($result['SHIPTOSTATE']) ? $result['SHIPTOSTATE'] : ''); update_user_meta($this->customer_id, 'billing_phone', isset($result['PHONENUM']) ? $result['PHONENUM'] : ''); update_user_meta($this->customer_id, 'billing_email', isset($result['EMAIL']) ? $result['EMAIL'] : ''); } } //reload the page wp_redirect(add_query_arg(array('pp_action' => 'revieworder'))); exit; } } } } elseif (isset($_GET['pp_action']) && $_GET['pp_action'] == 'payaction') { if (isset($_POST) || ($this->skip_final_review == 'yes' && (get_option('woocommerce_enable_guest_checkout') === "yes" || apply_filters('woocommerce_enable_guest_checkout', get_option('woocommerce_enable_guest_checkout')) == "yes" || is_user_logged_in()) || is_user_logged_in() && isset(WC()->session->checkout_form))) { $result = unserialize(WC()->session->RESULT); /* create account start */ if (isset($_POST['createaccount']) && !empty($_POST['createaccount'])) { $this->customer_id = apply_filters('woocommerce_checkout_customer_id', get_current_user_id()); $create_user_email = $_POST['email']; $create_user_name = sanitize_user(current(explode('@', $create_user_email)), true); // Ensure username is unique $append = 1; $o_username = $create_user_name; while (username_exists($create_user_name)) { $create_user_name = $o_username . $append; $append++; } //If have any issue, redirect to review-order page $create_acc_error = false; if (empty($_POST['email']) || !is_email($_POST['email'])) { wc_add_notice(__('Please provide a valid email address.', 'paypal-for-woocommerce'), 'error'); } elseif (get_user_by('email', $create_user_email) != false) { wc_add_notice(__('This email address is already registered.', 'paypal-for-woocommerce'), 'error'); $create_acc_error = true; } elseif (empty($_POST['create_act'])) { wc_add_notice(__('Password is required.', 'paypal-for-woocommerce'), 'error'); $create_acc_error = true; } else { $username = !empty($create_user_name) ? $create_user_name : ''; $password = !empty($_POST['create_act']) ? $_POST['create_act'] : ''; $email = $create_user_email; try { //try to create user $new_customer = wc_create_new_customer(sanitize_email($email), wc_clean($username), $password); if (is_wp_error($new_customer)) { wc_add_notice($new_customer->get_error_message(), 'error'); $create_acc_error = true; } if (apply_filters('paypal-for-woocommerce_registration_auth_new_customer', true, $new_customer)) { wc_set_customer_auth_cookie($new_customer); } //Log user in $creds = array('user_login' => wc_clean($username), 'user_password' => $password, 'remember' => true); $user = wp_signon($creds, false); if (is_wp_error($user)) { wc_add_notice($user->get_error_message(), 'error'); $create_acc_error = true; } else { wp_set_current_user($user->ID); //Here is where we update the global user variables $secure_cookie = is_ssl() ? true : false; wp_set_auth_cookie($user->ID, true, $secure_cookie); $this->customer_id = $user->ID; } } catch (Exception $e) { wc_add_notice('<strong>' . __('Error', 'paypal-for-woocommerce') . ':</strong> ' . $e->getMessage(), 'error'); $create_acc_error = true; } } if ($create_acc_error) { wp_redirect(add_query_arg(array('pp_action' => 'revieworder'))); exit; } } // Update customer shipping and payment method to posted method $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); if (isset($_POST['shipping_method']) && is_array($_POST['shipping_method'])) { foreach ($_POST['shipping_method'] as $i => $value) { $chosen_shipping_methods[$i] = wc_clean($value); } } WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods); if (WC()->cart->needs_shipping()) { // Validate Shipping Methods $packages = WC()->shipping->get_packages(); WC()->checkout()->shipping_methods = WC()->session->get('chosen_shipping_methods'); } $this->add_log('Start Pay Action'); if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } WC()->cart->calculate_totals(); if (sizeof(WC()->cart->get_cart()) == 0 || empty(WC()->session->TOKEN)) { $ms = sprintf(__('Sorry, your session has expired. <a href=%s>Return to homepage →</a>', 'paypal-for-woocommerce'), '"' . home_url() . '"'); $ec_confirm_message = apply_filters('angelleye_ec_confirm_message', $ms); wc_add_notice($ec_confirm_message, "error"); wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } $paid_order_id = $this->angelleye_prevent_duplicate_payment_request(WC()->session->TOKEN); if ($paid_order_id) { $ms = sprintf(__('Sorry, A successful transaction has already been completed for this token. <a href=%s>Return to homepage →</a>', 'paypal-for-woocommerce'), '"' . home_url() . '"'); $ec_confirm_message = apply_filters('angelleye_ec_confirm_message', $ms); wc_add_notice($ec_confirm_message, "error"); wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } $order_id = WC()->checkout()->create_order(); do_action('woocommerce_checkout_order_processed', $order_id, array()); /** * Update meta data with session data */ // Parse SHIPTONAME to fist and last name require_once PAYPAL_FOR_WOOCOMMERCE_PLUGIN_DIR . '/classes/lib/NameParser.php'; $parser = new FullNameParser(); $shiptoname_from_session = $this->get_session('shiptoname'); if (isset($shiptoname_from_session) && !empty($shiptoname_from_session)) { $split_name = $parser->split_full_name($this->get_session('shiptoname')); $shipping_first_name = $split_name['fname']; $shipping_last_name = $split_name['lname']; $full_name = $split_name['fullname']; } else { if (!isset($shipping_first_name)) { $shipping_first_name = ''; } if (!isset($shipping_last_name)) { $shipping_last_name = ''; } } $this->set_session('firstname', isset($result['FIRSTNAME']) ? $result['FIRSTNAME'] : $shipping_first_name); $this->set_session('lastname', isset($result['LASTNAME']) ? $result['LASTNAME'] : $shipping_last_name); update_post_meta($order_id, '_payment_method', $this->id); update_post_meta($order_id, '_payment_method_title', $this->title); $checkout_form_data = maybe_unserialize($this->get_session('checkout_form')); if (is_user_logged_in()) { $userLogined = wp_get_current_user(); update_post_meta($order_id, '_billing_email', $userLogined->user_email); update_post_meta($order_id, '_customer_user', $userLogined->ID); } else { if (isset($checkout_form_data['billing_email'])) { update_post_meta($order_id, '_billing_email', $checkout_form_data['billing_email']); } else { update_post_meta($order_id, '_billing_email', $this->get_session('payeremail')); } } //save PayPal email update_post_meta($order_id, 'paypal_email', $this->get_session('payeremail')); //Set POST data from SESSION $checkout_form_post_data = maybe_unserialize($this->get_session('checkout_form_post_data')); if (isset($checkout_form_post_data) && !empty($checkout_form_post_data)) { $_POST = $checkout_form_post_data; } do_action('woocommerce_checkout_update_user_meta', $this->customer_id, $checkout_form_data); do_action('woocommerce_checkout_update_order_meta', $order_id, $checkout_form_data); if (isset($this->billing_address) && $this->billing_address == 'yes' || empty($checkout_form_data['billing_country'])) { $checkout_form_data = array(); } if (isset($checkout_form_data) && !empty($checkout_form_data)) { foreach ($checkout_form_data as $key => $value) { if (strpos($key, 'billing_') !== false && !empty($value) && !is_array($value)) { if (isset($checkout_form_data['ship_to_different_address']) && $checkout_form_data['ship_to_different_address'] == false) { $shipping_key = str_replace('billing_', 'shipping_', $key); update_user_meta($this->customer_id, $shipping_key, $value); update_post_meta($order_id, '_' . $shipping_key, $value); } update_user_meta($this->customer_id, $key, $value); update_post_meta($order_id, '_' . $key, $value); } elseif (WC()->cart->needs_shipping() && strpos($key, 'shipping_') !== false && !empty($value) && !is_array($value)) { update_user_meta($this->customer_id, $key, $value); update_post_meta($order_id, '_' . $key, $value); } } } else { update_post_meta($order_id, '_shipping_first_name', isset($shipping_first_name) ? $shipping_first_name : ''); update_post_meta($order_id, '_shipping_last_name', isset($shipping_last_name) ? $shipping_last_name : ''); update_post_meta($order_id, '_shipping_full_name', isset($full_name) ? $full_name : ''); update_post_meta($order_id, '_shipping_company', $this->get_session('company')); update_post_meta($order_id, '_billing_phone', $this->get_session('phonenum')); update_post_meta($order_id, '_shipping_address_1', $this->get_session('shiptostreet')); update_post_meta($order_id, '_shipping_address_2', $this->get_session('shiptostreet2')); update_post_meta($order_id, '_shipping_city', $this->get_session('shiptocity')); update_post_meta($order_id, '_shipping_postcode', $this->get_session('shiptozip')); update_post_meta($order_id, '_shipping_country', $this->get_session('shiptocountrycode')); update_post_meta($order_id, '_shipping_state', $this->get_state_code($this->get_session('shiptocountrycode'), $this->get_session('shiptostate'))); update_post_meta($order_id, '_customer_user', get_current_user_id()); if ($this->billing_address == 'yes') { update_post_meta($order_id, '_billing_first_name', $this->get_session('firstname')); update_post_meta($order_id, '_billing_last_name', $this->get_session('lastname')); update_post_meta($order_id, '_billing_full_name', isset($full_name) ? $full_name : ''); update_post_meta($order_id, '_billing_company', $this->get_session('company')); update_post_meta($order_id, '_billing_address_1', $this->get_session('shiptostreet')); update_post_meta($order_id, '_billing_address_2', $this->get_session('shiptostreet2')); update_post_meta($order_id, '_billing_city', $this->get_session('shiptocity')); update_post_meta($order_id, '_billing_postcode', $this->get_session('shiptozip')); update_post_meta($order_id, '_billing_country', $this->get_session('shiptocountrycode')); update_post_meta($order_id, '_billing_state', $this->get_state_code($this->get_session('shiptocountrycode'), $this->get_session('shiptostate'))); } } $this->add_log('...Order ID: ' . $order_id); $order = new WC_Order($order_id); do_action('woocommerce_ppe_do_payaction', $order); $this->add_log('...Order Total: ' . $order->order_total); $this->add_log('...Cart Total: ' . WC()->cart->get_total()); $this->add_log("...Token:" . $this->get_session('TOKEN')); $result = $this->ConfirmPayment($order->order_total); // Set Customer Name if (!get_current_user_id()) { update_post_meta($order_id, '_billing_first_name', $this->get_session('firstname')); update_post_meta($order_id, '_billing_last_name', $this->get_session('lastname')); } /** * Customer Notes */ if ($this->get_session('customer_notes') != '') { $customer_notes = $this->get_session('customer_notes'); } else { if (isset($checkout_form_data['order_comments'])) { $customer_notes = $checkout_form_data['order_comments']; } } if (!empty($customer_notes)) { // Update post 37 $checkout_note = array('ID' => $order_id, 'post_excerpt' => $customer_notes); wp_update_post($checkout_note); $checkout_form_data['order_comments'] = ''; unset($checkout_form_data['order_comments']); } if ($result['ACK'] == 'Success' || $result['ACK'] == 'SuccessWithWarning') { $this->add_log('Payment confirmed with PayPal successfully'); $result = apply_filters('woocommerce_payment_successful_result', $result, $order_id); /** * Gift Wrap Notes */ if ($this->get_session('giftwrapamount') != '') { update_post_meta($order_id, 'giftwrapamount', $this->get_session('giftwrapamount')); if ($this->get_session('giftmessage') != '') { update_post_meta($order_id, 'giftmessage', $this->get_session('giftmessage')); } if ($this->get_session('giftwrapname') != '') { update_post_meta($order_id, 'giftwrapname', $this->get_session('giftwrapname')); } if ($this->get_session('giftmessage') != '') { update_post_meta($order_id, 'giftmessage', $this->get_session('giftmessage')); } $giftreceiptenable = strtolower($this->get_session('giftreceiptenable')) == 'true' ? 'true' : 'false'; update_post_meta($order_id, 'giftreceiptenable', $giftreceiptenable); } update_post_meta($order_id, '_express_checkout_token', $this->get_session('TOKEN')); $this->remove_session('TOKEN'); $order->add_order_note(__('PayPal Express payment completed', 'paypal-for-woocommerce') . ' ( Response Code: ' . $result['ACK'] . ", " . ' TransactionID: ' . $result['PAYMENTINFO_0_TRANSACTIONID'] . ' )'); $REVIEW_RESULT = unserialize($this->get_session('RESULT')); $payerstatus_note = __('Payer Status: ', 'paypal-for-woocommerce'); $payerstatus_note .= ucfirst($REVIEW_RESULT['PAYERSTATUS']); $order->add_order_note($payerstatus_note); $addressstatus_note = __('Address Status: ', 'paypal-for-woocommerce'); if (isset($REVIEW_RESULT['ADDRESSSTATUS']) && !empty($REVIEW_RESULT['ADDRESSSTATUS'])) { $addressstatus_note .= ucfirst($REVIEW_RESULT['ADDRESSSTATUS']); } $order->add_order_note($addressstatus_note); $order->payment_complete($result['PAYMENTINFO_0_TRANSACTIONID']); //add hook do_action('woocommerce_checkout_order_processed', $order_id); unset(WC()->session->checkout_form); unset(WC()->session->checkout_form_post_data); // Empty the Cart WC()->cart->empty_cart(); wp_redirect($this->get_return_url($order)); exit; } else { $this->add_log('...Error confirming order ' . $order_id . ' with PayPal'); $this->add_log('...response:' . print_r($result, true)); // Display a user friendly Error on the page and log details $ErrorCode = urldecode($result["L_ERRORCODE0"]); $ErrorShortMsg = urldecode($result["L_SHORTMESSAGE0"]); $ErrorLongMsg = urldecode($result["L_LONGMESSAGE0"]); $ErrorSeverityCode = urldecode($result["L_SEVERITYCODE0"]); $this->add_log('DoExpressCheckoutPayment API call failed. '); $this->add_log('Detailed Error Message: ' . $ErrorLongMsg); $this->add_log('Short Error Message: ' . $ErrorShortMsg); $this->add_log('Error Code: ' . $ErrorCode); $this->add_log('Error Severity Code: ' . $ErrorSeverityCode); if ($ErrorCode == '10486') { $this->RedirectToPayPal($this->get_session('TOKEN')); } // Notice admin if has any issue from PayPal $message = ''; if ($this->error_email_notify) { $admin_email = get_option("admin_email"); $message .= __("DoExpressCheckoutPayment API call failed.", "paypal-for-woocommerce") . "\n\n"; $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; $message .= __('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode . "\n"; $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; $message .= __('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg . "\n"; $message .= __('User IP: ', 'paypal-for-woocommerce') . $this->get_user_ip() . "\n"; $message .= __('Order ID: ') . $order_id . "\n"; $message .= __('Customer Name: ') . $this->get_session('shiptoname') . "\n"; $message .= __('Customer Email: ') . $this->get_session('payeremail') . "\n"; $error_email_notify_mes = apply_filters('ae_ppec_error_email_message', $message, $ErrorCode, $ErrorSeverityCode, $ErrorShortMsg, $ErrorLongMsg); $subject = "PayPal Express Checkout Error Notification"; $error_email_notify_subject = apply_filters('ae_ppec_error_email_subject', $subject); wp_mail($admin_email, $error_email_notify_subject, $error_email_notify_mes); } // Generate error message based on Error Display Type setting if ($this->error_display_type == 'detailed') { $sec_error_notice = $ErrorCode . ' - ' . $ErrorLongMsg; $error_display_type_message = sprintf(__($sec_error_notice, 'paypal-for-woocommerce')); } else { $error_display_type_message = sprintf(__('There was a problem paying with PayPal. Please try another method.', 'paypal-for-woocommerce')); } $error_display_type_message = apply_filters('ae_ppec_error_user_display_message', $error_display_type_message, $ErrorCode, $ErrorLongMsg); wc_add_notice($error_display_type_message, 'error'); wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } } } }
public function insertKNSBRating($date, $knsb, $club, $name, $rating, $type = 1) { $data["code"] = false; //Check if player exists $sql = "SELECT * FROM " . settings::prefix . "knsb WHERE id = " . $knsb; $data["sql"] = $sql; $query = mysql_query($sql); if (mysql_num_rows($query) == 0) { //INSERT PLAYER $naam = new FullNameParser(); $naamData = $naam->split_full_name($name); $sql = "INSERT INTO " . settings::prefix . "knsb (id, vereniging_id, lidmaatschap, achternaam, tussenvoegsel, voornaam) VALUES (" . $knsb . ", " . $club . ", 0, '" . $naamData["lname"] . "', '" . $naamData["suffix"] . "', '" . $naamData["fname"] . "')"; $query = mysql_query($sql); $data["sql"] = $sql; } else { $row = mysql_fetch_assoc($query); if ($row["vereniging_id"] != $club) { $sql = "UPDATE " . settings::prefix . "knsb SET vereniging_id = " . $club . " where id = " . $knsb; mysql_query($sql); } } //Check if rating exists $sql = "SELECT * FROM " . settings::prefix . "knsb_rating WHERE knsb = " . $knsb . " AND periode = '" . $date . "'"; $query = mysql_query($sql); if (mysql_num_rows($query) == 0) { $sql = "INSERT INTO " . settings::prefix . "knsb_rating (periode, knsb, naam, rating) VALUES ('" . $date . "','" . $knsb . "','" . $name . "','" . $rating . "') "; $data["sql"] = $sql; $query = mysql_query($sql); $data["code"] = 200; } return $data; }
public function executepay() { if (isset($_GET["token"]) && !empty($_GET["token"]) && isset($_GET["PayerID"]) && !empty($_GET["PayerID"])) { global $woocommerce; WC()->session->token = $_GET["token"]; WC()->session->paymentId = $_GET["paymentId"]; WC()->session->PayerID = $_GET["PayerID"]; $execution = new PaymentExecution(); $execution->setPayerId(WC()->session->PayerID); try { $payment = Payment::get(WC()->session->paymentId, $this->getAuth()); $payment->execute($execution, $this->getAuth()); $this->add_log(print_r($payment, true)); if ($payment->state == "approved") { //if state = approved continue.. global $wpdb; $this->log->add('paypal_plus', sprintf(__('Response: %s', 'paypal-for-woocommerce'), print_r($payment, true))); $order = new WC_Order(WC()->session->ppp_order_id); if ($this->billing_address == 'yes') { require_once "lib/NameParser.php"; $parser = new FullNameParser(); $split_name = $parser->split_full_name($payment->payer->payer_info->shipping_address->recipient_name); $shipping_first_name = $split_name['fname']; $shipping_last_name = $split_name['lname']; $full_name = $split_name['fullname']; update_post_meta($order->id, '_billing_first_name', $shipping_first_name); update_post_meta($order->id, '_billing_last_name', $shipping_last_name); update_post_meta($order->id, '_billing_full_name', $full_name); update_post_meta($order->id, '_billing_address_1', $payment->payer->payer_info->shipping_address->line1); update_post_meta($order->id, '_billing_address_2', $payment->payer->payer_info->shipping_address->line2); update_post_meta($order->id, '_billing_city', $payment->payer->payer_info->shipping_address->city); update_post_meta($order->id, '_billing_postcode', $payment->payer->payer_info->shipping_address->postal_code); update_post_meta($order->id, '_billing_country', $payment->payer->payer_info->shipping_address->country_code); update_post_meta($order->id, '_billing_state', $payment->payer->payer_info->shipping_address->state); } $order->add_order_note(__('PayPal Plus payment completed', 'paypal-for-woocommerce')); $order->payment_complete($payment->id); //add hook do_action('woocommerce_checkout_order_processed', $order->id); // Remove cart WC()->cart->empty_cart(); if (method_exists($order, 'get_checkout_order_received_url')) { $redirect = $order->get_checkout_order_received_url(); } else { $redirect = add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink(get_option('woocommerce_thanks_page_id')))); } wp_redirect($redirect); } else { wc_add_notice(__('Error Payment state:' . $payment->state, 'paypal-for-woocommerce'), 'error'); $this->add_log(__('Error Payment state:' . $payment->state, 'paypal-for-woocommerce')); wp_redirect($woocommerce->cart->get_cart_url()); exit; } } catch (PayPal\Exception\PayPalConnectionException $ex) { wc_add_notice(__("Error processing checkout. Please try again. ", 'paypal-for-woocommerce'), 'error'); $this->add_log($ex->getData()); wp_redirect($woocommerce->cart->get_cart_url()); exit; } catch (Exception $ex) { $this->add_log($ex->getMessage()); // Prints the Error Code wc_add_notice(__("Error processing checkout. Please try again.", 'paypal-for-woocommerce'), 'error'); wp_redirect($woocommerce->cart->get_cart_url()); exit; } } }
/** * PayPal Express Checkout * * Main action function that handles PPE actions: * 1. 'expresscheckout' - Initiates the Express Checkout process; called by the checkout button. * 2. 'revieworder' - Customer has reviewed the order. Saves shipping info to order. * 3. 'payaction' - Customer has pressed "Place Order" on the review page. */ function paypal_express_checkout($posted = null) { if (!empty($posted) || isset($_GET['pp_action']) && $_GET['pp_action'] == 'expresscheckout') { if (sizeof(WC()->cart->get_cart()) > 0) { // The customer has initiated the Express Checkout process with the button on the cart page if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } $this->add_log('Start Express Checkout'); /** * Check if the EC button used was the PayPal Credit button. * This $usePayPalCredit flag will be used to adjust the SEC request accordingly. */ if (isset($_GET['use_paypal_credit']) && 'true' == $_GET['use_paypal_credit']) { $usePayPalCredit = true; } else { $usePayPalCredit = false; } WC()->cart->calculate_totals(); //$paymentAmount = WC()->cart->get_total(); $paymentAmount = number_format(WC()->cart->total, 2, '.', ''); //Check if review order page is exist, otherwise re-create it on the fly $review_order_page_url = get_permalink(wc_get_page_id('review_order')); if (!$review_order_page_url) { $this->add_log(__('Review Order Page not found, re-create it. ', 'paypal-for-woocommerce')); include_once WC()->plugin_path() . '/includes/admin/wc-admin-functions.php'; $page_id = wc_create_page(esc_sql(_x('review-order', 'page_slug', 'woocommerce')), 'woocommerce_review_order_page_id', __('Checkout → Review Order', 'paypal-for-woocommerce'), '[woocommerce_review_order]', wc_get_page_id('checkout')); $review_order_page_url = get_permalink($page_id); } $returnURL = urlencode(add_query_arg('pp_action', 'revieworder', $review_order_page_url)); $cancelURL = urlencode(WC()->cart->get_cart_url()); $resArray = $this->CallSetExpressCheckout($paymentAmount, $returnURL, $cancelURL, $usePayPalCredit, $posted); $ack = strtoupper($resArray["ACK"]); /** * I've replaced the original redirect URL's here with * what the PayPal class library returns so that options like * "skip details" will work correctly with PayPal's review pages. */ if ($ack == "SUCCESS" || $ack == "SUCCESSWITHWARNING") { $this->add_log('Redirecting to PayPal'); if (is_ajax()) { $result = array('redirect' => $resArray['REDIRECTURL'], 'result' => 'success'); echo '<!--WC_START-->' . json_encode($result) . '<!--WC_END-->'; exit; } else { //$this->RedirectToPayPal( $resArray["TOKEN"] ); wp_redirect($resArray['REDIRECTURL']); exit; } } else { // Display a user friendly Error on the page and log details $ErrorCode = urldecode($resArray["L_ERRORCODE0"]); $ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]); $ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]); $ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]); $this->add_log(__('SetExpressCheckout API call failed. ', 'paypal-for-woocommerce')); $this->add_log(__('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg); $this->add_log(__('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg); $this->add_log(__('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode); $this->add_log(__('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode); // Notice admin if has any issue from PayPal $message = ''; if ($this->error_email_notify) { $admin_email = get_option("admin_email"); $message .= __("SetExpressCheckout API call failed.", "paypal-for-woocommerce") . "\n\n"; $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; $message .= __('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode . "\n"; $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; $message .= __('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg . "\n"; wp_mail($admin_email, "PayPal Express Checkout Error Notification", $message); } // Generate error message based on Error Display Type setting if ($this->error_display_type == 'detailed') { $sec_error_notice = $ErrorCode . ' - ' . $ErrorLongMsg; wc_add_notice(sprintf(__($sec_error_notice, 'paypal-for-woocommerce')), 'error'); } else { wc_add_notice(sprintf(__('There was a problem paying with PayPal. Please try another method.', 'paypal-for-woocommerce')), 'error'); } if (!is_ajax()) { wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } else { return; } } } } elseif (isset($_GET['pp_action']) && $_GET['pp_action'] == 'revieworder') { wc_clear_notices(); // The customer has logged into PayPal and approved order. // Retrieve the shipping details and present the order for completion. if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } $this->add_log('Start Review Order'); if (isset($_GET['token'])) { $token = $_GET['token']; $this->set_session('TOKEN', $token); } if (isset($_GET['PayerID'])) { $payerID = $_GET['PayerID']; $this->set_session('PayerID', $payerID); } $this->add_log("...Token:" . $this->get_session('TOKEN')); $this->add_log("...PayerID: " . $this->get_session('PayerID')); $result = $this->CallGetShippingDetails($this->get_session('TOKEN')); if (!empty($result)) { $this->set_session('RESULT', serialize($result)); if (isset($result['SHIPTOCOUNTRYCODE'])) { /** * Check if shiptocountry is in the allowed countries list */ if (!array_key_exists($result['SHIPTOCOUNTRYCODE'], WC()->countries->get_allowed_countries())) { wc_add_notice(sprintf(__('We do not sell in your country, please try again with another address.', 'paypal-for-woocommerce')), 'error'); wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } WC()->customer->set_shipping_country($result['SHIPTOCOUNTRYCODE']); } if (isset($result['SHIPTONAME'])) { WC()->customer->shiptoname = $result['SHIPTONAME']; } if (isset($result['SHIPTOSTREET'])) { WC()->customer->set_address($result['SHIPTOSTREET']); } if (isset($result['SHIPTOCITY'])) { WC()->customer->set_city($result['SHIPTOCITY']); } if (isset($result['SHIPTOCOUNTRYCODE'])) { WC()->customer->set_country($result['SHIPTOCOUNTRYCODE']); } if (isset($result['SHIPTOSTATE'])) { WC()->customer->set_state($this->get_state_code($result['SHIPTOCOUNTRYCODE'], $result['SHIPTOSTATE'])); } if (isset($result['SHIPTOZIP'])) { WC()->customer->set_postcode($result['SHIPTOZIP']); } if (isset($result['SHIPTOSTATE'])) { WC()->customer->set_shipping_state($this->get_state_code($result['SHIPTOCOUNTRYCODE'], $result['SHIPTOSTATE'])); } if (isset($result['SHIPTOZIP'])) { WC()->customer->set_shipping_postcode($result['SHIPTOZIP']); } /** * Save GECD data in sessions for use in DECP */ $this->set_session('firstname', isset($result['FIRSTNAME']) ? $result['FIRSTNAME'] : ''); $this->set_session('lastname', isset($result['LASTNAME']) ? $result['LASTNAME'] : ''); $this->set_session('shiptoname', isset($result['SHIPTONAME']) ? $result['SHIPTONAME'] : ''); $this->set_session('shiptostreet', isset($result['SHIPTOSTREET']) ? $result['SHIPTOSTREET'] : ''); $this->set_session('shiptostreet2', isset($result['SHIPTOSTREET2']) ? $result['SHIPTOSTREET2'] : ''); $this->set_session('shiptocity', isset($result['SHIPTOCITY']) ? $result['SHIPTOCITY'] : ''); $this->set_session('shiptocountrycode', isset($result['SHIPTOCOUNTRYCODE']) ? $result['SHIPTOCOUNTRYCODE'] : ''); $this->set_session('shiptostate', isset($result['SHIPTOSTATE']) ? $result['SHIPTOSTATE'] : ''); $this->set_session('shiptozip', isset($result['SHIPTOZIP']) ? $result['SHIPTOZIP'] : ''); $this->set_session('payeremail', isset($result['EMAIL']) ? $result['EMAIL'] : ''); $this->set_session('giftmessage', isset($result['GIFTMESSAGE']) ? $result['GIFTMESSAGE'] : ''); $this->set_session('giftreceiptenable', isset($result['GIFTRECEIPTENABLE']) ? $result['GIFTRECEIPTENABLE'] : ''); $this->set_session('giftwrapname', isset($result['GIFTWRAPNAME']) ? $result['GIFTWRAPNAME'] : ''); $this->set_session('giftwrapamount', isset($result['GIFTWRAPAMOUNT']) ? $result['GIFTWRAPAMOUNT'] : ''); $this->set_session('customer_notes', isset($result['PAYMENTREQUEST_0_NOTETEXT']) ? $result['PAYMENTREQUEST_0_NOTETEXT'] : ''); $this->set_session('phonenum', isset($result['PHONENUM']) ? $result['PHONENUM'] : ''); WC()->cart->calculate_totals(); } else { $this->add_log("...ERROR: GetShippingDetails returned empty result"); } if ($this->skip_final_review == 'yes') { $url = add_query_arg(array('wc-api' => 'WC_Gateway_PayPal_Express_AngellEYE', 'pp_action' => 'payaction'), home_url()); wp_redirect($url); exit; } if (isset($_POST['createaccount'])) { if (empty($_POST['username'])) { wc_add_notice(__('Username is required', 'paypal-for-woocommerce'), 'error'); } elseif (username_exists($_POST['username'])) { wc_add_notice(__('This username is already registered.', 'paypal-for-woocommerce'), 'error'); } elseif (empty($_POST['email'])) { wc_add_notice(__('Please provide a valid email address.', 'paypal-for-woocommerce'), 'error'); } elseif (empty($_POST['password']) || empty($_POST['repassword'])) { wc_add_notice(__('Password is required.', 'paypal-for-woocommerce'), 'error'); } elseif ($_POST['password'] != $_POST['repassword']) { wc_add_notice(__('Passwords do not match.', 'paypal-for-woocommerce'), 'error'); } elseif (get_user_by('email', $_POST['email']) != false) { wc_add_notice(__('This email address is already registered.', 'paypal-for-woocommerce'), 'error'); } else { $data = array('user_login' => addslashes($_POST['username']), 'user_email' => addslashes($_POST['email']), 'user_pass' => addslashes($_POST['password'])); $userID = wp_insert_user($data); if (!is_wp_error($userID)) { update_user_meta($userID, 'billing_first_name', $result['FIRSTNAME']); update_user_meta($userID, 'billing_last_name', $result['LASTNAME']); update_user_meta($userID, 'billing_address_1', $result['SHIPTOSTREET']); update_user_meta($userID, 'billing_state', $result['SHIPTOSTATE']); update_user_meta($userID, 'billing_email', $result['EMAIL']); /* USER SIGON */ $user_login = esc_attr($_POST["username"]); $user_password = esc_attr($_POST["password"]); $user_email = esc_attr($_POST["email"]); $creds = array('user_login' => $user_login, 'user_password' => $user_password, 'remember' => true); $user = wp_signon($creds, false); if (is_wp_error($user)) { wc_add_notice($user->get_error_message(), 'error'); } else { wp_set_current_user($user->ID); //Here is where we update the global user variables header("Refresh:0"); die; } } } } } elseif (isset($_GET['pp_action']) && $_GET['pp_action'] == 'payaction') { if (isset($_POST) || $this->skip_final_review == 'yes') { // Update customer shipping and payment method to posted method $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); if (isset($_POST['shipping_method']) && is_array($_POST['shipping_method'])) { foreach ($_POST['shipping_method'] as $i => $value) { $chosen_shipping_methods[$i] = wc_clean($value); } } WC()->session->set('chosen_shipping_methods', $chosen_shipping_methods); if (WC()->cart->needs_shipping()) { // Validate Shipping Methods $packages = WC()->shipping->get_packages(); WC()->checkout()->shipping_methods = WC()->session->get('chosen_shipping_methods'); } $this->add_log('Start Pay Action'); if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } WC()->cart->calculate_totals(); $order_id = WC()->checkout()->create_order(); /** * Update meta data with session data */ // Parse SHIPTONAME to fist and last name require_once "lib/NameParser.php"; $parser = new FullNameParser(); $split_name = $parser->split_full_name($this->get_session('shiptoname')); $shipping_first_name = $split_name['fname']; $shipping_last_name = $split_name['lname']; $full_name = $split_name['fullname']; update_post_meta($order_id, '_payment_method', $this->id); update_post_meta($order_id, '_payment_method_title', $this->title); if (is_user_logged_in()) { $userLogined = get_current_user(); update_post_meta($order_id, '_billing_email', $userLogined->user_email); } else { update_post_meta($order_id, '_billing_email', $this->get_session('payeremail')); } update_post_meta($order_id, '_shipping_first_name', $shipping_first_name); update_post_meta($order_id, '_shipping_last_name', $shipping_last_name); update_post_meta($order_id, '_shipping_full_name', $full_name); update_post_meta($order_id, '_shipping_company', ""); update_post_meta($order_id, '_billing_phone', $this->get_session('phonenum')); update_post_meta($order_id, '_shipping_address_1', $this->get_session('shiptostreet')); update_post_meta($order_id, '_shipping_address_2', $this->get_session('shiptostreet2')); update_post_meta($order_id, '_shipping_city', $this->get_session('shiptocity')); update_post_meta($order_id, '_shipping_postcode', $this->get_session('shiptozip')); update_post_meta($order_id, '_shipping_country', $this->get_session('shiptocountrycode')); update_post_meta($order_id, '_shipping_state', $this->get_state_code($this->get_session('shiptocountrycode'), $this->get_session('shiptostate'))); update_post_meta($order_id, '_customer_user', get_current_user_id()); $this->add_log('...Order ID: ' . $order_id); $order = new WC_Order($order_id); do_action('woocommerce_ppe_do_payaction', $order); $this->add_log('...Order Total: ' . $order->order_total); $this->add_log('...Cart Total: ' . WC()->cart->get_total()); $this->add_log("...Token:" . $this->get_session('TOKEN')); $result = $this->ConfirmPayment($order->order_total); // Set Customer Name if (!get_current_user_id()) { update_post_meta($order_id, '_billing_first_name', $this->get_session('firstname')); update_post_meta($order_id, '_billing_last_name', $this->get_session('lastname')); } /** * Customer Notes */ if ($this->get_session('customer_notes') != '') { $order->add_order_note(__('Customer Notes: ', 'paypal-for-woocommerce') . $this->get_session('customer_notes')); } if ($result['ACK'] == 'Success' || $result['ACK'] == 'SuccessWithWarning') { $this->add_log('Payment confirmed with PayPal successfully'); $result = apply_filters('woocommerce_payment_successful_result', $result); /** * Gift Wrap Notes */ if ($this->get_session('giftwrapamount') != '') { $giftwrap_note = __('Gift Wrap Added', 'paypal-for-woocommerce'); $giftwrap_note .= $this->get_session('giftwrapname') != '' ? ' - ' . $this->get_session('giftwrapname') : ''; $giftwrap_note .= $this->get_session('giftmessage') != '' ? '<br />Message: ' . $this->get_session('giftmessage') : ''; $giftwrap_note .= '<br />' . __('Use Gift Receipt?: ', 'paypal-for-woocommerce'); $giftwrap_note .= strtolower($this->get_session('giftreceiptenable')) == 'true' ? 'Yes' : 'No'; //$giftwrap_note .= '<br />Fee: ' . woocommerce_price(number_format($this->get_session('giftwrapamount'),2)); $order->add_order_note($giftwrap_note); } $order->add_order_note(__('PayPal Express payment completed', 'paypal-for-woocommerce') . ' ( Response Code: ' . $result['ACK'] . ", " . ' TransactionID: ' . $result['PAYMENTINFO_0_TRANSACTIONID'] . ' )'); $REVIEW_RESULT = unserialize($this->get_session('RESULT')); $payerstatus_note = __('Payer Status: ', 'paypal-for-woocommerce'); $payerstatus_note .= ucfirst($REVIEW_RESULT['PAYERSTATUS']); $order->add_order_note($payerstatus_note); $addressstatus_note = __('Address Status: ', 'paypal-for-woocommerce'); $addressstatus_note .= ucfirst($REVIEW_RESULT['ADDRESSSTATUS']); $order->add_order_note($addressstatus_note); $order->payment_complete($result['PAYMENTINFO_0_TRANSACTIONID']); //add hook do_action('woocommerce_checkout_order_processed', $order_id); // Empty the Cart WC()->cart->empty_cart(); wp_redirect($this->get_return_url($order)); exit; } else { $this->add_log('...Error confirming order ' . $order_id . ' with PayPal'); $this->add_log('...response:' . print_r($result, true)); // Display a user friendly Error on the page and log details $ErrorCode = urldecode($result["L_ERRORCODE0"]); $ErrorShortMsg = urldecode($result["L_SHORTMESSAGE0"]); $ErrorLongMsg = urldecode($result["L_LONGMESSAGE0"]); $ErrorSeverityCode = urldecode($result["L_SEVERITYCODE0"]); $this->add_log('SetExpressCheckout API call failed. '); $this->add_log('Detailed Error Message: ' . $ErrorLongMsg); $this->add_log('Short Error Message: ' . $ErrorShortMsg); $this->add_log('Error Code: ' . $ErrorCode); $this->add_log('Error Severity Code: ' . $ErrorSeverityCode); if ($ErrorCode == '10486') { $this->RedirectToPayPal($this->get_session('TOKEN')); } // Notice admin if has any issue from PayPal $message = ''; if ($this->error_email_notify) { $admin_email = get_option("admin_email"); $message .= __("DoExpressCheckoutPayment API call failed.", "paypal-for-woocommerce") . "\n\n"; $message .= __('Error Code: ', 'paypal-for-woocommerce') . $ErrorCode . "\n"; $message .= __('Error Severity Code: ', 'paypal-for-woocommerce') . $ErrorSeverityCode . "\n"; $message .= __('Short Error Message: ', 'paypal-for-woocommerce') . $ErrorShortMsg . "\n"; $message .= __('Detailed Error Message: ', 'paypal-for-woocommerce') . $ErrorLongMsg . "\n"; wp_mail($admin_email, "PayPal Express Checkout Error Notification", $message); } // Generate error message based on Error Display Type setting if ($this->error_display_type == 'detailed') { $sec_error_notice = $ErrorCode . ' - ' . $ErrorLongMsg; wc_add_notice(sprintf(__($sec_error_notice, 'paypal-for-woocommerce')), 'error'); } else { wc_add_notice(sprintf(__('There was a problem paying with PayPal. Please try another method.', 'paypal-for-woocommerce')), 'error'); } wp_redirect(get_permalink(wc_get_page_id('cart'))); exit; } } } }