Cloud Code

Last updated 4 months ago

Listing of Cloud Code functions inside Symbyoz engine

Stripe

/**
* @param {boolean} liveMode
* @return Stripe
*/
function getStripe(liveMode)
{
if (liveMode == false && process.env.STRIPE_DEV_KEY !== null) {
console.log('import Stripe with key : '+process.env.STRIPE_DEV_KEY);
return require("cloud/node_modules/stripe/lib/stripe.js")(process.env.STRIPE_DEV_KEY);
} else {
console.log('import Stripe with key : '+process.env.STRIPE_KEY);
return require("cloud/node_modules/stripe/lib/stripe.js")(process.env.STRIPE_KEY);
}
}
/**
* Create Stripe Charge with customer
*/
Parse.Cloud.define("StripeChargeWithCustomer", function(rq, response){
var user = rq.user;
if (!user) {
return response.error('User must be logged');
}
var params = rq.params;
if (!params.amount || !params.currency || !params.customer || !params.description) {
return response.error("Missing parameters: need amount, currency, customer and description");
}
if (!params.livemode) {
var livemode = false;
} else {
var livemode = params.livemode;
}
getStripe(livemode).charges.create({
amount: params.amount,
currency: params.currency,
customer: params.customer,
description: params.description
}, function(err, charge) {
if(err) {
return response.error(err.statusCode, err.raw);
}
if (charge.status == 'failed') {
return response.error('KO');
} else {
return response.success('OK');
}
});
});
/**
* Create Stripe Charge with source
*/
Parse.Cloud.define("StripeChargeWithSource", function(rq, response){
var user = rq.user;
if (!user) {
return response.error('User must be logged');
}
var params = rq.params;
if (!params.amount || !params.currency || !params.source || !params.description) {
return response.error("Missing parameters: need amount, currency, source and description");
}
if (!params.livemode) {
var livemode = false;
} else {
var livemode = params.livemode;
}
getStripe(livemode).charges.create({
amount: params.amount,
currency: params.currency,
source: params.source,
description: params.description
}, function(err, charge) {
if(err) {
return response.error(err.statusCode, err.raw);
}
if (charge.status == 'failed') {
return response.error('KO');
} else {
return response.success('OK');
}
});
});
/**
* Create Stripe Customer
*/
Parse.Cloud.define("StripeCustomerCreate", function(rq, response){
var user = rq.user;
if (!user) {
return response.error('User must be logged');
}
var params = rq.params;
if (!params.email || !params.source) {
return response.error("Missing parameters: need email, source");
}
if (!params.livemode) {
var livemode = false;
} else {
var livemode = params.livemode;
}
getStripe(livemode).customers.create({
email: params.email,
source: params.source,
}, function(err, customer) {
if(err) {
return response.error(err.statusCode, err.raw);
}
return response.success(customer.id);
});
});

SMS by AWS SNS

/**
* Send SMS with AWS SNS
*/
Parse.Cloud.define("sendAWSSMS", function(rq, response){
var AWS = require('cloud/node_modules/aws-sdk/index.js');
AWS.config.update({
accessKeyId: process.env.SNS_ACCESS_KEY,
secretAccessKey: process.env.SNS_SECRET_KEY,
region: process.env.SNS_REGION
});
var phone = rq.params.phone;
if (!phone) {
return response.error('phone parameter is missing');
}
var message = rq.params.message;
if (!message) {
return response.error('message parameter is missing');
}
var sns = new AWS.SNS();
sns.setSMSAttributes({
attributes: {
DefaultSenderID: process.env.APP_NAME
}
}, function(error, data){
if (error) {
return response.error("error when defining sms preferences", error);
}
sns.publish({
Message: message,
PhoneNumber: phone,
}, function(error, data) {
if (error) {
return response.error(error.statusCode, error.message);
}
console.log(data);
return response.success('Sending success');
});
});
});

SMS by SMSPartner

/**
* Send SMS with SMS Partner
*/
Parse.Cloud.define("sendSMSPartner", function(rq, response) {
var request = require('cloud/node_modules/request/index.js');
var phone = rq.params.phone;
if (!phone) {
return response.error('phone parameter is missing');
}
var message = rq.params.message;
if (!message) {
return response.error('message parameter is missing');
}
var requestData = {
apiKey: process.env.SMSPARTNER_KEY,
phoneNumbers: phone,
sender: process.env.APP_NAME,
message: message
}
request.post(
'https://api.smspartner.fr/v1/send',
{ json: requestData },
function (error, res, body) {
if (!error && res.statusCode == 200) {
return response.success(body);
} else {
return response.error(body);
}
}
);
});

Push-Notification to Channel

/**
* Send Push to channels
*/
Parse.Cloud.define("sendPushTo", function(rq, response){
var user = rq.user;
if (!user) {
return response.error('User must be logged');
}
var channels = rq.params.channels;
if (!channels) {
return response.error('channels parameter is missing');
} else if (typeof(channels) != 'object') {
channels = [channels];
}
var message = rq.params.message;
if (!message) {
return response.error('message parameter is missing');
}
var options = rq.params.options;
if (!options) {
options = {};
}
var data = {
alert: message,
badge: "Increment",
sound: "soundNotifUserContact.caf",
title: process.env.APP_NAME
};
for (var attr in options) {
data[attr] = options[attr];
}
Parse.Push.send(
{
channels: channels,
data: data
},
{
useMasterKey: true,
success: function() {
return response.success('OK');
},
error: function(error) {
return response.error(error);
}
}
);
});

Send Email with MailGun

Parse.Cloud.define("sendEmail", function(rq, response){
var user = rq.user;
if (!user) {
return response.error('User must be logged');
}
var templateId = rq.params.templateId;
if (!templateId) {
return response.error('templateId parameter is missing');
}
var emails = rq.params.emails;
var users = rq.params.users;
if (!emails && !users) {
return response.error('users or emails parameter is required');
}
var json = {
'users': users,
'emails': emails
};
var request = require('request');
var url = process.env.SYMBYOZ_URL+'/cloud/send-email/'+templateId;
console.log('Call URL '+url);
console.log('With header x-parse-session-token:'+rq.headers['x-parse-session-token']);
request({
url: url,
method: 'POST',
json: json,
headers: {
'x-parse-session-token': rq.headers['x-parse-session-token']
}
}, function (error, resp, body) {
if (!resp) {
console.log('Unable to connect to '+url);
return response.error(500, 'Unable to connect to Symbyoz');
}
if (resp.statusCode != 200) {
if (body.error) {
return response.error(resp.statusCode, body.error.message);
} else {
return response.error(resp.statusCode, body);
}
}
console.log(body);
return response.success('OK');
});
});