【问题标题】:Braintree Hosted Fields IntegrationBraintree 托管字段集成
【发布时间】:2016-03-04 19:46:46
【问题描述】:

我正在尝试在我的注册表单上设置 Braintree 托管字段。但是这些领域之间存在巨大的差距,而且看起来并不正确。我尝试过使用 Braintree 文档和指南,但它们非常模糊。

我认为自己是一名中级 Web 开发人员,我会说我对 php、html 和 css 有很好的了解。我对javascript知之甚少,但可以使用一点,但不如我对PHP的理解那么好。

您能否为我指明正确的方向以使付款页面看起来正确?我需要元素看起来像注册页面的前 2 部分..

请注意,表单尚未提交,我仍在处理外观。但是一旦集成了braintree,它只需要捕获表单数据并将其提交到数据库即可。

这是表格的链接:http://www.mcr-qr-time.com/registration-form/register.html?plan=1

并查看下面的代码:(我已删除 API 密钥)

    <?php
$plan = $_GET['plan'];
if($plan == "1"){
$amount = "4.99";
}
if($plan == "2"){
$amount = "14.99";
}
if($plan == "3"){
$amount = "34.99";
}
require_once 'braintree-php-3.9.0/lib/Braintree.php';

Braintree_Configuration::environment('production');
Braintree_Configuration::merchantId('#');
Braintree_Configuration::publicKey('#');
Braintree_Configuration::privateKey('#');
if(isset($_POST['submit'])){
    /* process transaction */
    $result = Braintree_Transaction::sale(array(
     'amount' => $amount,
     'creditCard' => array(
     'number' => '(dont know what to do here)',
     'expirationDate' => '(dont know what to do here)'
    )
  ));

if ($result->success) {
  print_r("success!: " . $result->transaction->id);
  } else if ($result->transaction) {
    print_r("Error processing transaction:");
    print_r("\n  code: " . $result->transaction->processorResponseCode);
    print_r("\n  text: " . $result->transaction->processorResponseText);
    } else {
      print_r("Validation errors: \n");
      print_r($result->errors->deepAll());
    }
}

$clientToken = Braintree_ClientToken::generate();

?>
<!DOCTYPE html>
<html lang="en">

    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Registration form</title>

        <!-- CSS -->
        <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500">
        <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="assets/css/form-elements.css">
        <link rel="stylesheet" href="assets/css/style.css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesnt work if you view the page via file:// -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <!-- Favicon and touch icons -->
        <link rel="shortcut icon" href="//www.mcr-qr-time.com/img/member-1.png">
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">

        <style>
.img-circle {
    border-radius: 50%;
}
</style>

    </head>

    <body>
<div class="contain">
        <!-- Top menu -->
        <nav class="navbar navbar-inverse navbar-no-bg" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#top-navbar-1">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="../index.html"><h1 class="navbar-brand">MCR Qr Time</h1></a>
                </div>
                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="top-navbar-1">
                    <ul class="nav navbar-nav navbar-right">
                        <li>

                            <span class="li-text">
                                Return to homepage: 
                            </span> 
                            <span class="li-social">
                                <a href="//mcr-qr-time.com"><i class="fa fa-home"></i></a> 
                            </span>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>

        <!-- Top content -->
        <div class="top-content">

            <div class="inner-bg">
                <div class="container">
                    <div class="row">
                    <br><br>
                        <div class="col-sm-8 col-sm-offset-2 text">

                            <div class="description">
                                <p>

                                </p>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-6 col-sm-offset-3 form-box">

                            <form role="form" action="register.php(not set up yet)" method="post" class="registration-form" id="form">

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                        <h1><strong>Registration Form</strong></h1><br>
                                            <h3>Step 1 / 3</h3>
                                            <p>Please fill out this form and once you have paid and confirmed your email you will be able to log in to your dashboard.</p>
                                        </div>
                                        <div class="form-top-right">
                                            <img class="img-circle" src="//www.mcr-qr-time.com/img/member-1.png" width="100" height="100">
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-first-name">First name</label>
                                            <input type="text" name="form-first-name" placeholder="First name..." class="form-first-name form-control" id="fname">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-last-name">Last name</label>
                                            <input type="text" name="form-last-name" placeholder="Last name..." class="form-last-name form-control" id="sname">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-company">Company</label>
                                            <input type="text" name="form-company" placeholder="company" class="form-control" id="company">
                                        </div>
                                        <button type="button" class="btn btn-next">Next</button>
                                    </div>
                                </fieldset>

                                <fieldset>
                                    <div class="form-top">
                                        <div class="form-top-left">
                                            <h3>Step 2 / 3</h3>
                                            <p>Set up your account:</p>
                                        </div>
                                        <div class="form-top-right">
                                            <i class="fa fa-key"></i>
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                        <div class="form-group">
                                            <label class="sr-only" for="form-email">Email</label>
                                            <input type="text" name="form-email" placeholder="Email..." class="form-email form-control" id="form-email">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-password">Password</label>
                                            <input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-repeat-password">Repeat password</label>
                                            <input type="password" name="form-repeat-password" placeholder="Repeat password..." 
                                                        class="form-repeat-password form-control" id="form-repeat-password">
                                        </div>
                                        <button type="button" class="btn btn-previous">Previous</button>
                                        <button type="button" class="btn btn-next">Next</button>
                                    </div>
                                </fieldset>

                                <fieldset>
                                    <div class="form-top">

                                        <div class="form-top-left">
                                            <h3>Step 3 / 3</h3>
                                            <p>Subscription payment setup:</p>
                                        </div>
                                        <div class="form-top-right">
                                            <i class="glyphicon glyphicon-credit-card"></i>
                                        </div>
                                    </div>
                                    <div class="form-bottom">
                                                <div class="form-group">
                                        <label for="card-number-field">Card Number</label>
                                                        <div id="card-number-field"></div>
                                                        </div>
                                                        <div class="form-group">
                                                        <label for="security-code-field">CVV</label>
                                                        <div id="security-code-field"></div>
                                                        </div>
                                                        <div class="form-group">
                                                        <label for="expiration-date-field">Expiration Date</label>
                                                        <div id="expiration-date-field"></div>
                                                        </div>
                                        <button type="button" class="btn btn-previous">Previous</button>
                                        <button type="submit" class="btn">Sign me up!</button>
                                    </div>
                                </fieldset>

                            </form>
                            <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
                            <script src="https://js.braintreegateway.com/js/braintree-2.21.0.min.js"></script>   
    <script>
        braintree.setup('<?php print $clientToken; ?>', 'custom', {
  id: 'form',
  hostedFields: {
    number: {
      selector: '#card-number-field',
      placeholder: 'Card Number'
    },
    cvv: {
      selector: '#security-code-field',
      placeholder: 'CVV'
    },
    expirationDate: {
      selector: '#expiration-date-field',
      placeholder: 'MM/YYYY'
    }

  }
});
    </script>
                        </div>
                    </div>
                </div>
            </div>

        </div>
