Skip to content

Commit 138888e

Browse files
author
Mostafa Kamal
committed
refactoring
1 parent efffbda commit 138888e

File tree

3 files changed

+115
-37
lines changed

3 files changed

+115
-37
lines changed

src/StripeCoupon.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
* @copyright Kawsar Soft. (http://kawsarsoft.com)
88
*/
99

10+
use Illuminate\Support\Str;
1011
use Stripe\Stripe;
1112
use Stripe\Coupon;
1213
use Config;
13-
14+
1415
/**
1516
* Coupon class
1617
* @source https://stripe.com/docs/api/coupons
@@ -84,6 +85,14 @@ public function setup($data)
8485
return $this;
8586
}
8687

88+
/**
89+
* Coupon amount
90+
* @param int|float $amount
91+
* @param string $type [fixed,per]
92+
* @param string $currency fixed amount purpose
93+
* @source https://stripe.com/docs/api/coupons/object#coupon_object-amount_off
94+
* @return $this
95+
*/
8796
public function amount($amount,$type,$currency = 'usd')
8897
{
8998
if ($type === 'fixed') {
@@ -94,10 +103,16 @@ public function amount($amount,$type,$currency = 'usd')
94103
$this->amount = $amount;
95104
$this->type = 'per';
96105
}
97-
98106
return $this;
99107
}
100108

109+
/**
110+
* Coupon duration
111+
* @param string $type [forever,once,repeating]
112+
* @param integer $month
113+
* @source https://stripe.com/docs/api/coupons/create#create_coupon-duration
114+
* @return $this
115+
*/
101116
public function duration($type,$month = 1)
102117
{
103118
//forever, once, or repeating.
@@ -108,12 +123,21 @@ public function duration($type,$month = 1)
108123
return $this;
109124
}
110125

126+
/**
127+
* Coupon name & id
128+
* @param string $name snake_case
129+
* @return $this
130+
*/
111131
public function name($name)
112132
{
113-
$this->name = $name;
133+
$this->name = Str::snake($name);
114134
return $this;
115135
}
116136

137+
/**
138+
* Create coupon & retrieve data
139+
* @return object
140+
*/
117141
public function get()
118142
{
119143
if ($this->type === 'per') {
@@ -127,6 +151,7 @@ public function get()
127151
$this->couponData['name'] = $this->name;
128152
$this->couponData['id'] = $this->name;
129153
}
154+
130155
if ($this->duration === 'forever' || $this->duration === 'once') {
131156
$this->couponData['duration'] = $this->duration;
132157
} else {
@@ -139,10 +164,15 @@ public function get()
139164
$coupon = Coupon::create($this->couponData);
140165
return $coupon;
141166
} catch (\Exception $e) {
142-
return (object)['isError' => 'true','message'=> $e->getMessage()];
167+
return (object)['isError' => 'true','message'=> $e->getMessage(),'stripe' => $e->getJsonBody()['error']];
143168
}
144169
}
145-
170+
171+
/**
172+
* Delete a coupon
173+
* @param string $id coupon id
174+
* @return object
175+
*/
146176
public function delete($id)
147177
{
148178
try {
@@ -151,9 +181,7 @@ public function delete($id)
151181
$coupon->delete();
152182
return $coupon;
153183
} catch (\Exception $e) {
154-
return (object)['isError' => 'true','message'=> $e->getMessage()];
184+
return (object)['isError' => 'true','message'=> $e->getMessage(),'stripe' => $e->getJsonBody()['error']];
155185
}
156186
}
157-
158-
159187
}

