-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.php
executable file
·228 lines (201 loc) · 7.08 KB
/
Plugin.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
<?php namespace SublimeArts\SublimeStripe;
use Backend, Event;
use System\Classes\PluginBase;
use SublimeArts\SublimeStripe\Models\Settings;
use SublimeArts\SublimeStripe\Models\User;
use Stripe\Stripe;
use RainLab\User\Models\User as BaseUser;
/**
* SublimeStripe Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* @var array Plugin dependencies
*/
public $require = ['RainLab.User'];
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Sublime Stripe',
'description' => 'Provides easy payment handling with Stripe',
'author' => 'Sublime Arts',
'homepage' => 'http://www.sublimearts.me',
'icon' => 'icon-cc-stripe'
];
}
/**
* Register method, called when the plugin is first registered.
*
* @return void
*/
public function register()
{
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
/**
* TODO: NEEDS A BETTER SOLUTION
*/
/** Check that required settings have been set */
// Settings::checkRequired();
/** Set the Stripe API Key */
Stripe::setApiKey(Settings::get('stripe_secret_key'));
/** Extend the RainLab User Model class */
BaseUser::extend(function($model) {
$model->hasOne['user'] = [
'SublimeArts\SublimeStripe\Models\User',
'key' => 'base_user_id',
'softDelete' => true
];
$model->bindEvent('model.afterCreate', function() use ($model) {
if ( ! User::where('base_user_id', $model->id)->first() ) {
User::create([
'base_user_id' => $model->id
]);
}
});
$model->bindEvent('model.afterDelete', function() use ($model) {
if ( $user = User::where('base_user_id', $model->id)->first() && ! $model->isSoftDelete()) {
$model->user()->forceDelete();
}
});
});
/** Extend the Product Model class */
$productModelClass = Settings::productModelClass();
$productModelClass::extend(function($model) {
$model->hasMany['singleCharges'] = [
'SublimeArts\SublimeStripe\Models\SingleCharge',
'softDelete' => true
];
$model->hasMany['subscriptions'] = [
'SublimeArts\SublimeStripe\Models\Subscription',
'softDelete' => true
];
});
/** Add some extra columns */
Event::listen('backend.list.extendColumns', function($widget) {
// Only for the User controller
if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
return;
}
// Only for the User model
if (!$widget->model instanceof \RainLab\User\Models\User) {
return;
}
/** Add extra columns to the User backend list */
$widget->addColumns([
'stripe_id' => [
'label' => 'Stripe ID',
'type' => 'partial',
'path' => '$/sublimearts/sublimestripe/models/user/_stripe_id.htm',
'clickable' => false
],
'activated' => [
'label' => 'Account Active?',
'type' => 'partial',
'path' => '$/sublimearts/sublimestripe/models/user/_activated.htm',
'clickable' => false
],
'stripe_active' => [
'label' => 'Stripe Active?',
'type' => 'partial',
'path' => '$/sublimearts/sublimestripe/models/user/_stripe_active.htm',
'clickable' => false
]
]);
});
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return [
'SublimeArts\SublimeStripe\Components\CheckoutButton' => 'stripeCheckoutButton',
'SublimeArts\SublimeStripe\Components\StripeJs' => 'stripeJS',
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return [
'sublimearts.sublimestripe.manage_settings' => [
'tab' => 'Sublime Stripe',
'label' => 'Manage plugin settings.'
],
'sublimearts.sublimestripe.manage_sublime_stripe' => [
'tab' => 'Sublime Stripe',
'label' => 'Manage Sublime Stripe.'
],
];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
*/
public function registerNavigation()
{
return [
'sublimestripe' => [
'label' => (Settings::get('backend_menu_item_title'))
? Settings::get('backend_menu_item_title')
: 'Stripe Payments',
'url' => Backend::url('sublimearts/sublimestripe/singlecharges'),
'icon' => 'icon-cc-stripe',
'iconSvg' => 'plugins/sublimearts/sublimestripe/assets/images/stripe-icon.svg',
'permissions' => ['sublimearts.sublimestripe.manage_sublime_stripe'],
'order' => 500,
'sideMenu' => [
'singlecharges' => [
'label' => 'Single Charges',
'icon' => 'icon-money',
'url' => Backend::url('sublimearts/sublimestripe/singlecharges'),
'permissions' => ['sublimearts.sublimestripe.*'],
],
'subscriptions' => [
'label' => 'Subscriptions',
'icon' => 'icon-repeat',
'url' => Backend::url('sublimearts/sublimestripe/subscriptions'),
'permissions' => ['sublimearts.sublimestripe.*'],
],
]
],
];
}
/**
* Register Backend Settings
* @return array
*/
public function registerSettings()
{
return [
'sublimestripe' => [
'label' => 'Sublime Stripe',
'description' => 'Manage settings related to Stripe integration.',
'icon' => 'icon-cc-stripe',
'class' => 'SublimeArts\SublimeStripe\Models\Settings',
'order' => 500,
'keywords' => 'sublimestripe stripe payment payments checkout',
'permissions' => ['sublimearts.sublimestripe.manage_settings']
]
];
}
}