javascript - Stripe Checkout undefined reference 错误

标签 javascript node.js ejs

我正在尝试将 Stripe 放入我的网页中以接受付款。但每当我加载页面时,我都会收到一条错误消息,指出 stripeCheckout 未定义。我知道这与我的脚本标签之一中导入的库有关,但我不确定为什么它显示为未定义。我认为导入的库是正确的。这是我的代码:

    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">





    <!-- CSS Link -->
    <link rel="stylesheet" href="styles.css">

    <title>Store</title>

    <script src="https://checkout.stripe.com/checkout.js" defer></script>
<script>
    var stripePublicKey = '<%= stripePublicKey %>'
</script>
<script src="store.js" defer></script>
</head>

这在我的 store.js 文件中

const stripeHandler = stripeCheckout.configure({
    key: stripePublicKey,
    locale: 'auto',
    token: function(token){

    }
})

// Removes cart items after they are purchased
const purchaseItems = () => {
    // let cartItems = document.getElementsByClassName('cart-items')[0]
    // while(cartItems.hasChildNodes()){
    //     cartItems.removeChild(cartItems.firstChild)
    // }
    // updateCartTotal()

    let priceElement = document.getElementsByClassName('cart-total-price')[0]
    let price = parseFloat(priceElement.innerText.replace('$', '')) * 100
    stripeHandler.open({
        amount: price
    })
}

最佳答案

我相信特定的 Stripe checkout API(和脚本)现已被弃用。

以下是新 API 文档的链接:https://stripe.com/docs/payments/checkout/migration#client-products

文档示例:

// client.html
<script src="https://js.stripe.com/v3"></script>
<button id="checkout-button">Pay</button>

// client.js
var stripe = Stripe('YOUR_API_KEY');

var checkoutButton = document.querySelector('#checkout-button');
checkoutButton.addEventListener('click', function () {
  stripe.redirectToCheckout({
    items: [{
      // Define the product and SKU in the Dashboard first, and use the SKU
      // ID in your client-side code.
      sku: 'sku_123',
      quantity: 1
    }],
    successUrl: 'https://www.example.com/success',
    cancelUrl: 'https://www.example.com/cancel'
  });
});

关于javascript - Stripe Checkout undefined reference 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58685813/

相关文章:

javascript - 自动填充 HTML 字段 - 使用 NodeJs 和 mySQL

javascript - Service Worker 在缓存更新时重新加载页面

javascript - Laravel 5 和 AngularJS ng-view 问题

javascript - 当代码是字符串时,将 Javascript ES6 重新编译为 ES5 代码

node.js - 如何在 Node.js 中获取输入的 POST 数据?

javascript - hexo中的 `__()`(双下划线)函数是什么?

javascript - Angular 1.5 组件 : passing a function

javascript - Node.js 事件循环对我来说没有意义

node.js - Windows 的 Node 版本管理器应该使用什么根目录?

mysql - 使用express.js + mysql + ejs + typeORM 数据未显示在 View 中