Skip to content

Instantly share code, notes, and snippets.

@erichelgeson
Created January 17, 2017 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichelgeson/16aa9b534a3111023c2b09009a8a9d24 to your computer and use it in GitHub Desktop.
Save erichelgeson/16aa9b534a3111023c2b09009a8a9d24 to your computer and use it in GitHub Desktop.
package com.grails3book.stripe
import com.agileorbit.gambda.ApiGatewayHandler
import com.agileorbit.gambda.ApiGatewayRequest
import com.agileorbit.gambda.ApiGatewayProxyResponse
import com.amazonaws.services.lambda.runtime.Context
import com.stripe.Stripe
import com.stripe.model.Charge
import groovy.transform.CompileStatic
@CompileStatic
class StripeChargeHandler implements ApiGatewayHandler {
ApiGatewayProxyResponse handleRequest(ApiGatewayRequest request, Context context) {
CORS = true
log.info("request: $request")
Stripe.apiKey = System.getenv("STRIPE_KEY")
def amount = System.getenv("PRICE") ?: 2500
String token = request?.queryStringParameters?.stripeToken
String email = request?.queryStringParameters?.stripeEmail
Map params = [:]
Charge charge
if(token && email) {
params.amount = amount
params.currency = "usd"
params.description = "Practical Grails 3"
params.source = token
params.metadata = [email: email]
try {
charge = Charge.create(params)
log.info(charge)
success([success: "Charge succeeded!", id: charge.id, email: email])
} catch(e) {
error([error: e.message])
}
} else {
error([error: "No token or email provided."])
}
}
}
package com.grails3book.stripe
import com.amazonaws.services.lambda.runtime.Context
import com.agileorbit.gambda.ApiGatewayProxyResponse
import com.agileorbit.gambda.ApiGatewayRequest
import com.agileorbit.gambda.mocks.MockContextBuilder
import spock.lang.Ignore
import spock.lang.Shared
import spock.lang.Specification
class StripeChargeHandlerSpec extends Specification {
@Shared Context ctx = new MockContextBuilder('hello-world')
.withRemainingTimeInMillis(2000)
.build()
@Shared StripeChargeHandler handler = new StripeChargeHandler()
def "No Token"() {
when:
ApiGatewayRequest request = new ApiGatewayRequest()
ApiGatewayProxyResponse response = handler.handleRequest(request, ctx)
then:
with(response) {
body == '{"error":"No token or email provided."}'
statusCode == 400
headers.containsKey('Access-Control-Allow-Origin')
}
}
def "No email"() {
when:
ApiGatewayRequest request = new ApiGatewayRequest(queryStringParameters: [stripeToken: "badToken"])
ApiGatewayProxyResponse response = handler.handleRequest(request, ctx)
then:
with(response) {
body == '{"error":"No token or email provided."}'
statusCode == 400
headers.containsKey('Access-Control-Allow-Origin')
}
}
def "Charge failed bad token"() {
when:
ApiGatewayRequest request = new ApiGatewayRequest(queryStringParameters: [stripeToken: "badToken", stripeEmail: "foo@bar.com"])
ApiGatewayProxyResponse response = handler.handleRequest(request, ctx)
then:
with(response) {
body == '{"error":"No such token: badToken"}'
statusCode == 400
headers.containsKey('Access-Control-Allow-Origin')
}
}
def "Cannot use a Stripe token more than once"() {
when:
ApiGatewayRequest request = new ApiGatewayRequest(queryStringParameters: [stripeToken: "tok_19cbsMIZKD2cSOPQsfSG93al", stripeEmail: "foo@bar.com"])
ApiGatewayProxyResponse response = handler.handleRequest(request, ctx)
then:
with(response) {
statusCode == 400
headers.containsKey('Access-Control-Allow-Origin')
}
}
@Ignore("Need live token for this to work, test manually")
def "Success"() {
when:
ApiGatewayRequest request = new ApiGatewayRequest(queryStringParameters: [stripeToken: "tok_19XCOuIZKD2cSOPQ3zt0YUth", stripeEmail: "foo@bar.com"])
ApiGatewayProxyResponse response = handler.handleRequest(request, ctx)
then:
with(response) {
statusCode == 200
headers.containsKey('Access-Control-Allow-Origin')
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment