簡體   English   中英

條紋充電

[英]Charging with Stripe

我在將充電與Stripe配合使用時遇到了一個問題。 Composer無法安裝,因此我已手動加載了它。 沒有收到PHP錯誤,我的令牌創建工作正常。 我看不到任何明顯的錯誤或語法錯誤,有人可以指出我正確的方向嗎?

謝謝

index.html代碼:

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


<script>
var handler = StripeCheckout.configure({
    key: 'xxxxxxxxxx',
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
    token: function(token) {
        /* Use the token to create the charge with a server-side script.
         You can access the token ID with `token.id`
         Pass along various parameters you get from the token response
         and your form.*/
        var myData = {
                token: token.id,
                email: token.email,
                amount: 500,
                message: $("#message").val()
        };
        /* Make an AJAX post request using JQuery,
           change the first parameter to your charge script*/
        $.post("charge2.php", myData,function (data) {
            // if you get some results back update results
            $(".results").html("Your charge was successful");
        }).fail(function () {
            // if things fail, tell us
            $(".results").html("I'm sorry something went wrong");
        })
    }
});
document.getElementById('customButton').addEventListener('click', function(e) {
    // Open Checkout with further options
    handler.open({
        name: 'GUTIC',
        description: 'Join today!',
        amount: 500
    });
    e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function () {
    handler.close();
});


</script>

charge2.php

<?php echo // Set your secret key: remember to change this to your live secret key in production
 // See your keys here: https://dashboard.stripe.com/account/apikeys
 require_once('stripe-php/init.php');

 \Stripe\Stripe::setApiKey("pk_test_DapJwVUCol6JDjJ4jsqEr6S7");

 // Token is created using Checkout or Elements!
 // Get the payment token ID submitted by the form:
 $token = $_POST['token'];
 $charge = \Stripe\Charge::create([
     'amount' => 999,
     'currency' => 'usd',
     'description' => 'Example charge',
     'source' => $token,

     print_r($_POST)


 ]);; ?>

看起來問題在於您在對\\Stripe\\Stripe::setApiKey的調用中使用了可發布的密鑰( pk_test_xxx )。但是,您應該在此處使用您的sk_test_xxx密鑰( sk_test_xxx ),因為創建費用需要使用一個sk_test_xxx密鑰用過的。 您可以在https://dashboard.stripe.com/account/apikeys上獲取密鑰。

使用錯誤的密鑰應該會給您一個API錯誤,並帶有403響應代碼。 您可以在Stripe 儀表板日志中看到它。 您可能需要在測試模式和實時模式之間切換(使用左側導航欄中的開關)才能查看相關請求。

另外,在代碼中, print_r語句位於\\Stripe\\Charge::create調用的參數內,這也會給您一個錯誤-相反,它應位於外部,很可能就在setApiKey行之后。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM