/**
  * @param int $order_id
  * @return array
  */
 function process_payment($order_id)
 {
     $order = new jigoshop_order($order_id);
     $this->init_paymentwall_configs();
     $return = array('result' => 'fail', 'redirect' => '');
     $charge = new Paymentwall_Charge();
     try {
         $charge->create(array_merge($this->prepare_user_profile_data($order), $this->prepare_card_info($order)));
         $response = $charge->getPublicData();
         if ($charge->isSuccessful()) {
             if ($charge->isCaptured()) {
                 // Add order note
                 $order->add_order_note(sprintf(__('Brick payment approved (ID: %s, Card: xxxx-%s)', PW_JIGO_TEXT_DOMAIN), $charge->getId(), $charge->getCard()->getAlias()));
                 // Payment complete
                 $order->payment_complete();
                 $return['result'] = 'success';
                 $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
                 $return['redirect'] = add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink($checkout_redirect)));
             } elseif ($charge->isUnderReview()) {
                 $order->update_status('on-hold');
             }
             // Clear shopping cart
             jigoshop_cart::empty_cart();
         } else {
             $errors = json_decode($response, true);
             jigoshop::add_error(__($errors['error']['message']), 'error');
         }
     } catch (Exception $e) {
         jigoshop::add_error($e->getMessage(), 'error');
     }
     // Return redirect
     return $return;
 }
 function receipt_page($order_id)
 {
     $this->init_paymentwall_configs();
     $order = new jigoshop_order($order_id);
     $widget = new Paymentwall_Widget($order->user_id == 0 ? $_SERVER['REMOTE_ADDR'] : $order->user_id, $this->widget, array(new Paymentwall_Product($order->id, $order->order_subtotal, $this->currency, 'Order #' . $order->id, Paymentwall_Product::TYPE_FIXED)), array_merge(array('email' => $order->billing_email, 'success_url' => $this->successurl, 'test_mode' => (int) $this->testmode == 'yes' ? 1 : 0, 'integration_module' => 'jigoshop'), $this->getUserProfileData($order)));
     echo $this->get_template('widget.html', array('iframe' => $widget->getHtmlCode(array('width' => '100%', 'height' => '400px')), 'thankyoutext' => __($this->thankyoutext, PW_JIGO_TEXT_DOMAIN)));
     jigoshop_cart::empty_cart();
 }
