-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (86 loc) · 3.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Server side function for node apps to support Quaderno
// https://github.com/quaderno/quaderno.js
var jwt = require('jwt-simple');
// stripeSecretKeyForQuaderno:String. Obtained from Quaderno via Settings -> Add Ons - > Stripe
module.exports = function(stripeSecretKeyForQuaderno){
// Make a JSON Web Token
// See https://github.com/quaderno/quaderno.js#creating-single-charges
// amount:Number required. Whole number, in cents / pence / eurocents etc.
// currency:String required. ISO_4217 currency code. See http://en.wikipedia.org/wiki/ISO_4217
// description:String optional. The statement you want to show on your receipt. https://quaderno.io/docs/guides/charge-form/
// date:Date optional (default current time). Generally only overridden for testing.
// Returns JSONWebToken:String
var getJSONWebToken = function(amount, currency, description, date){
// defines the seconds since the UNIX epoch
if ( ! date ) {
date = new Date()
}
if ( ! currency ) {
currency = 'USD'
}
if ( ! description ) {
description = ''
}
var issuedAt = Math.floor( date.valueOf() / 1000 );
var payload = {
"amount": amount,
"currency": currency,
"description": description,
"iat": issuedAt
}
return jwt.encode(payload, stripeSecretKeyForQuaderno);
}
// Decode a JSON Web Token
// token:String required
var decodeJSONWebToken = function(token){
return jwt.decode(token, stripeSecretKeyForQuaderno);
}
// Make a JSON Web Token for PayPal Subscriptions
// See https://quaderno.io/docs/checkout/#PayPal_Subscriptions
// amount:Number required. Whole number, in cents / pence / eurocents etc.
// unit:String required. Specify the units of the subscription frequency (D, W, M, Y). The default is M
// duration:Number required. Specify the subscription frequency. The default is 1
// currency:String required. ISO_4217 currency code. See http://en.wikipedia.org/wiki/ISO_4217
// description:String optional. The statement you want to show on your receipt. https://quaderno.io/docs/guides/charge-form/
// date:Date optional (default current time). Generally only overridden for testing.
// Returns JSONWebToken:String
var getJSONPayPalSubWebToken = function(amount, currency, unit, duration, description, date){
// defines the seconds since the UNIX epoch
if ( ! date ) {
date = new Date()
}
if ( ! currency ) {
currency = 'USD'
}
if ( ! unit ) {
unit = 'M'
}
if ( ! duration ) {
duration = 1
}
if ( ! description ) {
description = ''
}
var issuedAt = Math.floor( date.valueOf() / 1000 );
var payload = {
"amount": amount,
"currency": currency,
"subscription_unit": unit,
"subscription_duration": duration,
"description": description,
"iat": issuedAt
}
return jwt.encode(payload, stripeSecretKeyForQuaderno);
}
// Decode a JSON Web Token
// token:String required
var decodeJSONPayPalSubWebToken = function(token){
return jwt.decode(token, stripeSecretKeyForQuaderno);
}
return {
getJSONWebToken: getJSONWebToken,
decodeJSONWebToken: decodeJSONWebToken,
getJSONPayPalSubWebToken: getJSONPayPalSubWebToken,
decodeJSONPayPalSubWebToken: decodeJSONPayPalSubWebToken
}
}