hedera-web/src/stores/tpv.js

100 lines
2.9 KiB
JavaScript

import { defineStore } from 'pinia';
import { api } from 'boot/axios';
export const tpvStore = defineStore('tpv', {
actions: {
async check(route) {
const orderId = route.query.tpvOrder;
const status = route.query.tpvStatus;
if (!(orderId && status)) return null;
await api.post('tpvTransactions/end', {
orderId,
status
});
if (status === 'ko') {
const retry = confirm('retryPayQuestion');
if (retry) {
this.retryPay(orderId);
}
}
return status;
},
async pay(amount, company) {
await this.realPay(amount * 100, company);
},
async retryPay(orderId) {
const { data: transactions } = await api.get(
'clients/transactions',
{
params: {
orderFk: orderId
}
}
);
if (transactions && transactions.length > 0) {
const [transaction] = transactions;
await this.realPay(
transaction.amount * 100,
transaction.merchantFk
);
}
},
async realPay(amount, companyId) {
if (!isNumeric(amount) || amount <= 0) {
throw new Error('payAmountError');
}
const { data: json } = await api.post('tpvTransactions/start', {
amount,
companyId,
urlOk: this.makeUrl('ok'),
urlKo: this.makeUrl('ko')
});
const postValues = json.postValues;
const form = document.createElement('form');
form.method = 'POST';
form.action = json.url;
document.body.appendChild(form);
for (const field in postValues) {
const input = document.createElement('input');
input.type = 'hidden';
input.name = field;
form.appendChild(input);
if (postValues[field]) {
input.value = postValues[field];
}
}
form.submit();
},
makeUrl(status) {
let path = location.protocol + '//' + location.hostname;
path += location.port ? ':' + location.port : '';
path += location.pathname;
path += location.search ? location.search : '';
path += '#/ecomerce/orders';
path +=
'?' +
new URLSearchParams({
tpvStatus: status,
tpvOrder: '_transactionId_'
}).toString();
return path;
}
}
});
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}