Post by straightlight » Tue Oct 03, 2017 10:27 pm

Followed is a training on how to send values with jQuery codes upon successful checkout process. Whenever a payment provider generates a schema in order to integrate in Opencart, here is a good straightforward example that you could implement into your ocmod or VQmod files from now on. Obviously, the variables will need to be changed accordingly as well as the schema size to fit what's according with your provider.

Dependable on this topic schema example: viewtopic.php?f=190&t=191761

Take note that this strategy requires at least Opencart v2.x releases or above:

1 - In your catalog/controller/checkout/success.php file,

find:

Code: Select all

$this->cart->clear();
add before:

Code: Select all

if (!empty($this->session->data['order_id'])) {
    $data['order_id'] = (int)$this->session->data['order_id'];
}
Then, at the end of the file,

find:

Code: Select all

$this->response->setOutput($this->load->view('common/success', $data));
	}
replace with:

Code: Select all

$this->response->setOutput($this->load->view('common/success', $data));
	}
	
	public function send() {
		$json = array();
		
		if (!empty($this->request->post['order_id'])) {
			$order_id = (int)$this->request->post['order_id'];
			
			$this->load->model('checkout/order');
			
			$this->load->model('catalog/product');
			
			$this->load->model('catalog/category');
			
			$order_info = $this->model_checkout_order->getOrder($order_id);
			
			$order_products = $this->model_checkout_order->getOrderProducts($order_id);
			
			$products_array = array();
			
			$i = 0;
			
			$tax_value = 0.00;
			
			foreach ($order_products as $order_product) {
				$product = $this->model_catalog_product->getProduct($order_product['product_id']);
				
				if (!empty($product['product_id'])) {
					$categories = $this->model_catalog_product->getCategories($product['product_id']);
					
					$categories_array = array();
					
					$j = 0;
					
					foreach ($categories as $category) {
						if (!empty($category['product_id']) && !empty($category['category_id'])) {
							$category_info = $this->model_catalog_category->getCategory($category['category_id']);
							
							if (!empty($category_info['category_id'])) {
								$categories_array[$category['product_id']][$j] = strip_tags(html_entity_decode($category['name'], ENT_QUOTES, 'UTF-8'));
								
								++$j;
							}
						}
					}
					
					if (!empty($categories_array[$product['product_id']][$i])) {
						$products_array[] = array('sku'					=> strip_tags(html_entity_decode($product['sku'], ENT_QUOTES, 'UTF-8')),
												  'name'				=> strip_tags(html_entity_decode($product['name'], ENT_QUOTES, 'UTF-8')),
												  'category'			=> strip_tags(html_entity_decode($categories_array[$product['product_id']][$i], ENT_QUOTES, 'UTF-8')),
												  'price'				=> strip_tags(html_entity_decode($order_product['price'], ENT_QUOTES, 'UTF-8')),
												  'quantity'			=> (int)$order_product['quantity'],
												 );
												 
						$tax_value = (float)$order_product['tax'];
					}
					
					++$i;
				}
			}
			
			if (!empty($products_array)) {
				$json[] = array('transactionId'					=> strip_tags(html_entity_decode((string)$order_info['invoice_prefix'] . $order_info['invoice_no'], ENT_QUOTES, 'UTF-8')),
								'transactionAffiliation'		=> strip_tags(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')),
								'transactionTotal'				=> strip_tags(html_entity_decode(((float)$order_info['total'] - (float)$tax_value), ENT_QUOTES, 'UTF-8')),
								'transactionTax'				=> strip_tags(html_entity_decode($tax_value, ENT_QUOTES, 'UTF-8')),
								'transactionShipping'			=> (!empty($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]]) ? (float)$this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]] : '0',
								'transactionProducts'			=> $products_array,
							   );
			}
			
			$this->response->addHeader('Content-Type: application/json');
			$this->response->setOutput(json_encode($json));
		}
	}
In catalog/view/theme/<your_theme>/common/success.tpl file,

Version 2.x:

find:

Code: Select all

<?php echo $footer; ?>
add below:

Code: Select all

<?php if (!empty($order_id)) { ?>
<script type="text/javascript"><!--
$(document).ready(function() {
	$.ajax({
		url: 'index.php?route=checkout/success/send',
		type: 'post',
		data: 'order_id=' + encodeURIComponent('<?php echo $order_id; ?>'),
		dataType: 'json',
		success: function(json) {
		        // The myJsonString can be removed IF the provider does not require
		        // the use of json strings but rather an array. If an array, simply use the
		        // json variable along with the provider's function.
			var myJsonString = JSON.stringify(json);
						
			// dataLayer and the push function are only for the purposes of a specific API.
			// It can be changed to other relatives when needed.
			dataLayer.push(myJsonString);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script> 
<?php } ?>
Version 3.x:

find:

Code: Select all

{{ footer }}
add below:

Code: Select all

{% if order_id %}
<script type="text/javascript"><!--
$(document).ready(function() {
	$.ajax({
		url: 'index.php?route=checkout/success/send',
		type: 'post',
		data: 'order_id=' + encodeURIComponent('{{ order_id }}'),
		dataType: 'json',
		success: function(json) {
		        // The myJsonString can be removed IF the provider does not require
		        // the use of json strings but rather an array. If an array, simply use the
		        // json variable along with the provider's function.
			var myJsonString = JSON.stringify(json);
						
			// dataLayer and the push function are only for the purposes of a specific API.
			// It can be changed to other relatives when needed.
			dataLayer.push(myJsonString);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script> 
{% endif %}
This should resolved the problem.

The most generated errors being found on Opencart forum originates from contributed programming.

Regards,
Straightlight


Guru Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Thu Oct 05, 2017 6:10 am

The post has been updated above in compliance of the invoice prefix and the invoice number.

The most generated errors being found on Opencart forum originates from contributed programming.

Regards,
Straightlight


Guru Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 9 guests