Paypal 中的 RESOURCE_NOT_FOUND
Posted
技术标签:
【中文标题】Paypal 中的 RESOURCE_NOT_FOUND【英文标题】:RESOURCE_NOT_FOUND in Paypal 【发布时间】:2020-06-17 19:56:13 【问题描述】:我必须在我的 ReactJs 项目中设置 paypal 以进行付款。为此,我创建了贝宝按钮并致电贝宝以获取交易详细信息。看起来效果很好,我也收到了带有交易详细信息的成功消息。这里出现的问题是,当我尝试验证交易详细信息在我的服务器端,即 nodejs 中是否符合预期(此处提到:“https://developer.paypal.com/docs/checkout/reference/server-integration/capture-transaction/”)我得到了这个错误。在我的 nodjs 项目中,我使用的是“@paypal/checkout-server-sdk”。
我的代码是:
在 ReactJs 中: 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"]
我该如何解决这个问题?任何帮助和建议将不胜感激。
【问题讨论】:
您正在查找的示例 orderID 是什么?您可以打印并分享实际请求文本/json 的示例吗? 我的样品 orderId 是“6VG34423VV633792H”。成功的细节是 create_time: "2020-03-05T09:56:39Z" id: "6VG34423VV633792H" 意图:"CAPTURE" 链接:Array [ … ] 付款人:对象 email_address: "name@gmail. com", payer_id: "3LNXV55Q55GHE", 地址: …, … purchase_units: Array [ … ] status: "COMPLETED" update_time: "2020-03-05T09:57:09Z"6VG34423VV633792H
似乎不是有效的沙盒 orderID。你从哪里得到这个值?
return actions.order .capture() .then(details => --------------> from this details if (props.onSuccess) window. alert("支付成功..."); return props.onSuccess(data, details); )
【参考方案1】:
似乎正在发生的事情是您有一个用于处理交易的客户端集成,然后您尝试使用服务器端 API 调用来检查订单状态,但该服务器端 API 调用不是与您使用的订单处理兼容。
你有两个选择:
切换到付款 ID 上的服务器端 API 调用,而不是订单 ID。这记录在这里:https://developer.paypal.com/docs/api/payments/v2/#captures_get。无论如何,支付 ID 比订单 ID 更有用,因为支付/交易 ID 是在 www.paypal.com 中实际保留多年并用于会计目的的内容。订单 ID 仅在付款批准期间使用。
切换到服务器端集成模式,例如:https://developer.paypal.com/demo/checkout/#/pattern/server。然后,您将执行自己的 API 调用来设置和捕获付款,并且您现有的 OrdersGetRequest(..) 可以工作
【讨论】:
感谢您的回复。正如你提到的,我尝试了第一个,但我没有付款 ID。因为它是空的。不知道为什么。我开始实施第二个,即服务器端实施。为了在我的 createOrder const createOrder = (data, actions) => const clientID ="CLIENTID"; return fetch(https://www.paypal.com/sdk/js?client-id=$clientID
, method: "post" ) .then(function(res) return res.json(); ) .then(function(data) return data.orderID; ); ;
然后我阻止了跨域请求:同源策略不允许读取位于 paypal.com/sdk/js?client-id=sb¤cy=USD 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。我哪里出错了?
成功订单捕获的完整响应应在某处包含付款 ID,该 ID 与订单 ID 不同,且不为空。服务器端实现需要服务器上实现 API 调用的实际端点,您可以使用 fetch()。您不获取 PayPal SDK js,这没有任何意义。以上是关于Paypal 中的 RESOURCE_NOT_FOUND的主要内容,如果未能解决你的问题,请参考以下文章
php [PayPal Pro]更改PayPal Pro中的信用卡图标
create_with_paypal 节点 sdk PayPal 中的发票编号
paypal-php-sdk中的PayPal-Mock-Response