Paypal API Express 结账错误:文档已准备好,元素 [object Object] 不存在

Kim*_*rlo 5 javascript jquery paypal

我正在使用贝宝,我需要在我的表格中呈现多个贝宝快速结帐按钮。

\n\n

我遵循了在此链接中找到的解决方案

\n\n

这就是我所做的。

\n\n
  document.querySelectorAll(\'.btn-paypal\').forEach(function(){\n    var dis = $(this);\n    var amount = dis.data(\'amount\');\n\n    paypal.Button.render({\n\n        env: \'sandbox\', \n\n        style: {\n            label: \'checkout\',\n            size:  \'medium\',    // small | medium | large | responsive\n            shape: \'rect\',     // pill | rect\n            color: \'gold\'      // gold | blue | silver | black\n        },\n\n        client: {\n            sandbox:    \'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-w6Q9rX2BdH1\',\n            production: \'<insert production client id>\'\n        },\n\n        payment: function(data, actions) {\n            return actions.payment.create({\n                 payment: {\n                    transactions: [\n                        {\n                            amount: { total: amount, currency: \'JPY\' }\n                        }\n                    ]\n                }\n            });\n        },\n\n        commit: true,\n\n        onAuthorize: function(data, actions) {\n            return actions.payment.get().then(function(paymentDetails) {\n                console.log(paymentDetails)\n                swal({ \n                    title: "Confirm Payment!",\n                    text: "Confirm the amount of \xc2\xa5"+paymentDetails.transactions[0].amount.total,\n                    type: "info",\n                    confirmButtonClass: "btn btn-danger",\n                    confirmButtonText: "Yes! Continue Payment",\n                    showCancelButton: true,\n                    cancelButtonText: "No! Cancel Transaction"\n                  },\n                  function(){\n                    $(\'.ajax-loading\').show();\n                   return actions.payment.execute().then(function(){\n                    alert(\'Payment Success\');\n                   })\n                });\n            });\n        },\n        onError: function(err) {\n            $(\'.ajax-loading\').hide();\n            swal(\'Ooops!\',\'Paypal failed to load. Please click the paypal button again!\',\'error\');\n            console.log(err);\n        }\n    }, dis);\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我尝试运行此代码时,表中没有显示任何按钮,并且控制台中出现错误,提示:Uncaught Error: Document is ready and element [object Object] does not exist

\n\n

我这里的错误是什么?请帮忙。

\n\n

谢谢!

\n

Hel*_*wag 2

我有一个 Paypal 按钮,但遇到了同样的错误。HTML 示例指出

<div id="paypal-button"></div> 
Run Code Online (Sandbox Code Playgroud)

布伦特里手册

而脚本位于单独的文件中。脚本加载时,div 元素似乎尚未呈现。

我只是将脚本移至 div 元素内,现在按钮出现了。确保在此之前脚本不会在其他地方加载。