hedera-web/web/js/hedera/tpv.js

135 lines
2.6 KiB
JavaScript
Executable File

Vn.Tpv =
{
check: function (conn)
{
var tpvStatus = Vn.Hash.get ('tpv_status');
if (tpvStatus)
{
var batch = new Sql.Batch ();
batch.addValue ('transaction', Vn.Hash.get ('tpv_order'));
batch.addValue ('status', tpvStatus);
var query = 'CALL transaction_end (#transaction, #status)';
conn.execQuery (query, null, batch);
}
}
,pay: function (conn, amount, company)
{
if (amount > 0)
{
var request = new Vn.HttpRequest ();
request.add
({
'action': 'tpv'
,'amount': parseInt (amount * 100)
,'urlOk': this._makeUrl ('ok')
,'urlKo': this._makeUrl ('ko')
,'company': company
});
request.send ('rest.php',
this._onTransactionStart.bind (this, request));
}
else if (!isNaN (amount))
Htk.Toast.showError (_('AmountError'));
}
,_onTransactionStart: function (request, success)
{
var data = null;
var error = null;
if (success)
try {
var json = request.getJson ();
var data = json.data;
if (json.error)
error = new Vn.Error (
json.error.domain,
json.error.code,
json.error.message);
}
catch (e)
{
error = e;
}
if (success && data)
{
var form = document.createElement ('form');
form.method = 'post';
form.action = data.url;
document.body.appendChild (form);
var fieldsMap =
{
'Ds_SignatureVersion': 'HMAC_SHA256_V1'
,'Ds_MerchantParameters': data.params
,'Ds_Signature': data.signature
};
for (var field in fieldsMap)
{
var input = document.createElement ('input');
input.type = 'hidden';
input.name = field;
form.appendChild (input);
if (fieldsMap[field])
input.value = fieldsMap[field];
}
form.submit ();
}
else
Htk.Toast.showWarning (_('PayError'));
}
,_makeUrl: function (status, order)
{
var path = location.protocol +'//'+ location.host;
path += location.port ? ':'+ location.port : '';
path += location.pathname;
path += location.search ? location.search : '';
path += Vn.Hash.make ({
'form': 'ecomerce/orders',
'tpv_status': status,
'tpv_order': '%s'
}, true);
return path;
}
};
Vn.BasketChecker =
{
check: function (conn, callback)
{
conn.execQuery ('CALL basket_check ()',
this._onBasketCheck.bind (this, callback));
}
,_onBasketCheck: function (callback, resultSet)
{
var status = resultSet.fetchValue ();
if (!status)
return;
var isOk = status == 'UPDATED' || status == 'OK';
if (status == 'UPDATED')
Htk.Toast.showWarning (_('OrderItemsUpdated'));
if (callback)
callback (isOk);
if (!isOk)
Vn.Hash.set ({'form': 'ecomerce/checkout'});
}
};