-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathstripe.cfc
192 lines (170 loc) · 7.56 KB
/
stripe.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
component {
public struct function init( string apiKey = '', struct config = { } ) {
var configObj = new lib.config( apiKey, config );
var basePath = getDirectoryFromPath( getMetadata( this ).path ).replace( '\', '/', 'all' );
variables.objectMetadata = loadMetadata( basePath );
variables.httpService = new lib.httpService();
variables.parsers = {
arguments: new lib.parsers.arguments( configObj ),
headers: new lib.parsers.headers( configObj ),
response: new lib.parsers.response( configObj, objectMetadata )
};
for ( var resourcePath in listResources( basePath ) ) {
var resourceParts = resourcePath.listFirst( '.' ).listToArray( '/' );
var parent = this;
for ( var i = 1; i < resourceParts.len(); i++ ) {
if ( !structKeyExists( parent, resourceParts[ i ] ) ) {
parent[ resourceParts[ i ] ] = { };
}
parent = parent[ resourceParts[ i ] ];
}
parent[ resourceParts[ i ] ] = new lib.apiResource( this, configObj, resourceParts.toList( '.' ) );
}
this[ 'webhooks' ] = new lib.webhooks( parsers.response );
return this;
}
public any function call(
required string resourceName,
required string methodName,
required any argCollection,
required struct methodMetadata,
struct argOverrides = { }
) {
var hasPositionalArguments = arrayLen( argCollection ) == 0 || structKeyExists( argCollection, '1' );
var sources = getSources( argCollection, methodMetadata, hasPositionalArguments );
var ignoredArgs = [ ];
// collect positional args and add to params
if ( hasPositionalArguments ) {
var argIndex = 1;
for ( var argName in methodMetadata.positionalArgs ) {
if ( arrayLen( argCollection ) < argIndex ) {
validationFailure(
'`#resourceName#.#methodName#()` missing positional argument `#argName#` at index [#argIndex#]'
);
} else if ( !isSimpleValue( argCollection[ argIndex ] ) ) {
validationFailure(
'`#resourceName#.#methodName#()` positional argument `#argName#` at index [#argIndex#] is not a simple value'
);
} else {
sources.params[ argName ] = argCollection[ argIndex ];
}
argIndex++;
}
} else {
for ( var argName in methodMetadata.positionalArgs ) {
if ( !structKeyExists( argCollection, argName ) ) {
validationFailure( '`#resourceName#.#methodName#()` missing required argument `#argName#`' );
} else if ( !isSimpleValue( argCollection[ argName ] ) ) {
validationFailure( '`#resourceName#.#methodName#()` argument `#argName#` is not a simple value' );
}
}
}
sources.params.append( argOverrides, true );
// path
var path = 'https://' & methodMetadata.endpoint & methodMetadata.path;
for ( var argName in methodMetadata.pathArgs ) {
path = path.replace( '{#argName#}', sources.params[ argName ] );
ignoredArgs.append( argName );
}
// headers
var headerData = parsers.headers.parse( sources.headers, methodMetadata, hasPositionalArguments );
if ( !hasPositionalArguments ) {
ignoredArgs.append( headerData.headerArgNames, true );
}
// params
var params = parsers.arguments.parse( sources.params, methodMetadata.arguments, ignoredArgs );
var requestStart = getTickCount();
var rawResponse = httpService.makeRequest(
methodMetadata.httpmethod,
path,
headerData.headers,
params,
methodMetadata.multipart
);
// if we weren't able to connect to Stripe at all, throw an error
if ( rawResponse.statuscode.startswith( 'Connection Failure' ) ) {
throw(
type = 'StripeConnectionFailure',
message = 'Could not connect to Stripe API: ' & rawResponse.status_text,
detail = rawResponse.errorDetail
);
}
var response = { };
response[ 'duration' ] = getTickCount() - requestStart;
response[ 'headers' ] = rawResponse.responseheader;
response[ 'status' ] = listFirst( rawResponse.statuscode, ' ' );
response[ 'content' ] = rawResponse.filecontent;
if ( structKeyExists( response.headers, 'Request-Id' ) ) {
response[ 'requestId' ] = response.headers[ 'Request-Id' ];
} else if ( int( response.status ) >= 200 && int( response.status ) < 300 ) {
throw(
type = 'StripeResponseException',
message = 'Request-Id is missing from the response headers',
extendedInfo = serializeJSON( response )
);
}
if ( structKeyExists( response.headers, 'Content-Type' ) ) {
if ( response.headers[ 'Content-Type' ] == 'application/json' ) {
response.content = deserializeJSON( response.content );
parsers.response.parse( response.content );
}
} else if ( int( response.status ) >= 200 && int( response.status ) < 300 ) {
throw(
type = 'StripeResponseException',
message = 'Content-Type is missing from the response headers',
extendedInfo = serializeJSON( response )
);
}
return response;
}
private any function getSources(
required any argCollection,
required struct methodMetadata,
required boolean hasPositionalArguments
) {
var sourceKeys = [
{
name: 'params',
offset: 1
},
{
name: 'headers',
offset: 2
}
];
var sources = { };
for ( var source in sourceKeys ) {
if ( hasPositionalArguments ) {
var sourceIndex = arrayLen( methodMetadata.positionalArgs ) + source.offset;
sources[ source.name ] = arrayLen( argCollection ) >= sourceIndex ? argCollection[ sourceIndex ] : { };
} else {
sources[ source.name ] = argCollection;
}
}
return sources;
}
private struct function loadMetadata( required string basePath ) {
var metadataPath = basePath & 'metadata/';
var metadata = { };
var jsonFiles = directoryList( metadataPath, true, 'path', '*.json' );
for ( var path in jsonFiles ) {
var metaName = path
.replace( '\', '/', 'all' )
.replace( metadataPath, '' )
.listFirst( '.' )
.replace( '/', '.', 'all' );
metadata[ metaName ] = deserializeJSON( fileRead( path ) );
}
return metadata;
}
private array function listResources( required string basePath ) {
var resourcePath = basePath & 'lib/resources/';
var paths = directoryList( resourcePath, true, 'path', '*.cfc' );
return paths.map( function( path ) {
return path.replace( '\', '/', 'all' ).replace( resourcePath, '' );
} );
}
private void function validationFailure( required string message ) {
throw( type = 'StripeValidationException', message = message );
}
}