Skip to content

Commit 1e9a70a

Browse files
authored
Merge pull request openiap#41 from skadefro/master
bump
2 parents 1856d0e + cf50c0f commit 1e9a70a

16 files changed

Lines changed: 1043 additions & 6 deletions

OpenFlow/src/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export class Config {
1414

1515
public static NODE_ENV: string = Config.getEnv("NODE_ENV", "development");
1616

17+
public static stripe_api_key: string = Config.getEnv("stripe_api_key", "");
18+
public static stripe_api_secret: string = Config.getEnv("stripe_api_secret", "");
19+
1720
public static auto_create_users: boolean = Config.parseBoolean(Config.getEnv("auto_create_users", "false"));
1821
public static auto_create_domains: string[] = Config.parseArray(Config.getEnv("auto_create_domains", ""));
1922
public static allow_user_registration: boolean = Config.parseBoolean(Config.getEnv("allow_user_registration", "false"));

OpenFlow/src/LoginProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ export class LoginProvider {
237237
namespace: Config.namespace,
238238
nodered_domain_schema: Config.nodered_domain_schema,
239239
websocket_package_size: Config.websocket_package_size,
240-
version: Config.version
240+
version: Config.version,
241+
stripe_api_key: Config.stripe_api_key
241242
}
242243
res.end(JSON.stringify(res2));
243244
});

OpenFlow/src/Messages/Message.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ import * as path from "path";
4141
import { UpdateFileMessage } from "./UpdateFileMessage";
4242
import { DatabaseConnection } from "../DatabaseConnection";
4343
import { CreateWorkflowInstanceMessage } from "./CreateWorkflowInstanceMessage";
44+
import { StripeMessage } from "./StripeMessage";
45+
var request = require("request");
46+
var got = require("got");
47+
4448

4549
const safeObjectID = (s: string | number | ObjectID) => ObjectID.isValid(s) ? new ObjectID(s) : null;
4650
export class Message {
@@ -180,6 +184,9 @@ export class Message {
180184
case "createworkflowinstance":
181185
this.CreateWorkflowInstance(cli);
182186
break;
187+
case "stripemessage":
188+
this.StripeMessage(cli);
189+
break;
183190
default:
184191
this.UnknownCommand(cli);
185192
break;
@@ -1394,6 +1401,150 @@ export class Message {
13941401
}
13951402
this.Send(cli);
13961403
}
1404+
1405+
async async_request(options: any) {
1406+
return new Promise<any>(async (resolve, reject) => {
1407+
try {
1408+
request(options, (error, response, body) => {
1409+
if (error) return reject(error);
1410+
if (response.statusCode != 200) {
1411+
if (body != null) return reject(body);
1412+
return reject(response.statusText);
1413+
}
1414+
resolve(body);
1415+
});
1416+
} catch (error) {
1417+
reject(error);
1418+
}
1419+
});
1420+
}
1421+
isObject(obj) {
1422+
const type = typeof obj;
1423+
return (type === 'function' || type === 'object') && !!obj;
1424+
}
1425+
flattenAndStringify(data) {
1426+
const result = {};
1427+
1428+
const step = (obj, prevKey) => {
1429+
Object.keys(obj).forEach((key) => {
1430+
const value = obj[key];
1431+
1432+
const newKey = prevKey ? `${prevKey}[${key}]` : key;
1433+
1434+
if (this.isObject(value)) {
1435+
if (!Buffer.isBuffer(value) && !value.hasOwnProperty('data')) {
1436+
// Non-buffer non-file Objects are recursively flattened
1437+
return step(value, newKey);
1438+
} else {
1439+
// Buffers and file objects are stored without modification
1440+
result[newKey] = value;
1441+
}
1442+
} else {
1443+
// Primitives are converted to strings
1444+
result[newKey] = String(value);
1445+
}
1446+
});
1447+
};
1448+
step(data, undefined);
1449+
return result;
1450+
}
1451+
async StripeMessage(cli: WebSocketClient) {
1452+
this.Reply();
1453+
var msg: StripeMessage;
1454+
try {
1455+
msg = StripeMessage.assign(this.data);
1456+
1457+
if (Util.IsNullEmpty(msg.object)) throw new Error("object is mandatory");
1458+
var url = "https://api.stripe.com/v1/" + msg.object;
1459+
if (!Util.IsNullEmpty(msg.id)) url = url + "/" + msg.id;
1460+
if (!Util.IsNullEmpty(msg.url)) url = msg.url;
1461+
if (!cli.user.HasRoleName("admins")) {
1462+
if (!Util.IsNullEmpty(msg.url)) throw new Error("Custom url not allowed");
1463+
if (msg.object != "customers" && msg.object != "tax_ids"
1464+
&& msg.object != "products" && msg.object != "plans" &&
1465+
msg.object != "checkout.sessions" && msg.object != "tax_rates") throw new Error("Access to " + msg.object + " is not allowed");
1466+
}
1467+
if (msg.object == "tax_ids") {
1468+
if (Util.IsNullEmpty(msg.customerid)) throw new Error("Need customer to work with tax_id");
1469+
url = "https://api.stripe.com/v1/customers/" + msg.customerid + "/tax_ids";
1470+
if (msg.method == "DELETE" || msg.method == "PUT") {
1471+
if (Util.IsNullEmpty(msg.id)) throw new Error("Need id");
1472+
url = "https://api.stripe.com/v1/customers/" + msg.customerid + "/tax_ids/" + msg.id;
1473+
}
1474+
if (!Util.IsNullEmpty(msg.id)) {
1475+
url = "https://api.stripe.com/v1/customers/" + msg.customerid + "/tax_ids/" + msg.id;
1476+
}
1477+
}
1478+
if (msg.object == "checkout.sessions") {
1479+
if (msg.method != "GET") {
1480+
if (Util.IsNullEmpty(msg.customerid)) throw new Error("Need customer to work with sessions");
1481+
}
1482+
url = "https://api.stripe.com/v1/checkout/sessions";
1483+
if (!Util.IsNullEmpty(msg.id)) {
1484+
url = "https://api.stripe.com/v1/checkout/sessions/" + msg.id;
1485+
}
1486+
}
1487+
1488+
1489+
var auth = "Basic " + new Buffer(Config.stripe_api_secret + ":").toString("base64");
1490+
1491+
var options = {
1492+
headers: {
1493+
'Content-type': 'application/x-www-form-urlencoded',
1494+
'Authorization': auth
1495+
}
1496+
// , body: JSON.stringify(msg.payload)
1497+
};
1498+
if (msg.payload != null && msg.method != "GET" && msg.method != "DELETE") {
1499+
var flattenedData = this.flattenAndStringify(msg.payload);
1500+
(options as any).form = flattenedData;
1501+
}
1502+
if (msg.method == "POST") {
1503+
var response = await got.post(url, options);
1504+
msg.payload = JSON.parse(response.body);
1505+
}
1506+
if (msg.method == "GET") {
1507+
var response = await got.get(url, options);
1508+
msg.payload = JSON.parse(response.body);
1509+
}
1510+
if (msg.method == "PUT") {
1511+
var response = await got.put(url, options);
1512+
msg.payload = JSON.parse(response.body);
1513+
}
1514+
if (msg.method == "DELETE") {
1515+
var response = await got.delete(url, options);
1516+
msg.payload = JSON.parse(response.body);
1517+
}
1518+
if (msg.payload != null) {
1519+
if (msg.payload.deleted) {
1520+
msg.payload = null;
1521+
}
1522+
}
1523+
1524+
// msg.payload = JSON.parse(result);
1525+
} catch (error) {
1526+
if (error == null) new Error("Unknown error");
1527+
cli._logger.error(error);
1528+
if (Util.IsNullUndefinded(msg)) { (msg as any) = {}; }
1529+
if (msg !== null && msg !== undefined) {
1530+
msg.error = error;
1531+
if (error.message) msg.error = error.message;
1532+
// if (error.stack) msg.error = error.stack;
1533+
if (error.response && error.response.body) {
1534+
msg.error = error.response.body;
1535+
}
1536+
}
1537+
cli._logger.error(error);
1538+
}
1539+
try {
1540+
this.data = JSON.stringify(msg);
1541+
} catch (error) {
1542+
this.data = "";
1543+
cli._logger.error(error);
1544+
}
1545+
this.Send(cli);
1546+
}
1547+
13971548
}
13981549

