Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added aws-node-stripe-integration #128

Merged
merged 3 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Have an example? Submit a PR or [open an issue](https://github.com/serverless/ex
| [Aws Serve Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-node-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint | nodeJS |
| [Aws Single Page App Via Cloudfront](https://github.com/serverless/examples/tree/master/aws-node-single-page-app-via-cloudfront) <br/> Demonstrating how to deploy a Single Page Application with Serverless | nodeJS |
| [Serverless Single Page App Plugin](https://github.com/serverless/examples/tree/master/aws-node-single-page-app-via-cloudfront/serverless-single-page-app-plugin) <br/> A plugin to simplify deploying Single Page Application using S3 and CloudFront | nodeJS |
| [Aws Node Stripe Integration](https://github.com/serverless/examples/tree/master/aws-node-stripe-integration) <br/> This example for Stripe integration using AWS Lambda and API Gateway. | nodeJS |
| [Aws Text Analysis Via Sns Post Processing](https://github.com/serverless/examples/tree/master/aws-node-text-analysis-via-sns-post-processing) <br/> Example demonstrates how to setup a simple data processing pipeline | nodeJS |
| [Aws Node Twilio Send Text Message](https://github.com/serverless/examples/tree/master/aws-node-twilio-send-text-message) <br/> Send a text message via twilio from aws lambda. [See live demo](http://twilio-serverless-example.surge.sh) | nodeJS |
| [Upload To S3 And Postprocess](https://github.com/serverless/examples/tree/master/aws-node-upload-to-s3-and-postprocess) <br/> Upload a files to S3 to trigger a lambda function. | nodeJS |
Expand Down
9 changes: 9 additions & 0 deletions aws-node-stripe-integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless

# Config yaml (local.yaml)
config/local.yaml
61 changes: 61 additions & 0 deletions aws-node-stripe-integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Stripe Integration Example

This example for Stripe integration using AWS Lambda and API Gateway.

## Use Cases

- Notified about events that happen in a Stripe account.

## Setup

### Install npm packages
```bash
$ npm install
```

### Edit config overrides for production deployment
```bash
$ vi config/local.yaml
```

```yaml
stripe:
test_sk: 'Stripe_Test_Secret_Key_here'
live_sk: 'Stripe_Live_Secret_Key_here'
```

### Deploy!
```bash:development
$ serverless deploy -v
```

or production
```bash:production
$ serverless deploy -v --stage live
```

```
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3 (2.15 MB)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.....
Serverless: Stack update finished...
Serverless: Removing old service versions...
Service Information
service: aws-node-stripe-integration
stage: development
region: us-east-1
api keys:
None
endpoints:
POST - https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/test/stripe/incoming
functions:
incoming: aws-node-stripe-integration-test-incoming

Stack Outputs
ServiceEndpoint: https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/test
ServerlessDeploymentBucketName: aws-node-stripe-integration-serverlessdeploymentbuck-xxxxxxxxxxxx
IncomingLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:000000000000:function:aws-node-stripe-integration-test-incoming:20
```
3 changes: 3 additions & 0 deletions aws-node-stripe-integration/config/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
stripe:
test_sk: 'sk_test_XXXXXXXXXXXXXXXXXXXXXXXX'
live_sk: 'sk_live_XXXXXXXXXXXXXXXXXXXXXXXX'
46 changes: 46 additions & 0 deletions aws-node-stripe-integration/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const ConfigFile = require('config'); // eslint-disable-line

module.exports.incoming = (event, context, callback) => {
const requestContextStage =
event.requestContext
? event.requestContext.stage
: 'test';
const stripeApiKey =
requestContextStage === 'test'
? ConfigFile.stripe.test_sk
: ConfigFile.stripe.live_sk;
const stripe = require('stripe')(stripeApiKey); // eslint-disable-line

try {
// Parse Stripe Event
const jsonData = JSON.parse(event.body); // https://stripe.com/docs/api#event_object

// Verify the event by fetching it from Stripe
console.log("Stripe Event: %j", jsonData); // eslint-disable-line
stripe.events.retrieve(jsonData.id, (err, stripeEvent) => {
const eventType = stripeEvent.type ? stripeEvent.type : '';
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Stripe webhook incoming!',
stage: requestContextStage,
}),
};
console.log("Event Type: %j", eventType); // eslint-disable-line

// Branch by event type
switch (eventType) {
case 'invoice.created':
// invoice.created event
break;
default:
break;
}
callback(null, response);
});
} catch (err) {
context.fail(err);
}
};
17 changes: 17 additions & 0 deletions aws-node-stripe-integration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "aws-node-stripe-integration",
"version": "1.0.0",
"description": "This example for Stripe integration using AWS Lambda and API Gateway.",
"main": "handler.js",
"dependencies": {
"config": "^1.24.0",
"js-yaml": "^3.7.0",
"stripe": "^4.14.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
53 changes: 53 additions & 0 deletions aws-node-stripe-integration/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: aws-node-stripe-integration

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

provider:
name: aws
runtime: nodejs6.10
stage: test
region: us-east-1

# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "lambda:InvokeAsync"
# - "lambda:InvokeFunction"
# Resource:
# - "*"

# you can define service wide environment variables here
# environment:
# variable1: value1

# you can add packaging information here
package:
include:
- config/**
- node_modules/**
exclude:
- package.json

functions:
incoming:
handler: handler.incoming
events:
- http:
path: stripe/incoming
method: post