我正在构建一个用于库存管理的web应用程序。我在前端有React,在后端有Nodejs+mongodb。我们公司在当地的活动中售卖,我们的大部分销售额都是用信用卡支付的。为了处理信用卡支付,我们在手机上使用Paypal Here应用程序,它连接到读卡器,我们手动输入支付金额。由于我们有超过200个不同的产品(自定义艺术),我们决定建立此应用程序,以便我们可以快速搜索正在购买的产品(S),添加到“购物车”中的总价加税将自动计算,然后总共3个支付选项按钮将出现,一个现金,一个为venmo,一个为卡。起初,我认为卡选择按钮可以外部链接到Paypal Here应用程序,重定向时支付金额会自动填写,但后来我意识到我实际上可以在应用程序中集成一个Paypalhere sdk,这听起来比重定向要好。有三个不同的sdks,一个用于ios,一个用于android,另一个用于web,而用于web的sdks正是我所需要的。我寻找一个npm包,没有成功,然后我尝试通过react helment手动将脚本和src插入到文档中,没有成功,在componentDidMount上没有成功。我不习惯没有npm包可用,所以我今天的问题是如何将这个sdk集成到我的React应用程序中?
以下是web集成文档的链接:https://developer.paypal.com/docs/integration/paypal-here/sdk-dev/web/#integration
这是我用来手动插入脚本onComponentDidMount的代码,我不知道它是否工作,但即使它工作了,我也不知道如何访问它……
useEffect(() => {
const script = document.createElement("script");
script.src = "https://www.paypalobjects.com/pph/websdk/js/pphwebsdk-1.1.14.min.js";
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}, []);发布于 2021-09-28 20:45:44
不要在添加脚本后将其删除。
您可以设置一个回调函数,使使用PPH的代码在脚本加载后运行。这是一个带有回调函数的示例,它用于常规的PayPal按钮,而不是PPH,但您可以根据需要对其进行修改。
function loadAsync(url, callback) {
var s = document.createElement('script');
s.setAttribute('src', url); s.onload = callback;
document.head.insertBefore(s, document.head.firstElementChild);
}
loadAsync('https://www.paypal.com/sdk/js?client-id=sb¤cy=USD', function() {
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01'
}
}]
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name);
});
}
}).render('body');
});或者,您可以从应用程序的索引<head>中静态加载SDK,它将始终在那里,随时可供使用。
https://stackoverflow.com/questions/69368184
复制相似问题