我正在测试 fetch API 以使用 PayPal plus 创建付款
我的第一个获取功能正常工作,我执行 Oauth 来获取不记名 token ,这样我就可以获取 access_token 来进行付款
callPaypal = async () => {
try {
const details = {
grant_type: "client_credentials"
};
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
var request = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
Authorization:
"Basic <secrets>",
"cache-control": "no-cache"
},
body: formBody
};
const res = await fetch("https://api.sandbox.paypal.com/v1/oauth2/token", request);
const data = await res.json();
console.log(data);
const access_token = data.access_token;
this.makePaymentPaypal(access_token);
} catch (error) {}
};
makePaymentPaypal = async (access_token) => {
try {
var request = {
method: "POST",
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
Authorization: `Bearer ${access_token}`,
"cache-control": "no-cache"
},
body: JSON.stringify({
intent: "sale",
application_context: {
shipping_preference: "SET_PROVIDED_ADDRESS"
},
payer: {
payment_method: "paypal",
payer_info: {
billing_address: {
line1: "Mariano Escobedo 476 piso 14",
line2: "Anzures, Miguel Hidalgo",
city: "Mexico DF",
country_code: "MX",
postal_code: "11590",
state: "DF"
}
}
},
transactions: [
{
amount: {
currency: "MXN",
total: "522.00",
details: {
subtotal: "522.00"
}
},
description: "Pedido en Envia Flores",
payment_options: {
allowed_payment_method: "IMMEDIATE_PAY"
},
invoice_number: "94234212",
item_list: {
items: [
{
name: "Arreglo de flores",
description: "Amigo secreto",
quantity: "1",
price: "290.00",
sku: "sku01",
currency: "MXN"
},
{
name: "Ilumina su dia",
description: "Ilumina su dia",
quantity: "1",
price: "290.00",
sku: "sku02",
currency: "MXN"
},
{
name: "Descuento",
description: "Descuento",
quantity: "1",
price: "-58.00",
sku: "skiu12",
currency: "MXN"
}
],
shipping_address: {
recipient_name: "Costumer Costumer",
line1: "Mariano Escobedo 476 piso 14",
line2: "Anzures, Miguel Hidalgo",
city: "Mexico DF",
country_code: "MX",
postal_code: "11590",
state: "DF",
phone: "54545454"
}
}
}
],
redirect_urls: {
cancel_url: "https://www.example.com",
return_url: "https://www.example.com"
}
})
};
const res = await fetch("https://api.sandbox.paypal.com/v1/payments/payment", request, {
mode: "no-cors"
});
const data = await res.json();
console.log(data)
} catch (error) {}
};
编辑问题,因为我已经解决了它,缺少下面的代码,我需要将响应转换为 json。
const data = await res.json();
console.log(data)
添加此新代码之前的旧错误消息是:
Response {type: "cors", url: "https://api.sandbox.paypal.com/v1/payments/payment", redirected: false, status: 201, ok: true, …}
最佳答案
我终于找到了解决方案,代码没问题,缺少的是响应需要像这样用 json() 转换:
const res = await fetch("https://api.sandbox.paypal.com/v1/payments/payment", request);
var data = await res.json();
console.log(data);
在我将 res.json 添加到响应后,我可以正确读取它。
关于javascript - 如何修复响应 : {type: "cors"} when trying to create payment through paypal api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200562/