2
2
3
3
namespace Code4mk \LaraStripe \Lib ;
4
4
5
- /**
6
- * @author @code4mk <[email protected] >
7
- * @author @kawsarsoft <[email protected] >
8
- * @copyright Kawsar Soft. (http://kawsarsoft.com)
9
- */
5
+ use Stripe \StripeClient ;
6
+ use Code4mk \LaraStripe \Lib \StripeProducts ;
10
7
11
- use Config ;
12
- use Stripe \Plan ;
13
- use Stripe \Stripe ;
14
- use Stripe \Product ;
15
-
16
- /**
17
- * Plan class.
18
- *
19
- * @source https://stripe.com/docs/api/plans/create
20
- */
21
8
class StripePlans
22
9
{
23
10
/**
@@ -67,108 +54,107 @@ class StripePlans
67
54
*
68
55
* @var int
69
56
*/
70
- private $ trial ;
57
+ private $ trial_time ;
71
58
72
- public function __construct ()
73
- {
74
- if (config::get ('lara-stripe.driver ' ) === 'config ' ) {
75
- $ this ->secretKey = config::get ('lara-stripe.secret_key ' );
76
- }
77
- }
59
+ /**
60
+ * Stripe instance.
61
+ */
62
+ public $ stripe ;
78
63
79
64
/**
80
- * Set secret key
81
- *
82
- * @param string $data
83
- * @return $this
65
+ * A brief description of the plan, hidden from customers.
84
66
*/
85
- public function setup ($ data )
86
- {
87
- if (isset ($ data ['secret_key ' ])) {
88
- $ this ->secretKey = $ data ['secret_key ' ];
89
- }
67
+ private $ nickname ;
90
68
91
- return $ this ;
69
+ public function __construct ()
70
+ {
71
+ $ this ->secretKey = config ('lara-stripe.secret_key ' );
72
+ $ this ->stripe = new StripeClient ($ this ->secretKey );
92
73
}
93
74
94
75
/**
95
76
* Plan products
96
77
*
97
- * @param aray $data
98
- *
99
- * @source https://stripe.com/docs/api/plans/create#create_plan-product
100
- *
78
+ * @param array $data
101
79
* @return $this
102
80
*/
103
81
public function product ($ data )
104
82
{
105
83
$ this ->product = $ data ;
106
-
107
84
return $ this ;
108
85
}
109
86
110
87
/**
111
88
* Plan price
112
89
*
113
- * @param int|float $amount
90
+ * @param int|float $amount
114
91
* @return $this
115
92
*/
116
93
public function amount ($ amount )
117
94
{
118
95
$ this ->amount = round ($ amount , 2 ) * 100 ;
119
-
120
96
return $ this ;
121
97
}
122
98
123
99
/**
124
100
* Plan recurring interval
125
101
*
126
- * @param string $type [day,week,month,year]
102
+ * @param string $type [day,week,month,year]
127
103
* @return $this
128
104
*/
129
105
public function interval ($ type )
130
106
{
131
107
$ this ->interval = $ type ;
132
-
133
108
return $ this ;
134
109
}
135
110
136
111
/**
137
112
* Plan currency
138
113
*
139
- * @param $currency
114
+ * @param string $currency
140
115
* @return $this
141
116
*/
142
117
public function currency ($ currency )
143
118
{
144
119
$ this ->currency = $ currency ;
145
-
146
120
return $ this ;
147
121
}
148
122
149
123
/**
150
- * Plan extra properties
124
+ * Plan extra properties.
151
125
*
152
- * @param array $data associate array
126
+ * @param array $data associate array
153
127
* @return $this
154
128
*/
155
129
public function extra ($ data )
156
130
{
157
131
$ this ->extra = $ data ;
158
-
159
132
return $ this ;
160
133
}
161
134
162
135
/**
163
136
* Plan trial time (day).
164
137
*
165
- * @param int $day
138
+ * @param int $day
166
139
* @return $this
167
140
*/
168
141
public function trial ($ day )
169
142
{
170
- $ this ->trial = $ day ;
143
+ $ this ->trial_time = $ day ;
144
+ return $ this ;
145
+ }
171
146
147
+ /**
148
+ * Plan description.
149
+ *
150
+ * @param string $data.
151
+ * @return $this
152
+ */
153
+ public function description ($ data )
154
+ {
155
+ if ($ data !='' ) {
156
+ $ this ->nickname = $ data ;
157
+ }
172
158
return $ this ;
173
159
}
174
160
@@ -177,19 +163,25 @@ public function trial($day)
177
163
*
178
164
* @return object
179
165
*/
180
- public function get ()
166
+ public function createPlan ()
181
167
{
182
- try {
183
- Stripe::setApiKey ($ this ->secretKey );
184
- $ plan = Plan::create ([
185
- 'amount ' => $ this ->amount ,
186
- 'currency ' => $ this ->currency ,
187
- 'interval ' => $ this ->interval ,
188
- 'product ' => $ this ->product ,
189
- 'trial_period_days ' => $ this ->trial ,
190
- $ this ->extra ,
191
- ]);
168
+ $ planData = [
169
+ 'amount ' => $ this ->amount ,
170
+ 'currency ' => $ this ->currency ,
171
+ 'interval ' => $ this ->interval ,
172
+ 'product ' => $ this ->product ,
173
+ ];
174
+
175
+ if ($ this ->trial_time != '' ) {
176
+ $ planData ['trial ' ] = $ this ->trial_time ;
177
+ }
178
+
179
+ if ($ this ->nickname != '' ) {
180
+ $ planData ['nickname ' ] = $ this ->nickname ;
181
+ }
192
182
183
+ try {
184
+ $ plan = $ this ->stripe ->plans ->create ($ planData );
193
185
return $ plan ;
194
186
} catch (\Exception $ e ) {
195
187
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage ()];
@@ -199,15 +191,13 @@ public function get()
199
191
/**
200
192
* Retrieve a plan with $id.
201
193
*
202
- * @param string $id
194
+ * @param string $id
203
195
* @return object
204
196
*/
205
197
public function retrieve ($ id )
206
198
{
207
199
try {
208
- Stripe::setApiKey ($ this ->secretKey );
209
- $ plan = Plan::retrieve ($ id );
210
-
200
+ $ plan = $ this ->stripe ->plans ->retrieve ($ id );
211
201
return $ plan ;
212
202
} catch (\Exception $ e ) {
213
203
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage ()];
@@ -217,18 +207,16 @@ public function retrieve($id)
217
207
/**
218
208
* Active a plan
219
209
*
220
- * @param string $id
210
+ * @param string $id
221
211
* @return object
222
212
*/
223
213
public function active ($ id )
224
214
{
225
215
try {
226
- Stripe::setApiKey ($ this ->secretKey );
227
- $ plan = Plan::update (
216
+ $ plan = $ this ->stripe ->plans ->update (
228
217
$ id ,
229
218
['active ' => true ]
230
219
);
231
-
232
220
return $ plan ;
233
221
} catch (\Exception $ e ) {
234
222
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage ()];
@@ -238,18 +226,16 @@ public function active($id)
238
226
/**
239
227
* Deactive a plan.
240
228
*
241
- * @param string $id
229
+ * @param string $id
242
230
* @return $this
243
231
*/
244
232
public function deactive ($ id )
245
233
{
246
234
try {
247
- Stripe::setApiKey ($ this ->secretKey );
248
- $ plan = Plan::update (
235
+ $ plan = $ this ->stripe ->plans ->update (
249
236
$ id ,
250
237
['active ' => false ]
251
238
);
252
-
253
239
return $ plan ;
254
240
} catch (\Exception $ e ) {
255
241
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage ()];
@@ -259,23 +245,34 @@ public function deactive($id)
259
245
/**
260
246
* Delete a plan and same time delete product.
261
247
*
262
- * @param string $id
248
+ * @param string $id
263
249
* @return object
264
250
*/
265
251
public function delete ($ id )
266
252
{
267
253
try {
268
- Stripe::setApiKey ($ this ->secretKey );
269
- $ plan = Plan::retrieve ($ id );
270
- $ product = $ plan ->product ;
254
+ $ plan = $ this ->stripe ->plans ->retrieve ($ id );
255
+ $ planProduct = $ plan ->product ;
271
256
$ plan ->delete ();
272
-
273
- $ getProduct = Product::retrieve ($ product );
257
+
258
+ $ product = new StripeProducts ();
259
+ $ getProduct = $ product ->retrieve ($ planProduct );
274
260
$ getProduct ->delete ();
275
261
276
262
return $ plan ;
277
263
} catch (\Exception $ e ) {
278
264
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage ()];
279
265
}
280
266
}
267
+
268
+ /**
269
+ * Retrive all plans
270
+ *
271
+ * @return array
272
+ */
273
+ public function lists ()
274
+ {
275
+ $ plans = $ this ->stripe ->plans ->all ();
276
+ return $ plans ;
277
+ }
281
278
}
0 commit comments