2015-04-02 62 views
0

我試圖在我的網站中實施條形支付,以便客戶可以自行收取金額。條紋文件不是直接的,整合困難。自動輸入條形支付的收費金額

<form action="charge.php" method="POST"> 
    <script 
    src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
    data-key="pk_test_xxxxxxxxxxxxxxxxxxxx" 
    data-amount="CHARGE AMOUNT" 
    data-name="Maid In Raleigh" 
    data-description="Service Charge" 
    data-image="/128x128.png"> 
    </script> 
</form> 

我希望我的客戶更改「數據量」,以便他們可以更改付款的價值。我確信下面給出的charge.php是一團糟。儘管儀表板將令牌註冊到他們的日誌文件中,但我無法使其工作。

<?php 
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxxxxxx"); 
// Get the credit card details submitted by the form 
$token = $_POST['stripeToken']; 
// Create the charge on Stripe's servers - this will charge the user's card 
try { 
$charge = \Stripe\Charge::create(array(
    "amount" => CHARGEAMOUNT, // amount in cents, again 
    "currency" => "usd", 
    "source" => $token, 
    "description" => "Service Charge") 
); 
    echo "<h2>Thank you!</h2>" 
    echo $_POST['stripeEmail']; 
} catch(\Stripe\Error\Card $e) { 
    // The card has been declined 
} 
    echo "<h2>Thank you!</h2>" 
?> 

有沒有什麼辦法可以避免從客戶端收費javascript,而是使用PHP來處理?

如果有人能幫忙,謝謝!

回答

1

幾天前我必須使用stripe api來處理動態金額的付款。我已經使用了下面的代碼,我沒有使用名稱空間。但我相信你可以工作。

$card = array(
    "number" => '',  // credit card number you are about to charge 
    "exp_month" => '', // card expire month 
    "exp_year" => '', // card expire year 
    "cvc" => '' // cvc code 
); 

該數組需要生成令牌。

$token_id = Stripe_Token::create(array(
    "card" => $card 
)); 

現在是處理付款的時間。但首先檢查令牌是否有效

if($token_id->id !=''){ 
    $charge = Stripe_Charge::create(array( 
     "amount" => '', // amount to charge 
     "currency" => '', // currency 
     "card" => $token_id->id, // generated token id 
     "metadata" => '' // some metadata that you want to store with the payment 
    )); 

    if ($charge->paid == true) { 
     // payment successful 
    } 
    else{ 
     // payment failed 
    } 
} 
else{ 
    // card is declined. 
} 

我使用此代碼來設置循環支付系統。它的工作!我希望這對你也有幫助。 :)

+0

謝謝!讓我看看我能在這裏做什麼。我會回來讓你知道。 – 2015-04-03 00:52:06