未定义Stripe Checkout参考错误

时间:2019-11-04 00:17:29

标签: 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
    })
}

1 个答案:

答案 0 :(得分:1)

我认为特定的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'
  });
});