javascript - Paypal 中的 RESOURCE_NOT_FOUND

标签 javascript node.js reactjs paypal

我必须在我的 ReactJs 项目中设置 paypal 进行付款。为此,我创建 Paypal 按钮并调用 Paypal 以获取交易详细信息。看起来它工作得很好,我也收到了包含交易详细信息的成功消息。这里出现的问题是当我尝试验证交易详细信息是否符合我的服务器端(即nodejs)中的预期(如此处提到的:“https://developer.paypal.com/docs/checkout/reference/server-integration/capture-transaction/”)我收到这个错误。在我的 nodjs 项目中,我使用“@paypal/checkout-server-sdk”。

我的代码是:

在 ReactJ 中: PaypalButton.js

import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";

const PaypalButton = props => {
const [sdkReady, setSdkReady] = useState(false);

const addPaypalSdk = () => {
const clientID ="**********CLIENT_ID**************";
const script = document.createElement("script");
script.type = "text/javascript";
script.src = `https://www.paypal.com/sdk/js?client-id=${clientID}`;
script.async = true;
script.onload = () => {
  setSdkReady(true);
};
script.onerror = () => {
  throw new Error("Paypal SDK could not be loaded.");
};

document.body.appendChild(script);
};

useEffect(() => {
if (window !== undefined && window.paypal === undefined) {
  addPaypalSdk();
} else if (
  window !== undefined &&
  window.paypal !== undefined &&
  props.onButtonReady
) {
  props.onButtonReady();
}
}, []);

//amount goes in the value field we will use props of the button for this
const createOrder = (data, actions) => {
console.log("createOrder", data);
return actions.order.create({
  purchase_units: [
    {
      amount: {
        currency_code: "USD",
        value: props.amount
      }
    }
  ]
});
};

const onApprove = (data, actions) => {
return actions.order
  .capture()
  .then(details => {
    if (props.onSuccess) {
      window.alert("Payment Sucessfull...");
      return props.onSuccess(data, details);
    }
  })
  .catch(err => {
    console.log(err);
  });
  };

 const clickPaypal = flag => {
    return props.onClick(flag);
 };

if (!sdkReady && window.paypal === undefined) {
  return <div>Loading...</div>;
}

const Button = window.paypal.Buttons.driver("react", {
React,
ReactDOM
});

return (
  <div style={{ width: 200 }}>
  <Button
    {...props}
    createOrder={
      props.amount && !createOrder
        ? (data, actions) => createOrder(data, actions)
        : (data, actions) => createOrder(data, actions)
    }
    onApprove={
      props.onSuccess
        ? (data, actions) => onApprove(data, actions)
        : (data, actions) => onApprove(data, actions)
    }
    style={{
      layout: "horizontal",
      color: "gold",
      shape: "pill",
      label: "pay",
      size: "responsive",
      tagline: true
    }}
    onClick={props.onClick ? clickPaypal : clickPaypal}
  />
</div>
);
};
export default PaypalButton;

Pay.js

onSuccess = (payment, details) => {
const {
  processInvoicePaypalPayment
} = this.props;

this.paypalSucesssDetails = { ...details };

processInvoicePaypalPayment( .     //call to server
  {
    invoice: invoice.data._id,
    orderId: this.paypalSucesssDetails.id // This order id is from sucess transaction details
  },
  this.paypalCapture
);
};

render=()=>{
   <PaypalButton
    amount={invoiceData.amount}
    onSuccess={this.onSuccess}
    env={"sandbox"}
    onClick={this.clickPaypal}
   />
}

在我的nodejs中

 import paypal from "@paypal/checkout-server-sdk";
      const clientId = "CLIENTID";
      const clientSecret = "SECRET";
      const environment = new paypal.core.SandboxEnvironment(clientId, clientSecret);
      const client = new paypal.core.PayPalHttpClient(environment);

      try{
      export const processInvoicePaypalPayment = async (user, data) => {
             const customerId = user.customer,
             { invoice: invoiceId, orderId: orderID } = data;

             let request = new paypal.orders.OrdersGetRequest(orderID);
       const createOrder = async () => {
       const response = await client.execute(request);

       return { ok: true, data: { _id: invoiceId, payment: response.result } };

       };

       const result = await createOrder();

       }
       }catch (err) {
         console.log(err);
       }

结果是:

  { Error: {"name":"RESOURCE_NOT_FOUND","details": 
  [{"location":"path","issue":"INVALID_RESOURCE_ID","description":"Specified resource ID does not exist. Please check the resource ID and try again."}],"message":"The specified resource does not exist.","debug_id":"c3b5026d6dd74","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_RESOURCE_ID","rel":"information_link","method":"GET"}]}

我该如何解决这个问题?任何帮助和建议将不胜感激。

最佳答案

似乎发生的情况是,您有一个客户端集成来处理交易,然后您尝试使用服务器端 API 调用来检查订单状态,但该服务器端 API 调用与您使用的订单处理不兼容。

您有两个选择:

  1. 切换到支付 ID(而非订单 ID)上的服务器端 API 调用。这记录在此处:https://developer.paypal.com/docs/api/payments/v2/#captures_get 。无论如何,支付 ID 比订单 ID 更有用,因为支付/交易 ID 实际上在 www.paypal.com 中保留多年并用于会计目的。订单 ID 仅在付款审批期间使用。

  2. 切换到服务器端集成模式,如下所示:https://developer.paypal.com/demo/checkout/#/pattern/server 。然后,您将执行自己的 API 调用来设置和捕获付款,并且您现有的 OrdersGetRequest(..) 可以工作

关于javascript - Paypal 中的 RESOURCE_NOT_FOUND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60541052/

相关文章:

node.js - 如何在 session expressjs中存储套接字对象

javascript - 使用 NodeJS 和 HighStocks(或 Google Annotation Time Line)设计 Web 应用程序

javascript - 当通过 2 个组件传递 props 时,我失去了在数组上使用 .map 的能力

javascript - 如何将 limit 和 startAt 应用于 firestore 文档的子项目

javascript - 页面加载后运行javascript

javascript - 构建 javascript 数组但有空值

node.js - 在centos上安装带有sequelize的nodejs时出错

reactjs - 如何使用 Golang Gorilla/mux 托管并发 websocket 连接?

javascript - 编码方式: storing methods as object properties

javascript - priv/static/js/app.js 在更改 Vue 文件时随机更新 - Phoenix/Elixir/Vue.js