Elena Rubilova
Elena Rubilova

Reputation: 469

Braintree API: Cannot determine payment method for sandbox transactions

I'm currently testing Braintree API with sandbox. When I post nonce to server I get the messages from Braintree API: Amount is required. Cannot determine payment method. I found out that I can use hard coded value for nonce: paymentMethodNonce: "fake-valid-nonce" and in this case I can see the transaction in sandbox. But I would like to see the credit card that I entered in drop-in UI. What might be the cause of "Cannot determine payment method" message? My server-side node.js code is the following:

  var amount = req.body.amount;
  // Use the payment method nonce here
  var nonceFromTheClient = req.body.paymentMethodNonce;

  var newTransaction = gateway.transaction.sale({
                      amount: amount,                      
                      //paymentMethodNonce: "fake-valid-nonce",

                      paymentMethodNonce: nonceFromTheClient,
                      options: {                          
                      submitForSettlement: true }
                      }, function(error, result) {
                      if (result) {
                          res.send(result);
                      } else {                               
                          res.status(500).send(error);
                      }
                   });  

My client side code in Swift:

func sendRequestPaymentToServer(nonce: String, amount: String) {

    let paymentURL = URL(string: "http://localhost:5000/checkout")!
    var request = URLRequest(url: paymentURL)
    request.httpBody = "paymentMethodNonce=\(nonce)&amount=\(amount)".data(using: String.Encoding.utf8)
    request.httpMethod = "POST"

    URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
        guard let data = data else {
            print(error!.localizedDescription)
            return
        }
        if let result = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
            if result?["success"] as? Bool == true {
                print("Successfully charged. Thanks So Much :)")
            }
            else if let message = result?["message"] {
                print(message)
            }
            //dump(result)
        } else {
            print("No json result.")
        }
        }.resume()
}

Upvotes: 5

Views: 4649

Answers (1)

Elena Rubilova
Elena Rubilova

Reputation: 469

The issue was in my body parser - it wasn't configured properly. Solved the issue by the following line of code:

.use(bodyParser.urlencoded({extended: true}))

after user @CJoseph confirmed that the code works on her side.

Upvotes: 3

Related Questions