Braintree Hosted Fields Integration

时间:2016-03-04 19:46:47

标签: javascript php jquery forms braintree

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

我认为自己是一名中级网络开发人员,并且我会说我对php,html和css有很好的了解。我对javascript了解不多,但可以稍微使用它,但不能像我对PHP的理解那样好。

你能指出我正确的方向让付款页面看起来正确吗?我需要这些元素看起来像注册页面的前两部分。

请注意表单尚未提交,我仍然在研究外观和感觉。但是,一旦整合了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>

2 个答案:

答案 0 :(得分:0)

完全披露:我在Braintree工作。如果您有任何其他问题,请随时与我们的support小组联系。

使用Drop-in或Hosted字段,Braintree为您提供代表用户付款方式的随机数。这减轻了您必须处理信用卡号和有效期信息以及随之而来的PCI负担。以下是将随机​​数与Braintree_Transaction::sale一起使用的示例:

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

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

答案 1 :(得分:0)

这可能对某些人有用,当使用托管字段或插件时,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
  ]
]);

?>