将输入值和变量传递给 POST 的最佳实践?

Best practice to pass input values and variables to POST?

我会尽量保持简洁。
我正在使用 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 输入值,这样当我向用户收费时,我可以看到更多用户信息。
我把它缩短了,完整版有地址,post代码,电子邮件等

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

POST 输入值和变量的最佳做法是什么?

非常感谢任何帮助。
谢谢

您不能将 first_name 参数作为参数传递给 Stripe.card.createToken,因为此调用不需要这样的参数。 (持卡人全名有一个可选的 name 参数。)

将(non-PCI 敏感)字段发送到您的服务器的最简单方法是向该字段添加一个 name 属性,这样它就可以提交到您的服务器,而您不必将其重新创建为隐藏字段。

基本上:

  1. non-card 相关输入字段应具有 name="..." 属性。卡片相关的输入字段应该 而不是 (否则它们会被提交到您的服务器,否定标记化的目的并使您没有资格 PCI SAQ A)。

  2. 在表单的提交处理程序中,使用 Stripe.js 创建令牌并将令牌作为隐藏字段添加到表单中。

  3. 提交表格。浏览器会将每个命名字段发送到表单的 action URL.