2
チャージクレジットカードの場合、jQueryを使用してphpスクリプトにdinamically amount値を渡したいと思います。私は、さまざまなオプションで選択を使用することを選択します。しかし、ユーザーがこれらの値を変更できるため安全ではありません。何か案は?ここ は私のスクリプトです:ストライプチェックチャージ負担金額
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の値が変数であることです。だから私は以前に配列を定義することはできません。 –
@エースンハント大歓迎です。 –