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

150 lines
3.1 KiB
JavaScript

Vn.Tpv = new Class
({
Extends: Vn.Object
,tpvOrder: null
,tpvStatus: null
,check: function (callback)
{
this.tpvOrder = Vn.Hash.get ('tpv_order');
this.tpvStatus = Vn.Hash.get ('tpv_status');
if (this.tpvStatus)
{
var batch = new Sql.Batch ();
batch.addValue ('transaction', this.tpvOrder);
batch.addValue ('status', this.tpvStatus);
var query = 'CALL transaction_end (#transaction, #status)';
this.conn.execQuery (query, null, batch);
}
if (callback)
callback (this, this.tpvOrder, this.tpvStatus);
}
,pay: function (amount, company)
{
this._realPpay (amount * 100, company);
}
,_realPpay: function (amount, company)
{
if (amount > 0)
{
var params = {
'amount': parseInt (amount)
,'urlOk': this._makeUrl ('ok')
,'urlKo': this._makeUrl ('ko')
,'company': company
};
var request = new Vn.JsonRequest ('tpv');
request.send (params,
this._onTransactionStart.bind (this));
}
else if (!isNaN (amount))
Htk.Toast.showError (_('AmountError'));
}
,_onTransactionStart: function (request, json, error)
{
if (json)
{
var form = document.createElement ('form');
form.method = 'post';
form.action = json.url;
document.body.appendChild (form);
var fieldsMap =
{
'Ds_SignatureVersion': 'HMAC_SHA256_V1'
,'Ds_MerchantParameters': json.params
,'Ds_Signature': json.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'));
}
,retryPay: function ()
{
var batch = new Sql.Batch ();
batch.addValue ('transaction', parseInt (this.tpvOrder));
var query = 'SELECT t.amount, m.company_id '
+'FROM tpv_transaction_view t '
+'JOIN tpv_merchant m ON t.merchant_id = m.id '
+'WHERE t.id = #transaction';
this.conn.execQuery (query,
this._onRetryPayDone.bind (this), batch);
}
,_onRetryPayDone: function (resultSet)
{
var res = resultSet.fetchResult ();
if (res.next ())
this._realPpay (res.get ('amount'), res.get ('company_id'));
else
Htk.Toast.showError (_('AmountError'));
}
,_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'});
}
};