Esempio n. 3
0
 /**
  * Process the payment and return the result
  **/
 function process_payment($order_id)
 {
     $order = new jigoshop_order($order_id);
     $status = Jigoshop_Base::get_options()->get('jigoshop_cod_status', 'processing');
     $order->update_status($status, __('Waiting for cash delivery.', 'jigoshop'));
     // Remove cart
     jigoshop_cart::empty_cart();
     // Return thankyou redirect
     $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
     return array('result' => 'success', 'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink($checkout_redirect))));
 }
Esempio n. 4
0
 /**
  * Process the payment and return the result
  **/
 function process_payment($order_id)
 {
     $order = new jigoshop_order($order_id);
     // Mark as on-hold (we're awaiting the cheque)
     $order->update_status('waiting-for-payment', __('Awaiting cheque payment', 'jigoshop'));
     // Remove cart
     jigoshop_cart::empty_cart();
     // Return thankyou redirect
     $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
     return array('result' => 'success', 'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink($checkout_redirect))));
 }
 function receipt_paymentwall($order_id)
 {
     $this->paymentwall_init();
     $order = new jigoshop_order($order_id);
     $productNames = array();
     foreach ($order->items as $item) {
         array_push($productNames, $item['name']);
     }
     echo '<p>' . __($this->thankyoutext, 'jigoshop') . '</p>';
     $widget = new Paymentwall_Widget($order->user_id == 0 ? $_SERVER['REMOTE_ADDR'] : $order->user_id, $this->widget, array(new Paymentwall_Product($order->id, $order->order_subtotal, $this->currency, implode(', ', $productNames), Paymentwall_Product::TYPE_FIXED)), array('email' => $order->billing_email, 'integration_module' => 'jigoshop', 'success_url' => $this->successurl, 'test_mode' => (int) $this->testmode));
     echo $widget->getHtmlCode();
     jigoshop_cart::empty_cart();
 }
 /**
  * Process the checkout after the confirm order button is pressed
  */
 public function process_checkout()
 {
     if (!defined('JIGOSHOP_CHECKOUT')) {
         define('JIGOSHOP_CHECKOUT', true);
     }
     // Initialize cart
     jigoshop_cart::get_cart();
     jigoshop_cart::calculate_totals();
     if (isset($_POST) && $_POST && !isset($_POST['login'])) {
         jigoshop::verify_nonce('process_checkout');
         // this will fill in our $posted array with validated data
         self::validate_checkout();
         $gateway = jigoshop_payment_gateways::get_gateway($this->posted['payment_method']);
         if (self::process_gateway($gateway)) {
             $gateway->validate_fields();
         }
         do_action('jigoshop_after_checkout_validation', $this->posted, $_POST, sizeof(jigoshop::$errors));
         if (jigoshop::has_errors()) {
             return false;
         }
         if (!isset($_POST['update_totals'])) {
             $user_id = get_current_user_id();
             // Create customer account and log them in
             if ($this->show_signup && !$user_id && $this->posted['create_account']) {
                 $user_id = $this->create_user_account();
                 if ($user_id === 0) {
                     return false;
                 }
             }
             $billing = array('first_name' => $this->posted['billing_first_name'], 'last_name' => $this->posted['billing_last_name'], 'company' => $this->posted['billing_company'], 'address_1' => $this->posted['billing_address_1'], 'address_2' => $this->posted['billing_address_2'], 'city' => $this->posted['billing_city'], 'state' => $this->posted['billing_state'], 'postcode' => $this->posted['billing_postcode'], 'country' => $this->posted['billing_country'], 'phone' => $this->posted['billing_phone'], 'email' => $this->posted['billing_email']);
             jigoshop_customer::set_country($billing['country']);
             jigoshop_customer::set_state($billing['state']);
             jigoshop_customer::set_postcode($billing['postcode']);
             if (isset($this->posted['billing_euvatno']) && $this->valid_euvatno) {
                 $billing['euvatno'] = $this->posted['billing_euvatno'];
                 $billing['euvatno'] = str_replace(' ', '', $billing['euvatno']);
                 // If country code is not provided - add one.
                 if (strpos($billing['euvatno'], $billing['country']) === false) {
                     $billing['euvatno'] = $billing['country'] . $billing['euvatno'];
                 }
             }
             // Get shipping/billing
             if (!empty($this->posted['shiptobilling'])) {
                 $shipping = $billing;
                 unset($shipping['phone'], $shipping['email']);
             } elseif (jigoshop_shipping::is_enabled()) {
                 $shipping = array('first_name' => $this->posted['shipping_first_name'], 'last_name' => $this->posted['shipping_last_name'], 'company' => $this->posted['shipping_company'], 'address_1' => $this->posted['shipping_address_1'], 'address_2' => $this->posted['shipping_address_2'], 'city' => $this->posted['shipping_city'], 'state' => $this->posted['shipping_state'], 'postcode' => $this->posted['shipping_postcode'], 'country' => $this->posted['shipping_country']);
             }
             jigoshop_customer::set_shipping_country($shipping['country']);
             jigoshop_customer::set_shipping_state($shipping['state']);
             jigoshop_customer::set_shipping_postcode($shipping['postcode']);
             // Update totals based on processed customer address
             jigoshop_cart::calculate_totals();
             // Save billing/shipping to user meta fields
             if ($user_id > 0) {
                 foreach ($billing as $field => $value) {
                     update_user_meta($user_id, 'billing_' . $field, $value);
                 }
                 if (isset($shipping)) {
                     foreach ($shipping as $field => $value) {
                         update_user_meta($user_id, 'shipping_' . $field, $value);
                     }
                 }
             }
             if (!isset($_POST['submit_action']) || $_POST['submit_action'] != 'place_order') {
                 $result = jigoshop::redirect(jigoshop_get_page_id(JIGOSHOP_CHECKOUT));
                 return array('result' => 'redirect', 'redirect' => $result);
             }
             // Order meta data
             $data = array();
             $applied_coupons = array_map(function ($coupon) {
                 return JS_Coupons::get_coupon($coupon);
             }, jigoshop_cart::get_coupons());
             do_action('jigoshop_checkout_update_order_total', $this->posted);
             foreach ($billing as $field => $value) {
                 $data['billing_' . $field] = $value;
             }
             if (isset($shipping)) {
                 foreach ($shipping as $field => $value) {
                     $data['shipping_' . $field] = $value;
                 }
             }
             $data['order_discount_coupons'] = $applied_coupons;
             $data['shipping_method'] = $this->posted['shipping_method'];
             $data['shipping_service'] = $this->posted['shipping_service'];
             $data['payment_method'] = $this->posted['payment_method'];
             $data['payment_method_title'] = $gateway->title;
             $data['order_subtotal'] = jigoshop_cart::get_subtotal();
             $data['order_discount_subtotal'] = jigoshop_cart::get_discount_subtotal();
             $data['order_shipping'] = jigoshop_cart::get_shipping_total();
             $data['order_discount'] = jigoshop_cart::get_total_discount(false);
             $data['order_tax'] = jigoshop_cart::get_taxes_as_string();
             $data['order_tax_no_shipping_tax'] = jigoshop_cart::get_total_cart_tax_without_shipping_tax();
             $data['order_tax_divisor'] = jigoshop_cart::get_tax_divisor();
             $data['order_shipping_tax'] = jigoshop_cart::get_shipping_tax();
             $data['order_total'] = jigoshop_cart::get_total(false);
             $data['order_total_prices_per_tax_class_ex_tax'] = jigoshop_cart::get_price_per_tax_class_ex_tax();
             if ($this->valid_euvatno) {
                 $data['order_tax'] = '';
                 $temp = jigoshop_cart::get_total_cart_tax_without_shipping_tax();
                 $data['order_total'] -= $data['order_shipping_tax'] + $temp;
                 $data['order_shipping_tax'] = 0;
             }
             // Cart items
             $order_items = array();
             foreach (jigoshop_cart::get_cart() as $values) {
                 /** @var jigoshop_product $product */
                 $product = $values['data'];
                 // Check stock levels
                 if (!$product->has_enough_stock($values['quantity'])) {
                     jigoshop::add_error(sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->get_title()));
                     if (self::get_options()->get('jigoshop_show_stock') == 'yes') {
                         jigoshop::add_error(sprintf(__('We have only %d available at this time.', 'jigoshop'), $product->get_stock()));
                     }
                     break;
                 }
                 // Calc item tax to store
                 $rates = $product->get_tax_destination_rate();
                 $rates = current($rates);
                 if (isset($rates['rate'])) {
                     $rate = $rates['rate'];
                 } else {
                     $rate = 0.0;
                 }
                 if ($this->valid_euvatno) {
                     $rate = 0.0;
                 }
                 $price_inc_tax = $product->get_price_with_tax();
                 if (!empty($values['variation_id'])) {
                     $product_id = $values['variation_id'];
                 } else {
                     $product_id = $values['product_id'];
                 }
                 $custom_products = (array) jigoshop_session::instance()->customized_products;
                 $custom = isset($custom_products[$product_id]) ? $custom_products[$product_id] : '';
                 if (!empty($custom)) {
                     unset($custom_products[$product_id]);
                     jigoshop_session::instance()->customized_products = $custom_products;
                 }
                 $order_items[] = apply_filters('new_order_item', array('id' => $values['product_id'], 'variation_id' => $values['variation_id'], 'variation' => $values['variation'], 'customization' => $custom, 'name' => $product->get_title(), 'qty' => (int) $values['quantity'], 'cost' => $product->get_price_excluding_tax(), 'cost_inc_tax' => $price_inc_tax, 'taxrate' => $rate), $values);
             }
             if (jigoshop::has_errors()) {
                 return false;
             }
             // Insert or update the post data
             $create_new_order = true;
             $order_data = array('post_type' => 'shop_order', 'post_title' => 'Order &ndash; ' . date('F j, Y @ h:i A'), 'post_status' => 'publish', 'post_excerpt' => $this->posted['order_comments'], 'post_author' => 1);
             $order_id = 0;
             if (isset(jigoshop_session::instance()->order_awaiting_payment) && jigoshop_session::instance()->order_awaiting_payment > 0) {
                 $order_id = absint(jigoshop_session::instance()->order_awaiting_payment);
                 $terms = wp_get_object_terms($order_id, 'shop_order_status', array('fields' => 'slugs'));
                 $order_status = isset($terms[0]) ? $terms[0] : 'pending';
                 // Resume the unpaid order if its pending
                 if ($order_status == 'pending' || $order_status == 'failed') {
                     $create_new_order = false;
                     $order_data['ID'] = $order_id;
                     wp_update_post($order_data);
                 }
             }
             if ($create_new_order) {
                 $order_id = wp_insert_post($order_data);
             }
             if (is_wp_error($order_id) || $order_id === 0) {
                 jigoshop::add_error(__('Error: Unable to create order. Please try again.', 'jigoshop'));
                 return false;
             }
             // Update post meta
             update_post_meta($order_id, 'order_data', $data);
             update_post_meta($order_id, 'order_key', uniqid('order_'));
             update_post_meta($order_id, 'customer_user', (int) $user_id);
             update_post_meta($order_id, 'order_items', $order_items);
             wp_set_object_terms($order_id, 'pending', 'shop_order_status');
             $order = new jigoshop_order($order_id);
             /* Coupon usage limit */
             foreach ($data['order_discount_coupons'] as $coupon) {
                 $coupon_id = JS_Coupons::get_coupon_post_id($coupon['code']);
                 if ($coupon_id !== false) {
                     $usage_count = get_post_meta($coupon_id, 'usage', true);
                     $usage_count = empty($usage_count) ? 1 : $usage_count + 1;
                     update_post_meta($coupon_id, 'usage', $usage_count);
                 }
             }
             if ($create_new_order) {
                 do_action('jigoshop_new_order', $order_id);
             } else {
                 do_action('jigoshop_resume_order', $order_id);
             }
             do_action('jigoshop_checkout_update_order_meta', $order_id, $this->posted);
             // can't just simply check needs_payment() here, as paypal may have force payment set to true
             if (self::process_gateway($gateway)) {
                 // Store Order ID in session so it can be re-used after payment failure
                 jigoshop_session::instance()->order_awaiting_payment = $order_id;
                 // Process Payment
                 $result = $gateway->process_payment($order_id);
                 // Redirect to success/confirmation/payment page
                 if ($result['result'] == 'success') {
                     return $result;
                 }
                 return false;
             } else {
                 // No payment was required for order
                 $order->payment_complete();
                 // Empty the Cart
                 jigoshop_cart::empty_cart();
                 // Redirect to success/confirmation/payment page
                 $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
                 return array('result' => 'redirect', 'redirect' => $checkout_redirect);
             }
         }
     }
     return true;
 }
Esempio n. 7
0
	/**
	 * Process the payment and return the result
	 **/
	function process_payment( $order_id ) {
		
		$order = &new jigoshop_order( $order_id );
		
		// Mark as on-hold (we're awaiting the cheque)
		$order->update_status('on-hold', __('Awaiting cheque payment', 'jigoshop'));
		
		// Remove cart
		jigoshop_cart::empty_cart();
			
		// Return thankyou redirect
		return array(
			'result' 	=> 'success',
			'redirect'	=> add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('jigoshop_thanks_page_id'))))
		);
		
	}
