175 lines
5.0 KiB
JavaScript
175 lines
5.0 KiB
JavaScript
|
$(document).ready(function() {
|
||
|
//online pay onload submit
|
||
|
if(document.getElementById('onlinPayFormSubmit')!=null){
|
||
|
//document.getElementById('onlinPayFormSubmit').submit();
|
||
|
onM1PayReady();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
function paymentCancel() {
|
||
|
document.getElementById("paymentStatus").value = "cancel";
|
||
|
document.getElementById('payGwId').submit();
|
||
|
}
|
||
|
|
||
|
|
||
|
var channel = '';
|
||
|
|
||
|
function onM1PayReady() {
|
||
|
var emptyInputs = [];
|
||
|
var res = $('#result');
|
||
|
var merchantOrderNo = $('#merchantOrderNo');
|
||
|
var exchangeOrderNo = $('#exchangeOrderNo');
|
||
|
var transactionAmount = $('#transactionAmount');
|
||
|
var emailAddress = $('#emailAddress');
|
||
|
var phoneNumber = $('#phoneNumber');
|
||
|
var transactionCurrency = "MYR";
|
||
|
var productDescription = $('#productDescription');
|
||
|
var serialNumber = $('#serialNumber');
|
||
|
var pinNumber = $('#pinNumber');
|
||
|
var fpxBankId = $('#fpxBankId');
|
||
|
|
||
|
if (!merchantOrderNo.val()) {
|
||
|
emptyInputs.push('merchantOrderNo')
|
||
|
|
||
|
}
|
||
|
if (!transactionAmount.val()) {
|
||
|
emptyInputs.push('transactionAmount');
|
||
|
|
||
|
}
|
||
|
if (!emailAddress.val()) {
|
||
|
emptyInputs.push('emailAddress');
|
||
|
}
|
||
|
|
||
|
if (!productDescription.val()) {
|
||
|
emptyInputs.push('productDescription');
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
if (emptyInputs.length == 0) {
|
||
|
|
||
|
res.text("Sending to payment to merchant host...");
|
||
|
console.log("Sending to payment to merchant host...");
|
||
|
console.log(phoneNumber.val());
|
||
|
console.log(fpxBankId.val());
|
||
|
m1pay.init({
|
||
|
merchantHostURL: 'https://gateway.m1pay.com.my/eipay/api/m1pay-transaction',
|
||
|
// merchantHostURL: 'http://localhost:8885/api/m1pay-transaction',
|
||
|
merchantOrderNo: merchantOrderNo.val(),
|
||
|
exchangeOrderNo: exchangeOrderNo.val(),
|
||
|
transactionCurrency: transactionCurrency,
|
||
|
skipConfirmation: true,
|
||
|
transactionAmount: transactionAmount.val(),
|
||
|
emailAddress: emailAddress.val(),
|
||
|
productDescription: productDescription.val(),
|
||
|
phoneNumber: phoneNumber.val(),
|
||
|
fpxBankId: fpxBankId.val(),
|
||
|
fpxBank: 1,
|
||
|
channel: channel
|
||
|
|
||
|
});
|
||
|
m1pay.on("payment.success", function (response) {
|
||
|
res.text('Return Code: ' + response);
|
||
|
});
|
||
|
m1pay.on("payment.error", function (response, error) {
|
||
|
console.log("payment.error");
|
||
|
//alert(JSON.stringify(error + ' ' + response.detail));
|
||
|
//document.getElementById("response").innerHTML = 'Error Number: '+response + '<br> Detail: ' + error;
|
||
|
//var res = $('#result');
|
||
|
res.css('color', 'red').css('font-weight', 'Bold');
|
||
|
res.text('Return Code: ' + response + ' Error : ' + error);
|
||
|
//return false;
|
||
|
});
|
||
|
m1pay.on("payment.networkError", function (error) {
|
||
|
//TODO handle this
|
||
|
res.text('Network error happens');
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
for (i = 0; i < emptyInputs.length; i++) {
|
||
|
var elementId = emptyInputs[i] + '_invalid';
|
||
|
console.log(elementId);
|
||
|
//document.getElementById(elementId).style.display = "block";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
window.m1pay = {};
|
||
|
|
||
|
function successCallback(transaction) {}
|
||
|
|
||
|
function failureCallback(transaction, error) {}
|
||
|
|
||
|
function networkFailureCallback(error) {}
|
||
|
|
||
|
|
||
|
m1pay.init = function (merchantInfo) {
|
||
|
var formData = new URLSearchParams();
|
||
|
Object.keys(merchantInfo).forEach(key => formData.append(key, merchantInfo[key]))
|
||
|
|
||
|
xhr = new XMLHttpRequest();
|
||
|
|
||
|
xhr.open('POST', merchantInfo.merchantHostURL);
|
||
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
||
|
xhr.onload = function() {
|
||
|
if (xhr.status === 200) {
|
||
|
successCallback(xhr.status);
|
||
|
window.location.replace(xhr.responseText);
|
||
|
}
|
||
|
else {
|
||
|
failureCallback(xhr.status,JSON.parse(xhr.response).message);
|
||
|
|
||
|
|
||
|
}
|
||
|
};
|
||
|
//TODO handle unauthorised error here in case merchant host api is secured by merchant
|
||
|
xhr.send(formData);
|
||
|
|
||
|
|
||
|
/*fetch('http://localhost:8885/api/fpx-transaction', {
|
||
|
headers: {}, method: 'POST', body: formData
|
||
|
}).then(handleErrors)
|
||
|
.then((response) => {
|
||
|
//console.log(response.getResponseHeader("location"));
|
||
|
window.location.replace(response.responseText);
|
||
|
|
||
|
})
|
||
|
.catch((error) => {
|
||
|
networkFailureCallback(error)
|
||
|
});*/
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
function handleErrors(response) {
|
||
|
|
||
|
if (!response.ok) {
|
||
|
response.json().then(function (object) {
|
||
|
failureCallback(object, response.status);
|
||
|
})
|
||
|
|
||
|
throw Error(response.statusText);
|
||
|
} else {
|
||
|
return response;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
m1pay.on = function (resultMessage, resultFunc) {
|
||
|
if (resultMessage == 'payment.success') {
|
||
|
successCallback = resultFunc;
|
||
|
|
||
|
}
|
||
|
if (resultMessage == 'payment.error') {
|
||
|
failureCallback = resultFunc;
|
||
|
|
||
|
}
|
||
|
if (resultMessage == 'payment.networkError') {
|
||
|
networkFailureCallback = resultFunc;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|