![](/img/trans.png)
[英]RESOURCE_NOT_FOUND while creating Subscription Api response in PayPal in JavaScript
[英]RESOURCE_NOT_FOUND in Paypal
我必須在我的 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"}]}
我該如何解決這個問題? 任何幫助和建議將不勝感激。
似乎正在發生的事情是您有一個用於處理交易的客戶端集成,然后您嘗試使用服務器端 API 調用來檢查訂單狀態,但該服務器端 API 調用與您使用的訂單處理。
您有兩個選擇:
切換到針對付款 ID而不是訂單 ID 的服務器端 API 調用。 這在此處記錄:https ://developer.paypal.com/docs/api/payments/v2/#captures_get 。 無論如何,付款 ID 比訂單 ID 更有用,因為付款/交易 ID 是在 www.paypal.com 中實際保存多年並用於會計目的的 ID。 訂單 ID 僅在付款批准期間使用。
切換到服務器端集成模式,如下所示: https : //developer.paypal.com/demo/checkout/#/pattern/server 。 然后您將執行自己的 API 調用來設置和捕獲付款,並且您現有的 OrdersGetRequest(..) 可以工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.