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
- */
10
-
11
- use Config ;
12
- use Stripe \Coupon ;
13
- use Stripe \Stripe ;
14
5
use Illuminate \Support \Str ;
6
+ use Stripe \StripeClient ;
15
7
16
- /**
17
- * Coupon class
18
- *
19
- * @source https://stripe.com/docs/api/coupons
20
- */
21
8
class StripeCoupon
22
9
{
23
10
/**
@@ -32,14 +19,14 @@ class StripeCoupon
32
19
*
33
20
* @var string
34
21
*/
35
- private $ currency ;
22
+ private $ currency = ' usd ' ;
36
23
37
24
/**
38
25
* Coupon amount
39
26
*
40
27
* @var int|float
41
28
*/
42
- private $ amount ;
29
+ private $ amount = 0 ;
43
30
44
31
/**
45
32
* Coupon amount type
@@ -69,55 +56,35 @@ class StripeCoupon
69
56
*/
70
57
private $ durationMonth ;
71
58
72
- /**
73
- * All coupon data for create
74
- *
75
- * @var [type]
76
- */
77
- private $ couponData ;
59
+ private $ stripe ;
78
60
79
61
public function __construct ()
80
62
{
81
- if (config::get ('lara-stripe.driver ' ) === 'config ' ) {
82
- $ this ->secretKey = config::get ('lara-stripe.secret_key ' );
83
- }
63
+ $ this ->secretKey = config ('lara-stripe.secret_key ' );
64
+ $ this ->stripe = new StripeClient ($ this ->secretKey );
84
65
}
85
66
86
- /**
87
- * Set secret key
88
- *
89
- * @param string $data
90
- * @return $this
91
- */
92
- public function setup ($ data )
93
- {
94
- if (isset ($ data ['secret_key ' ])) {
95
- $ this ->secretKey = $ data ['secret_key ' ];
96
- }
97
-
98
- return $ this ;
99
- }
100
67
101
68
/**
102
69
* Coupon amount
103
70
*
104
- * @param int|float $amount
105
- * @param string $type [fixed,per]
106
- * @param string $currency fixed amount purpose
71
+ * @param int|float $amount
72
+ * @param string $currency fixed amount purpose
73
+ * @param string $type [ fixed,percent]
107
74
*
108
75
* @source https://stripe.com/docs/api/coupons/object#coupon_object-amount_off
109
76
*
110
77
* @return $this
111
78
*/
112
- public function amount ($ amount , $ type , $ currency = 'usd ' )
79
+ public function amount ($ amount , $ type = ' fixed ' , $ currency = 'usd ' )
113
80
{
114
81
if ($ type === 'fixed ' ) {
115
82
$ this ->amount = round ($ amount , 2 ) * 100 ;
116
83
$ this ->type = 'fixed ' ;
117
84
$ this ->currency = $ currency ;
118
85
} else {
119
86
$ this ->amount = $ amount ;
120
- $ this ->type = 'per ' ;
87
+ $ this ->type = 'percent ' ;
121
88
}
122
89
123
90
return $ this ;
@@ -126,19 +93,16 @@ public function amount($amount, $type, $currency = 'usd')
126
93
/**
127
94
* Coupon duration
128
95
*
129
- * @param string $type [forever,once,repeating]
130
- * @param int $month
131
- *
132
- * @source https://stripe.com/docs/api/coupons/create#create_coupon-duration
133
- *
96
+ * @param string $type [forever,once,repeating]
97
+ * @param int $month
134
98
* @return $this
135
99
*/
136
100
public function duration ($ type , $ month = 1 )
137
101
{
138
- //forever, once, or repeating.
139
102
if ($ type === 'repeating ' ) {
140
103
$ this ->durationMonth = $ month ;
141
104
}
105
+
142
106
$ this ->duration = $ type ;
143
107
144
108
return $ this ;
@@ -147,46 +111,64 @@ public function duration($type, $month = 1)
147
111
/**
148
112
* Coupon name & id
149
113
*
150
- * @param string $name snake_case
114
+ * @param string $name snake_case
151
115
* @return $this
152
116
*/
153
117
public function name ($ name )
154
118
{
155
119
$ this ->name = Str::snake ($ name );
156
-
157
120
return $ this ;
158
121
}
159
122
160
123
/**
161
124
* Create coupon & retrieve data
162
125
*
163
- * @return object
126
+ * @return object
164
127
*/
165
- public function get ()
128
+ public function create ()
166
129
{
167
- if ($ this ->type === 'per ' ) {
168
- $ this ->couponData ['percent_off ' ] = $ this ->amount ;
130
+ $ couponData = [];
131
+
132
+ if ($ this ->type === 'percent ' ) {
133
+ $ couponData ['percent_off ' ] = $ this ->amount ;
169
134
} else {
170
- $ this -> couponData ['amount_off ' ] = $ this ->amount ;
171
- $ this -> couponData ['currency ' ] = $ this ->currency ;
135
+ $ couponData ['amount_off ' ] = $ this ->amount ;
136
+ $ couponData ['currency ' ] = $ this ->currency ;
172
137
}
173
138
174
139
if ($ this ->name ) {
175
- $ this ->couponData ['name ' ] = $ this ->name ;
176
- $ this ->couponData ['id ' ] = $ this ->name ;
140
+ $ couponData ['name ' ] = $ this ->name ;
177
141
}
178
142
179
143
if ($ this ->duration === 'forever ' || $ this ->duration === 'once ' ) {
180
- $ this -> couponData ['duration ' ] = $ this ->duration ;
144
+ $ couponData ['duration ' ] = $ this ->duration ;
181
145
} else {
182
- $ this -> couponData ['duration ' ] = $ this ->duration ;
183
- $ this -> couponData ['duration_in_months ' ] = $ this ->durationMonth ;
146
+ $ couponData ['duration ' ] = $ this ->duration ;
147
+ $ couponData ['duration_in_months ' ] = $ this ->durationMonth ;
184
148
}
185
149
186
150
try {
187
- Stripe::setApiKey ($ this ->secretKey );
188
- $ coupon = Coupon::create ($ this ->couponData );
151
+ $ coupon = $ this ->stripe ->coupons ->create ($ couponData );
152
+ return $ coupon ;
153
+ } catch (\Exception $ e ) {
154
+ return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage (), 'stripe ' => $ e ->getJsonBody ()['error ' ]];
155
+ }
156
+ }
189
157
158
+ public function retrieve ($ id )
159
+ {
160
+ try {
161
+ $ coupon = $ this ->stripe ->coupons ->retrieve ($ id );
162
+ return $ coupon ;
163
+ } catch (\Exception $ e ) {
164
+ return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage (), 'stripe ' => $ e ->getJsonBody ()['error ' ]];
165
+ }
166
+ }
167
+
168
+ public function lists ()
169
+ {
170
+ try {
171
+ $ coupon = $ this ->stripe ->coupons ->all ();
190
172
return $ coupon ;
191
173
} catch (\Exception $ e ) {
192
174
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage (), 'stripe ' => $ e ->getJsonBody ()['error ' ]];
@@ -202,10 +184,7 @@ public function get()
202
184
public function delete ($ id )
203
185
{
204
186
try {
205
- Stripe::setApiKey ($ this ->secretKey );
206
- $ coupon = Coupon::retrieve ($ id );
207
- $ coupon ->delete ();
208
-
187
+ $ coupon = $ this ->stripe ->coupons ->delete ($ id );
209
188
return $ coupon ;
210
189
} catch (\Exception $ e ) {
211
190
return (object ) ['isError ' => 'true ' , 'message ' => $ e ->getMessage (), 'stripe ' => $ e ->getJsonBody ()['error ' ]];
0 commit comments