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

feat: added project ci #4

Merged
merged 9 commits into from
Mar 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [ambianic]
38 changes: 38 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Ambianic Functions API CI Test

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on: [pull_request]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Git checkout
uses: actions/checkout@v2

- name: Install Node JS ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Newman dependency
run: npm install --prefix tests/postman/

- name: Run the API and Postman's tests
run: |
cd tests/postman/ && newman run *
env:
CI: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ dist

# TernJS port file
.tern-port
.env
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# ambianic-subscriptions
Repo for ambianic premium subscription management

This repo hosts [Netlify functions](https://www.netlify.com/products/functions/) that handle Ambianic premium user subscriptions. This is code that cannot run in the browser PWA, because it needs access keys to subscription and payment gateway services (Stripe) shared between all app users. If the code runs in the PWA, users will be able to see and manipulate each other's premium subscription data.
This repo hosts [Netlify functions](https://www.netlify.com/products/functions/) that handle Ambianic premium user subscriptions. This is code that cannot run in the browser PWA, because it needs access keys to subscription and payment gateway services (Stripe) shared between all app users. If the code runs in the PWA, users will be able to see and manipulate each other's premium subscription data

A high level prototype of how these functions make up the ambianic serverless feature can be found [here](https://drive.google.com/file/d/181etJhBye0u1zvqzAnBY0VtCS0A3SlQ-/view?usp=sharing).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vickywane in the diagram towards the bottom it shows Edge Calls Google API, I think the intention is to show that Edge calls Ambianic Notification Service APIs.


21 changes: 21 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html class="no-js" lang="">

<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Place favicon.ico in the root directory -->
<meta name="theme-color" content="#fafafa">
</head>

<body>

<!-- Add your site or application content here -->
<p>Hello world! This is Ambianic functions</p>

</body>

</html>
8 changes: 8 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build]
command = "npm build"
functions = "netlify/functions"

[[headers]]
for = "/*"
[headers.values]
Access-Control-Allow-Origin = "*"
19 changes: 19 additions & 0 deletions netlify/functions/cancelSubscription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require('dotenv').config()
const stripe = require('stripe')(process.env.STRIPE_TEST_KEY)

exports.handler = (event, context, callback) => {
const { stripeId } = event.queryStringParameters
stripe.subscriptions.del(stripeId).then(() => {
return callback(null, {
statusCode: 200,
body: JSON.stringify({ message: 'CUSTOMER HAS BEEN UNSUBSCRIBED' })
})
})
.catch(error => {
callback(null, {
statusCode: 422,
body: JSON.stringify({ message: 'ERROR GETTING CUSTOMERS'}),
error
})
})
}
25 changes: 25 additions & 0 deletions netlify/functions/getCustomers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('dotenv').config()
const stripe = require('stripe')(process.env.STRIPE_TEST_KEY)

const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE'
}

exports.handler = (event, context, callback) => {
stripe.customers
.list()
.then(customers => {
return callback(null, {
statusCode: 200,
headers,
body: JSON.stringify({ customers })
})
})
.catch(error => callback(null, {
statusCode: 422,
headers,
body: JSON.stringify({ message: 'ERROR GETTING CUSTOMERS', error })
}))
}
66 changes: 66 additions & 0 deletions netlify/functions/subscriptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require('dotenv').config()
const stripe = require('stripe')(process.env.STRIPE_TEST_KEY)

const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE'
}

// TODO: Refactor to use async await.
exports.handler = (event, context, callback) => {
const { email, card } = event.queryStringParameters

// eslint-disable-next-line camelcase
const { number, cvc, exp_month, exp_year } = JSON.parse(card)

stripe.sources.create({
type: 'ach_credit_transfer',
currency: 'usd',
owner: {
email
}
}, (err, source) => {
if (err) {
console.log(err)
}
// Create a payment method for user using the Card Details
stripe.paymentMethods.create({
type: 'card',
card: {
number, cvc, exp_year, exp_month
}
}).then(({ id: paymentID }) => {
// Creates Customer ==>
stripe.customers
.create({
email,
description: 'Ambianic customer using premium service',
payment_method: paymentID,
source: source.id
})
.then(({ id: customerID }) => {
stripe.paymentMethods.attach(paymentID, { customer: customerID }).then((data) => {
stripe.subscriptions.create({
customer: customerID,
items: [{
price: process.env.EMAIL_PRODUCT_ID
}]
}).then(() => callback(null, {
statusCode: 200,
headers,
body: JSON.stringify({ message: 'EMAIl SUBSCRIPTION CREATED' })
})).catch(e => {
console.log(`Err creating subscription : ${e}`)
})
}).catch(e => {
console.log(`Creating customer ${e}`)
})
})
}).catch(error => callback(null, {
statusCode: 422,
headers,
body: JSON.stringify({ message: 'ERROR CREATING USER', error })
}))
})
}
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "functions",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"build": "parcel build index.html",
"dev": "parcel index.html",
},
"license": "MIT",
"dependencies": {
"newman": "^5.2.2",
"stripe": "^8.135.0"
},
"devDependencies": {
"parcel-bundler": "^1.12.4"
}
}
Loading