使用Ajax Braintree付款的空Payment_method_nonce

时间:2018-12-31 02:20:04

标签: php braintree

我正在尝试创建Braintree ajax付款,但是无法在ajax帖子中获得<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Rock Paper Scissors</title> <meta name="description" content="DESCRIPTION"> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>Rock Paper Scissors</h1> </header> <div class="score-board"> <div id="user-label" class="badge">user</div> <div id="computer-label" class="badge">comp</div> <span id="user-score">0</span>:<span id="computer-score">0</span> </div> <div class="result"> <p>Paper cover rock. You win!</p> </div> <div class="choices"> <div class="choice" id="r"> <img src="images/rock.png" alt="rock"> </div> <div class="choice" id="p"> <img src="images/paper.png" alt="paper"> </div> <div class="choice" id="s"> <img src="images/scissors.png" alt="scissors"> </div> </div> <p id="action-message">Make your move</p> </body> <script src="app.js"></script> </html>的价值。

结果:

payment_method_nonce

如何解决此问题并获得成功消息?

index.php

91508: Cannot determine payment method.

payment.php

<!DOCTYPE html>
<html lang='en'>
<head>

  <meta charset='UTF-8'>
    <title>Pay with BrainTree</title>

  <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'>
  <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
  <script src='https://js.braintreegateway.com/js/braintree-2.31.0.min.js'></script>

  <script>
  $('#checkout_card').submit(function(event){
    event.preventDefault();
    $.ajax({
      url: 'token.php',
      type: 'get',
      dataType: 'json',
      success: function (data) {
        braintree.setup(data, 'custom', {
          id: 'checkout_card',
          clientToken: 'sandbox_zjkxfrp2_8tkp93dz64x39pgs'
        });
      }
    });
  });
  </script>

</head>
<body>

  <form action='payment.php' method='post' class='payment-form' id='checkout_card'>

    <input type='hidden' name='payment_method_nonce'>

    <input type='text' name="number" value="4242424242424242">
    <input type='text' name="cvv" value="123">

    <input type='text' name="expiration_month" value="12">
    <input type='text' name="expiration_year" value="2019">

    <input type='text' name='firstName' value='John'>
    <input type='text' name='lastName' value='Smith'>
    <input type='text' name='amount' value='50'>

    <button type='submit'>Submit</button>

  </form>

</body>
</html>

boot.php

<?php
require "boot.php";

if ($_POST) {
    print_r($_POST);
    $nonce = $_POST["payment_method_nonce"];

    $result = Braintree_Transaction::sale([
        'amount' => $_POST['amount'],
        'paymentMethodNonce' => $nonce,
        'customer' => [
            'firstName' => $_POST['firstName'],
            'lastName' => $_POST['lastName'],
        ],
        'options' => [
            'submitForSettlement' => true
        ]
    ]);

    if ($result->success) {
    echo($result->customer->id);
    echo($result->customer->creditCards[0]->token);
  } else {
    foreach($result->errors->deepAll() AS $error) {
      echo($error->code . ": " . $error->message . "\n");
    }
  }
}

token.php

<?php
require "vendor/autoload.php";

Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('...');
Braintree_Configuration::publicKey('...');
Braintree_Configuration::privateKey('...');

0 个答案:

没有答案