Esempio n. 8
0
function jigoshop_clear_cart_after_payment( $url = false ) {
	
	if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) :
		
		$order = &new jigoshop_order($_SESSION['order_awaiting_payment']);
		
		if ($order->id > 0 && ($order->status=='completed' || $order->status=='processing')) :
			
			jigoshop_cart::empty_cart();
			
			unset($_SESSION['order_awaiting_payment']);
			
		endif;
		
	endif;
	
}
 function receipt_kaznachey($order_id)
 {
     $this->kaznachey_init();
     $order = new jigoshop_order($order_id);
     $sum = $qty = 0;
     foreach ($order->items as $item) {
         $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($item['id']), 'large');
         $request['Products'][] = array("ProductId" => $item['id'], "ProductName" => $item['name'], "ProductPrice" => $item['cost'], "ProductItemsNum" => $item['qty'], "ImageUrl" => isset($thumb[0]) ? $thumb[0] : '');
         $sum += $item['cost'] * $item['qty'];
         $qty += $item['qty'];
     }
     // Доставка
     if (jigoshop_shipping::is_enabled()) {
         if ($order->order_shipping > 0) {
             $request['Products'][] = array("ProductId" => 1, "ProductName" => 'Доставка', "ProductPrice" => $order->order_shipping, "ProductItemsNum" => 1, "ImageUrl" => '');
             $sum += $order->order_shipping;
             $qty += 1;
         }
     }
     $request["MerchantGuid"] = $this->merchantGuid;
     $request['SelectedPaySystemId'] = $_COOKIE['cc_types'] ? $_COOKIE['cc_types'] : $this->GetMerchnatInfo(false, true);
     $request['Currency'] = $this->currency;
     $request['Language'] = $this->language;
     $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
     $request['PaymentDetails'] = array("EMail" => $order->billing_email, "PhoneNumber" => $order->billing_phone, "MerchantInternalPaymentId" => $order->id, "MerchantInternalUserId" => $order->user_id, "StatusUrl" => add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, get_permalink($checkout_redirect))), "ReturnUrl" => get_permalink($checkout_redirect), "CustomMerchantInfo" => "", "BuyerCountry" => $order->billing_country, "BuyerFirstname" => $order->billing_first_name, "BuyerPatronymic" => '', "BuyerLastname" => $order->billing_last_name, "BuyerStreet" => $order->billing_address_1, "BuyerZone" => $order->billing_state, "BuyerZip" => '', "BuyerCity" => $order->billing_city, "DeliveryFirstname" => $order->shipping_first_name, "DeliveryPatronymic" => '', "DeliveryLastname" => $order->shipping_last_name, "DeliveryZip" => '', "DeliveryCountry" => $order->shipping_country, "DeliveryStreet" => $order->shipping_address_1, "DeliveryCity" => $order->shipping_city, "DeliveryZone" => $order->shipping_state);
     $request["Signature"] = md5(strtoupper($this->merchantGuid) . number_format($sum, 2, ".", "") . $request["SelectedPaySystemId"] . $request["PaymentDetails"]["EMail"] . $request["PaymentDetails"]["PhoneNumber"] . $request["PaymentDetails"]["MerchantInternalUserId"] . $request["PaymentDetails"]["MerchantInternalPaymentId"] . strtoupper($request["Language"]) . strtoupper($request["Currency"]) . strtoupper($this->merchnatSecretKey));
     $response = $this->sendRequestKaznachey(json_encode($request), "CreatePaymentEx");
     $result = json_decode($response, true);
     if ($result['ErrorCode'] != 0) {
         wp_redirect(home_url());
         exit;
     }
     echo base64_decode($result["ExternalForm"]);
     jigoshop_cart::empty_cart();
     exit;
 }
