2017-08-14 182 views
1

我有一段代碼在工作,我想知道是否有可能在結賬過程的「成功代幣」內進行分條付費?下面的所有代碼都在工作,我只是想知道在哪裏插入創建acctuall費用的代碼。 使用JavaScript創建條紋費用

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

    <button id="payment-button" type="button" class="btn btn-success">Pay With Card</button> 

    <script> 
    var handler = StripeCheckout.configure({ 
     key: 'mykeyhere', 
     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. 
     document.getElementById("book-appointment-submit").style.display="block"; 
     document.getElementById("payment-button").style.display="none"; 
     } 
    }); 

    document.getElementById('payment-button').addEventListener('click', function(e) { 
     // Open Checkout with further options: 
     handler.open({ 
     name: 'DenchCodeLTD', 
     description: '2 widgets', 
     zipCode: true, 
     currency: 'gbp', 
     amount: 2000 
     }); 
     e.preventDefault(); 
    }); 

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

<!-- End Of Stripe Payment --> 

<button id="book-appointment-submit" type="button" class="btn btn-success" style="display:none"> 
    <span class="glyphicon glyphicon-ok"></span> 
    <?php 
     echo (!$manage_mode) ? $this->lang->line('confirm') 
       : $this->lang->line('update'); 
    ?> 
</button> 

回答

2

你不能在JS內部收費。使用Stripe爲客戶收費是一個兩步驟的過程。

第1步:您通過Checkout收集客戶的信用卡信息,然後發送給Stripe。你找回一個令牌。這就是你在上面的代碼中所做的。

步驟2:您必須將此令牌傳遞給您的後端,然後通知Stripe對其進行收費,或將其保存至客戶以便稍後進行收費。您需要在您的服務器上使用腳本(PHP,Ruby等)來處理此步驟。

第二步的一些短的例子在這裏:https://stripe.com/docs/charges

內以最低的token: function(token) { }回調你要搶token.id,其追加到和.submit()一個<form></form>周圍您的Checkout按鈕。

如果您想要一些更復雜的東西,您可以通過AJAX請求將令牌ID傳遞給您的後端,然後將收費請求的成功或失敗轉交給用戶。

https://stripe.com/docs/checkout#integration-custom