Page 1 of 1

[Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Tue Oct 03, 2017 10:27 pm
by straightlight
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.

Re: [Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Thu Oct 05, 2017 6:10 am
by straightlight
The post has been updated above in compliance of the invoice prefix and the invoice number.

Re: [Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Fri Mar 23, 2018 5:51 am
by brinquedoseducativos
Anyone have a tutorial to make a simple onepage custom checkout (from scratch)?
moto infantil,moto elétrica,moto elétrica infantil

Re: [Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Fri Mar 23, 2018 6:02 am
by straightlight
To make sure you're posting on the right topic, you're looking to do use a one page checkout with an API?

Re: [Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Sat Oct 20, 2018 5:42 pm
by grubybarti666
Ok but can you tell me what should I paste over here ? Especialy - "ORDER_ID" and "CUSTOMER_EMAIL"

<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
// WYMAGANE
"merchant_id":"12345",
"order_id": "ORDER_ID",
"email": "CUSTOMER_EMAIL",
"delivery_country": "COUNTRY_CODE",
"estimated_delivery_date": "YYYY-MM-DD",

// OPCJONALNE
"opt_in_style": "OPT_IN_STYLE"
});
});
}
</script>

Re: [Opencart - Tutorial v2.x +] - Checkout Success Page API

Posted: Sun Oct 21, 2018 1:07 am
by straightlight
grubybarti666 wrote:
Sat Oct 20, 2018 5:42 pm
Ok but can you tell me what should I paste over here ? Especialy - "ORDER_ID" and "CUSTOMER_EMAIL"

<script>
window.renderOptIn = function() {
window.gapi.load('surveyoptin', function() {
window.gapi.surveyoptin.render(
{
// WYMAGANE
"merchant_id":"12345",
"order_id": "ORDER_ID",
"email": "CUSTOMER_EMAIL",
"delivery_country": "COUNTRY_CODE",
"estimated_delivery_date": "YYYY-MM-DD",

// OPCJONALNE
"opt_in_style": "OPT_IN_STYLE"
});
});
}
</script>
Partial codes posted.