Skip to content

Commit 589b06c

Browse files
author
Mostafa Kamal
committed
coupon, subscription, plan
1 parent 21040a3 commit 589b06c

File tree

3 files changed

+252
-2
lines changed

3 files changed

+252
-2
lines changed

src/StripeCoupon.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
namespace Code4mk\LaraStripe;
3+
4+
use Stripe\Stripe;
5+
use Stripe\Coupon;
6+
use Config;
7+
8+
// https://stripe.com/docs/api/coupons
9+
10+
class StripeCoupon
11+
{
12+
/**
13+
* Secret key
14+
* @var string
15+
*/
16+
private $secretKey;
17+
/**
18+
* Customer all data after create
19+
* @var object
20+
*/
21+
private $currency;
22+
23+
private $amount;
24+
private $type;
25+
private $duration;
26+
private $name;
27+
private $durationMonth;
28+
private $couponData;
29+
30+
public function __construct()
31+
{
32+
if(config::get('lara-stripe.driver') === 'config') {
33+
$this->secretKey = config::get('lara-stripe.secret_key');
34+
}
35+
}
36+
/**
37+
* Set secret key
38+
* @param string $data
39+
* @return $this
40+
*/
41+
public function setup($data)
42+
{
43+
if (isset($data['secret_key'])) {
44+
$this->secretKey = $data['secret_key'];
45+
}
46+
return $this;
47+
}
48+
49+
public function amount($amount,$type,$currency = 'usd')
50+
{
51+
if ($type === 'fixed') {
52+
$this->amount = round($amount,2) * 100;
53+
$this->type = 'fixed';
54+
$this->currency = $currency;
55+
} else {
56+
$this->amount = $amount;
57+
$this->type = 'per';
58+
}
59+
60+
return $this;
61+
}
62+
63+
public function duration($type,$month = 1)
64+
{
65+
//forever, once, or repeating.
66+
if($type === 'repeating') {
67+
$this->durationMonth = $month;
68+
}
69+
$this->duration = $type;
70+
return $this;
71+
}
72+
73+
public function name($name)
74+
{
75+
$this->name = $name;
76+
return $this;
77+
}
78+
79+
public function get()
80+
{
81+
if ($this->type === 'per') {
82+
$this->couponData['percent_off'] = $this->amount;
83+
} else {
84+
$this->couponData['amount_off'] = $this->amount;
85+
$this->couponData['currency'] = $this->currency;
86+
}
87+
88+
if ($this->name) {
89+
$this->couponData['name'] = $this->name;
90+
$this->couponData['id'] = $this->name;
91+
}
92+
if ($this->duration === 'forever' || $this->duration === 'once') {
93+
$this->couponData['duration'] = $this->duration;
94+
} else {
95+
$this->couponData['duration'] = $this->duration;
96+
$this->couponData['duration_in_months'] = $this->durationMonth;
97+
}
98+
99+
try {
100+
Stripe::setApiKey($this->secretKey);
101+
$coupon = Coupon::create($this->couponData);
102+
return $coupon;
103+
} catch (\Exception $e) {
104+
return (object)['isError' => 'true','message'=> $e->getMessage()];
105+
}
106+
}
107+
108+
public function delete($id)
109+
{
110+
try {
111+
Stripe::setApiKey($this->secretKey);
112+
$coupon = Coupon::retrieve($id);
113+
$coupon->delete();
114+
return $coupon;
115+
} catch (\Exception $e) {
116+
return (object)['isError' => 'true','message'=> $e->getMessage()];
117+
}
118+
}
119+
120+
121+
}

src/StripePlans.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Stripe\Customer;
55
use Stripe\Stripe;
66
use Stripe\Plan;
7+
use Stripe\Product;
78
use Config;
89

910
// https://stripe.com/docs/api/plans/create
@@ -27,6 +28,10 @@ class StripePlans
2728

2829
private $product = [];
2930

