2016-04-25 52 views
0

我試圖在我的網站上設置一個收集卡片詳細信息(使用條紋),另存爲客戶,並在以後收費的部分。查看了幾個教程,但仍然出現錯誤,特別是:條紋標記錯誤

未定義的變量:第21行中的/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/billing.php中的令牌無法將客戶ID保存到db 。我希望能夠將客戶ID保存到我的數據庫(用戶已創建)中的用戶表中,以便以後用於付款,但對於我而言,我似乎無法通過此錯誤!任何幫助將不勝感激。

在此先感謝

王小強

這裏是我到目前爲止的代碼:

支付頁面,用表格:

<?php $userID = (int) $_GET['id']; 
require('../inc/connect/config.php'); 
?> 
<header> 
<!-- CSS --> 
<link href="admin.css" rel="stylesheet"> 
<link href="bootstrap.min.css" rel="stylesheet"> 

<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 
<?php 
echo '<script type="text/javascript"> 
Stripe.setPublishableKey("' . STRIPE_PUBLIC_KEY . '"); 
</script>'; 
?> 
<script type="text/javascript" src="../js/billing.js"></script> 
</header> 
<body> 
<form action="billing.php?id=<?php echo $userID; ?>" method="POST" id="payment-form"> 
<span id="payment-errors" class=""></span> 

<div class="form-row"> 
    <label> 
    <span>Card Number</span> 
    <input type="text" size="20" data-stripe="number" class="card-number"> 
    </label> 
</div> 

<div class="form-row"> 
    <label> 
    <span>Expiration (MM/YY)</span> 
    <input type="text" size="2" data-stripe="exp_month" class="card-expiry-month"> 
    </label> 
    <span>/</span> 
    <input type="text" size="2" data-stripe="exp_year" class="card-expiry-year"> 
</div> 

<div class="form-row"> 
    <label> 
    <span>CVC</span> 
    <input type="text" size="4" data-stripe="cvc" class="card-cvc"> 
    </label> 
</div> 

<input id="submitBtn" type="submit" class="submit" value="Submit Payment"> 
</form> 
</body> 

billing.js:

function reportError(msg) { 
// Show the error in the form: 
$('#payment-errors').text(msg).addClass('alert alert-danger'); 
// re-enable the submit button: 
$('#submitBtn').prop('disabled', false); 
return false; 
} 

// Assumes jQuery is loaded! 
// Watch for the document to be ready: 
$(document).ready(function() { 

// Watch for a form submission: 
$("#payment-form").submit(function(event) { 

    // Flag variable: 
    var error = false; 

    // disable the submit button to prevent repeated clicks: 
    $('#submitBtn').attr("disabled", "disabled"); 

    // Get the values: 
    var ccNum = $('.card-number').val(), cvcNum = $('.card-cvc').val(), expMonth = $('.card-expiry-month').val(), expYear = $('.card-expiry-year').val(); 

    // Validate the number: 
    if (!Stripe.card.validateCardNumber(ccNum)) { 
     error = true; 
     reportError('The credit card number appears to be invalid.'); 
    } 

    // Validate the CVC: 
    if (!Stripe.card.validateCVC(cvcNum)) { 
     error = true; 
     reportError('The CVC number appears to be invalid.'); 
    } 

    // Validate the expiration: 
    if (!Stripe.card.validateExpiry(expMonth, expYear)) { 
     error = true; 
     reportError('The expiration date appears to be invalid.'); 
    } 

    // Validate other form elements, if needed! 

    // Check for errors: 
    if (!error) { 

     // Get the Stripe token: 
     Stripe.card.createToken({ 
      number: ccNum, 
      cvc: cvcNum, 
      exp_month: expMonth, 
      exp_year: expYear 
     }, stripeResponseHandler); 

    } 

    // Prevent the form from submitting: 
    return false; 

}); // Form submission 

}); // Document ready. 

// Function handles the Stripe response: 
function stripeResponseHandler(status, response) { 

// Check for an error: 
if (response.error) { 

    reportError(response.error.message); 

} else { // No errors, submit the form: 

    var f = $("#payment-form"); 

    // Token contains id, last4, and card type: 
    var token = response['id']; 

    // Insert the token into the form so it gets submitted to the server 
    f.append("<input type='hidden' name='stripeToken' value='" + token + "' />"); 

    // Submit the form: 
    f.get(0).submit(); 

} 

} // End of stripeResponseHandler() function. 

和billing.php:

<?php 
$userID = (int) $_GET['id']; 
require('../inc/connect/config.php'); 
require_once('inc/stripe-php/init.php'); 

session_start(); 
\Stripe\Stripe::setApiKey(STRIPE_PRIVATE_KEY); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
$errors = array(); 
if (isset($_POST['stripeToken'])) { 
    $token = $_POST['stripeToken']; 
} else { 
    $errors['token'] = 'Your payment details cannot be processed. You have not been charged. 
         Please confirm that you have JavaScript enabled and try again.'; 
} 
} // End of form submission conditional. 

// Create a Customer 
$customer = \Stripe\Customer::create(array(
"source" => $token, 
"description" => "Example customer") 
); 
$custID = $customer->id; 

try { 
$sql = 'INSERT INTO users(cust) VALUES(:cust) WHERE id LIKE :id'; 
$query = $db->prepare($sql); 
$query->execute(array(':cust'=>$custID, ':id'=>$userID)); 
header('Location: ../admin/signup.php?joined'); 
} catch (PDOException $e) { 
echo 'failed to save customer id to db'; 
} 

?> 
+1

$令牌在你使用它的點是不明確的。您可能希望將代碼的底部移到if/else部分之一中。 – rjdown

+1

執行print_r($ _ POST)以確保令牌正確傳遞到後端。 –

+0

您的JavaScript使用jquery,但您永遠不會包含它 –

回答

0

這裏是更新PHP:

<?php 
$userID = (int) $_GET['id']; 
require('../inc/connect/config.php'); 
require_once('inc/stripe-php/init.php'); 

session_start(); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
$errors = array(); 
if (isset($_POST['stripeToken'])) { 
    $token = $_POST['stripeToken']; 
    // Check for a duplicate submission, just in case: 
    // Uses sessions, you could use a cookie instead. 
    if (isset($_SESSION['token']) && ($_SESSION['token'] == $token)) { 
     $errors['token'] = 'You have apparently resubmitted the form. Please do not do that.'; 
    } else { // New submission. 
     $_SESSION['token'] = $token; 
    } 
} else { 
    $errors['token'] = 'Your payment details cannot be processed. You have not been charged. 
         Please confirm that you have JavaScript enabled and try again.'; 
} 
if(empty($errors)) { 
\Stripe\Stripe::setApiKey(STRIPE_PRIVATE_KEY); 
// Create a Customer 
$customer = \Stripe\Customer::create(array(
    "source" => $token, 
    "description" => "Example customer") 
); 
$custID = $customer->id; 
try { 
     $sql = 'INSERT INTO users(cust) VALUES(:cust) WHERE id LIKE :id'; 
     $query = $db->prepare($sql); 
     $query->execute(array(':cust'=>$custID, ':id'=>$userID)); 
     header('Location: ../admin/signup.php?joined'); 
} catch (PDOException $e) { 
    echo 'failed to save customer id to db'; 
} 
} 
} // End of form submission conditional. 

?>