Esempio n. 10
0
function jigoshop_clear_cart_after_payment($url = false)
{
    if (isset(jigoshop_session::instance()->order_awaiting_payment) && jigoshop_session::instance()->order_awaiting_payment > 0) {
        $order = new jigoshop_order(jigoshop_session::instance()->order_awaiting_payment);
        if ($order->id > 0 && ($order->status == 'completed' || $order->status == 'processing')) {
            jigoshop_cart::empty_cart();
            unset(jigoshop_session::instance()->order_awaiting_payment);
        }
    }
}
Esempio n. 11
0
 /**
  * Process the payment and return the result
  **/
 function process_payment($order_id)
 {
     $order = new jigoshop_order($order_id);
     $order->update_status('waiting-for-payment', __('Awaiting Bank Transfer', 'jigoshop'));
     jigoshop_cart::empty_cart();
     $checkout_redirect = apply_filters('jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks'));
     return array('result' => 'success', 'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink($checkout_redirect))));
 }
	/** Process the checkout after the confirm order button is pressed */
	function process_checkout() {
	
		global $wpdb;
		
		do_action('jigoshop_before_checkout_process');
		
		if (isset($_POST) && $_POST && !isset($_POST['login'])) :

			jigoshop_cart::calculate_totals();
			
			jigoshop::verify_nonce('process_checkout');
			
			if (sizeof(jigoshop_cart::$cart_contents)==0) :
				jigoshop::add_error( sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage &rarr;</a>','jigoshop'), home_url()) );
			endif;
						
			// Checkout fields
			$this->posted['shiptobilling'] = isset($_POST['shiptobilling']) ? jigowatt_clean($_POST['shiptobilling']) : '';
			$this->posted['payment_method'] = isset($_POST['payment_method']) ? jigowatt_clean($_POST['payment_method']) : '';
			$this->posted['shipping_method'] = isset($_POST['shipping_method']) ? jigowatt_clean($_POST['shipping_method']) : '';
			$this->posted['order_comments'] = isset($_POST['order_comments']) ? jigowatt_clean($_POST['order_comments']) : '';
			$this->posted['terms'] = isset($_POST['terms']) ? jigowatt_clean($_POST['terms']) : '';
			$this->posted['createaccount'] = isset($_POST['createaccount']) ? jigowatt_clean($_POST['createaccount']) : '';
			$this->posted['account-username'] = isset($_POST['account-username']) ? jigowatt_clean($_POST['account-username']) : '';
			$this->posted['account-password'] = isset($_POST['account-password']) ? jigowatt_clean($_POST['account-password']) : '';
			$this->posted['account-password-2'] = isset($_POST['account-password-2']) ? jigowatt_clean($_POST['account-password-2']) : '';
			
			if (jigoshop_cart::ship_to_billing_address_only()) $this->posted['shiptobilling'] = 'true';
			
			// Billing Information
			foreach ($this->billing_fields as $field) :
				
				$this->posted[$field['name']] = isset($_POST[$field['name']]) ? jigowatt_clean($_POST[$field['name']]) : '';
				
				// Format
				if (isset($field['format'])) switch ( $field['format'] ) :
					case 'postcode' : $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']])); break;
				endswitch;
				
				// Required
				if ( isset($field['required']) && $field['required'] && empty($this->posted[$field['name']]) ) jigoshop::add_error( $field['label'] . __(' (billing) is a required field.','jigoshop') );
	
				// Validation
				if (isset($field['validate']) && !empty($this->posted[$field['name']])) switch ( $field['validate'] ) :
					case 'phone' :
						if (!jigoshop_validation::is_phone( $this->posted[$field['name']] )) : jigoshop::add_error( $field['label'] . __(' (billing) is not a valid number.','jigoshop') ); endif;
					break;
					case 'email' :
						if (!jigoshop_validation::is_email( $this->posted[$field['name']] )) : jigoshop::add_error( $field['label'] . __(' (billing) is not a valid email address.','jigoshop') ); endif;
					break;
					case 'postcode' :
						if (!jigoshop_validation::is_postcode( $this->posted[$field['name']], $_POST['billing-country'] )) : jigoshop::add_error( $field['label'] . __(' (billing) is not a valid postcode/ZIP.','jigoshop') ); 
						else :
							$this->posted[$field['name']] = jigoshop_validation::format_postcode( $this->posted[$field['name']], $_POST['billing-country'] );
						endif;
					break;
				endswitch;
				
			endforeach;
			
			// Shipping Information
			if (jigoshop_cart::needs_shipping() && !jigoshop_cart::ship_to_billing_address_only() && empty($this->posted['shiptobilling'])) :
				
				foreach ($this->shipping_fields as $field) :
					if (isset( $_POST[$field['name']] )) $this->posted[$field['name']] = jigowatt_clean($_POST[$field['name']]); else $this->posted[$field['name']] = '';
					
					// Format
					if (isset($field['format'])) switch ( $field['format'] ) :
						case 'postcode' : $this->posted[$field['name']] = strtolower(str_replace(' ', '', $this->posted[$field['name']])); break;
					endswitch;
					
					// Required
					if ( isset($field['required']) && $field['required'] && empty($this->posted[$field['name']]) ) jigoshop::add_error( $field['label'] . __(' (shipping) is a required field.','jigoshop') );
		
					// Validation
					if (isset($field['validate']) && !empty($this->posted[$field['name']])) switch ( $field['validate'] ) :
						case 'postcode' :
							if (!jigoshop_validation::is_postcode( $this->posted[$field['name']], $this->posted['shipping-country'] )) : jigoshop::add_error( $field['label'] . __(' (shipping) is not a valid postcode/ZIP.','jigoshop') ); 
							else :
								$this->posted[$field['name']] = jigoshop_validation::format_postcode( $this->posted[$field['name']], $this->posted['shipping-country'] );
							endif;
						break;
					endswitch;
					
				endforeach;
			
			endif;

			if (is_user_logged_in()) :
				$this->creating_account = false;
			elseif (isset($this->posted['createaccount']) && $this->posted['createaccount']) :
				$this->creating_account = true;
			elseif ($this->must_create_account) :
				$this->creating_account = true;
			else :
				$this->creating_account = false;
			endif;
			
			if ($this->creating_account && !$user_id) :
			
				if ( empty($this->posted['account-username']) ) jigoshop::add_error( __('Please enter an account username.','jigoshop') );
				if ( empty($this->posted['account-password']) ) jigoshop::add_error( __('Please enter an account password.','jigoshop') );
				if ( $this->posted['account-password-2'] !== $this->posted['account-password'] ) jigoshop::add_error( __('Passwords do not match.','jigoshop') );
			
				// Check the username
				if ( !validate_username( $this->posted['account-username'] ) ) :
					jigoshop::add_error( __('Invalid email/username.','jigoshop') );
				elseif ( username_exists( $this->posted['account-username'] ) ) :
					jigoshop::add_error( __('An account is already registered with that username. Please choose another.','jigoshop') );
				endif;
				
				// Check the e-mail address
				if ( email_exists( $this->posted['billing-email'] ) ) :
					jigoshop::add_error( __('An account is already registered with your email address. Please login.','jigoshop') );
				endif;
			endif;
			
			// Terms
			if (!isset($_POST['update_totals']) && empty($this->posted['terms']) && get_option('jigoshop_terms_page_id')>0 ) jigoshop::add_error( __('You must accept our Terms &amp; Conditions.','jigoshop') );
			
			if (jigoshop_cart::needs_shipping()) :
			
				// Shipping Method
				$available_methods = jigoshop_shipping::get_available_shipping_methods();
				if (!isset($available_methods[$this->posted['shipping_method']])) :
					jigoshop::add_error( __('Invalid shipping method.','jigoshop') );
				endif;	
			
			endif;	
			
			if (jigoshop_cart::needs_payment()) :
				// Payment Method
				$available_gateways = jigoshop_payment_gateways::get_available_payment_gateways();
				if (!isset($available_gateways[$this->posted['payment_method']])) :
					jigoshop::add_error( __('Invalid payment method.','jigoshop') );
				else :
					// Payment Method Field Validation
					$available_gateways[$this->posted['payment_method']]->validate_fields();
				endif;
			endif;
					
			if (!isset($_POST['update_totals']) && jigoshop::error_count()==0) :
				
				$user_id = get_current_user_id();
				
				while (1) :
					
					// Create customer account and log them in
					if ($this->creating_account && !$user_id) :
				
						$reg_errors = new WP_Error();
						do_action('register_post', $this->posted['billing-email'], $this->posted['billing-email'], $reg_errors);
						$errors = apply_filters( 'registration_errors', $reg_errors, $this->posted['billing-email'], $this->posted['billing-email'] );
				
		                // if there are no errors, let's create the user account
						if ( !$reg_errors->get_error_code() ) :
		
			                $user_pass = $this->posted['account-password'];
			                $user_id = wp_create_user( $this->posted['account-username'], $user_pass, $this->posted['billing-email'] );
			                if ( !$user_id ) {
			                	jigoshop::add_error( sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !', 'jigoshop'), get_option('admin_email')));
			                    break;
			                }
		
		                    // Change role
		                    wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ;
		
		                    // send the user a confirmation and their login details
		                    wp_new_user_notification( $user_id, $user_pass );
		
		                    // set the WP login cookie
		                    $secure_cookie = is_ssl() ? true : false;
		                    wp_set_auth_cookie($user_id, true, $secure_cookie);
						
						else :
							jigoshop::add_error( $reg_errors->get_error_message() );
		                	break;                    
						endif;
						
					endif;

					// Get shipping/billing
					if ( !empty($this->posted['shiptobilling']) ) :
					
						$shipping_first_name = $this->posted['billing-first_name'];
						$shipping_last_name = $this->posted['billing-last_name'];
						$shipping_company = $this->posted['billing-company'];
						$shipping_address_1 = $this->posted['billing-address'];
						$shipping_address_2 = $this->posted['billing-address-2'];
						$shipping_city = $this->posted['billing-city'];							
						$shipping_state = $this->posted['billing-state'];
						$shipping_postcode = $this->posted['billing-postcode'];	
						$shipping_country = $this->posted['billing-country'];
						
					elseif ( jigoshop_cart::needs_shipping() ) :
								
						$shipping_first_name = $this->posted['shipping-first_name'];
						$shipping_last_name = $this->posted['shipping-last_name'];
						$shipping_company = $this->posted['shipping-company'];
						$shipping_address_1 = $this->posted['shipping-address'];
						$shipping_address_2 = $this->posted['shipping-address-2'];
						$shipping_city = $this->posted['shipping-city'];							
						$shipping_state = $this->posted['shipping-state'];
						$shipping_postcode = $this->posted['shipping-postcode'];	
						$shipping_country = $this->posted['shipping-country'];
						
					endif;
					
					// Save billing/shipping to user meta fields
					if ($user_id>0) :
						update_user_meta( $user_id, 'billing-first_name', $this->posted['billing-first_name'] );
						update_user_meta( $user_id, 'billing-last_name', $this->posted['billing-last_name'] );
						update_user_meta( $user_id, 'billing-company', $this->posted['billing-company'] );
						update_user_meta( $user_id, 'billing-email', $this->posted['billing-email'] );
						update_user_meta( $user_id, 'billing-address', $this->posted['billing-address'] );
						update_user_meta( $user_id, 'billing-address-2', $this->posted['billing-address-2'] );
						update_user_meta( $user_id, 'billing-city', $this->posted['billing-city'] );
						update_user_meta( $user_id, 'billing-postcode', $this->posted['billing-postcode'] );
						update_user_meta( $user_id, 'billing-country', $this->posted['billing-country'] );
						update_user_meta( $user_id, 'billing-state', $this->posted['billing-state'] );
						update_user_meta( $user_id, 'billing-phone', $this->posted['billing-phone'] );

						if ( empty($this->posted['shiptobilling']) && jigoshop_cart::needs_shipping() ) :
							update_user_meta( $user_id, 'shipping-first_name', $this->posted['shipping-first_name'] );
							update_user_meta( $user_id, 'shipping-last_name', $this->posted['shipping-last_name'] );
							update_user_meta( $user_id, 'shipping-company', $this->posted['shipping-company'] );
							update_user_meta( $user_id, 'shipping-address', $this->posted['shipping-address'] );
							update_user_meta( $user_id, 'shipping-address-2', $this->posted['shipping-address-2'] );
							update_user_meta( $user_id, 'shipping-city', $this->posted['shipping-city'] );
							update_user_meta( $user_id, 'shipping-postcode', $this->posted['shipping-postcode'] );
							update_user_meta( $user_id, 'shipping-country', $this->posted['shipping-country'] );
							update_user_meta( $user_id, 'shipping-state', $this->posted['shipping-state'] );
						elseif ( $this->posted['shiptobilling'] && jigoshop_cart::needs_shipping() ) :
							update_user_meta( $user_id, 'shipping-first_name', $this->posted['billing-first_name'] );
							update_user_meta( $user_id, 'shipping-last_name', $this->posted['billing-last_name'] );
							update_user_meta( $user_id, 'shipping-company', $this->posted['billing-company'] );
							update_user_meta( $user_id, 'shipping-address', $this->posted['billing-address'] );
							update_user_meta( $user_id, 'shipping-address-2', $this->posted['billing-address-2'] );
							update_user_meta( $user_id, 'shipping-city', $this->posted['billing-city'] );
							update_user_meta( $user_id, 'shipping-postcode', $this->posted['billing-postcode'] );
							update_user_meta( $user_id, 'shipping-country', $this->posted['billing-country'] );
							update_user_meta( $user_id, 'shipping-state', $this->posted['billing-state'] );
						endif;
						
					endif;
					
					// Create Order (send cart variable so we can record items and reduce inventory). Only create if this is a new order, not if the payment was rejected last time.
					
					$_tax = new jigoshop_tax();
					
					$order_data = array(
						'post_type' => 'shop_order',
						'post_title' => 'Order &ndash; '.date('F j, Y @ h:i A'),
						'post_status' => 'publish',
						'post_excerpt' => $this->posted['order_comments'],
						'post_author' => 1
					);
					
					// Order meta data
					$data = array();
					$data['billing_first_name'] 	= $this->posted['billing-first_name'];
					$data['billing_last_name'] 		= $this->posted['billing-last_name'];
					$data['billing_company'] 		= $this->posted['billing-company'];
					$data['billing_address_1'] 		= $this->posted['billing-address'];
					$data['billing_address_2'] 		= $this->posted['billing-address-2'];
					$data['billing_city'] 			= $this->posted['billing-city'];
					$data['billing_postcode'] 		= $this->posted['billing-postcode'];
					$data['billing_country'] 		= $this->posted['billing-country'];
					$data['billing_state'] 			= $this->posted['billing-state'];
					$data['billing_email']			= $this->posted['billing-email'];
					$data['billing_phone']			= $this->posted['billing-phone'];
					$data['shipping_first_name'] 	= $shipping_first_name;
					$data['shipping_last_name'] 	= $shipping_last_name;
					$data['shipping_company']	 	= $shipping_company;
					$data['shipping_address_1']		= $shipping_address_1;
					$data['shipping_address_2']		= $shipping_address_2;
					$data['shipping_city']			= $shipping_city;
					$data['shipping_postcode']		= $shipping_postcode;
					$data['shipping_country']		= $shipping_country;
					$data['shipping_state']			= $shipping_state;
					$data['shipping_method']		= $this->posted['shipping_method'];
					$data['payment_method']			= $this->posted['payment_method'];
					$data['order_subtotal']			= number_format(jigoshop_cart::$subtotal_ex_tax, 2, '.', '');
					$data['order_shipping']			= number_format(jigoshop_cart::$shipping_total, 2, '.', '');
					$data['order_discount']			= number_format(jigoshop_cart::$discount_total, 2, '.', '');
					$data['order_tax']				= number_format(jigoshop_cart::$tax_total, 2, '.', '');
					$data['order_shipping_tax']		= number_format(jigoshop_cart::$shipping_tax_total, 2, '.', '');
					$data['order_total']			= number_format(jigoshop_cart::$total, 2, '.', '');
					
					// Cart items
					$order_items = array();
					
					foreach (jigoshop_cart::$cart_contents as $item_id => $values) :
						
						$_product = $values['data'];
			
						// Calc item tax to store
						$rate = '';
						if ( $_product->is_taxable()) :
							$rate = $_tax->get_rate( $_product->data['tax_class'] );
						endif;
						
						$order_items[] = array(
					 		'id' 		=> $item_id,
					 		'name' 		=> $_product->get_title(),
					 		'qty' 		=> (int) $values['quantity'],
					 		'cost' 		=> $_product->get_price_excluding_tax(),
					 		'taxrate' 	=> $rate
					 	);
					 	
					 	// Check stock levels
					 	if ($_product->managing_stock()) :
							if (!$_product->is_in_stock() || !$_product->has_enough_stock( $values['quantity'] )) :
								
								jigoshop::add_error( sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologise for any inconvenience caused.', 'jigoshop'), $_product->get_title() ) );
		                		break;
								
							endif;
						else :
						
							if (!$_product->is_in_stock()) :
							
								jigoshop::add_error( sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologise for any inconvenience caused.', 'jigoshop'), $_product->get_title() ) );
		                		break;

							endif;
							
						endif;
					 	
					endforeach;
					
					if (jigoshop::error_count()>0) break;
					
					// Insert or update the post data
					if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) :
						
						$order_id = (int) $_SESSION['order_awaiting_payment'];
						$order_data['ID'] = $order_id;
						wp_update_post( $order_data );
					
					else :
						$order_id = wp_insert_post( $order_data );
						
						if (is_wp_error($order_id)) :
							jigoshop::add_error( 'Error: Unable to create order. Please try again.' );
			                break;
						endif;
					endif;

					// Update post meta
					update_post_meta( $order_id, 'order_data', $data );
					update_post_meta( $order_id, 'order_key', uniqid('order_') );
					update_post_meta( $order_id, 'customer_user', (int) $user_id );
					update_post_meta( $order_id, 'order_items', $order_items );
					wp_set_object_terms( $order_id, 'pending', 'shop_order_status' );
					
					$order = &new jigoshop_order($order_id);
					
					// Inserted successfully 
					do_action('jigoshop_new_order', $order_id);

					if (jigoshop_cart::needs_payment()) :
						
						// Store Order ID in session so it can be re-used after payment failure
						$_SESSION['order_awaiting_payment'] = $order_id;
					
						// Process Payment
						$result = $available_gateways[$this->posted['payment_method']]->process_payment( $order_id );
						
						// Redirect to success/confirmation/payment page
						if ($result['result']=='success') :
						
							if (is_ajax()) : 
								ob_clean();
								echo json_encode($result);
								exit;
							else :
								wp_safe_redirect( $result['redirect'] );
								exit;
							endif;
							
						endif;
					
					else :
					
						// No payment was required for order
						$order->payment_complete();
						
						// Empty the Cart
						jigoshop_cart::empty_cart();
						
						// Redirect to success/confirmation/payment page
						if (is_ajax()) : 
							ob_clean();
							echo json_encode( array('redirect'	=> get_permalink(get_option('jigoshop_thanks_page_id'))) );
							exit;
						else :
							wp_safe_redirect( get_permalink(get_option('jigoshop_thanks_page_id')) );
							exit;
						endif;
						
					endif;
					
					// Break out of loop
					break;
				
				endwhile;
	
			endif;
			
			// If we reached this point then there were errors
			if (is_ajax()) : 
				ob_clean();
				jigoshop::show_messages();
				exit;
			else :
				jigoshop::show_messages();
			endif;
		
		endif;
	}
Esempio n. 13
0
	/**
	 * Process the payment and return the result
	 **/
	function process_payment( $order_id ) {
		
		$order = &new jigoshop_order( $order_id );
		$order->update_status('on-hold', __('Awaiting Bank Transfer', 'jigoshop'));
		jigoshop_cart::empty_cart();
			
		return array(
			'result' 	=> 'success',
			'redirect'	=> add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('jigoshop_thanks_page_id'))))
		);
		
	}