簡體   English   中英

生成支付令牌時出現 JS 401 循環錯誤

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

我正在嘗試為支付網關 (Paymob Accept) 創建支付令牌。 當我檢查瀏覽器控制台時,出現循環 POST 錯誤 401。我附上了兩個屏幕截圖。

401循環錯誤

錯誤詳情

我的代碼是:

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()

我在控制台中嘗試了每一步,stepOne 和 stepTwo 工作正常。 添加 stepThree 后出現錯誤。

我錯過了什么? 提前感謝您的支持!

就像在評論部分所說的那樣, secondStep遞歸地調用自己,也沒有任何參數。 這將導致無限循環。 我們通過調用thirdStep來修復它。

同樣在cardPayment函數中,您正在使用函數中未定義的finalToken 我們通過將其作為參數來修復它。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM