Cloud Code
Listing of Cloud Code functions inside Symbyoz engine

Stripe

1
/**
2
* @param {boolean} liveMode
3
* @return Stripe
4
*/
5
6
function getStripe(liveMode)
7
{
8
if (liveMode == false && process.env.STRIPE_DEV_KEY !== null) {
9
console.log('import Stripe with key : '+process.env.STRIPE_DEV_KEY);
10
return require("cloud/node_modules/stripe/lib/stripe.js")(process.env.STRIPE_DEV_KEY);
11
} else {
12
console.log('import Stripe with key : '+process.env.STRIPE_KEY);
13
return require("cloud/node_modules/stripe/lib/stripe.js")(process.env.STRIPE_KEY);
14
}
15
}
16
17
/**
18
* Create Stripe Charge with customer
19
*/
20
21
Parse.Cloud.define("StripeChargeWithCustomer", function(rq, response){
22
var user = rq.user;
23
if (!user) {
24
return response.error('User must be logged');
25
}
26
27
var params = rq.params;
28
if (!params.amount || !params.currency || !params.customer || !params.description) {
29
return response.error("Missing parameters: need amount, currency, customer and description");
30
}
31
if (!params.livemode) {
32
var livemode = false;
33
} else {
34
var livemode = params.livemode;
35
}
36
37
getStripe(livemode).charges.create({
38
amount: params.amount,
39
currency: params.currency,
40
customer: params.customer,
41
description: params.description
42
}, function(err, charge) {
43
if(err) {
44
return response.error(err.statusCode, err.raw);
45
}
46
47
if (charge.status == 'failed') {
48
return response.error('KO');
49
} else {
50
return response.success('OK');
51
}
52
});
53
});
54
55
/**
56
* Create Stripe Charge with source
57
*/
58
Parse.Cloud.define("StripeChargeWithSource", function(rq, response){
59
var user = rq.user;
60
if (!user) {
61
return response.error('User must be logged');
62
}
63
64
var params = rq.params;
65
if (!params.amount || !params.currency || !params.source || !params.description) {
66
return response.error("Missing parameters: need amount, currency, source and description");
67
}
68
if (!params.livemode) {
69
var livemode = false;
70
} else {
71
var livemode = params.livemode;
72
}
73
74
getStripe(livemode).charges.create({
75
amount: params.amount,
76
currency: params.currency,
77
source: params.source,
78
description: params.description
79
}, function(err, charge) {
80
if(err) {
81
return response.error(err.statusCode, err.raw);
82
}
83
84
if (charge.status == 'failed') {
85
return response.error('KO');
86
} else {
87
return response.success('OK');
88
}
89
});
90
});
91
92
/**
93
* Create Stripe Customer
94
*/
95
Parse.Cloud.define("StripeCustomerCreate", function(rq, response){
96
var user = rq.user;
97
if (!user) {
98
return response.error('User must be logged');
99
}
100
101
var params = rq.params;
102
if (!params.email || !params.source) {
103
return response.error("Missing parameters: need email, source");
104
}
105
if (!params.livemode) {
106
var livemode = false;
107
} else {
108
var livemode = params.livemode;
109
}
110
111
getStripe(livemode).customers.create({
112
email: params.email,
113
source: params.source,
114
}, function(err, customer) {
115
if(err) {
116
return response.error(err.statusCode, err.raw);
117
}
118
return response.success(customer.id);
119
});
120
});
121
Copied!

SMS by AWS SNS

1
/**
2
* Send SMS with AWS SNS
3
*/
4
Parse.Cloud.define("sendAWSSMS", function(rq, response){
5
var AWS = require('cloud/node_modules/aws-sdk/index.js');
6
7
AWS.config.update({
8
accessKeyId: process.env.SNS_ACCESS_KEY,
9
secretAccessKey: process.env.SNS_SECRET_KEY,
10
region: process.env.SNS_REGION
11
});
12
13
var phone = rq.params.phone;
14
if (!phone) {
15
return response.error('phone parameter is missing');
16
}
17
var message = rq.params.message;
18
if (!message) {
19
return response.error('message parameter is missing');
20
}
21
22
var sns = new AWS.SNS();
23
24
sns.setSMSAttributes({
25
attributes: {
26
DefaultSenderID: process.env.APP_NAME
27
}
28
}, function(error, data){
29
if (error) {
30
return response.error("error when defining sms preferences", error);
31
}
32
33
sns.publish({
34
Message: message,
35
PhoneNumber: phone,
36
}, function(error, data) {
37
if (error) {
38
return response.error(error.statusCode, error.message);
39
}
40
41
console.log(data);
42
return response.success('Sending success');
43
});
44
});
45
});
46
Copied!

