简体   繁体   English

加载 PayPal JS SDK 并渲染智能按钮 onclick/onmouseover

[英]Load PayPal JS SDK and render smart buttons onclick/onmouseover

I'd like to use the Paypal's smart buttons, because payment by card is also inserted as an option and the visitor/customer doesn't need to leave my page in order to purchase.我想使用 Paypal 的智能按钮,因为卡支付也作为一个选项插入,并且访问者/客户无需离开我的页面即可购买。 Using the Pp code works fine, but I prefer to load the external javascript and to render the buttons on click or mouse over, mostly for speed and SEO purposes.使用 Pp 代码工作正常,但我更喜欢加载外部 javascript 并在单击或鼠标悬停时呈现按钮,主要是为了速度和 SEO 目的。 From my little knowledge, the below should work:据我所知,以下应该有效:

 <div id="paypal-button-container"></div> <a href="#" onclick="initPayPalButton();this.onclick= null;">Pay by Paypal</a> <script> // uncommented by me window.addEventListener('load', function() { function initPayPalButton() { paypal.Buttons({ // and here is the Uncaught ReferenceError: paypal is not defined style: { shape: 'rect', color: 'gold', layout: 'vertical', label: 'checkout', }, createOrder: function(data, actions) { return actions.order.create({ purchase_units: [{ "description": "My awesome product", "amount": { "currency_code": "USD", "value": 111 } }] }); }, onApprove: function(data, actions) { return actions.order.capture().then(function(orderData) { console.log('Capture result', orderData, JSON.stringify(orderData, null, 2)); const element = document.getElementById('paypal-button-container'); element.innerHTML = ''; element.innerHTML = '<h3>Thank you for your payment;</h3>'; }), }: onError. function(err) { console;log(err). } });render('#paypal-button-container'). //my approach starts var e = document;createElement("script"). e,type = "text/javascript". e,async = 0. e:src = "https.//www.paypal?com/sdk/js,client-id=xxx-yyy&enable-funding=venmo&currency=USD". (document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0]);appendChild(e); //my approach ends } // uncommented by me initPayPalButton(); // uncommented by me }): </script> <.-- uncommented by me <script async src="https.//www?paypal.com/sdk/js?client-id=xxx-yyy&enable-funding=venmo&currency=USD" data-sdk-integration-source="button-factory"></script>-->

Fails miserably, with the error message 'Uncaught ReferenceError: paypal is not defined'.惨遭失败,错误消息为“Uncaught ReferenceError: paypal is not defined”。 You can see above where the error occurs.您可以在上面看到错误发生的位置。

Please help.请帮忙。 I'm using jQuery as well, so any jQuery solution that works will be appreciated.我也在使用 jQuery,因此任何有效的 jQuery 解决方案都将受到赞赏。

I've tried to append the sdk js above the render('#paypal-button-container'), it didn't work.我试过渲染('#paypal-button-container')上方的 append sdk js,它没有用。 I've also tried to have 2 functions triggered by onclick, one to load the sdk and the second to render the buttons... still no use.我还尝试让 onclick 触发 2 个函数,一个用于加载 sdk,第二个用于呈现按钮......仍然没有用。

OK, a little later....the best I could come up with was to have 2 functions: 1 that would load the external sdk and one that would render the buttons, then add 2 onclick events to the Pay by Paypal link:好的,稍后....我能想到的最好的是有 2 个功能:1 个将加载外部 sdk,一个将呈现按钮,然后将 2 个 onclick 事件添加到 Pay by Paypal 链接:

onclick="loadpaypalsdk();setTimeout(initPayPalButton, 3000);"

This seems to work, although the 3s delay is a little annoying.这似乎可行,尽管 3 秒的延迟有点烦人。 Shorter than this is could trigger another error, due to the sdk not being completely loaded.由于 sdk 未完全加载,比这更短可能会触发另一个错误。 Surely there are other better ways?当然还有其他更好的方法吗? Any ideas?有任何想法吗?

You'll need an onload callback function to invoke paypal.Buttons after the PayPal JS SDK script finishes loading.在 PayPal JS SDK 脚本完成加载,您需要一个onload回调 function 来调用 paypal.Buttons。 Here's a loadAsync helper function for this...这是一个 loadAsync 帮助程序 function 用于此...

//helper function
function loadAsync(url, callback) {
  var s = document.createElement('script');
  s.setAttribute('src', url); s.onload = callback;
  document.head.insertBefore(s, document.head.firstElementChild);
}

// Example usage -- a callback function is inlined here
// (but could be a named function like your own initPayPalButton)
loadAsync('https://www.paypal.com/sdk/js?client-id=test&currency=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) {
           //...
        });
    }

  }).render('#paypal-button-container');
});

I don't necessarily endorse your idea of only loading the JS SDK onclick/onmouseover;我不一定赞同你只加载 JS SDK onclick/onmouseover 的想法; the buttons would appear faster if you render them on page load to a container div that is style="display:none;"如果您在页面加载时将按钮渲染到style="display:none;"的容器 div,按钮会显示得更快, and then have your onclick/onmouseover reveal them with $('#paypal-button-container').show(); ,然后让您的 onclick/onmouseover 使用$('#paypal-button-container').show(); or similar或类似的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM