@@ -41,6 +41,10 @@ import * as path from "path";
4141import { UpdateFileMessage } from "./UpdateFileMessage" ;
4242import { DatabaseConnection } from "../DatabaseConnection" ;
4343import { CreateWorkflowInstanceMessage } from "./CreateWorkflowInstanceMessage" ;
44+ import { StripeMessage } from "./StripeMessage" ;
45+ var request = require ( "request" ) ;
46+ var got = require ( "got" ) ;
47+
4448
4549const safeObjectID = ( s : string | number | ObjectID ) => ObjectID . isValid ( s ) ? new ObjectID ( s ) : null ;
4650export 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
13991550export class JSONfn {
0 commit comments