Skip to content

Commit 5d81940

Browse files
committed
subscriptions , prices, product, plan
1 parent 943c37c commit 5d81940

File tree

5 files changed

+434
-151
lines changed

5 files changed

+434
-151
lines changed

src/Facades/LStripePlan.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
<?php
2-
32
namespace Code4mk\LaraStripe\Facades;
43

5-
/**
6-
* @author @code4mk <[email protected]>
7-
* @author @kawsarsoft <[email protected]>
8-
* @copyright Kawsar Soft. (http://kawsarsoft.com)
9-
*/
10-
114
use Illuminate\Support\Facades\Facade;
125

136
class LStripePlan extends Facade

src/Lib/StripePlans.php

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,9 @@
22

33
namespace Code4mk\LaraStripe\Lib;
44

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;
107

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-
*/
218
class StripePlans
229
{
2310
/**
@@ -67,108 +54,107 @@ class StripePlans
6754
*
6855
* @var int
6956
*/
70-
private $trial;
57+
private $trial_time;
7158

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;
7863

7964
/**
80-
* Set secret key
81-
*
82-
* @param string $data
83-
* @return $this
65+
* A brief description of the plan, hidden from customers.
8466
*/
85-
public function setup($data)
86-
{
87-
if (isset($data['secret_key'])) {
88-
$this->secretKey = $data['secret_key'];
89-
}
67+
private $nickname;
9068

91-
return $this;
69+
public function __construct()
70+
{
71+
$this->secretKey = config('lara-stripe.secret_key');
72+
$this->stripe = new StripeClient($this->secretKey);
9273
}
9374

9475
/**
9576
* Plan products
9677
*
97-
* @param aray $data
98-
*
99-
* @source https://stripe.com/docs/api/plans/create#create_plan-product
100-
*
78+
* @param array $data
10179
* @return $this
10280
*/
10381
public function product($data)
10482
{
10583
$this->product = $data;
106-
10784
return $this;
10885
}
10986

11087
/**
11188
* Plan price
11289
*
113-
* @param int|float $amount
90+
* @param int|float $amount
11491
* @return $this
11592
*/
11693
public function amount($amount)
11794
{
11895
$this->amount = round($amount, 2) * 100;
119-
12096
return $this;
12197
}
12298

12399
/**
124100
* Plan recurring interval
125101
*
126-
* @param string $type [day,week,month,year]
102+
* @param string $type [day,week,month,year]
127103
* @return $this
128104
*/
129105
public function interval($type)
130106
{
131107
$this->interval = $type;
132-
133108
return $this;
134109
}
135110

136111
/**
137112
* Plan currency
138113
*
139-
* @param $currency
114+
* @param string $currency
140115
* @return $this
141116
*/
142117
public function currency($currency)
143118
{
144119
$this->currency = $currency;
145-
146120
return $this;
147121
}
148122

149123
/**
150-
* Plan extra properties
124+
* Plan extra properties.
151125
*
152-
* @param array $data associate array
126+
* @param array $data associate array
153127
* @return $this
154128
*/
155129
public function extra($data)
156130
{
157131
$this->extra = $data;
158-
159132
return $this;
160133
}
161134

162135
/**
163136
* Plan trial time (day).
164137
*
165-
* @param int $day
138+
* @param int $day
166139
* @return $this
167140
*/
168141
public function trial($day)
169142
{
170-
$this->trial = $day;
143+
$this->trial_time = $day;
144+
return $this;
145+
}
171146

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+
}
172158
return $this;
173159
}
174160

@@ -177,19 +163,25 @@ public function trial($day)
177163
*
178164
* @return object
179165
*/
180-
public function get()
166+
public function createPlan()
181167
{
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+
}
192182

183+
try {
184+
$plan = $this->stripe->plans->create($planData);
193185
return $plan;
194186
} catch (\Exception $e) {
195187
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
@@ -199,15 +191,13 @@ public function get()
199191
/**
200192
* Retrieve a plan with $id.
201193
*
202-
* @param string $id
194+
* @param string $id
203195
* @return object
204196
*/
205197
public function retrieve($id)
206198
{
207199
try {
208-
Stripe::setApiKey($this->secretKey);
209-
$plan = Plan::retrieve($id);
210-
200+
$plan = $this->stripe->plans->retrieve($id);
211201
return $plan;
212202
} catch (\Exception $e) {
213203
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
@@ -217,18 +207,16 @@ public function retrieve($id)
217207
/**
218208
* Active a plan
219209
*
220-
* @param string $id
210+
* @param string $id
221211
* @return object
222212
*/
223213
public function active($id)
224214
{
225215
try {
226-
Stripe::setApiKey($this->secretKey);
227-
$plan = Plan::update(
216+
$plan = $this->stripe->plans->update(
228217
$id,
229218
['active' => true]
230219
);
231-
232220
return $plan;
233221
} catch (\Exception $e) {
234222
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
@@ -238,18 +226,16 @@ public function active($id)
238226
/**
239227
* Deactive a plan.
240228
*
241-
* @param string $id
229+
* @param string $id
242230
* @return $this
243231
*/
244232
public function deactive($id)
245233
{
246234
try {
247-
Stripe::setApiKey($this->secretKey);
248-
$plan = Plan::update(
235+
$plan = $this->stripe->plans->update(
249236
$id,
250237
['active' => false]
251238
);
252-
253239
return $plan;
254240
} catch (\Exception $e) {
255241
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
@@ -259,23 +245,34 @@ public function deactive($id)
259245
/**
260246
* Delete a plan and same time delete product.
261247
*
262-
* @param string $id
248+
* @param string $id
263249
* @return object
264250
*/
265251
public function delete($id)
266252
{
267253
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;
271256
$plan->delete();
272-
273-
$getProduct = Product::retrieve($product);
257+
258+
$product = new StripeProducts();
259+
$getProduct = $product->retrieve($planProduct);
274260
$getProduct->delete();
275261

276262
return $plan;
277263
} catch (\Exception $e) {
278264
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
279265
}
280266
}
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+
}
281278
}

0 commit comments

Comments
 (0)