2
充值信用卡,我想通過dinquically金額值我的PHP腳本與jQuery。我選擇使用具有不同選項的選擇。但它不安全,因爲用戶可以修改這些值。有任何想法嗎? 這裏是我的腳本:條紋結賬收費 - 通過金額
HTML + JS
<script src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<button id="customButton">Buy</button>
<select id="myselect">
<option value="2000">20 euros</option>
<option value="4000">40 euros</option>
</select>
<script>
var handler = StripeCheckout.configure({
key: '*****************',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
var stripeToken = token.id;
var stripeEmail = token.email;
$.post(
"charge.php",
{ stripeToken: token.id, stripeEmail: stripeEmail, amount: $("#myselect").val()},
function(data) {
console.log(data);
}
);
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
handler.open({
name: 'Test',
currency: 'eur',
amount: $("#myselect").val()
});
e.preventDefault();
});
window.addEventListener('popstate', function() {
handler.close();
});
</script>
PHP(charge.php)
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => $_POST['stripeEmail'],
'card' => $token
));
try {
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $_POST['amount'],
'currency' => 'eur')
));
echo '<h1>Successfully charged'.$_POST['amount'].'</h1>';
}
catch(\Stripe\Error\Card $e) {
echo '<h1>Card declined</h1>';
}
謝謝@Mandeep Gill。問題是select中的值是變量。所以我不能定義一個數組。 –
@EthanHunt您的歡迎。 –