</div>

        <!-- Javascript -->
        <script src="assets/js/jquery-1.11.1.min.js"></script>
        <script src="assets/bootstrap/js/bootstrap.min.js"></script>
        <script src="assets/js/jquery.backstretch.min.js"></script>
        <script src="assets/js/retina-1.1.0.min.js"></script>
        <script src="assets/js/scripts.js"></script>

        <!--[if lt IE 10]>
            <script src="assets/js/placeholder.js"></script>
        <![endif]-->

    </body>

</html>

【问题讨论】:

  • 要到达最后一页,只需在字段中输入任何字母,以便继续..
  • 我还上传了braintree-php-3.9.0.tar.gz 并将其解压到我的主目录中,还有什么需要我处理的吗?
  • 您提供的链接与您发布的代码示例不匹配。具体来说,存在输入标签而不是 div,并且不存在托管字段代码。
  • 那是因为braintree不接受我的申请,所以我改了..
  • 我也许可以再次申请,只是想知道是否有人知道我在“/* process transaction */”顶部写“(不知道在这里做什么)”的地方放什么? ?

标签: javascript php jquery forms braintree


【解决方案1】:

全面披露:我在 Braintree 工作。如果您还有任何问题,请随时联系我们的support 团队。

Braintree 使用 Drop-in 或 Hosted Fields 为您提供代表用户付款方式的 nonce。这使您不必处理信用卡号和到期日期信息以及随之而来的 PCI 负担。下面是一个在Braintree_Transaction::sale 中使用随机数的示例:

$result = Braintree_Transaction::sale([
  'amount' => '10.00',
  'paymentMethodNonce' => nonceFromTheClient,
  'options' => [
    'submitForSettlement' => True
  ]
]);

访问Braintree_Transaction::sale reference,了解有关处理随机数的更多信息。

【讨论】:

    【解决方案2】:

    这可能对某些人有用,当使用托管字段或插件时,Braintree 在表单中放置一个隐藏的 nonce 输入,您只需在表单处理脚本中调用它,如下所示:

    <?php
    
    require_once 'braintree-php-3.9.0/lib/Braintree.php';
    
    Braintree_Configuration::environment('production');
    Braintree_Configuration::merchantId('#');
    Braintree_Configuration::publicKey('#');
    Braintree_Configuration::privateKey('#');
    
    $paymentMethodNonce = $_POST['payment_method_nonce'];
    
    $result = Braintree_Transaction::sale([
      'amount' => '10.00',
      'paymentMethodNonce' => $paymentMethodNonce,
      'options' => [
        'submitForSettlement' => True
      ]
    ]);
    
    ?>
    

    【讨论】:

    • $paymentMethodToken$paymentMethodNonce 不同。 Braintree 支付方式令牌是长期存在的令牌,您可以使用它来引用存储在您的保管库中的原始支付方式。付款方式随机数是一次性使用的标识符,用于识别付款方式,直到它存储在保险库中。
    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2017-10-28
    • 2015-08-21
    相关资源
    最近更新 更多