[英]JS 401 loop error while generating a payment token
我正在尝试为支付网关 (Paymob Accept) 创建支付令牌。 当我检查浏览器控制台时,出现循环 POST 错误 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.