2016-06-28 36 views
1

我在網站上使用Stripe/Checkout以接受付款。條紋是相當簡單的用這種方式,因爲他們給下面的腳本在網頁中嵌入:在Stripe/Checkout中預填充電子郵件地址

<form id="monthly" action="/subscribe" method="POST"> 
    <script 
    src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
    data-key="pk_test_xxxxxxxxx" 
    data-image="http://mywebsite.com/assets/img/logo_raw.png" 
    data-name="My Website" 
    data-description="Monthly Subscription" 
    data-amount="6900" 
    data-email="[email protected]" 
    data-allow-remember-me=false 
    data-label="Subscribe" > 
    </script> 
</form> 

我希望能夠做的是預先填充的電子郵件地址值(數據電子郵件= 「[email protected]」)與我的custom.js jQuery腳本中的變量相關聯。我精通jQuery,但我可以用jQuery修改這個嵌入式腳本嗎?

回答

5

您需要使用custom integration,做這樣的事情:

<script src="https://checkout.stripe.com/checkout.js"></script> 

<button id="customButton">Purchase</button> 

<script> 
    var email = "[email protected]"; // or use jQuery to dynamically populate the variable 

    var handler = StripeCheckout.configure({ 
    key: 'pk_test_...', 
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png', 
    locale: 'auto', 
    token: function(token) { 
     // You can access the token ID with `token.id`. 
     // Get the token ID to your server-side code for use. 
    } 
    }); 

    $('#customButton').on('click', function(e) { 
    // Open Checkout with further options: 
    handler.open({ 
     name: 'Stripe.com', 
     description: '2 widgets', 
     amount: 2000, 
     email: email 
    }); 
    e.preventDefault(); 
    }); 

    // Close Checkout on page navigation: 
    $(window).on('popstate', function() { 
    handler.close(); 
    }); 
</script> 
+0

完美!謝謝。我甚至沒有看到定製集成。 – Joel

+0

我現在遇到的問題是使用自定義集成的複雜性。檢出設置很簡單:我需要做的只是在表單的「操作」部分提供回調。它看起來像自定義腳本,我需要做更多的工作服務器端。 – Joel

+1

使用簡單與定製集成不應該改變任何服務器端。自定義集成僅允許您使用JavaScript調用Checkout而不是使用HTML標記,並允許您定義自己的回調以根據所得令牌執行所需操作。 – Ywain

相關問題