Skip to content

Commit

Permalink
Merge pull request #3 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 f559aae + fa57e35 commit d8fd4b4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 42 deletions.
69 changes: 50 additions & 19 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,76 @@
</head>
<body ng-controller="AppController">
<h1>STRIPE TEST</h1>
<button ng-click="payment($event)">pay with stripe</button>
<ui>
<li ng-repeat="item in list">
{{ item.name }}
<button ng-click="payment($event, item.amount)">pay with stripe</button>
</li>
</ui>
<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) {
angular.module('stripeApp', [])
.controller('AppController', ['$scope', 'AppService',function ($scope, AppService) {
AppService.getList()
.then(function (ok) {
$scope.list = ok;
}, function (err) {
console.log(err);
});

$scope.paymentDetails = {
name: 'NAME',
description: 'DESCRIPTION',
amount: '888'
};
var paymentDetails = { };
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);
})
AppService.payment(token, paymentDetails)
.then(function (ok) {
alert('ok');
}, function (err) {
alert('err');
})
}
});

$scope.payment = function (e) {
handler.open($scope.paymentDetails);
$scope.payment = function (e, itemAmount) {
paymentDetails = {
name: 'NAME',
description: 'DESCRIPTION',
amount: itemAmount
}
handler.open(paymentDetails);
e.preventDefault();
};

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

}])
.service('AppService', ['$http', '$q', function ($http, $q) {
return {
getList: function () {
var defer = $q.defer();

$http.get('/list')
.success(defer.resolve)
.error(defer.reject);

return defer.promise;
},
payment: function (token, paymentDetails) {
var defer = $q.defer();

$http.post('/stripe/charge', { token: token, paymentDetails: paymentDetails })
.success(defer.resolve)
.error(defer.reject);

return defer.promise;
}
};
}]);
</script>
</body>
Expand Down
50 changes: 27 additions & 23 deletions router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ router.get('/', function (req, res) {
res.sendFile(path.join(__dirname+'/index.html'));
});

router.get('/list', function (req, res) {
res.json([
{
name: 'item 1',
amount: 1000
},
{
name: 'item 2',
amount: 2000
},
{
name: 'item 3',
amount: 3000
},
{
name: 'item 4',
amount: 4000
}
]);
});

router.get('/stripe/:email/:cardNumber/:mounth/:year/:cvc/:amount', function (req, res) {
var params = {
email: req.params.email,
Expand Down Expand Up @@ -78,39 +99,22 @@ 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) {
router.post('/stripe/charge', function(req, res) {
stripe.charges.create({
amount: req.body.paymentDetails.amount,
currency: 'usd',
currency: 'GBP',
source: req.body.token.id,
description: req.body.paymentDetails.description
}, function(err, charge) {
if (err) { res.send(err); }
if (err) { return res.send(err); }

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


module.exports = router;

0 comments on commit d8fd4b4

Please sign in to comment.