Я попробую и сжал его.
Я использую Stripe JS для обработки платежей.
Код для моей формы ниже:Лучшая практика передачи входных значений и переменных POST?
<form id="payment-form" action="components/charge.php" method="POST" onSubmit={this.submitFormHandler.bind(this)}>
<div id="payment-errors" className="payment-errors"></div>
<label>Name</label>
<input className="first-name" id="first_name" type="text" size="50" autoComplete="off" data-stripe="first_name"/>
<input className="card-number" type="text" size="20" autoComplete="off" />
<input className="card-cvc" type="text" size="4" autoComplete="off" />
<input className="card-expiry-month" type="text" size="2" />
<input className="card-expiry-year" type="text" size="4" />
<input type="submit" id="submitBtn" className="submit" value="Submit Payment" />
</form>
код, который обрабатывает форму и заряжает пользователю ниже:
if (!error) {
// Get the Stripe token:
Stripe.card.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, function(status, response){
console.log(response);
if (response.error) {
this.reportError(response.error.message);
} else { // No errors, submit the form.
// Get a reference to the form:
var paymentForm = $("#payment-form");
// Get the token from the response:
var token = response.id;
// Add the token to the form:
paymentForm.append('<input type="hidden" name="stripeToken" value="' + token + '" />');
paymentForm.append('<input type="hidden" name="first_name" value="' + firstName + '" />');
// Submit the form:
paymentForm.get(0).submit();
}
});
}
Я включил paymentForm.append('<input type="hidden" name="first_name" value="' + firstName + '" />');
, чтобы сохранить значение first_name
входного сигнала, так что, когда я заряжаю пользователь я могу видеть дополнительную информацию о пользователе.
Я сократил его, полная версия имеет адрес, почтовый индекс, адрес электронной почты и т.д.
charge.php код ниже:
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey(<MY_KEY_HERE>);
$token = $_POST['stripeToken'];
$first_name = $_POST['first_name'];
try{
\Stripe\Stripe::setApiKey(<MY_KEY_HERE>);
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => "description here",
"email" => "[email protected]",
"shipping" => array(
"name" => "test",
"address" => array(
"line1" => "123 Fake St",
"city" => "Melbourne",
"country" => "Australia",
"postal_code" => "6969"
)
)
)
);
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => 1500, // amount in cents, again
"currency" => "aud",
"description" => "[email protected]",
"customer" => $customer->id
));
} catch(\Stripe\Error\Card $e) {
echo "Your card has been declined";
}
?>
Эта логика работает и добавляет клиента и заряжает их, как и ожидалось , Я просто не знаю, подходит ли метод для сохранения входного значения first_name
. Я попытался добавить входное значение в функцию Stripe.card.createToken
, но он не работает, я получаю неопределенную переменную в процессе POST
.
Я не могу найти другой способ ввода входных значений/переменных в POST
.
Что является наилучшей практикой для значений и переменных ввода POST
?
Любая помощь очень ценится.
Благодаря
Моу