Skip to content

Commit

Permalink
Merge pull request #2 from ConstantineZarzhytskyy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ConstantineZarzhytskyy authored Aug 31, 2016
2 parents 56a1393 + b58774f commit f559aae
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
50 changes: 50 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html ng-app=stripeApp>
<head>
<title>stripe test</title>
</head>
<body ng-controller="AppController">
<h1>STRIPE TEST</h1>
<button ng-click="payment($event)">pay with stripe</button>
<h3>Thx 4 pay!!</h3>


<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script>
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
angular
.module('stripeApp', [])
.controller('AppController', ['$scope', '$http', '$q', function ($scope, $http, $q) {

$scope.paymentDetails = {
name: 'NAME',
description: 'DESCRIPTION',
amount: '888'
};
var handler = StripeCheckout.configure({
key: 'pk_test_x5UrUcpDMTQcqHosJ78sibQf',
locale: 'auto',
token: function (token) {
$http.post('/charge', { token: token, paymentDetails: $scope.paymentDetails })
.success(function (ok) {
console.log('ok = ', ok);
})
.error(function (err) {
console.log(err);
})
}
});

$scope.payment = function (e) {
handler.open($scope.paymentDetails);
e.preventDefault();
};

window.onpopstate = function (event) {
handler.close();
}

}]);
</script>
</body>
</html>
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node app.js"
"start": "nodemon app.js"
},
"repository": {
"type": "git",
Expand All @@ -22,5 +22,8 @@
"cookie-parser": "~1.3.5",
"express": "^4.14.0",
"stripe": "^4.4.0"
},
"devDependencies": {
"nodemon": "^1.10.2"
}
}
69 changes: 47 additions & 22 deletions router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
var express = require('express');
var router = express.Router();
var path = require('path');
var async = require('async');
var stripe = require('stripe')('sk_test_eBBR4DkvJFUA4sqw8Vfaja0N');

router.get('/', function (req, res) {
res.json('ok');
res.sendFile(path.join(__dirname+'/index.html'));
});

router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (req, res) {
Expand All @@ -19,7 +20,6 @@ router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (re
console.log('params = ', params);

async.waterfall([
getToken,
createCustomer,
createPlan,
createSubscribtion
Expand All @@ -31,26 +31,16 @@ router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (re
res.json(done);
});

function getToken(callback) {
stripe.tokens.create({
card: {
number: params.cardNumber,
function createCustomer(callback) {
stripe.customers.create({
source: {
object: "card",
exp_month: params.mounth,
exp_year: params.year,
cvc: params.cvc
}
}, function (err, token) {
console.log('token = ', token);
if (err) { return callback(err); }

callback(null, token);
});
}

function createCustomer(token, callback) {
stripe.customers.create({
source: token.id,
number: params.cardNumber
},
email: params.email,
account_balance: params.amount,
description: 'Customer ' + params.email + ' is devTest account'
}, function (err, customer) {
console.log('customer = ', customer);
Expand All @@ -62,11 +52,11 @@ router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (re

function createPlan(customer, callback) {
stripe.plans.create({
amount: 200,
amount: 100,
interval: 'day',
name: '7$ 2',
name: '1$',
currency: 'usd',
id: '7$ 2'
id: '1$'
}, function (err, plan) {
console.log('plan = ', plan);
if (err) { return callback(err); }
Expand All @@ -88,4 +78,39 @@ router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (re
}
});

router.get('/stripe/charges', function (req, res) {
stripe.charges.create({
amount: 2000,
currency: "usd",
source: {
number: '4242424242424242',
exp_month: 12,
exp_year: 2017,
cvc: '123'
},
description: "Charge for [email protected]"
}, function(err, charge) {
if (err) { res.send(err); }

res.json(charge);
});
})

router.post('/stripe/webhooks', function (req, res) {
res.json({ "webhooks": "ok" });
})

router.post('/charge', function(req, res) {
stripe.charges.create({
amount: req.body.paymentDetails.amount,
currency: 'usd',
source: req.body.token.id,
description: req.body.paymentDetails.description
}, function(err, charge) {
if (err) { res.send(err); }

res.json(charge);
});
})

module.exports = router;

0 comments on commit f559aae

Please sign in to comment.