src/StripePlans.php

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright Kawsar Soft. (http://kawsarsoft.com)
88
*/
99

10-
use Stripe\Product;
10+
use Stripe\Product;
1111
use Stripe\Stripe;
1212
use Stripe\Plan;
1313
use Config;
@@ -23,10 +23,6 @@ class StripePlans
2323
* @var string
2424
*/
2525
private $secretKey;
26-
/**
27-
* Customer all data after create
28-
* @var object
29-
*/
3026

3127
/**
3228
* Set currency for plan.
@@ -35,8 +31,8 @@ class StripePlans
3531
private $currency;
3632

3733
/**
38-
* [private description]
39-
* @var [type]
34+
* plan billing recurring interval
35+
* @var string
4036
*/
4137
private $interval;
4238

@@ -46,8 +42,16 @@ class StripePlans
4642
*/
4743
private $amount;
4844

45+
/**
46+
* Plan products
47+
* @var array
48+
*/
4949
private $product = [];
5050

51+
/**
52+
* Plan extra properties
53+
* @var array
54+
*/
5155
private $extra = [];
5256

5357
/**
@@ -75,30 +79,56 @@ public function setup($data)
7579
return $this;
7680
}
7781

82+
/**
83+
* Plan products
84+
* @param aray $data
85+
* @source https://stripe.com/docs/api/plans/create#create_plan-product
86+
* @return $this
87+
*/
7888
public function product($data)
7989
{
8090
$this->product = $data;
8191
return $this;
8292
}
8393

94+
/**
95+
* Plan price
96+
* @param int|float $amount
97+
* @return $this
98+
*/
8499
public function amount($amount)
85100
{
86101
$this->amount = round($amount,2) * 100;
87102
return $this;
88103
}
89104

105+
/**
106+
* Plan recurring interval
107+
* @param string $type [day,week,month,year]
108+
* @return $this
109+
*/
90110
public function interval($type)
91111
{
92112
$this->interval = $type;
93113
return $this;
94114
}
95115

116+
/**
117+
* Plan currency
118+
* @param $currency
119+
* @return $this
120+
*/
96121
public function currency($currency)
97122
{
98123
$this->currency = $currency;
99124
return $this;
100125
}
101126

127+
/**
128+
* Plan extra properties
129+
* @param array $data associate array
130+
* @return $this
131+
*/
102132
public function extra ($data)
103133
{
104134
$this->extra = $data;
@@ -130,7 +160,7 @@ public function get()
130160
'interval' => $this->interval,
131161
'product' => $this->product,
132162
'trial_period_days' => $this->trial,
133-
// $this->extra
163+
$this->extra
134164
]);
135165
return $plan;
136166
} catch (\Exception $e) {
@@ -155,20 +185,18 @@ public function retrieve($id)
155185
}
156186

157187
/**
158-
* Delete a plan and same time delete product.
188+
* Active a plan
159189
* @param string $id
160190
* @return object
161191
*/
162-
public function delete($id)
192+
public function active($id)
163193
{
164194
try {
165195
Stripe::setApiKey($this->secretKey);
166-
$plan = Plan::retrieve($id);
167-
$product = $plan->product;
168-
$plan->delete();
169-
170-
$getProduct = Product::retrieve($product);
171-
$getProduct->delete();
196+
$plan = Plan::update(
197+
$id,
198+
['active' => true]
199+
);
172200

173201
return $plan;
174202
} catch (\Exception $e) {
@@ -177,38 +205,40 @@ public function delete($id)
177205
}
178206

179207
/**
180-
* Active a plan
208+
* Deactive a plan.
181209
* @param string $id
182-
* @return object
210+
* @return $this
183211
*/
184-
public function active($id)
212+
public function deactive($id)
185213
{
186214
try {
187215
Stripe::setApiKey($this->secretKey);
188216
$plan = Plan::update(
189217
$id,
190-
['active' => true]
218+
['active' => false]
191219
);
192-
193220
return $plan;
194221
} catch (\Exception $e) {
195222
return (object)['isError' => 'true','message'=> $e->getMessage()];
196223
}
197224
}
198225

199226
/**
200-
* Deactive a plan.
227+
* Delete a plan and same time delete product.
201228
* @param string $id
202-
* @return $this
229+
* @return object
203230
*/
204-
public function deactive($id)
231+
public function delete($id)
205232
{
206233
try {
207234
Stripe::setApiKey($this->secretKey);
208-
$plan = Plan::update(
209-
$id,
210-
['active' => false]
211-
);
235+
$plan = Plan::retrieve($id);
236+
$product = $plan->product;
237+
$plan->delete();
238+
239+
$getProduct = Product::retrieve($product);
240+
$getProduct->delete();
241+
212242
return $plan;
213243
} catch (\Exception $e) {
214244
return (object)['isError' => 'true','message'=> $e->getMessage()];

src/StripeSubscription.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class StripeSubscription
2323
* @var string
2424
*/
2525
private $secretKey;
26+
2627
/**
27-
* Customer all data after create.
28+
* Subscription Customer
2829
* @var object
2930
*/
3031
private $customer;
@@ -60,6 +61,12 @@ class StripeSubscription
6061
*/
6162
private $trial;
6263

64+
/**
65+
* Card source
66+
* @var string
67+
*/
68+
private $source;
69+
6370
/**
6471
* Subcription with coupon.
6572
* @var string
@@ -141,6 +148,16 @@ public function trial($day) {
141148
return $this;
142149
}
143150

151+
/**
152+
* set customer card source
153+
* @param string $code
154+
* @return $this
155+
*/
156+
public function source($code) {
157+
$this->source = $code;
158+
return $this;
159+
}
160+
144161
/**
145162
* Coupon apply
146163
* @param string $code
@@ -168,6 +185,9 @@ public function get()
168185
if ($this->coupon) {
169186
$this->createSubscriptionData['coupon'] = $this->coupon;
170187
}
188+
if ($this->source) {
189+
$this->createSubscriptionData['default_source'] = $this->source;
190+
}
171191
$subsData = array_merge($this->createSubscriptionData,$this->extra);
172192

173193
try {

0 commit comments

Comments
 (0)