31+
private $extra = [];
32+
33+
private $trial;
34+
3035
public function __construct()
3136
{
3237
if(config::get('lara-stripe.driver') === 'config') {
@@ -54,7 +59,7 @@ public function product($data)
5459

5560
public function amount($amount)
5661
{
57-
$this->amount = $amount;
62+
$this->amount = round($amount,2) * 100;
5863
return $this;
5964
}
6065

@@ -70,6 +75,18 @@ public function currency($currency)
7075
return $this;
7176
}
7277

78+
public function extra ($data)
79+
{
80+
$this->extra = $data;
81+
return $this;
82+
}
83+
84+
public function trial($day)
85+
{
86+
$this->trial = $day;
87+
return $this;
88+
}
89+
7390
public function get()
7491
{
7592
try {
@@ -78,11 +95,70 @@ public function get()
7895
'amount' => $this->amount,
7996
'currency' => $this->currency,
8097
'interval' => $this->interval,
81-
'product' => $this->product
98+
'product' => $this->product,
99+
'trial_period_days' => $this->trial,
100+
// $this->extra
82101
]);
83102
return $plan;
84103
} catch (\Exception $e) {
85104
return (object)['isError' => 'true','message'=> $e->getMessage()];
86105
}
87106
}
107+
108+
public function retrieve($id)
109+
{
110+
try {
111+
Stripe::setApiKey($this->secretKey);
112+
$plan = Plan::retrieve($id);
113+
return $plan;
114+
} catch (\Exception $e) {
115+
return (object)['isError' => 'true','message'=> $e->getMessage()];
116+
}
117+
}
118+
119+
public function delete($id)
120+
{
121+
try {
122+
Stripe::setApiKey($this->secretKey);
123+
$plan = Plan::retrieve($id);
124+
$product = $plan->product;
125+
$plan->delete();
126+
127+
$getProduct = Product::retrieve($product);
128+
$getProduct->delete();
129+
130+
return $plan;
131+
} catch (\Exception $e) {
132+
return (object)['isError' => 'true','message'=> $e->getMessage()];
133+
}
134+
}
135+
136+
public function active($id)
137+
{
138+
try {
139+
Stripe::setApiKey($this->secretKey);
140+
$plan = Plan::update(
141+
$id,
142+
['active' => true]
143+
);
144+
145+
return $plan;
146+
} catch (\Exception $e) {
147+
return (object)['isError' => 'true','message'=> $e->getMessage()];
148+
}
149+
}
150+
151+
public function deactive($id)
152+
{
153+
try {
154+
Stripe::setApiKey($this->secretKey);
155+
$plan = Plan::update(
156+
$id,
157+
['active' => false]
158+
);
159+
return $plan;
160+
} catch (\Exception $e) {
161+
return (object)['isError' => 'true','message'=> $e->getMessage()];
162+
}
163+
}
88164
}

src/StripeSubscription.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class StripeSubscription
2626
private $plan;
2727

2828
private $extra = [];
29+
// trail day from plan
30+
private $trialPlan = false;
31+
32+
private $trial;
33+
private $coupon;
2934

3035
public function __construct()
3136
{
@@ -66,10 +71,35 @@ public function extra($data = [])
6671
return $this;
6772
}
6873

74+
public function trialPlan()
75+
{
76+
$this->trialPlan = true;
77+
return $this;
78+
}
79+
80+
public function trial($day) {
81+
$this->trial = $day;
82+
return $this;
83+
}
84+
85+
public function coupon($code) {
86+
$this->coupon = $code;
87+
return $this;
88+
}
89+
6990
public function get()
7091
{
7192
$this->createSubscriptionData['customer'] = $this->customer;
7293
$this->createSubscriptionData['items'] = [['plan' => $this->plan]];
94+
if ($this->trialPlan) {
95+
$this->createSubscriptionData['trial_from_plan'] = true;
96+
}
97+
if ($this->trial) {
98+
$this->createSubscriptionData['trial_period_days'] = $this->trial;
99+
}
100+
if ($this->coupon) {
101+
$this->createSubscriptionData['coupon'] = $this->coupon;
102+
}
73103
$subsData = array_merge($this->createSubscriptionData,$this->extra);
74104

75105
try {
@@ -80,4 +110,27 @@ public function get()
80110
return (object)['isError' => 'true','message'=> $e->getMessage()];
81111
}
82112
}
113+
114+
public function retrieve($id)
115+
{
116+
try {
117+
Stripe::setApiKey($this->secretKey);
118+
$subs = Subscription::retrieve($id);
119+
return $subs;
120+
} catch (\Exception $e) {
121+
return (object)['isError' => 'true','message'=> $e->getMessage()];
122+
}
123+
}
124+
125+
public function cancel($id)
126+
{
127+
try {
128+
Stripe::setApiKey($this->secretKey);
129+
$subs = Subscription::retrieve($id);
130+
$subs->cancel();
131+
return $subs;
132+
} catch (\Exception $e) {
133+
return (object)['isError' => 'true','message'=> $e->getMessage()];
134+
}
135+
}
83136
}

0 commit comments

Comments
 (0)