13991550
export class JSONfn {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export class stripe_base {
2+
public id: string;
3+
public object: string;
4+
public created: number;
5+
public livemode: boolean;
6+
public deleted: boolean;
7+
}
8+
export class StripeMessage implements IReplyMessage {
9+
public error: string;
10+
public jwt: any;
11+
public method: string;
12+
public object: string;
13+
public id: string;
14+
public customerid: string;
15+
public url: string;
16+
public payload: stripe_base;
17+
18+
static assign(o: any): StripeMessage {
19+
if (typeof o === "string" || o instanceof String) {
20+
return Object.assign(new StripeMessage(), JSON.parse(o.toString()));
21+
}
22+
return Object.assign(new StripeMessage(), o);
23+
}
24+
}
25+
export class stripe_list<T> {
26+
public object: string;
27+
public has_more: boolean;
28+
public url: string;
29+
public data: T[];
30+
}
31+
export class stripe_customer extends stripe_base {
32+
}
33+
export class stripe_tax_id extends stripe_base {
34+
}
35+
export class stripe_subscription extends stripe_base {
36+
public address: string;
37+
public balance: number;
38+
public currency: string;
39+
public subscriptions: stripe_list<stripe_subscription>;
40+
public tax_ids: stripe_list<stripe_tax_id>;
41+
}

OpenFlow/src/public/CommonControllers.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ module openflow {
9797
}
9898
}
9999
if (data === null || data === undefined) {
100-
if (this.$location.path() !== "/Login") {
100+
if (this.$location.path() !== "/Login" && this.$location.path() !== "/Signup") {
101101
var _url = this.$location.absUrl();
102102
this.setCookie("url", _url, 365);
103103
this.$location.path("/Login");
@@ -315,6 +315,13 @@ module openflow {
315315
var result: UpdateFileMessage = await this.WebSocketClient.Send<UpdateFileMessage>(msg);
316316
return;
317317
}
318+
async Stripe<T extends stripe_base>(method: string, object: string, customerid: string, id: string, payload: stripe_base): Promise<T> {
319+
var q: StripeMessage = new StripeMessage();
320+
q.method = method; q.object = object; q.customerid = customerid; q.id = id; q.payload = payload;
321+
var msg: Message = new Message(); msg.command = "stripemessage"; msg.data = JSON.stringify(q);
322+
q = await this.WebSocketClient.Send<StripeMessage>(msg);
323+
return (q.payload as any);
324+
}
318325

319326
setCookie(cname, cvalue, exdays) {
320327
var d = new Date();
@@ -671,7 +678,6 @@ module openflow {
671678
query = { $and: [query, { $or: finalor.concat() }] };
672679
}
673680
}
674-
console.log(this.orderby);
675681
this.models = await this.api.Query(this.collection, query, this.baseprojection, this.orderby, 100, 0, this.basequeryas);
676682
this.loading = false;
677683
if (this.autorefresh) {

0 commit comments

Comments
 (0)