Skip to content

Commit

Permalink
extra work on subscription cancellation workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
osahyoun committed Nov 21, 2017
1 parent 486a8cb commit 61ebe35
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 151 deletions.
81 changes: 20 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"axios-es6": "^0.11.1",
"braintree-node": "^1.1.2",
"braintree": "^2.4.0",
"guid": "0.0.12",
"lodash": "^4.17.4",
"source-map-support": "^0.5.0"
Expand Down
39 changes: 19 additions & 20 deletions payments-service/payments-service-dynamodb-stream.json
Original file line number Diff line number Diff line change
@@ -1,58 +1,57 @@
{
"Records": [
{
"eventID": "a0b0c46f575600e1f9bd12e0d33f6e5c",
"eventID": "47798cd879e9ff3ce53ee2dfe4fe89a2",
"eventName": "INSERT",
"eventVersion": "1.1",
"eventSource": "aws:dynamodb",
"awsRegion": "us-east-1",
"dynamodb": {
"ApproximateCreationDateTime": 1511256060,
"ApproximateCreationDateTime": 1511282400,
"Keys": {
"createdAt": {
"S": "2017-11-21T09:21:22.811Z"
"S": "2017-11-21T16:40:18.591Z"
},
"id": {
"S": "ae75403d-9baf-01da-2065-bd10f7135bd9"
"S": "3a2e8033-cc41-a897-c831-64932944be0b"
}
},
"NewImage": {
"createdAt": {
"S": "2017-11-21T09:21:22.811Z"
"S": "2017-11-21T16:40:18.591Z"
},
"data": {
"M": {
"recurringId": {
"S": "fz27ww"
},
"paymentProcessor": {
"S": "braintree"
}
}
},
"eventType": {
"S": "PAYMENT_SERVICE:SUBSCRIPTION:CANCEL"
},
"id": {
"S": "ae75403d-9baf-01da-2065-bd10f7135bd9"
},
"data": {
"paymentProcessor": {
"S": "braintree"
},
"recurringId": {
"S": "c5dg96"
}
"S": "3a2e8033-cc41-a897-c831-64932944be0b"
},
"status": {
"M": {
"actionkit": {
"S": "PENDING"
},
"braintree": {
"S": "SUCCESS"
},
"champaign": {
"S": "PENDING"
}
}
}
},
"SequenceNumber": "3294700000000040528742710",
"SizeBytes": 273,
"SequenceNumber": "100000000008752455558",
"SizeBytes": 280,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventSourceARN": "arn:aws:dynamodb:us-east-1:108409592308:table/OperationsTable-omar/stream/2017-11-20T16:28:36.723"
"eventSourceARN": "arn:aws:dynamodb:us-east-1:108409592308:table/OperationsTable-omar/stream/2017-11-21T16:38:13.504"
}
]
}
53 changes: 18 additions & 35 deletions payments-service/subscriptions-delete-actionkit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { post } from 'axios-es6';
import AWS from 'aws-sdk';
import updateOperationsLog from './updateOperationsLog';

const documentClient = new AWS.DynamoDB.DocumentClient();
const CANCEL_PAYMENT_EVENT = 'PAYMENT_SERVICE:SUBSCRIPTION:CANCEL';

const validEventType = record => {
if (record.eventName !== 'INSERT') return false;
if (record.dynamodb.NewImage.eventType.S !== CANCEL_PAYMENT_EVENT)
return false;

return true;
};

export const handler = (event, context, callback) => {
if (!validEventType(event.Records[0])) {
return;
}

const record = event.Records[0].dynamodb.NewImage;
const recurringId = record.data.recurringId.S;

const recurringId = record.data.M.recurringId.S;
const id = record.id.S;
const createdAt = record.createdAt.S;

Expand All @@ -18,47 +33,15 @@ export const handler = (event, context, callback) => {
canceled_by: 'admin',
});

const params = {
TableName: process.env.DB_LOG_TABLE,
Key: {
id: id,
createdAt: createdAt,
},
UpdateExpression: 'set #s.#ak = :success',
ExpressionAttributeNames: { '#s': 'status', '#ak': 'actionkit' },
ExpressionAttributeValues: {
':success': 'PENDING',
},
};

documentClient
.update(params)
.promise()
.then(resp => console.log(resp))
.catch(err => console.log(err));

return;

post(url, data, {
headers: {
'Content-Type': 'application/json',
},
})
.then(resp => {
const params = {
TableName: process.env.DB_LOG_TABLE,
Item: {
id: id,
createdAt: createdAt,
status: {
braintree: 'SUCCESS',
actionkit: 'PENDING',
champaign: 'PENDING',
},
},
};
updateOperationsLog(id, createdAt, 'SUCCESS', 'actionkit');
})
.catch(function(error) {
console.log(error);
updateOperationsLog(id, createdAt, 'FAILURE', 'actionkit');
});
};
4 changes: 2 additions & 2 deletions payments-service/subscriptions-delete-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
},
"queryStringParameters": null,
"pathParameters": {
"id": "2"
"id": "5b5n8m"
},
"stageVariables": null,
"requestContext": {
"path": "/omar/payments-service/subscriptions/1",
"path": "/omar/payments-service/subscriptions/7cqjbb",
"accountId": "108409592308",
"resourceId": "gyp387",
"stage": "omar",
Expand Down
52 changes: 20 additions & 32 deletions payments-service/subscriptions-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,44 @@ import braintree from '../shared/clients/braintree';
import guid from 'guid';
import AWS from 'aws-sdk';

export const handler = (event, context, callback) => {
const id = event.pathParameters.id;
const documentClient = new AWS.DynamoDB.DocumentClient();
// braintree.subscription.cancel(id, (err, result) => {
// console.log(JSON.stringify(result, null, 2));
// });
const documentClient = new AWS.DynamoDB.DocumentClient();

const logOperation = id => {
const params = {
TableName: process.env.DB_LOG_TABLE,
Item: {
id: guid.raw(),
createdAt: new Date().toISOString(),
eventType: 'PAYMENT_SERVICE:SUBSCRIPTION:CANCEL',
data: {
recurring_id: id,
recurringId: id,
paymentProcessor: 'braintree',
},
status: {
braintree: 'SUCCESS',
actionkit: 'PENDING',
champaign: 'PENDING',
},
},
};

// example of scaning a dynamodb Table
// documentClient.scan({
// TableName : process.env.DB_LOG_TABLE,
// }).promise()
// .then((data) => {
// console.log(data.Items);
// })

documentClient
.put(params)
.promise()
.then(
data => {
callback(
null,
ok({
body: {
message:
'Go Serverless Webpack (Ecma Script) v1.0! First module!',
event,
},
})
);
},
err => console.log(err)
)
.catch(err => console.log(err));
.then(resp => console.log('TABLE PUT RESPONSE:', resp))
.catch(err => console.log('TABLE PUT ERROR', err));
};

export const handler = (event, context, callback) => {
const id = event.pathParameters.id;

braintree.subscription
.cancel(id)
.then(() => {
logOperation(id);
callback(null, ok());
})
.catch(err => {
console.log('BRAINTREE SUBSCRIPTION CANCEL ERROR:', err);
callback(null, badRequest());
});
};

0 comments on commit 61ebe35

Please sign in to comment.