Django:给Stripe ACH存款验证的最佳方法错误

时间:2018-05-29 03:19:14

标签: javascript django stripe-payments

我正在进行Stripe ACH验证,我在其中输入两个与其银行帐户中的存款相对应的数字。当他们输入的值不是1到99之间的整数时,错误输出html字段的最佳方法是什么。这应该是javascript side(jquery?)还是在我看来。我的直觉告诉我它需要在视图中完成,但我不知道如何将错误消息转发回用户。我应该为此创建一个表单吗?我不这么认为,因为我没有把东西保存到数据库中。

思想?

我在Django中的视图

def ach_payment_verify_updateview(request):
    request.stripe_id = request._post['token']
    print('hi')
    try:
    if not isinstance(request._post['deposit_1'], int):
    ### some kind of error message here
    print(request._post['deposit_1'])
    print(request._post['deposit_2'])

我目前的javascript代码。

document.querySelector('form.ach-payment-verify-form').addEventListener('submit', function(e) {
    e.preventDefault();
  var nextUrl = paymentForm.attr('data-next-url');
  var deposit_1 = document.getElementById('deposit-1').value;
  var deposit_2 = document.getElementById('deposit-2').value;
  stripeDepositHandler(nextUrl, deposit_1, deposit_2)
  });
function stripeDepositHandler(nextUrl, deposit_1, deposit_2){
  var paymentMethodEndpoint = '/billing/ach-payment-verify/create/'
  var data = {
    'token': 'ba_1CWoJSFAasdfafsdReMae',
    'deposit_1':deposit_1,
    'deposit_2':deposit_2,
  }
  $.ajax({
      data: data,
      url: paymentMethodEndpoint,
      method: "POST",
      success: function(data){
        var successMsg = data.message || "Success! Your account has been verified."
        $("form.ach-payment-verify-form")[0].reset();
        if (nextUrl){
          successMsg = successMsg + "<br/><br/><i class='fa fa-spin fa-spinner'></i> Redirecting..." //<i class> - 'font awesome'
        }
        if ($.alert){  // if alert message is installed
          $.alert(successMsg)
        } else {
          alert("")
        } 
        redirectToNext(nextUrl, 1500)
      },
      error: function(error){
        console.log(error)
      }
  })
}

1 个答案:

答案 0 :(得分:0)

Please try this validation in your code : 
document.querySelector('form.ach-payment-verify-form').addEventListener('submit', function(e) {
    e.preventDefault();
  var nextUrl = paymentForm.attr('data-next-url');
  var deposit_1 = document.getElementById('deposit-1').value;
  var deposit_2 = document.getElementById('deposit-2').value;

      if (Number.isInteger(deposit_1) && Number.isInteger(deposit_2)) {
          stripeDepositHandler(nextUrl, deposit_1, deposit_2)
      }
      else {
          console.log("Please enter valide number. Thank You !")
      }