如何解决响应:尝试通过Paypal API创建付款时,{type:“ cors”}

时间:2019-05-18 15:50:00

标签: javascript reactjs paypal cors fetch

我正在测试获取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, …}

1 个答案:

答案 0 :(得分:-1)

我终于找到了解决方案,代码还可以,缺少的是响应需要像这样用json()进行转换:

  const res = await fetch("https://api.sandbox.paypal.com/v1/payments/payment", request);
  var data = await res.json();
  console.log(data);

在将res.json添加到响应后,可以正确读取它。