Skip to content

Commit

Permalink
Merge pull request #116 from Kondeo/stripeAutoRenew
Browse files Browse the repository at this point in the history
Stripe auto renew
  • Loading branch information
julianpoy committed Jun 2, 2016
2 parents d5638d9 + 02176a6 commit 6e207d1
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/config/constants.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"VERSION": {
"NUMBER": "0.1.8",
"NUMBER": "0.1.9",
"DEPRICATED": "0.1.7",
"LAST_COMPATIBLE": "0.1.7"
},
Expand Down
4 changes: 4 additions & 0 deletions backend/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var User = new mongoose.Schema({
stripeId: {
type: String
},
stripeInit: {
type: Boolean,
default: false
},
memberPrice: {
type: Boolean,
default: false
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backend",
"version": "0.0.0",
"version": "0.1.9",
"private": true,
"scripts": {
"start": "node ./bin/www"
Expand Down
45 changes: 45 additions & 0 deletions backend/routes/stripeHooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose'),
moment = require('moment'),
StripeService = require('../services/stripe.js'),
CONST = require('../config/constants.json'),
User = mongoose.model('User');
Expand All @@ -12,6 +13,10 @@ router.post('/', function(req, res, next) {
if(req.body.type == "customer.subscription.deleted"){
console.log("User unsubscribe event")
cancelUser(req.body.data.object.customer, req.body.data.object.id);
} else if(req.body.type == "invoice.payment_succeeded"){
console.log(req.body);
console.log("User paid event")
renewUser(req.body.data.object.customer, req.body.data.object.subscription);
}

res.status(200).send("ok");
Expand All @@ -22,6 +27,46 @@ router.post('/', function(req, res, next) {
});
});

function renewUser(stripeId, subscriptionId){
User.findOne({
stripeId: stripeId,
subscriptionId: subscriptionId
})
.select('subscription subscriptionId stripeId stripeInit')
.exec(function(err, user) {
if (err) {
console.log("ALERT! Database error!")
console.log(err);
} else if (!user) {
console.log("User not subscribed. Autosubscription error, fragmented database.");
StripeService.unsubscribe(subscriptionId, function(confirmation){
console.log("Unsubscribed user to fix fragmentation")
}, function(err){
console.log("Stripe has no record of a subscription it just sent a webhook for. This is bad.")
});
} else {
if(user.stripeInit){
var origSub = moment(user.subscription);
var today = moment();

var newSub = moment.max(origSub, today).add(1, 'month');

user.subscription = newSub.toDate();
user.save(function(err){
if(err) console.log("User subscription update error (database)! " + err);
else console.log("Customer subscription autoextended.");
});
} else {
user.stripeInit = true;
user.save(function(err){
if(err) console.log("Stripe initialization error! " + err);
else console.log("Customer initialized.");
});
}
}
});
}

function cancelUser(stripeId, subscriptionId){
User.findOne({
stripeId: stripeId,
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CCRAECompendium",
"version": "0.1.8",
"version": "0.1.9",
"dependencies": {
"angular-animate": "1.5.3",
"angular-hotkeys": "chieffancypants/angular-hotkeys#~1.6.0",
Expand Down
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.Kondeo.CCRAECompendium" version="0.1.8" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<widget id="com.Kondeo.CCRAECompendium" version="0.1.9" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Compendium</name>
<description>
CCRA ECompendium
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CCRAECompendium",
"version": "0.1.8",
"version": "0.1.9",
"private": true,
"dependencies": {
"moment": "^2.11.1"
Expand Down

0 comments on commit 6e207d1

Please sign in to comment.