OpenCart 2.0 is a free web platform that enables users to quickly start an eCommerce website. With some settings and configurations , we can integrate OpenPay payment method to OpenCart platform.
To setup opencart,
1) Locally to test, follow this link - https://code.tutsplus.com/articles/from-beginner-to-advanced-in-opencart-installation--cms-20627
2) On a server follow either of the links below-
a) http://docs.opencart.com/installation/
b) https://blog.coingate.com/2017/02/install-opencart-guide/>/a>
To migrate OpenCart to a server follow this link - https://isenselabs.com/posts/step-by-step-opencart-migration-to-a-new-server
Now, we shall integrate OpenPay. Unzip the zipped folder provided to you by AOPEN. The directory contains all the necessary files required to integrate openpay payment system. These 8 files should be dropped into corresponding OpenCart directory as specified below.
1) admin_controller_openpay.php - opencart/admin/controller/extension/payment/admin_controller_openpay.php and rename it to openpay.php.
2) admin_language_openpay.php - opencart/admin/language/engb/extension/payment/admin_language_openpay.php and rename it to openpay.php.
3) admin_view_openpay.tpl - opencart/admin/view/template/extension/payment/admin_view_openpay.tpl and rename it to openpay.tpl
4) catalog_controller_openpay.php - opencart/catalog/controller/extension/payment/catalog_controller_openpay.php and rename it to openpay.php
5) catalog_language_openpay.php - opencart/catalog/language/engb/extension/payment/catalog_language_openpay.php and rename it to openpay.php
6) catalog_model_openpay.php - opencart/catalog/model/extension/payment/catalog_model_openpay.php and rename it to openpay.php
7) catalog_view_openpay.tpl - opencart/catalog/view/theme/default/template/extension/payment/catalog_view_openpay.tpl and rename it to openpay.tpl
Note: This is specific to openpay only
1) In opencart/catalog/controller/common/home.php, inside index() controller, add these lines
// this is for openpay : openpay assumes callback link will not have existing
// query variables and just strips or ignore them:
// This script simply takes all the $_REQUEST vars and redirects them to the real callback:
if(isset($_GET["status"]) && isset($_GET["planid"]) && isset($_GET["orderid"])){
$callbackurl = $_SERVER['SERVER_NAME']."/index.php?route=extension/payment/openpay/callback";
$query = "";
foreach($_REQUEST as $key => $value) {
$query .= '&' . $key . '=' . $value;
}
header("Location: http://$callbackurl$query");
}
2) Add this jquery snippet at the bottom of the page in opencart/catalog/view/theme/default/template/checkout/payment_method.tpl
Note: openpay does not accept orders less than $100. The following code should be implemented to set up this functionality.
<input type="hidden" value="<?php echo $total_amount;?>" id="total_amount" />
<script>
$(document).ready(function() {
var total_amount = $("#total_amount").val();
var payment_method_value = $('input[name="payment_method"]').val();
check_valid_amount(payment_method_value);
$('input[type="radio"]').change(function(e) {
payment_method_value = this.value;
if(payment_method_value != "openpay" ){
$("#total_amount_less").hide();
}
check_valid_amount(payment_method_value);
});
$(document).on("click",".close-alert",function(e) {
e.preventDefault();
$("#total_amount_less").hide();
});
function check_valid_amount(payment_method_value){
if(payment_method_value == "openpay" && total_amount <100 ){
$("#button-payment-method").attr('disabled','disabled');
$("#total_amount_less").css({"display":"block"});
$("#total_amount").next().html('<div class="alert alert-danger alert-dismissable" id="total_amount_less"> <span class="close-alert" aria-label="close">×</span> openpay does not accept amount less than $100. </div>');
} else {
$("#button-payment-method").removeAttr('disabled');
}
}
});
</script>
3) In opencart/catalog/controller/checkout/payment_method.php, inside index() controller at the top, add this code
$data["total_amount"] = $this->cart->getTotal();
4) In opencart/admin/controller/sale/order.php, inside info() controller, add this code
// this is for openpay planid
$order_query_plan_id = $this->db->query("SELECT plan_id from `" . DB_PREFIX . "order` WHERE order_id = '" . (int)$order_id . "'");
if ($order_query_plan_id->num_rows) {
$data['plan_id'] = $order_query_plan_id->row['plan_id'];
}
$data['payment_mode'] = $order_info['payment_code'];
5) In admin/view/template/sale/order_info.tpl, before the <scripts> add these lines,
<div id="ship_msg" style="display:none"> <span class="close-alert" aria-label="close">×</span> Cannot ship at the moment. </div>
<input type="hidden" value="<?php echo $payment_mode ; ?>" id="payment_mode"/>
and in <scripts>, we need to do more modifications.
Because, we need to send OnlineOrderDisptchStatus when the status is set to shipped, we need to make sure to update to history only when the ResponseOnlineOrderDisptchStatus is true(0).
$('#button-history').on('click', function() {
var status_id = $('select[name="order_status_id"]').val();
// this is for openpay
$("#ship_msg .close-alert").on("click",function(e) {
e.preventDefault();
$("#ship_msg").hide();
});
var payment_mode = $("#payment_mode").val();
if(payment_mode == "openpay" && status_id == '3') {
$.ajax({
url: 'index.php?route=extension/payment/openpay/getXmlJSONRequest&token=<?php echo $token; ?>&order_id=<?php echo $order_id; ?>&plan_id=<?php echo $plan_id; ?>&status_id=' + status_id,
type: 'post',
dataType: 'json',
success: function(res) {
console.log(res['status']);
if(res['status'] == 0){
addToHistory();
console.log("succesfully dispatched");
$("#ship_msg").hide();
} else {
$("#ship_msg").show();
$("#ship_msg").css({"background":"#f2dede", "color":"#a94442", "height":"30px", "text-align":"center", "line-height":"30px"});
}
},
error: function() {
console.log("error");
}
});
} else {
addToHistory();
$("#ship_msg").hide();
}
});
function addToHistory() {
$.ajax({
url: '<?php echo $catalog; ?>index.php?route=api/order/history&token=' + token + '&store_id=<?php echo $store_id; ?>&order_id=<?php echo $order_id; ?>',
type: 'post',
dataType: 'json',
data: 'order_status_id=' + encodeURIComponent($('select[name=\'order_status_id\']').val()) + '¬ify=' + ($('input[name=\'notify\']').prop('checked') ? 1 : 0) + '&override=' + ($('input[name=\'override\']').prop('checked') ? 1 : 0) + '&append=' + ($('input[name=\'append\']').prop('checked') ? 1 : 0) + '&comment=' + encodeURIComponent($('textarea[name=\'comment\']').val()),
beforeSend: function() {
$('#button-history').button('loading');
},
complete: function() {
$('#button-history').button('reset');
},
success: function(json) {
$('.alert').remove();
if (json['error']) {
$('#history').before('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">×</button></div>');
}
if (json['success']) {
$('#history').load('index.php?route=sale/order/history&token=<?php echo $token; ?>&order_id=<?php echo $order_id; ?>');
$('#history').before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">×</button></div>');
$('textarea[name=\'comment\']').val('');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
Module Configuration
1) Go to https:<yourstore>/admin
2) login username and password
3) On the left tab, click "Extensions" and select "Extensions from the dropdown" .
4) From "Choose the extension type" , select Payments.
5) Search OpenPay and click on "Edit" button.
6) Fill in the blanks
a) merchantid : JAM AUTH Token supplied by OpenPay
b) Authkey: is the part of merchantid, which is after "|" or as supplied by OpenPay.
c) API URL: provided by OpenPay
d) HandOver URL : provided by OpenPay
There are three states defined by openpay: success, fail and cancel.
The success url will
- Redirect the customer to a success page
- Send an order email confirmation to the customer (currently blocked: https://cloud.google.com/compute/docs/tutorials/sending-mail/)
The cancel and fail url will:
- Put the order status into 'cancelled' or 'failed'
In the admin, when browsing orders, setting an order Status as ‘shipped’ - by selecting ‘shipped’ and then ‘Add History’, triggers an Openpay OnlineDispatchPlan. On success, it will add a row with "shipped" status in the "Order History" table. On error the error will be displayed on the admin interface.
Comments
0 comments
Please sign in to leave a comment.