Search code examples
androidfirebasebraintree

Having trouble implementing Braintree payment for Android using Firebase as backend


I get this output on firebase: "Cannot determine payment method"

Note: I don't have too much knowledge on JavaScript and Firebase functions and can't get this to work.

I have been following Braintree's guide to setting up the payment method. This is the flow of events:

Step 1 Your front-end requests a client token from your server and initializes the client SDK.

Step 2 Your server generates and sends a client token back to your client using the server SDK.

Step 3 The customer submits payment information, the client SDK communicates that information to Braintree and returns a payment method nonce.

Step 4 Your front-end sends the payment method nonce to your server.

Step 5 Your server code receives the payment method nonce and then uses the server SDK to create a transaction.

So far I've been stuck at step 5. The nonce gets posted to my Firebase database in the following format:

-nonce
   -LsHNQLklFEX48oiSt1H:"tokencc_bc_vbmypn_dvn4gy_xy47fw_th6vv9_bq6"
   -LsHn2hilKlmVeT1g1Ow: tokencc_bf_8qrjsk_j7bg44_p8kk88_qz7vn2_vk6"

How would i go about step 5?

Functions code:

'use strict';

const functions = require('firebase-functions');
const express = require('express');
const app = express(); 
const braintree = require('braintree');
const admin = require('firebase-admin');

admin.initializeApp();

var clientToken;
var nonceFromTheClient;
var transaction;

var gateway = braintree.connect({
                        environment: braintree.Environment.Sandbox,
                        merchantId:"",
                        publicKey: "",
                        privateKey: ""
});

exports.createToken = functions.https.onRequest((request, response) => {

gateway.clientToken.generate({}, function (err, response) {
    if (err){
        console.log('inside error',err);    
    } else {
        clientToken = response.clientToken;
    }
});
response.send(clientToken);
});

exports.createTransaction = functions.https.onRequest((req, res) => {

app.post("/nonce", function (req, res) {
    nonceFromTheClient = req.body.payment_method_nonce;
    console.log("nonce" + nonceFromTheClient)
});

gateway.transaction.sale({amount: '10.00', paymentMethodNonce: nonceFromTheClient, options: {submitForSettlement: true}}, function (err, result) {
    if (err) {
        console.error(err);
        return;
    }
    if (result.success) {
        console.log('Transaction status: ' + result.transaction.status);
    } else {
        console.error(result.message);
    }
});
res.send(transaction);
});

Activity code:

if (cardPayment.isChecked) {
            val client = AsyncHttpClient()
            client.get(
                "https://.../createToken",
                object : TextHttpResponseHandler() {
                    override fun onFailure(
                        statusCode: Int,
                        headers: Array<out Header>?,
                        responseString: String?,
                        throwable: Throwable?
                    ) {

                    }

                    override fun onSuccess(
                        statusCode: Int,
                        headers: Array<Header>,
                        clientToken: String
                    ) {
                        mClientToken = clientToken

                        onBraintreeSubmit(it)
                    }
                })
        }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_CODE && data != null) {
        if (resultCode == Activity.RESULT_OK) {
            val result =
                data.getParcelableExtra<DropInResult>(DropInResult.EXTRA_DROP_IN_RESULT)
            val nonce = result.paymentMethodNonce?.nonce
            if (nonce != null) {
                FirebaseShoppingCartDatabase.postNonceToDatabase(nonce)
                val client = AsyncHttpClient()

                client.get(
                    "https://..../createTransaction",
                    object : TextHttpResponseHandler() {
                        override fun onFailure(
                            statusCode: Int,
                            headers: Array<out Header>?,
                            responseString: String?,
                            throwable: Throwable?
                        ) {

                        }

                        override fun onSuccess(
                            statusCode: Int,
                            headers: Array<Header>,
                            clientToken: String
                        ) {

                        }
                    })
            }

        } else if (resultCode == RESULT_CANCELED) {

        } else {
            // handle errors here, an exception may be available in
            val error = data.getSerializableExtra(DropInActivity.EXTRA_ERROR) as Exception
            System.out.println("eroare " + error)
        }
    }
}

fun onBraintreeSubmit(v: View) {
    val dropInRequest = DropInRequest()
        .clientToken(mClientToken)
    dropInRequest
        .disablePayPal()
    startActivityForResult(dropInRequest.getIntent(context), REQUEST_CODE)
}
}

Solution

  • Got it to work. I was passing the nonce to the server incorrectly. (using a different URL)