如何将产品总量传递到支付网关

Posted

技术标签:

【中文标题】如何将产品总量传递到支付网关【英文标题】:how to pass the total amount of product to payment gateway 【发布时间】:2022-01-11 10:19:09 【问题描述】:

我正在使用 django 构建一个电子商务网站。一旦用户按下结帐,就会弹出一个 html 计费表单并提示用户输入地址、电子邮件等。

我还传递了电子邮件等数据,它运行良好。但是当我尝试它没有通过的金额时

我如何将金额传递给支付网关?

HTML 表单

<form>
<div class="container-fluid">
  <div class="row no-gutter">
    <div class="col-md-12">
      <div class="login d-flex align-items-center py-5">
        <div class="container">
          <div class="row">
            <div id="payment-form" class="col-12 col-lg-6 mx-auto">
              <h3 class="mb-3">Billing address</h3>
              <!-- Error messages in this element -->
                  
                  <div class="row g-3">
                    <div class="col-sm-7">
                      <label for="firstName" class="form-label">Full Name</label>
                      <input type="text" class="form-control" id="fullName" placeholder="" required>
                      <div class="invalid-feedback">
                        Valid first name is required.
                      </div>
                    </div>
                    <div class="col-12">
                      
                      <label for="email" class="form-label">Email <span class="text-muted">(Optional)</span></label>
                      <input type="email" class="form-control" id="email"  placeholder="you@example.com">
                      <div class="invalid-feedback">
                        Please enter a valid email address for shipping updates.
                      </div>
                    </div>
                    <div class="col-12">
                      <label for="address" class="form-label">Address</label>
                      <input type="text" class="form-control" id="custAdd" placeholder="1234 Main St" required>
                      <div class="invalid-feedback">
                        Please enter your shipping address.
                      </div>
                    </div>
                    <div class="col-12">
                      <label for="address2" class="form-label">Address 2 <span
                          class="text-muted">(Optional)</span></label>
                      <input type="text" class="form-control" id="custAdd2" placeholder="Apartment or suite">
                    </div>
                    <div class="col-md-5">
                      <label for="country" class="form-label">Country</label>
                      <select class="form-select" id="country" required>
                        <option value="">Choose...</option>
                        <option>United States</option>
                      </select>
                      <div class="invalid-feedback">
                        Please select a valid country.
                      </div>
                    </div>
                    <div class="col-md-4">
                      <label for="state" class="form-label">State</label>
                      <select class="form-select" id="state" required>
                        <option value="">Choose...</option>
                        <option>California</option>
                      </select>
                      <div class="invalid-feedback">
                        Please provide a valid state.
                      </div>
                    </div>
                    <div class="col-md-3">
                      <label for="Postcode" class="form-label">Postcode</label>
                      <input type="text" class="form-control" id="postCode" placeholder="" required>
                      <div class="invalid-feedback">
                        Zip code required.
                      </div>
                    </div>
                  </div>


                  <hr class="my-4">
                  <p id="cart.get_total_price"></p>
                  <script src="https://checkout.flutterwave.com/v3.js"></script>
                  <button type="button" data-secret=="cart.get_total_price" id="submit" class="btn btn-primary w-100 fw-bold" >Pay $cart.get_total_price</button>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript" src="https://api.ravepay.co/flwv3-pug/getpaidx/api/flwpbf-inline.js"></script>
<script src="% static 'js/submit_checkout.js' %"></script>

我的 js 脚本

    document.addEventListener("DOMContentLoaded", (event) => 
  // Add an event listener for when the user clicks the submit button to pay
  document.getElementById("submit").addEventListener("click", (e) => 
      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】:

您没有将这些变量 totalcart 传递给模板上下文

改变这个

return render(request, 'payment/payment_form.html',)

return render(request, 'payment/payment_form.html', 'total': total, 'cart': cart)

【讨论】:

以上是关于如何将产品总量传递到支付网关的主要内容,如果未能解决你的问题,请参考以下文章

添加一个复选框作为禁用支付网关的 WooCommerce 管理产品选项

根据 WooCommerce 购物车中的商品数量和产品类别禁用支付网关

是否可以从另一个网站将产品添加到 Magento 购物车,而产品不在 magento 商店中

如何开通配资支付接口,目前国内有哪些平台

如何使用印度银行账户设置条带账户并接受国际支付?

市场应用支付网关