Skip to content

Commit b2a9058

Browse files
Merge Functions and android project
1 parent 9a5ca61 commit b2a9058

File tree

100 files changed

+9023
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+9023
-0
lines changed

Firebase_Functions/.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "uberapp-408c8"
4+
}
5+
}

Firebase_Functions/.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

Firebase_Functions/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 SimCoder
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Firebase_Functions/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# uber_firebase_functions
2+
uber firebase functions -> Payout

Firebase_Functions/firebase.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"hosting": {
3+
"public": "public",
4+
"ignore": ["firebase.json","**/.*","**/node_modules/**"],
5+
"rewrite":[{"source": "/payout", "function": "payout"}]
6+
},
7+
"functions": {
8+
"predeploy": [
9+
"npm --prefix \"$RESOURCE_DIR\" run lint"
10+
]
11+
}
12+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"parserOptions": {
3+
// Required for certain syntax usages
4+
"ecmaVersion": 6
5+
},
6+
"plugins": [
7+
"promise"
8+
],
9+
"extends": "eslint:recommended",
10+
"rules": {
11+
// Removed rule "disallow the use of console" from recommended eslint rules
12+
"no-console": "off",
13+
14+
// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
15+
"no-regex-spaces": "off",
16+
17+
// Removed rule "disallow the use of debugger" from recommended eslint rules
18+
"no-debugger": "off",
19+
20+
// Removed rule "disallow unused variables" from recommended eslint rules
21+
"no-unused-vars": "off",
22+
23+
// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
24+
"no-mixed-spaces-and-tabs": "off",
25+
26+
// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
27+
"no-undef": "off",
28+
29+
// Warn against template literal placeholder syntax in regular strings
30+
"no-template-curly-in-string": 1,
31+
32+
// Warn if return statements do not either always or never specify values
33+
"consistent-return": 1,
34+
35+
// Warn if no return statements in callbacks of array methods
36+
"array-callback-return": 1,
37+
38+
// Require the use of === and !==
39+
"eqeqeq": 2,
40+
41+
// Disallow the use of alert, confirm, and prompt
42+
"no-alert": 2,
43+
44+
// Disallow the use of arguments.caller or arguments.callee
45+
"no-caller": 2,
46+
47+
// Disallow null comparisons without type-checking operators
48+
"no-eq-null": 2,
49+
50+
// Disallow the use of eval()
51+
"no-eval": 2,
52+
53+
// Warn against extending native types
54+
"no-extend-native": 1,
55+
56+
// Warn against unnecessary calls to .bind()
57+
"no-extra-bind": 1,
58+
59+
// Warn against unnecessary labels
60+
"no-extra-label": 1,
61+
62+
// Disallow leading or trailing decimal points in numeric literals
63+
"no-floating-decimal": 2,
64+
65+
// Warn against shorthand type conversions
66+
"no-implicit-coercion": 1,
67+
68+
// Warn against function declarations and expressions inside loop statements
69+
"no-loop-func": 1,
70+
71+
// Disallow new operators with the Function object
72+
"no-new-func": 2,
73+
74+
// Warn against new operators with the String, Number, and Boolean objects
75+
"no-new-wrappers": 1,
76+
77+
// Disallow throwing literals as exceptions
78+
"no-throw-literal": 2,
79+
80+
// Require using Error objects as Promise rejection reasons
81+
"prefer-promise-reject-errors": 2,
82+
83+
// Enforce “for” loop update clause moving the counter in the right direction
84+
"for-direction": 2,
85+
86+
// Enforce return statements in getters
87+
"getter-return": 2,
88+
89+
// Disallow await inside of loops
90+
"no-await-in-loop": 2,
91+
92+
// Disallow comparing against -0
93+
"no-compare-neg-zero": 2,
94+
95+
// Warn against catch clause parameters from shadowing variables in the outer scope
96+
"no-catch-shadow": 1,
97+
98+
// Disallow identifiers from shadowing restricted names
99+
"no-shadow-restricted-names": 2,
100+
101+
// Enforce return statements in callbacks of array methods
102+
"callback-return": 2,
103+
104+
// Require error handling in callbacks
105+
"handle-callback-err": 2,
106+
107+
// Warn against string concatenation with __dirname and __filename
108+
"no-path-concat": 1,
109+
110+
// Prefer using arrow functions for callbacks
111+
"prefer-arrow-callback": 1,
112+
113+
// Return inside each then() to create readable and reusable Promise chains.
114+
// Forces developers to return console logs and http calls in promises.
115+
"promise/always-return": 2,
116+
117+
//Enforces the use of catch() on un-returned promises
118+
"promise/catch-or-return": 2,
119+
120+
// Warn against nested then() or catch() statements
121+
"promise/no-nesting": 1
122+
}
123+
}

