forked from lewiswharf/stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.driver.php
367 lines (323 loc) · 15.7 KB
/
extension.driver.php
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
require_once(EXTENSIONS . '/stripe/lib/class.stripegeneral.php');
class Extension_Stripe extends Extension {
/*-------------------------------------------------------------------------
Delegates:
-------------------------------------------------------------------------*/
public function getSubscribedDelegates() {
return array(
array(
'page' => '/blueprints/events/',
'delegate' => 'EventPreEdit',
'callback' => 'actionEventPreEdit'
),
array(
'page' => '/blueprints/events/new/',
'delegate' => 'AppendEventFilter',
'callback' => 'actionAppendEventFilter'
),
array(
'page' => '/blueprints/events/edit/',
'delegate' => 'AppendEventFilter',
'callback' => 'actionAppendEventFilter'
),
array(
'page' => '/blueprints/events/',
'delegate' => 'AppendEventFilterDocumentation',
'callback' => 'actionAppendEventFilterDocumentation'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPreSaveFilter',
'callback' => 'actionEventPreSaveFilter'
),
array(
'page' => '/frontend/',
'delegate' => 'EventPostSaveFilter',
'callback' => 'actionEventPostSaveFilter'
),
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'actionAddCustomPreferenceFieldsets'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'actionSave'
)
);
}
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function actionEventPreEdit($context) {
// Your code goes here...
}
public function actionAppendEventFilter($context) {
$filters = Stripe_General::getAllFilters();
foreach ($filters as $key => $val) {
if (is_array($context['selected'])) {
$selected = in_array($key, $context['selected']);
$context['options'][] = array($key, $selected, $val);
}
}
}
public function actionAppendEventFilterDocumentation($context) {
// Todo not firing
}
public function actionEventPreSaveFilter($context) {
$filters = $context['event']->eParamFILTERS;
$proceed = false;
foreach ($filters as $key => $val) {
if (in_array($val, array_keys(Stripe_General::getAllFilters()))) {
$proceed = true;
}
}
if(!$proceed) {
return true;
}
// print_r($_POST); die();
if(is_array($_SESSION)) {
if(array_key_exists('order-id', $_SESSION)) {
$session_ord = $_SESSION['order-id'];
} else {
$session_ord = false;
}
} else {
$session_ord = false;
}
if($session_ord!=$_POST['fields']['order-id']) {
require_once(EXTENSIONS . '/stripe/lib/api/lib/Stripe.php');
Stripe::setApiKey(Stripe_General::getApiKey());
$fields = $_POST['stripe'];
// Convert handles if Symphony standard
foreach ($fields as $key => $val) {
$key = str_replace('-', '_', $key);
$fields[$key] = $val;
}
foreach ($filters as $key => $val) {
if (in_array($val, array_keys(Stripe_General::getAllFilters()))) {
try {
switch($val) {
case 'Stripe_Customer-create':
$stripe = Stripe_Customer::create($fields);
break;
case 'Stripe_Customer-retrieve-update':
$stripe = Stripe_Customer::retrieve($fields['id']);
unset($fields['id']);
$stripe = Stripe_General::setStripeFieldsToUpdate($stripe, $fields);
$stripe = $stripe->save();
break;
case 'Stripe_Customer-retrieve-delete':
$stripe = Stripe_Customer::retrieve($fields['id']);
$stripe = $stripe->delete();
break;
case 'Stripe_Customer-retrieve-updateSubscription':
$stripe = Stripe_Customer::retrieve($fields['id']);
unset($fields['id']);
$stripe = $stripe->updateSubscription($fields);
break;
case 'Stripe_Customer-retrieve-cancelSubscription':
$stripe = Stripe_Customer::retrieve($fields['id']);
$stripe = $stripe->cancelSubscription();
break;
case 'Stripe_Customer-retrieve-deleteDiscount':
$stripe = Stripe_Customer::retrieve($fields['id']);
$stripe = $stripe->deleteDiscount();
break;
case 'Stripe_Charge-create':
$stripe = Stripe_Charge::create($fields);
break;
case 'Stripe_Charge-retrieve-refund':
$stripe = Stripe_Charge::retrieve($fields['id']);
$stripe = $stripe->refund();
break;
case 'Stripe_InvoiceItem-create':
$stripe = Stripe_InvoiceItem::create($fields);
break;
case 'Stripe_Invoice-create':
$stripe = Stripe_Invoice::create($fields);
break;
case 'Stripe_InvoiceItem-retrieve-update':
$stripe = Stripe_InvoiceItem::retrieve($fields['id']);
unset($fields['id']);
$stripe = Stripe_General::setStripeFieldsToUpdate($stripe, $fields);
$stripe = $stripe->save();
break;
case 'Stripe_Invoice-retrieve-closed':
$stripe = Stripe_Invoice::retrieve($fields['id']);
$stripe->closed;
$stripe = $stripe->save();
break;
case 'Stripe_InvoiceItem-retrieve-delete':
$stripe = Stripe_InvoiceItem::retrieve($fields['id']);
$stripe = $stripe->delete();
break;
case 'Stripe_Invoice-retrieve-pay':
$stripe = Stripe_Invoice::retrieve($fields['id']);
$stripe = $stripe->pay();
break;
}
} catch (Stripe_InvalidRequestError $e) {
// Invalid parameters were supplied to Stripe's API
$context['messages'][] = array('stripe', false, $e->getMessage());
Stripe_General::emailPrimaryDeveloper($e->getMessage());
return $context;
} catch (Stripe_AuthenticationError $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
$context['messages'][] = array('stripe', false, $e->getMessage());
Stripe_General::emailPrimaryDeveloper($e->getMessage());
return $context;
} catch (Stripe_ApiConnectionError $e) {
// Network communication with Stripe failed
$context['messages'][] = array('stripe', false, $e->getMessage());
Stripe_General::emailPrimaryDeveloper($e->getMessage());
return $context;
} catch (Stripe_Error $e) {
// Display a very generic error to the user, and maybe send
$context['messages'][] = array('stripe', false, $e->getMessage());
Stripe_General::emailPrimaryDeveloper($e->getMessage());
return $context;
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
$context['messages'][] = array('stripe', false, $e->getMessage());
Stripe_General::emailPrimaryDeveloper($e->getMessage());
return $context;
}
}
}
} else {
$context['messages'][] = array('stripe', false, 'Duplicate purchase by an accidental page refresh was rejected.');
// $stripe = unserialize($_SESSION['symphony-stripe']);
// Ensure updated stripe[...] fields replace empty fields
// foreach($stripe as $key => $val) {
// if(empty($val) && isset($_POST['stripe'][$key])) {
// $stripe[$key] = $_POST['stripe'][$key];
// Todo consider updating stripe if user changes an optional field after tripe creation but prior to symphony event success
// }
// }
}
if (!empty($stripe)) {
// Convert stripe object to array so that it can be looped
if(is_object($stripe)) {
$stripe = $stripe->__toArray();
foreach($stripe as $key => $val){
if(is_object($val)) {
$stripe[$key] = $val->__toArray();
}
}
}
// Add values of response for Symphony event to process
if(is_array($context['fields'])) {
$context['fields'] = array_merge(Stripe_General::addStripeFieldsToSymphonyEventFields($stripe), $context['fields']);
} else {
$context['fields'] = Stripe_General::addStripeFieldsToSymphonyEventFields($stripe);
}
// Create the post data cookie element
General::array_to_xml($context['post_values'], $stripe, true);
// Add stripe response to session in case event fails
$_SESSION['symphony-stripe'] = serialize($stripe);
}
$_SESSION['order-id'] = $_POST['fields']['order-id'];
return $context;
}
public function actionEventPostSaveFilter($context) {
// Clear session saved response
}
public function actionAddCustomPreferenceFieldsets($context) {
// If the Payment Gateway Interface extension is installed, don't
// double display the preference, unless this function is called from
// the `pgi-loader` context.
if (in_array('pgi_loader', Symphony::ExtensionManager()->listInstalledHandles()) xor isset($context['pgi-loader'])) return;
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Stripe')));
$div = new XMLElement('div', null);
$group = new XMLElement('div', null, array('class' => 'group'));
// Build the Gateway Mode
$label = new XMLElement('label', __('Stripe Mode'));
$options = array(
array('test', Stripe_General::isTestMode(), __('Test')),
array('live', !Stripe_General::isTestMode(), __('Live'))
);
$label->appendChild(Widget::Select('settings[stripe][gateway-mode]', $options));
$div->appendChild($label);
$fieldset->appendChild($div);
// Live Public API Key
$label = new XMLElement('label', __('Live Secret API Key'));
$label->appendChild(
Widget::Input('settings[stripe][live-api-key]', Symphony::Configuration()->get("live-api-key", 'stripe'))
);
$group->appendChild($label);
// Test Public API Key
$label = new XMLElement('label', __('Test Secret API Key'));
$label->appendChild(
Widget::Input('settings[stripe][test-api-key]', Symphony::Configuration()->get("test-api-key", 'stripe'))
);
$group->appendChild($label);
$fieldset->appendChild($group);
$context['wrapper']->appendChild($fieldset);
}
public function actionSave($context) {
$settings = $context['settings'];
Symphony::Configuration()->set('test-api-key', $settings['stripe']['test-api-key'], 'stripe');
Symphony::Configuration()->set('live-api-key', $settings['stripe']['live-api-key'], 'stripe');
Symphony::Configuration()->set('gateway-mode', $settings['stripe']['gateway-mode'], 'stripe');
return Symphony::Configuration()->write();
}
public function install() {
// Create stripe_customer_id field database:
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_fields_stripe_customer_id` (
`id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`field_id` INT(11) unsigned NOT NULL,
`validator` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`disabled` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
// Create stripe_customer_link field database:
Symphony::Database()->query("
CREATE TABLE IF NOT EXISTS `tbl_fields_stripe_customer_link` (
`id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
`field_id` INT(11) unsigned NOT NULL,
`related_field_id` VARCHAR(255) NOT NULL,
`show_association` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
`disabled` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
");
}
public function uninstall() {
// Drop field tables:
Symphony::Database()->query("DROP TABLE `tbl_fields_stripe_customer_id`");
Symphony::Database()->query("DROP TABLE `tbl_fields_stripe_customer_link`");
// Clean configuration
Symphony::Configuration()->remove('test-api-key', 'stripe');
Symphony::Configuration()->remove('live-api-key', 'stripe');
Symphony::Configuration()->remove('gateway-mode', 'stripe');
return Symphony::Configuration()->write();
}
// public function fetchNavigation() {
// return array(
// array(
// 'location' => 1000,
// 'name' => __('Stripe'),
// 'children' => array(
// array(
// 'name' => __('Plans'),
// 'link' => '/plans/'
// ),
// array(
// 'name' => __('Coupons'),
// 'link' => '/coupons/'
// )
// )
// )
// );
// }
}