Search code examples
swiftbraintree

How to find out if card was declined in Braintree Payments


Im currently using braintree as my payment system for my app the issue im having is that that I have no way of finding out if the card was declined or accepted before it proceed to another viewcontroller please take a look at my current function. Thank you in advance.

func postNonceToServer(paymentMethodNonce: String) {

        let paymentURL = NSURL(string: "http://salesandsolutionsplus.com/braintree_php_api/public_html/checkout.php")!

        let postAmount = formInfo["servicePrice"]!

        let request = NSMutableURLRequest(URL: paymentURL)
        request.HTTPBody = "payment_method_nonce=\(paymentMethodNonce)&amount=\(postAmount)".dataUsingEncoding(NSUTF8StringEncoding)
        request.HTTPMethod = "POST"

        NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
            // TODO: Handle success or failure

            let dbRef = FIRDatabase.database().reference()
            let value = self.formInfo


            if error != nil {


                print(error)

                return
            }





            dbRef.child("streetServices/Posts").childByAutoId().setValue(value, withCompletionBlock: { (error, fir) in
                if error != nil {

                    print("alert error")
                    print(error)

                    return
                }

                // present confirmation after payment process
                let next = self.storyboard?.instantiateViewControllerWithIdentifier("vc3") as! ConfirmationViewController

                self.presentViewController(next, animated: true, completion: nil)

            })


        }.resume()
    }

Solution

  • Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

    The result object from a Braintree_Transaction contains a success attribute that will either be true or false. Depending on the value of success, you will either have a transaction_result or an error result to give you more information. Once this reponse is parsed in your server side code, you can send it to the client any way you choose.