Передача итоговой стоимости в платежный шлюз
Имеется кривенький самописный магазин. В магазине реализована корзина, вот кусок кода на JS, который отвечает за отображение товара, стоимости позиции, количества, кнопок "добавить", "удалить", и главное -- итоговой стоимости:
function showCart() {
$.getJSON('catalog/wine-sets.json', function (data) {
//вывод корзины
if (!isEmtpy(cart)) {
$('.main-cart').html('the cart is empty');
}
else {
var goods = data;
var out = '';
var unitsprice = 0;
ttlbttl = 0;
var delcost = 0;
for (var id in cart) {
ttlbttl += goods[id].bottles * cart[id];
}
for (var id in cart) {
//out = Object.keys(cart).reduce((total, id) => total += cart[id], 0);
}
for (var id in cart) {
unitsprice += goods[id].price * cart[id];
}
for (var id in cart) {
//var bttlsumm = goods[id].bottles*cart[id];
out += `<div class="main-cart-unit"><img src="${goods[id].image}">`;
out += `<div class="main-cart-description-block"><div class="cart-goods-name"> ${goods[id].name } </div>`;
out += `<div class="cart-goods-description"> ${goods[id].country }`;
out += ` ${goods[id].region },`;
out += ` ${goods[id].producer } </div>`;
out += `<div class="cart-goods-subdesription"> ${goods[id].year }`;
out += ` ${goods[id].sweetness },`;
out += ` ${goods[id].color },`;
out += ` ${goods[id].sparkling } </div>`;
out += `<div class="main-cart-description-buttons-block"><button data-id="${id}" class="delete-position-cart">-</button>`;
out += `<div class="id-counting-cart"> ${cart[id] } </div>`;
out += `<button data-id="${id}" class="add-position-cart">+</button> </div> </div>`;
out += `<div class="cart-goods-price"> ${(cart[id]*goods[id].price).toFixed(2)}€ </div> </div>`;
out += '<br>';
}
$('.main-cart').html(out);
if (ttlbttl >= 36) {
delcost = 0;
}
else {
delcost = ttlbttl;
}
$('.delcost').html(`Delivery cost: <div class="subdelcost">${(delcost).toFixed(2)}€</div>`);
$('.unitsprice').html(`TOTAL: <div class="subunitsprice">${(unitsprice + delcost).toFixed(2)}€</div>`);
$('.delete-position-cart').on('click', delGoods);
$('.add-position-cart').on('click', addlGoods);
console.log(unitsprice, ttlbttl);
$('#cheched').on('change', function () {
if ( $('#cheched').prop('checked') && ttlbttl >=6 ) {
$('.cart-submit-button').attr('disabled', false);
} else {
$('.cart-submit-button').attr('disabled', true);
}
});
}
});
}
Для приема платежей в магазине был найден платежный шлюз от Paydoo. В документации требуется создать post-запрос, откуда будет выдергиваться идентификатор платежа и т.д., вот его код:
function request() {
$url = "https://test.oppwa.com/v1/checkouts";
$data = "entityId=8a8294185e5c61f5015e61d25f250dca" .
"&amount=92.00" .
"¤cy=EUR" .
"&paymentType=DB";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization:Bearer OG***gzOQ=='));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// this should be set to true in production
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responseData = curl_exec($ch);
if(curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $responseData;
}
$responseData = request();
Затем скриптом вызывается виджет для ввода данных карты, вот его код:
<form class="paymentWidgets" action="{success_url.php}" data-brands="MASTER VISA">
<script src="https://test.oppwa.com/v1/paymentWidgets.js?checkoutId={checkoutId}"></script>
</form>
Каким образом можно итоговую стоимость из моей корзины передать в виджет от Paydoo?