Firebase_Functions/functions/index.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
2+
'use strict';
3+
const functions = require('firebase-functions');
4+
const paypal = require('paypal-rest-sdk');
5+
const admin = require('firebase-admin');
6+
admin.initializeApp(functions.config().firebase);
7+
8+
paypal.configure({
9+
mode: 'sandbox',
10+
client_id: functions.config().paypal.client_id,
11+
client_secret: functions.config().paypal.client_secret
12+
})
13+
14+
15+
exports.newRequest = functions.database.ref('/history/{pushId}').onCreate(event => {
16+
var requestSnapshot = event.data;
17+
18+
var distance = requestSnapshot.child('distance').val();
19+
var price = distance * 0.5;
20+
21+
var pushId = event.params.pushId;
22+
23+
return requestSnapshot.ref.parent.child(pushId).child('price').set(price);
24+
25+
});
26+
27+
28+
function getPayoutsPending(uid){
29+
return admin.database().ref('Users/Drivers/' + uid + '/history').once('value').then((snap) =>{
30+
if(snap === null){
31+
throw new Error("profile doesn't exist");
32+
}
33+
var array = [];
34+
if(snap.hasChildren()){
35+
snap.forEach(element => {
36+
if(element.val() === true){
37+
array.push(element.key);
38+
}
39+
});
40+
}
41+
return array;
42+
}).catch((error) => {
43+
return console.error(error);
44+
});
45+
}
46+
47+
48+
function getPayoutsAmount(array){
49+
return admin.database().ref('history').once('value').then((snap) =>{
50+
var value = 0.0;
51+
if(snap.hasChildren()){
52+
snap.forEach(element => {
53+
if(array.indexOf(element.key) > -1){
54+
if(element.child('price').val() !== null){
55+
value += element.child('price').val();
56+
}
57+
}
58+
});
59+
return value;
60+
}
61+
return value;
62+
}).catch((error) => {
63+
return console.error(error);
64+
});
65+
}
66+
67+
68+
69+
function updatePaymentsPending(uid, paymentId){
70+
return admin.database().ref('Users/Drivers/' + uid + '/history').once('value').then((snap) =>{
71+
if(snap === null){
72+
throw new Error("profile doesn't exist");
73+
}
74+
75+
if(snap.hasChildren()){
76+
snap.forEach(element => {
77+
if(element.val() === true){
78+
admin.database().ref('Users/Drivers/' + uid + '/history/' + element.key).set({
79+
timestamp: admin.database.ServerValue.TIMESTAMP,
80+
paymentId: paymentId
81+
});
82+
admin.database().ref('history/' + element.key + '/driverPaidOut').set(true);
83+
}
84+
});
85+
}
86+
return null;
87+
}).catch((error) => {
88+
return console.error(error);
89+
});
90+
}
91+
92+
93+
94+
95+
96+
97+
98+
exports.payout = functions.https.onRequest((request, response) => {
99+
getPayoutsPending(request.body.uid).then((array) => {
100+
getPayoutsAmount(array).then((value) => {
101+
102+
var valueTrunc = parseFloat(Math.round(value * 100) / 100).toFixed(2);
103+
104+
const sender_batch_id = Math.random().toString(36).substring(9);
105+
const sync_mode = 'true';
106+
const payReq = JSON.stringify({
107+
sender_batch_header: {
108+
sender_batch_id: sender_batch_id,
109+
email_subject: "You have a payment"
110+
},
111+
items: [
112+
{
113+
recipient_type: "EMAIL",
114+
amount: {
115+
value: valueTrunc,
116+
currency: "USD"
117+
},
118+
receiver: request.body.email,
119+
note: "Thank you.",
120+
sender_item_id: "item_3"
121+
}
122+
]
123+
});
124+
paypal.payout.create(payReq, sync_mode, (error, payout) => {
125+
if(error){
126+
console.warn(error.response);
127+
response.status('500').end();
128+
throw error;
129+
}else{
130+
console.info("payout created");
131+
console.info(payout);
132+
updatePaymentsPending(request.body.uid, sender_batch_id).then(() =>{
133+
response.status('200').end();
134+
return;
135+
}).catch((error) => {
136+
return console.error(error);
137+
})
138+
}
139+
});
140+
141+
142+
143+
return null;
144+
}).catch((error) => {
145+
return console.error(error);
146+
})
147+
148+
return null;
149+
}).catch((error) => {
150+
return console.error(error);
151+
})
152+
153+
});
154+
155+

0 commit comments

Comments
 (0)