-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.driver.php
executable file
·140 lines (112 loc) · 3.69 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
<?php
Class extension_Paypal extends Extension {
public static $config_handle = 'paypal';
public function getSubscribedDelegates(){
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
),
);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public function getSetting($key) {
return Symphony::Configuration()->get($key, self::$config_handle);
}
/*-------------------------------------------------------------------------
Preferences:
-------------------------------------------------------------------------*/
public function getPreferencesData() {
$data = array(
'api-username' => '',
'api-password' => '',
'api-signature' => '',
'gateway-mode' => 'sandbox'
);
foreach ($data as $key => &$value) {
$value = $this->getSetting($key);
}
return $data;
}
/**
* Allow the user to add their Paypal keys.
*
* @uses AddCustomPreferenceFieldsets
*/
public function appendPreferences($context) {
$data = $this->getPreferencesData();
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('Paypal Details')));
$this->buildPreferences($fieldset, array(
array(
'label' => 'API username',
'name' => 'api-username',
'value' => $data['api-username'],
'type' => 'text'
),
array(
'label' => 'API password',
'name' => 'api-password',
'value' => $data['api-password'],
'type' => 'password'
),
array(
'label' => 'API signature',
'name' => 'api-signature',
'value' => $data['api-signature'],
'type' => 'text'
)
));
$context['wrapper']->appendChild($fieldset);
}
public function buildPreferences($fieldset, $data) {
$row = null;
foreach ($data as $index => $item) {
if ($index % 2 == 0) {
if ($row) $fieldset->appendChild($row);
$row = new XMLElement('div');
$row->setAttribute('class', 'group');
}
$label = Widget::Label(__($item['label']));
$name = 'settings[' . self::$config_handle . '][' . $item['name'] . ']';
$input = Widget::Input($name, $item['value'], $item['type']);
$label->appendChild($input);
$row->appendChild($label);
}
// Build the Gateway Mode
$label = new XMLElement('label', __('Gateway Mode'));
$options = array(
array('sandbox', $this->isTesting() , __('Sandbox')),
array('live', !$this->isTesting(), __('Live'))
);
$label->appendChild(Widget::Select('settings[paypal][gateway-mode]', $options));
$row->appendChild($label);
$fieldset->appendChild($row);
}
public static function isTesting() {
return Symphony::Configuration()->get('gateway-mode', 'paypal') == 'sandbox';
}
/**
* Saves the Paypal to the configuration
*
* @uses savePreferences
*/
public function savePreferences(array &$context){
$settings = $context['settings'];
// Active Section
Symphony::Configuration()->set('api-username', $settings['paypal']['api-username'], 'paypal');
Symphony::Configuration()->set('api-password', $settings['paypal']['api-password'], 'paypal');
Symphony::Configuration()->set('api-signature', $settings['paypal']['api-signature'], 'paypal');
Symphony::Configuration()->set('gateway-mode', $settings['paypal']['gateway-mode'], 'paypal');
Administration::instance()->saveConfig();
}
}