Skip to content

Commit 47e2f2b

Browse files
committed
subscription
1 parent 5d81940 commit 47e2f2b

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/Lib/StripeSubscription.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class StripeSubscription
3838
*
3939
* @var array
4040
*/
41-
private $extra = [];
41+
private $metadata = [];
4242

4343
/**
4444
* Trial default from plan.
@@ -67,7 +67,7 @@ class StripeSubscription
6767
*
6868
* @var string
6969
*/
70-
private $coupon;
70+
private $couponCode;
7171

7272
private $stripe;
7373

@@ -107,12 +107,11 @@ public function priceId($id)
107107
* @param array $data
108108
* @return $this
109109
*/
110-
public function extra($data = [])
110+
public function metaData($data = [])
111111
{
112112
if (is_array($data)) {
113-
$this->extra = $data;
113+
$this->metadata = $data;
114114
}
115-
116115
return $this;
117116
}
118117

@@ -138,7 +137,6 @@ public function trialPlan()
138137
public function trial($day)
139138
{
140139
$this->trial = $day;
141-
142140
return $this;
143141
}
144142

@@ -151,20 +149,18 @@ public function trial($day)
151149
public function source($code)
152150
{
153151
$this->source = $code;
154-
155152
return $this;
156153
}
157154

158155
/**
159156
* Coupon apply
160157
*
161-
* @param string $code
158+
* @param string $code
162159
* @return $this
163160
*/
164161
public function coupon($code)
165162
{
166-
$this->coupon = $code;
167-
163+
$this->couponCode = $code;
168164
return $this;
169165
}
170166

@@ -182,6 +178,26 @@ public function create()
182178
],
183179
];
184180

181+
// meta data associate array.
182+
if (count($this->metadata) > 0) {
183+
$subscriptionsData['metadata'] = $this->metadata;
184+
}
185+
186+
// copuon
187+
if ($this->couponCode != '') {
188+
$subscriptionsData['coupon'] = $this->couponCode;
189+
}
190+
191+
// default source
192+
if ($this->source != '') {
193+
$subscriptionsData['default_source'] = $this->source;
194+
}
195+
196+
// trial in days
197+
if ($this->trial) {
198+
$subscriptionsData['trial_period_days'] = $this->trial;
199+
}
200+
185201
try {
186202
$subscriptions = $this->stripe->subscriptions->create($subscriptionsData);
187203
return $subscriptions;
@@ -193,15 +209,15 @@ public function create()
193209
/**
194210
* Retrieve a subscription with id
195211
*
196-
* @param string $id
212+
* @param string $id
197213
* @return object
198214
*/
199215
public function retrieve($id)
200216
{
201217
try {
202218

203-
$subs = $this->stripe->subscriptions->retrieve($id);
204-
return $subs;
219+
$subscription = $this->stripe->subscriptions->retrieve($id);
220+
return $subscription;
205221
} catch (\Exception $e) {
206222
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
207223
}
@@ -216,11 +232,8 @@ public function retrieve($id)
216232
public function cancel($id)
217233
{
218234
try {
219-
// Stripe::setApiKey($this->secretKey);
220-
// $subs = Subscription::retrieve($id);
221-
// $subs->cancel();
222-
223-
// return $subs;
235+
$subscription = $this->stripe->subscriptions->cancel($id);
236+
return $subscription;
224237
} catch (\Exception $e) {
225238
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
226239
}

0 commit comments

Comments
 (0)