function cheque_checkout_action()
 {
     $retval = array();
     $items = $this->param('items');
     if (!$items) {
         return array('error' => __('Your cart is empty', 'nggallery'));
     }
     $customer = array('name' => $this->param('customer_name'), 'email' => $this->param('customer_email'), 'address' => $this->param('customer_address'), 'city' => $this->param('customer_city'), 'state' => $this->param('customer_state'), 'postal' => $this->param('customer_postal'), 'country' => $this->param('customer_country'));
     $retval['customer'] = $customer;
     // Presently we only do basic field validation: ensure that each field is filled and that
     // the country selected exists in C_NextGen_Pro_Currencies::$countries
     foreach ($customer as $key => $val) {
         if (empty($val)) {
             $retval['error'] = __('Please fill all fields and try again', 'nggallery');
             break;
         }
     }
     // No error yet?
     if (!isset($retval['error'])) {
         if (empty(C_NextGen_Pro_Currencies::$countries[$customer['country']])) {
             return array('error' => __('Invalid country selected, please try again.', 'nggallery'));
         } else {
             $customer['country'] = C_NextGen_Pro_Currencies::$countries[$customer['country']]['name'];
         }
         $checkout = new C_NextGen_Pro_Checkout();
         $cart = new C_NextGen_Pro_Cart();
         $settings = C_NextGen_Settings::get_instance();
         $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency];
         foreach ($items as $image_id => $image_items) {
             if ($image = C_Image_Mapper::get_instance()->find($image_id)) {
                 $cart->add_image($image_id, $image);
                 foreach ($image_items as $item_id => $quantity) {
                     if ($item = C_Pricelist_Item_Mapper::get_instance()->find($item_id)) {
                         $item->quantity = $quantity;
                         $cart->add_item($image_id, $item_id, $item);
                     }
                 }
             }
         }
         // Calculate the total
         $use_home_country = intval($this->param('use_home_country'));
         $order_total = $cart->get_total($use_home_country);
         // Create the order
         if (!$cart->has_items()) {
             return array('error' => __('Your cart is empty', 'nggallery'));
         }
         $order = $checkout->create_order($cart->to_array(), $customer['name'], $customer['email'], $order_total, 'cheque', $customer['address'], $customer['city'], $customer['state'], $customer['postal'], $customer['country'], $use_home_country, 'unverified');
         $order->status = 'unverified';
         $order->gateway_admin_note = __('Payment was successfully made via Check. Once you have received payment, you can click “Verify” in the View Orders page and a confirmation email will be sent to the user.');
         C_Order_Mapper::get_instance()->save($order);
         $checkout->send_email_notification($order->hash);
         $retval['order'] = $order->hash;
         $retval['redirect'] = $checkout->get_thank_you_page_url($order->hash, TRUE);
     }
     return $retval;
 }
 /**
  * Processes 'verify cheque payment' bulk action
  */
 function process_cheque_bulk_actions()
 {
     global $typenow;
     if ($typenow !== 'ngg_order') {
         return;
     }
     if (empty($_REQUEST['post'])) {
         return;
     }
     $wp_list_table = _get_list_table('WP_Posts_List_Table');
     $action = $wp_list_table->current_action();
     $ids = array_map('intval', $_REQUEST['post']);
     if (empty($ids)) {
         return;
     }
     $url = remove_query_arg(array('verify_cheques'), wp_get_referer());
     if (!$url) {
         $url = admin_url('edit.php?post_type=ngg_order');
     }
     $url = add_query_arg('paged', $wp_list_table->get_pagenum(), $url);
     switch ($action) {
         case 'verify_cheques':
             $checkout = new C_NextGen_Pro_Checkout();
             $verified = 0;
             foreach ($ids as $post_id) {
                 $order = C_Order_Mapper::get_instance()->find($post_id, TRUE);
                 if ($order->status !== 'unverified' || $order->payment_gateway !== 'cheque') {
                     continue;
                 }
                 $order->status = 'verified';
                 if ($order->save()) {
                     $verified++;
                     $checkout->send_email_receipt($order->hash);
                 }
             }
             if (session_id() == '') {
                 session_start();
             }
             $_SESSION['ngg_verified_cheques'] = $verified;
             session_write_close();
             wp_redirect($url);
             throw new E_Clean_Exit();
         default:
             return;
     }
 }
 function paypal_standard_order_action()
 {
     $retval = array();
     if ($items = $this->param('items')) {
         $checkout = new C_NextGen_Pro_Checkout();
         $cart = new C_NextGen_Pro_Cart();
         $settings = C_NextGen_Settings::get_instance();
         $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency];
         foreach ($items as $image_id => $image_items) {
             if ($image = C_Image_Mapper::get_instance()->find($image_id)) {
                 $cart->add_image($image_id, $image);
                 foreach ($image_items as $item_id => $quantity) {
                     if ($item = C_Pricelist_Item_Mapper::get_instance()->find($item_id)) {
                         $item->quantity = $quantity;
                         $cart->add_item($image_id, $item_id, $item);
                     }
                 }
             }
         }
         // Calculate the total
         $use_home_country = intval($this->param('use_home_country'));
         $order_total = $cart->get_total($use_home_country);
         // Create the order
         if ($cart->has_items()) {
             $order = $checkout->create_order($cart->to_array(), __('PayPal Customer', 'nggallery'), 'Unknown', $order_total, 'paypal_standard');
             $order->status = 'unverified';
             $order->use_home_country = $use_home_country;
             $order->gateway_admin_note = __('Payment was successfully made via PayPal Standard, with no further payment action required.');
             C_Order_Mapper::get_instance()->save($order);
             $retval['order'] = $order->hash;
         } else {
             $retval['error'] = __('Your cart is empty', 'nggallery');
         }
     }
     return $retval;
 }
 function is_order_verified_action()
 {
     $retval = array('verified' => FALSE);
     if ($order = C_Order_Mapper::get_instance()->find_by_hash($this->param('order'))) {
         if ($order->status == 'verified') {
             $retval['verified'] = TRUE;
             $checkout = C_NextGen_Pro_Checkout::get_instance();
             $retval['thank_you_page_url'] = $checkout->get_thank_you_page_url($order->hash, TRUE);
         }
     } else {
         $retval['error'] = __("We're sorry, but we couldn't find your order.", 'nextgen-gallery-pro');
     }
     return $retval;
 }
 function index_action()
 {
     wp_enqueue_style('ngg-digital-downloads-page', $this->get_static_url('photocrati-nextgen_pro_ecommerce#digital_downloads_page.css'));
     $retval = __('Oops! This page usually displays details for image purchases, but you have not ordered any images yet. Please feel free to continue browsing. Thanks for visiting.', 'nextgen-gallery-pro');
     if ($order = C_Order_Mapper::get_instance()->find_by_hash($this->param('order'), TRUE)) {
         // Display digital downloads for verified transactions
         if ($order->status == 'verified') {
             $retval = $this->render_download_list($order);
         } else {
             $retval = $this->render_partial('photocrati-nextgen_pro_ecommerce#waiting_for_confirmation', array('msg' => __("We haven't received payment confirmation yet. This may take a few minutes. Please wait...")), TRUE);
         }
     }
     return $retval;
 }
 function redirect_to_thank_you_page($order_hash)
 {
     // Expose hook for third-parties
     do_action('ngg_pro_purchase_complete');
     // Get the destination url
     $order_details_page = $this->get_thank_you_page_url($order_hash, TRUE);
     // Get the order
     if ($order = C_Order_Mapper::get_instance()->find_by_hash($order_hash)) {
         if (!isset($order->sent_emails) or !$order->sent_emails) {
             // Send the admin notification only when the purchase has been verified
             if ($order->status == 'verified') {
                 $this->send_email_notification($order_hash);
             }
             // Send the e-mail receipt as soon as we can
             $this->send_email_receipt($order_hash);
         }
     } else {
         die(__("We couldn't find your order. We apologize for the inconvenience", 'nextgen-gallery-pro'));
     }
     wp_redirect($order_details_page);
     throw new E_Clean_Exit();
 }
 function custom_edit_link($url)
 {
     global $post;
     // we can't always assume $post exists
     if (empty($post)) {
         return $url;
     }
     if ($post->post_type == 'ngg_pricelist') {
         $url = admin_url('/edit.php?post_type=ngg_pricelist&ngg_edit=1&id=' . $post->ID);
     } elseif ($post->post_type == 'ngg_order') {
         $mapper = C_Order_Mapper::get_instance();
         if ($order = $mapper->find($post->ID)) {
             $checkout = C_NextGen_Pro_Checkout::get_instance();
             $url = $checkout->get_thank_you_page_url($order->hash);
         }
     }
     return $url;
 }
 function paypal_ipn_listener()
 {
     // STEP 1: read POST data
     // Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
     // Instead, read raw POST data from the input stream.
     $raw_post_data = file_get_contents('php://input');
     $raw_post_array = explode('&', $raw_post_data);
     $myPost = array();
     foreach ($raw_post_array as $keyval) {
         $keyval = explode('=', $keyval);
         if (count($keyval) == 2) {
             $myPost[$keyval[0]] = urldecode($keyval[1]);
         }
     }
     // read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
     $req = 'cmd=_notify-validate';
     if (function_exists('get_magic_quotes_gpc')) {
         $get_magic_quotes_exists = true;
     }
     foreach ($myPost as $key => $value) {
         if ($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
             $value = urlencode(stripslashes($value));
         } else {
             $value = urlencode($value);
         }
         $req .= "&{$key}={$value}";
     }
     // STEP 2: Validate the IPN
     if (isset($_REQUEST['custom'])) {
         $response = wp_remote_post($this->get_paypal_url(), array('body' => $req));
         if ($order = $this->validate_order($_REQUEST['custom'], isset($_REQUEST['mc_gross']) ? $_REQUEST['mc_gross'] : 0.0, isset($_REQUEST['first_name']) && isset($_REQUEST['last_name']) ? $_REQUEST['first_name'] . ' ' . $_REQUEST['last_name'] : '', isset($_REQUEST['payer_email']) ? $_REQUEST['payer_email'] : '', isset($_REQUEST['address_street']) ? $_REQUEST['address_street'] : '', isset($_REQUEST['address_city']) ? $_REQUEST['address_city'] : '', isset($_REQUEST['address_state']) ? $_REQUEST['address_state'] : '', isset($_REQUEST['address_zip']) ? $_REQUEST['address_zip'] : '', isset($_REQUEST['address_country']) ? $_REQUEST['address_country'] : '', isset($_REQUEST['contact_phone']) ? $_REQUEST['contact_phone'] : '')) {
             $order_mapper = C_Order_Mapper::get_instance();
             // Fraud detected?
             if (stripos($response['body'], 'VERIFIED') === FALSE) {
                 $order->status = 'fraud';
                 $order_mapper->save($order);
             } else {
                 $order->status = 'verified';
                 $order->sent_emails = TRUE;
                 $order_mapper->save($order);
                 $this->send_email_notification($order->hash);
                 $this->send_email_receipt($order->hash);
             }
         }
     }
     throw new E_Clean_Exit();
 }