我会尽量保持简洁 .
我正在使用Stripe JS来处理付款 .
我的表格的代码如下:
<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" => "tester@testemail.com",
"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" => "tester@tester",
"customer" => $customer->id
));
} catch(\Stripe\Error\Card $e) {
echo "Your card has been declined";
}
?>
此逻辑工作并添加客户并按预期收费 . 我只是不知道保存 first_name
输入值的方法是否是最佳做法 . 我试图在 Stripe.card.createToken
函数中添加输入值,但它不起作用,我在 POST
进程中得到一个未定义的变量 .
我找不到另一种方法将输入值/变量发布到 POST
.
POST
输入值和变量的最佳做法是什么?
任何帮助深表感谢 .
谢谢
萌
1 回答
您不能将
first_name
参数作为参数传递给Stripe.card.createToken,因为此调用不需要这样的参数 . (完整的持卡人姓名有一个可选的name
参数 . )向服务器发送(非PCI敏感)字段的最简单方法是向字段添加
name
属性,以便将其提交到服务器,而无需将其重新创建为隐藏字段 .基本上:
非卡相关输入字段应具有
name="..."
属性 . 卡相关的输入字段不应该(否则它们会被提交到您的服务器,否定令牌化的目的并使您不符合PCI SAQ A的条件) .在表单的提交处理程序中,使用Stripe.js创建标记,并将标记作为隐藏字段添加到表单中 .
提交表格 . 浏览器会将每个命名字段发送到表单的
action
URL .