#html #json #django #ajax #e-commerce
#HTML #json #джанго #аякс #электронная коммерция
Вопрос:
я создаю сайт электронной коммерции с помощью django. как только пользователь нажимает «Оформить заказ», появляется html-форма выставления счетов, которая всплывает и предлагает пользователю ввести адрес, адрес электронной почты и т.д.
я также передавал данные, такие как электронная почта, и это работало нормально. но когда я пытаюсь получить сумму, она не проходит
как бы я передал сумму на платежный шлюз?
Html-форма
lt;formgt; lt;div class="container-fluid"gt; lt;div class="row no-gutter"gt; lt;div class="col-md-12"gt; lt;div class="login d-flex align-items-center py-5"gt; lt;div class="container"gt; lt;div class="row"gt; lt;div id="payment-form" class="col-12 col-lg-6 mx-auto"gt; lt;h3 class="mb-3"gt;Billing addresslt;/h3gt; lt;!-- Error messages in this element --gt; lt;div class="row g-3"gt; lt;div class="col-sm-7"gt; lt;label for="firstName" class="form-label"gt;Full Namelt;/labelgt; lt;input type="text" class="form-control" id="fullName" placeholder="" requiredgt; lt;div class="invalid-feedback"gt; Valid first name is required. lt;/divgt; lt;/divgt; lt;div class="col-12"gt; lt;label for="email" class="form-label"gt;Email lt;span class="text-muted"gt;(Optional)lt;/spangt;lt;/labelgt; lt;input type="email" class="form-control" id="email" placeholder="you@example.com"gt; lt;div class="invalid-feedback"gt; Please enter a valid email address for shipping updates. lt;/divgt; lt;/divgt; lt;div class="col-12"gt; lt;label for="address" class="form-label"gt;Addresslt;/labelgt; lt;input type="text" class="form-control" id="custAdd" placeholder="1234 Main St" requiredgt; lt;div class="invalid-feedback"gt; Please enter your shipping address. lt;/divgt; lt;/divgt; lt;div class="col-12"gt; lt;label for="address2" class="form-label"gt;Address 2 lt;span class="text-muted"gt;(Optional)lt;/spangt;lt;/labelgt; lt;input type="text" class="form-control" id="custAdd2" placeholder="Apartment or suite"gt; lt;/divgt; lt;div class="col-md-5"gt; lt;label for="country" class="form-label"gt;Countrylt;/labelgt; lt;select class="form-select" id="country" requiredgt; lt;option value=""gt;Choose...lt;/optiongt; lt;optiongt;United Stateslt;/optiongt; lt;/selectgt; lt;div class="invalid-feedback"gt; Please select a valid country. lt;/divgt; lt;/divgt; lt;div class="col-md-4"gt; lt;label for="state" class="form-label"gt;Statelt;/labelgt; lt;select class="form-select" id="state" requiredgt; lt;option value=""gt;Choose...lt;/optiongt; lt;optiongt;Californialt;/optiongt; lt;/selectgt; lt;div class="invalid-feedback"gt; Please provide a valid state. lt;/divgt; lt;/divgt; lt;div class="col-md-3"gt; lt;label for="Postcode" class="form-label"gt;Postcodelt;/labelgt; lt;input type="text" class="form-control" id="postCode" placeholder="" requiredgt; lt;div class="invalid-feedback"gt; Zip code required. lt;/divgt; lt;/divgt; lt;/divgt; lt;hr class="my-4"gt; lt;p id="{{cart.get_total_price}}"gt;lt;/pgt; lt;script src="https://checkout.flutterwave.com/v3.js"gt;lt;/scriptgt; lt;button type="button" data-secret=="{{cart.get_total_price}}" id="submit" class="btn btn-primary w-100 fw-bold" gt;Pay ${{cart.get_total_price}}lt;/buttongt; lt;/divgt; lt;/divgt; lt;/divgt; lt;/divgt; lt;/divgt; lt;/divgt; lt;/divgt; lt;script type="text/javascript" src="https://api.ravepay.co/flwv3-pug/getpaidx/api/flwpbf-inline.js"gt;lt;/scriptgt; lt;script src="{% static 'js/submit_checkout.js' %}"gt;lt;/scriptgt;
мой js-скрипт
document.addEventListener("DOMContentLoaded", (event) =gt; { // Add an event listener for when the user clicks the submit button to pay document.getElementById("submit").addEventListener("click", (e) =gt; { e.preventDefault(); const PBFKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; // paste in the public key from your dashboard here const txRef = '' Math.floor((Math.random() * 1000000000) 1); //Generate a random id for the transaction reference const email = document.getElementById('email').value; var fullname= document.getElementById('fullName').value; var address1= document.getElementById('custAdd').value; var address2= document.getElementById('custAdd2').value; var country= document.getElementById('country').value; var state= document.getElementById('state').value; var address1= document.getElementById('postCode').value; const amount= document.getElementById('{{cart.get_total_price}}').value; // getpaidSetup is Rave's inline script function. it holds the payment data to pass to Rave. getpaidSetup({ PBFPubKey: PBFKey, customer_email: email, amount:amount*100, currency: "USD", // Select the currency. leaving it empty defaults to NGN txref: txRef, // Pass your UNIQUE TRANSACTION REFERENCE HERE. onclose: function() {}, callback: function(response) { flw_ref = response.tx.flwRef;// collect flwRef returned and pass to a server page to complete status check. console.log("This is the response returned after a charge", response); if(response.tx.chargeResponse =='00' || response.tx.chargeResponse == '0') { // redirect to a success page } else { // redirect to a failure page. } } }); }); });
число просмотров
@login_required def CartView(request): cart = Cart(request) total = str(cart.get_total_price()) total = total.replace('.', '') total = int(total) return render(request, 'payment/payment_form.html',)
Ответ №1:
Вы не передаете эти переменные total
и cart
контекст шаблона
Измените это
return render(request, 'payment/payment_form.html',)
Для
return render(request, 'payment/payment_form.html', {'total': total, 'cart': cart})