Я тестирую API получения для создания платежа с помощью PayPal plus
Моя первая функция получения работает правильно, я делаю Oauth, чтобы получить токен на предъявителя, чтобы я мог получить свой 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, …}