简体   繁体   English

生成支付令牌时出现 JS 401 循环错误

[英]JS 401 loop error while generating a payment token

I'm trying to create a payment token for the payment gateway (Paymob Accept).我正在尝试为支付网关 (Paymob Accept) 创建支付令牌。 When I check my browser console I get a loop POST error 401. I've attached two screenshots.当我检查浏览器控制台时,出现循环 POST 错误 401。我附上了两个屏幕截图。

401循环错误

错误详情

My code is:我的代码是:

const API = 'APK_KEY'
async function firstStep() {
    let data = {
        "api_key": API
    }
    let request = fetch('https://accept.paymob.com/api/auth/tokens', {
        method: 'post',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = (await request).json()
    let token = (await response).token

    secondStep(token)

}

async function secondStep(token) {
    let data = {
    "auth_token":  token,
    "delivery_needed": "false",
    "amount_cents": "100",
    "currency": "EGP",
    "items": [],
    }
    let request = await fetch('https://accept.paymob.com/api/ecommerce/orders', {
        method : 'POST',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = request.json()
    let id = (await response).id
  
    secondStep()
}

async function thirdStep(token, id) {
    let data = {
        "auth_token": token,
        "amount_cents": "100", 
        "expiration": 3600, 
        "order_id": id,
        "billing_data": {
          "apartment": "803", 
          "email": "claudette09@exa.com", 
          "floor": "42", 
          "first_name": "Clifford", 
          "street": "Ethan Land", 
          "building": "8028", 
          "phone_number": "+86(8)9135210487", 
          "shipping_method": "PKG", 
          "postal_code": "01898", 
          "city": "Jaskolskiburgh", 
          "country": "CR", 
          "last_name": "Nicolas", 
          "state": "Utah"
        }, 
        "currency": "EGP", 
        "integration_id": 13034
      }
    let request = fetch('https://accept.paymob.com/api/acceptance/payment_keys', {
        method: 'post',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = (await request).json()
    let finalToken = (await response).token

    cardPayment(finalToken)
}

async function cardPayment() {
    let iframeURL = `https://accept.paymob.com/api/acceptance/iframes/18922?payment_token=${finalToken}`
    console.log(iframeURL)
}
firstStep()

I tried each step in the console, stepOne and stepTwo work fine.我在控制台中尝试了每一步,stepOne 和 stepTwo 工作正常。 After I added stepThree I get the error.添加 stepThree 后出现错误。

What am I missing?我错过了什么? Thanks in advance for your support!提前感谢您的支持!

Like said in the comment section, the secondStep is calling itself recursively, also without any arguments.就像在评论部分所说的那样, secondStep递归地调用自己,也没有任何参数。 This will cause an infinite loop.这将导致无限循环。 We fix it by calling thirdStep .我们通过调用thirdStep来修复它。

Also in the cardPayment function, you are using finalToken , which is not defined in the function.同样在cardPayment函数中,您正在使用函数中未定义的finalToken We fix it that by making it the argument.我们通过将其作为参数来修复它。

const API = 'APK_KEY'
async function firstStep() {
    let data = {
        "api_key": API
    }
    let request = fetch('https://accept.paymob.com/api/auth/tokens', {
        method: 'post',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = (await request).json()
    let token = (await response).token

    secondStep(token)
}

async function secondStep(token) {
    let data = {
    "auth_token":  token,
    "delivery_needed": "false",
    "amount_cents": "100",
    "currency": "EGP",
    "items": [],
    }
    let request = await fetch('https://accept.paymob.com/api/ecommerce/orders', {
        method : 'POST',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = request.json()
    let id = (await response).id
  
    thirdStep(token, id)
}

async function thirdStep(token, id) {
    let data = {
        "auth_token": token,
        "amount_cents": "100", 
        "expiration": 3600, 
        "order_id": id,
        "billing_data": {
          "apartment": "803", 
          "email": "claudette09@exa.com", 
          "floor": "42", 
          "first_name": "Clifford", 
          "street": "Ethan Land", 
          "building": "8028", 
          "phone_number": "+86(8)9135210487", 
          "shipping_method": "PKG", 
          "postal_code": "01898", 
          "city": "Jaskolskiburgh", 
          "country": "CR", 
          "last_name": "Nicolas", 
          "state": "Utah"
        }, 
        "currency": "EGP", 
        "integration_id": 13034
      }
    let request = fetch('https://accept.paymob.com/api/acceptance/payment_keys', {
        method: 'post',
        headers: {'content-type' : 'application/json'},
        body: JSON.stringify(data)
    })

    let response = (await request).json()
    let finalToken = (await response).token

    cardPayment(finalToken)
}

async function cardPayment(finalToken) {
    let iframeURL = `https://accept.paymob.com/api/acceptance/iframes/18922?payment_token=${finalToken}`
    console.log(iframeURL)
}
firstStep()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ESPIRMA 生成 JS AST 时忽略错误 - Ignore error while generating JS AST using ESPIRMA 可以在没有支付信息的情况下使用 Stripe JS 创建令牌 - Possible to create Token with Stripe JS with no payment information OAuth请求令牌错误:在flickr中为401 - OAuth Request Token Error:401 in flickr Spotify API:错误 401,“未提供令牌” - Spotify API: Error 401, “no token provided” 使用API​​ 401错误的Bigcommerce处理付款,代码为10001 - Bigcommerce Process Payment with api 401 error with code 10001 虽然循环不是随机生成数据 - While loop is not generating data randomly 使用for循环时出现“未捕获的SyntaxError:意外令牌;”错误 - Getting “Uncaught SyntaxError: Unexpected token ;” error while using for loop 在node-webkit上时,Node.js错误:“意外令牌;” - Node.js error: “Unexpected token ;” while on node-webkit 使用Angular.js时在JSON错误中获取意外令牌 - Getting Unexpected token in JSON error while using Angular.js 尝试在后端nodejs服务器上验证时,在js chrome扩展中生成令牌时出错 - Error when generating a token in a js chrome extension when trying to verify it on a backend nodejs server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM