[英]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。我附上了两个屏幕截图。
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.