braintree沙箱和paymentMethodNonce

时间:2015-12-14 00:23:09

标签: php paypal braintree

我正在测试与php和braintree沙箱的braintree集成。 我测试了静态nonce,一切都很好,工作正常。 但是,如果我测试信用卡测试,并尝试从客户端获取paymentMethodNonce我得到一个错误 未知的paymentMethodNonce。

我设置了我的商家ID和所有内容,服务器端我生成了ClientToken。是否可以在沙箱中传递paymentMethodNonce或仅在生产中传递?

我在开头包含此文件:     

Braintree_Configuration::environment('sandbox');
Braintree_Configuration::merchantId('mymerchantid');
Braintree_Configuration::publicKey('mypublickey');
Braintree_Configuration::privateKey('myprivatekey');

$clientToken = Braintree_ClientToken::generate();

这是我的页面,其格式为:

<form id="checkout" method="post" action="/checkout/index.php" autocomplete="off">
  <input type="hidden" name="payment_method_nonce" id="payment_method_nonce">
  <input type="submit" value="Pay">
</form>

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
  var clientToken = "<?php print $clientToken; ?>";

  braintree.setup(clientToken, "dropin", {
    container: "payment-form",
    form: "checkout",
    onPaymentMethodReceived: function (nonce) {
      console.log(nonce);
      var form = document.getElementById('checkout');
      var payment_method_nonce = document.getElementById('payment_method_nonce');
      payment_method_nonce.value = nonce;
      payment_method_nonce.form.submit();
    }
  });
</script>

这是结帐页面:

$nonce = $_POST["payment_method_nonce"];

$result = Braintree_Transaction::sale(array(
   'amount' => '10.00',
   'paymentMethodNonce' => $nonce,
));

if ($result->success) {
   echo("Success! Transaction ID: " . $result->transaction->id);
} else if ($result->transaction) {
   echo("Error: " . $result->message);
   echo("<br/>");
   echo("Code: " . $result->transaction->processorResponseCode);
} else {
   echo("Validation errors:<br/>");
   foreach (($result->errors->deepAll()) as $error) {
     echo("- " . $error->message . "<br/>");
     echo("- " . $error->code . "<br/>");
   }
}

0 个答案:

没有答案