javascript - 贝宝订阅按钮未显示在手机上
问题描述
我在应用程序的 4 个不同位置使用 Paypal 3 个订阅和一次付款。
一切都在桌面上运行,但订阅按钮没有显示在手机上。
我觉得问题出在导入库,这个导入有什么问题吗?
没有任何错误,我检查并发现 PayPal 已加载到手机上,但没有显示按钮。
<script
src=`https://www.paypal.com/sdk/js?clientid=${process.env.PAYPAL_CLIENT_ID}
¤cy=GBP&disable-funding=credit,card&vault=true&intent=subscription`
></script>
解决方案
所以问题是按钮被删除了,因为paypalButton.close()
当状态渲染时被调用(不知道为什么)。
请参阅此问题以获取有关该问题的更多信息:
https://github.com/paypal/paypal-checkout-components/issues/1506
通过在渲染前休眠 1 秒解决,我不知道为什么,但这已经解决了问题。
所以我的代码是:
睡眠功能:
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
支付宝;按钮组件:
const paypalRef = useRef();
// options
const btnOptions = {
createSubscription: function (data, actions) {
return actions.subscription.create({
plan_id: planID,
});
},
onApprove: function (data, actions) {
alert(data.subscriptionID);
},
};
// effect, make sure to listen to Paypal library load
useEffect(() => {
(async () => {
await sleep(1000) // sleep second before rendering. <---------------------- solution here;
if (paypalRef.current) {
btn = window.paypal.Buttons(btnOptions);
btn.render(paypalRef.current);
}
})();
}, [window.paypal]);
return <div className={styles.paypal_btn} ref={paypalRef}></div>;
推荐阅读
- python-3.x - 快速 BERT 卡门培尔奶酪,位于不同 GPU 上的参数错误
- java - 对两个不同类对象的列表进行排序
- javascript - 何时使用 addEventHandler 方法
- python - 对 DataFrame 行和列求和
- python - 如何更改请求(GET)URL 中的语言?
- ios - 在 for 循环中使用异步调用处理完成
- continuous-integration - 为以前的管道禁用给定的手动作业
- c++ - 如何阅读这个反编译的语句?
- android - 将数组列表的大小显示到文本视图
- python - 如何通过 Python 连接到 Splunk API,收到 javascript 错误