function woo_pp_start_checkout() { $checkout = wc_gateway_ppec()->checkout; try { $redirect_url = $checkout->start_checkout_from_cart(); wp_safe_redirect($redirect_url); exit; } catch (PayPal_API_Exception $e) { wc_gateway_ppec_format_paypal_api_exception($e->errors); $redirect_url = WC()->cart->get_cart_url(); $settings = wc_gateway_ppec()->settings; $client = wc_gateway_ppec()->client; if ($settings->is_enabled() && $client->get_payer_id()) { ob_end_clean(); ?> <script type="text/javascript"> if( ( window.opener != null ) && ( window.opener !== window ) && ( typeof window.opener.paypal != "undefined" ) && ( typeof window.opener.paypal.checkout != "undefined" ) ) { window.opener.location.assign( "<?php echo $redirect_url; ?> " ); window.close(); } else { window.location.assign( "<?php echo $redirect_url; ?> " ); } </script> <?php exit; } else { wp_safe_redirect($redirect_url); exit; } } }
/** * Process payments */ public function process_payment($order_id) { $checkout = wc_gateway_ppec()->checkout; $order = wc_get_order($order_id); $session = WC()->session->get('paypal'); // Redirect them over to PayPal if they have no current session (this is for PayPal Mark). if (!$checkout->has_active_session() || !$session->checkout_completed) { try { return array('result' => 'success', 'redirect' => $checkout->start_checkout_from_checkout($order_id)); } catch (PayPal_API_Exception $e) { wc_gateway_ppec_format_paypal_api_exception($e->errors); } } else { try { // Get details $checkout_details = $checkout->getCheckoutDetails($session->token); // Store addresses given by PayPal $order->set_address($checkout->get_mapped_billing_address($checkout_details), 'billing'); $order->set_address($checkout->get_mapped_shipping_address($checkout_details), 'shipping'); // Complete the payment now. $checkout->do_payment($order, $session->token, $session->payerID); // Clear Cart WC()->cart->empty_cart(); return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } catch (PayPal_Missing_Session_Exception $e) { // For some reason, our session data is missing. Generally, if we've made it this far, this shouldn't happen. wc_add_notice(__('Sorry, an error occurred while trying to process your payment. Please try again.', 'woocommerce-gateway-paypal-express-checkout'), 'error'); } catch (PayPal_API_Exception $e) { // Did we get a 10486 or 10422 back from PayPal? If so, this means we need to send the buyer back over to // PayPal to have them pick out a new funding method. $error_codes = wp_list_pluck($e->errors, 'error_code'); if (in_array('10486', $error_codes) || in_array('10422', $error_codes)) { $session->checkout_completed = false; $session->source = 'order'; $session->order_id = $order_id; WC()->session->set('paypal', $session); return array('result' => 'success', 'redirect' => wc_gateway_ppec()->settings->get_paypal_redirect_url($session->token, true)); } else { wc_gateway_ppec_format_paypal_api_exception($e->errors); } } } }