Я пытаюсь создать ajax-платежи Braintree, но не могу получить значение payment_method_nonce
в сообщении ajax.
Результат:
91508: Cannot determine payment method.
Как мне решить эту проблему и получитьсообщение об успехе?
index.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>
payment.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");
}
}
}
boot.php
<?php
require "vendor/autoload.php";
Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('...');
Braintree_Configuration::publicKey('...');
Braintree_Configuration::privateKey('...');
token.php
<?php
require "boot.php";
echo json_encode(Braintree_ClientToken::generate());