Skip to content

Commit

Permalink
Add functions, fix menu, add admin screen
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeBellTMH committed Feb 14, 2022
1 parent 6ca6855 commit c4357da
Show file tree
Hide file tree
Showing 35 changed files with 5,604 additions and 305 deletions.
27 changes: 27 additions & 0 deletions amplify/backend/api/jcmobile/schema.graphql
Expand Up @@ -1414,4 +1414,31 @@ type SubMenu
action:String
params:String
readGroups:[UserGroupType]
}
enum CustomPricingType{
monthly
weekly
yearly
oneTime
}
type CustomPricing
@model
@auth(
rules: [
{ allow: private, provider:iam, operations:[read]}
{ allow: private, provider:userPools, operations:[read]}
{ allow: groups, groups: ["verifiedUsers"], operations: [read] }
{allow: groups, groups:["admin"], operations: [create, update, delete, read]},
]
){
id:ID!
emailAddress:String
type:CustomPricingType
lineItems:[LineItem]
}
type LineItem{
itemId:String
count:String
amount:String
description:String
}
19 changes: 19 additions & 0 deletions amplify/backend/backend-config.json
Expand Up @@ -337,6 +337,25 @@
]
}
]
},
"jcmobileStripListProducts": {
"build": true,
"providerPlugin": "awscloudformation",
"service": "Lambda",
"dependsOn": [
{
"category": "auth",
"resourceName": "jcmobile",
"attributes": [
"UserPoolId"
]
}
]
},
"jcmobileShared": {
"providerPlugin": "awscloudformation",
"service": "LambdaLayer",
"build": true
}
},
"auth": {
Expand Down
@@ -0,0 +1,74 @@
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "{\"createdOn\":\"Mac\",\"createdBy\":\"Amplify\",\"createdWith\":\"7.6.15\",\"stackType\":\"function-LambdaLayer\",\"metadata\":{}}",
"Parameters": {
"env": {
"Type": "String"
},
"deploymentBucketName": {
"Type": "String"
},
"s3Key": {
"Type": "String"
},
"description": {
"Type": "String",
"Default": ""
},
"runtimes": {
"Type": "List<String>"
}
},
"Resources": {
"LambdaLayerVersion2ffb38ed": {
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"CompatibleRuntimes": {
"Ref": "runtimes"
},
"Content": {
"S3Bucket": {
"Ref": "deploymentBucketName"
},
"S3Key": {
"Ref": "s3Key"
}
},
"Description": {
"Ref": "description"
},
"LayerName": {
"Fn::Sub": [
"jcmobileShared-${env}",
{
"env": {
"Ref": "env"
}
}
]
}
},
"DeletionPolicy": "Delete",
"UpdateReplacePolicy": "Retain"
},
"LambdaLayerPermissionPrivate2ffb38ed": {
"Type": "AWS::Lambda::LayerVersionPermission",
"Properties": {
"Action": "lambda:GetLayerVersion",
"LayerVersionArn": {
"Ref": "LambdaLayerVersion2ffb38ed"
},
"Principal": {
"Ref": "AWS::AccountId"
}
}
}
},
"Outputs": {
"Arn": {
"Value": {
"Ref": "LambdaLayerVersion2ffb38ed"
}
}
}
}
15 changes: 15 additions & 0 deletions amplify/backend/function/jcmobileShared/layer-configuration.json
@@ -0,0 +1,15 @@
{
"permissions": [
{
"type": "Private"
}
],
"runtimes": [
{
"value": "nodejs",
"name": "NodeJS",
"runtimePluginId": "amplify-nodejs-function-runtime-provider",
"layerExecutablePath": "nodejs"
}
]
}
193 changes: 193 additions & 0 deletions amplify/backend/function/jcmobileShared/lib/nodejs/JCStripe.ts
@@ -0,0 +1,193 @@
import Stripe from "stripe"
export default class JCStripe {
static async getSecret(name: string) {
try {
var AWS = require("aws-sdk"),
region = "us-east-1",
secretName = "jcmobile/" + process.env.ENV + "/secrets",
secret,
decodedBinarySecret

// Create a Secrets Manager client
var client = new AWS.SecretsManager({
region: region,
})
const data = await client.getSecretValue({ SecretId: secretName }).promise()

if ("SecretString" in data) {
secret = JSON.parse(data.SecretString)
} else {
let buff = new Buffer(data.SecretBinary, "base64")
decodedBinarySecret = buff.toString("ascii")
}
console.log("Loading Secret Done")

return secret[name]
} catch (e) {
console.log({ error: e })
throw "Secret not loaded"
}
}
static async createCustomer(
customer: Stripe.CustomerCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.Customer>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const customerResult = await stripe.customers.create(customer, {
idempotencyKey: idempotency + "CC",
})
return customerResult
}
static async updateCustomer(
stripeCustomerID: string,
customer: Stripe.CustomerUpdateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.Customer>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const customerResult = await stripe.customers.update(stripeCustomerID, customer, {
idempotencyKey: idempotency + "CC",
})
return customerResult
}

static async createSetupIntent(
setupIntent: Stripe.SetupIntentCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.SetupIntent>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const paymentMethodResult = await stripe.setupIntents.create(setupIntent, {
idempotencyKey: idempotency + "CC",
})
return paymentMethodResult
}
static async createPaymentMethod(
paymentMethod: Stripe.PaymentMethodCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.PaymentMethod>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const paymentMethodResult = await stripe.paymentMethods.create(paymentMethod, {
idempotencyKey: idempotency + "CC",
})
return paymentMethodResult
}
static async listPaymentMethods(
stripeCustomerID: string,
type: Stripe.PaymentMethodListParams.Type
): Promise<Stripe.Response<Stripe.ApiList<Stripe.PaymentMethod>>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const customerResult = await stripe.paymentMethods.list({
customer: stripeCustomerID,
type: type,
})
return customerResult
}
static async getPaymentMethod(
paymentMethodId: string
): Promise<Stripe.Response<Stripe.PaymentMethod>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const customerResult = await stripe.paymentMethods.retrieve(paymentMethodId)
return customerResult
}
static async detatchPaymentMethod(
paymentMethodId: string
): Promise<Stripe.Response<Stripe.PaymentMethod>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const customerResult = await stripe.paymentMethods.detach(paymentMethodId)
return customerResult
}
static async createInvoiceItem(
payment: Stripe.InvoiceItemCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.InvoiceItem>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.invoiceItems.create(payment, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
static async createInvoice(
payment: Stripe.InvoiceCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.Invoice>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.invoices.create(payment, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
static async createPayment(
payment: Stripe.PaymentIntentCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.PaymentIntent>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.paymentIntents.create(payment, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
static async createSubscription(
subscription: Stripe.SubscriptionCreateParams,
idempotency: string
): Promise<Stripe.Response<Stripe.Subscription>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.subscriptions.create(subscription, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
static async getSubscription(
subscriptionId: string
): Promise<Stripe.Response<Stripe.Subscription>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.subscriptions.retrieve(subscriptionId)
return subscriptionResult
}
static async deleteSubscription(
subscriptionId: string,
idempotency: string
): Promise<Stripe.Response<Stripe.Subscription>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.subscriptions.del(subscriptionId, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
static async listSubscription(
subscription: Stripe.SubscriptionListParams,
idempotency: string
): Promise<Stripe.Response<Stripe.ApiList<Stripe.Subscription>>> {
const stripe = new Stripe(await this.getSecret("stripeSecret"), {
apiVersion: "2020-08-27",
})
const subscriptionResult = await stripe.subscriptions.list(subscription, {
idempotencyKey: idempotency + "CC",
})
return subscriptionResult
}
}
@@ -0,0 +1 @@
Replace this file with your layer files
29 changes: 29 additions & 0 deletions amplify/backend/function/jcmobileShared/lib/nodejs/package.json
@@ -0,0 +1,29 @@
{
"version": "1.0.0",
"description": "",
"main": "index.ts",
"dependencies": {
"@aws-amplify/api": "^4.0.5",
"@aws-amplify/auth": "^4.1.1",
"@aws-amplify/core": "^4.1.3",
"@babel/plugin-transform-typescript": "^7.14.6",
"@babel/preset-flow": "^7.14.5",
"babel-plugin-add-module-exports": "^1.0.4",
"node-fetch": "^2.6.1",
"stripe": "^8.125.0"
},
"scripts": {
"compile": "tsc",
"build": "tsc"
},
"devDependencies": {
"@babel/cli": "7.14.5",
"@babel/core": "^7.14.6",
"@babel/preset-env": "7.14.5",
"@types/aws-lambda": "^8.10.51",
"@types/domhandler": "2.4.2",
"@types/node": "^16.3.1",
"aws-sdk": "^2.953.0",
"typescript": "^4.3.5"
}
}

0 comments on commit c4357da

Please sign in to comment.