Search code examples
javaamazon-web-serviceshttp-postaws-lambdabraintree

How do I get the parameters from a webook notification into my AWS Lambda function?


I am a bit confused as to how I am meant to hook up my Lambda functions via API Gateway to Braintree's Webhooks. I know webhooks invoke my lambda functions via api gateway via and endpoint URL but I am unsure how to set up my lambda function to handle this properly and use the values that webhooks will pass as parameters when invoking the function. I have the following right now:

package com.amazonaws.lambda.submerchantapproved;

import java.util.HashMap;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import com.amazonaws.services.lambda.runtime.events.DynamodbEvent;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Environment;
import com.braintreegateway.WebhookNotification;
import com.braintreegateway.WebhookNotification.Kind;

public class SubmerchantApproved implements RequestHandler<Object, String> {


    public String handleRequest(Object request, Context context) {

        BraintreeGateway gateway = new BraintreeGateway(
              Environment.SANDBOX,
              "MyValue",
              "MyValue",
              "MyValue"
        );

        WebhookNotification webhookNotification = gateway.webhookNotification().parse(
                request.queryParams("bt_signature"),
                request.queryParams("bt_payload")
       );


        String woofer = "";



        return woofer;
    }

}

This is not working or correct though. How exactly am I meant to get these bt_signature and by_payload values into my lambda function?? The webhooks pass the data in via a http-POST request which is relevant.


Solution

  • Well, your Object request is exactly where those request parameters should be.

    There are two main scenarios for Java Lambdas:

    1. You can configure API Gateway to in proxy mode and parse input stream in you Java code. AWS guys were very kind to write this ready-to-use example for you: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-java enter image description here
    2. You can use traditional API Gateway mapping, but then you'll have to implement a request class that will implement those parameters like bt_signature and by_payload. Again, a great template/example is available from AWS: http://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html