SMS by SMSPartner

1
/**
2
* Send SMS with SMS Partner
3
*/
4
Parse.Cloud.define("sendSMSPartner", function(rq, response) {
5
var request = require('cloud/node_modules/request/index.js');
6
var phone = rq.params.phone;
7
if (!phone) {
8
return response.error('phone parameter is missing');
9
}
10
var message = rq.params.message;
11
if (!message) {
12
return response.error('message parameter is missing');
13
}
14
15
var requestData = {
16
apiKey: process.env.SMSPARTNER_KEY,
17
phoneNumbers: phone,
18
sender: process.env.APP_NAME,
19
message: message
20
}
21
22
request.post(
23
'https://api.smspartner.fr/v1/send',
24
{ json: requestData },
25
function (error, res, body) {
26
if (!error && res.statusCode == 200) {
27
return response.success(body);
28
} else {
29
return response.error(body);
30
}
31
}
32
);
33
});
Copied!

Push-Notification to Channel

1
/**
2
* Send Push to channels
3
*/
4
5
Parse.Cloud.define("sendPushTo", function(rq, response){
6
var user = rq.user;
7
if (!user) {
8
return response.error('User must be logged');
9
}
10
var channels = rq.params.channels;
11
if (!channels) {
12
return response.error('channels parameter is missing');
13
} else if (typeof(channels) != 'object') {
14
channels = [channels];
15
}
16
var message = rq.params.message;
17
if (!message) {
18
return response.error('message parameter is missing');
19
}
20
var options = rq.params.options;
21
if (!options) {
22
options = {};
23
}
24
25
var data = {
26
alert: message,
27
badge: "Increment",
28
sound: "soundNotifUserContact.caf",
29
title: process.env.APP_NAME
30
};
31
32
for (var attr in options) {
33
data[attr] = options[attr];
34
}
35
36
Parse.Push.send(
37
{
38
channels: channels,
39
data: data
40
},
41
{
42
useMasterKey: true,
43
success: function() {
44
return response.success('OK');
45
},
46
error: function(error) {
47
return response.error(error);
48
}
49
}
50
);
51
});
52
Copied!

Send Email with MailGun

1
Parse.Cloud.define("sendEmail", function(rq, response){
2
var user = rq.user;
3
if (!user) {
4
return response.error('User must be logged');
5
}
6
7
var templateId = rq.params.templateId;
8
if (!templateId) {
9
return response.error('templateId parameter is missing');
10
}
11
12
var emails = rq.params.emails;
13
var users = rq.params.users;
14
if (!emails && !users) {
15
return response.error('users or emails parameter is required');
16
}
17
18
var json = {
19
'users': users,
20
'emails': emails
21
};
22
var request = require('request');
23
var url = process.env.SYMBYOZ_URL+'/cloud/send-email/'+templateId;
24
console.log('Call URL '+url);
25
console.log('With header x-parse-session-token:'+rq.headers['x-parse-session-token']);
26
request({
27
url: url,
28
method: 'POST',
29
json: json,
30
headers: {
31
'x-parse-session-token': rq.headers['x-parse-session-token']
32
}
33
}, function (error, resp, body) {
34
if (!resp) {
35
console.log('Unable to connect to '+url);
36
return response.error(500, 'Unable to connect to Symbyoz');
37
}
38
if (resp.statusCode != 200) {
39
if (body.error) {
40
return response.error(resp.statusCode, body.error.message);
41
} else {
42
return response.error(resp.statusCode, body);
43
}
44
}
45
console.log(body);
46
return response.success('OK');
47
});
48
49
});
Copied!