7
7
* @copyright Kawsar Soft. (http://kawsarsoft.com)
8
8
*/
9
9
10
- use Stripe \Stripe ;
11
- use Stripe \Token ;
12
- use Stripe \Charge ;
13
10
use Stripe \Checkout \Session ;
11
+ use Stripe \Stripe ;
12
+ use Stripe \Refund ;
13
+ use Stripe \Customer ;
14
14
use Config ;
15
15
16
16
class StripeCheckout
17
17
{
18
+ /**
19
+ * Checkout Currency
20
+ * @var string length 3 and lowercase
21
+ */
18
22
private $ currency = 'usd ' ;
23
+
24
+ /**
25
+ * Checkout description
26
+ * @var string
27
+ */
19
28
private $ description = 'Stripe payment checkout by lara-stripe ' ;
29
+
30
+ /**
31
+ * Checkout products data
32
+ * @var array
33
+ */
20
34
private $ products = [];
35
+
36
+ /**
37
+ * Secret key
38
+ * @var string
39
+ */
21
40
private $ secretKey ;
41
+
42
+ /**
43
+ * Public key
44
+ * @var string
45
+ */
22
46
private $ publicKey ;
47
+
48
+ /**
49
+ * Checkout success url
50
+ * @var string
51
+ */
23
52
private $ successURI ;
53
+
54
+ /**
55
+ * Checkout cancel url
56
+ * @var string
57
+ */
24
58
private $ cancelURI ;
59
+
60
+ /**
61
+ * Checkout ref ex: product id , payment id, card id similar.
62
+ * @var string
63
+ */
25
64
private $ referenceKey ;
26
65
66
+ private $ checkoutData = [];
67
+
68
+ private $ isFuture = false ;
69
+
27
70
public function __construct ()
28
71
{
29
72
if (config::get ('lara-stripe.driver ' ) === 'config ' ) {
@@ -95,6 +138,12 @@ public function products($data)
95
138
}
96
139
return $ this ;
97
140
}
141
+
142
+ public function future ()
143
+ {
144
+ $ this ->isFuture = true ;
145
+ return $ this ;
146
+ }
98
147
/**
99
148
* Get session id and public key
100
149
*
@@ -112,28 +161,38 @@ public function getSession()
112
161
113
162
try {
114
163
Stripe::setApiKey ($ this ->secretKey );
115
- if (is_array ($ this ->products ) && sizeof ($ this ->products ) > 0 ) {
116
- $ session = Session::create ([
117
- 'payment_method_types ' => ['card ' ],
118
- 'line_items ' => $ this ->products ,
119
- 'success_url ' => $ this ->successURI ,
120
- 'cancel_url ' => $ this ->cancelURI ,
121
- 'client_reference_id ' => $ this ->referenceKey ,
122
-
123
- ]);
164
+ $ this ->checkoutData ['payment_method_types ' ] = ['card ' ];
165
+ $ this ->checkoutData ['success_url ' ] = $ this ->successURI ;
166
+ $ this ->checkoutData ['cancel_url ' ] = $ this ->cancelURI ;
167
+ $ this ->checkoutData ['client_reference_id ' ] = $ this ->referenceKey ;
168
+ if (is_array ($ this ->products ) && (sizeof ($ this ->products ) > 0 ) && (!$ this ->isFuture )) {
169
+ $ this ->checkoutData ['line_items ' ] = $ this ->products ;
170
+ $ session = Session::create ($ this ->checkoutData );
171
+ $ output = [
172
+ 'sid ' => $ session ->id ,
173
+ 'pkey ' => $ this ->publicKey
174
+ ];
175
+ return (object ) $ output ;
176
+ }
177
+ // https://stripe.com/docs/payments/checkout/collecting
178
+ if ($ this ->isFuture ) {
179
+ $ this ->checkoutData ['mode ' ] = 'setup ' ;
180
+ $ session = Session::create ($ this ->checkoutData );
124
181
$ output = [
125
182
'sid ' => $ session ->id ,
126
183
'pkey ' => $ this ->publicKey
127
184
];
128
185
return (object ) $ output ;
129
186
}
187
+
188
+
130
189
} catch (\Exception $ e ) {
131
190
return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
132
191
}
133
192
}
134
193
135
194
/**
136
- * Retrieve session.
195
+ * Retrieve session (checkout) .
137
196
*
138
197
* @param string $sessionToken
139
198
* @return object $infos
@@ -147,7 +206,42 @@ public function retrieve($sessionToken)
147
206
} catch (\Exception $ e ) {
148
207
return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
149
208
}
209
+ }
150
210
211
+ /**
212
+ * Checkout refund
213
+ * Store payment_intent when checkout success in DB.
214
+ * @param string $payment_intent get from database
215
+ * @return object
216
+ */
217
+ public function refund ($ payment_intent )
218
+ {
219
+ try {
220
+ Stripe::setApiKey ($ this ->secretKey );
221
+ $ intent = \Stripe \PaymentIntent::retrieve ($ payment_intent );
222
+ $ re = \Stripe \Refund::create ([
223
+ 'charge ' => $ intent ->charges ->data [0 ]->id
224
+ ]);
225
+ return $ re ;
226
+ } catch (\Exception $ e ) {
227
+ return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
228
+ }
229
+ }
230
+
231
+ public function storeFuture ($ session )
232
+ {
233
+ try {
234
+ $ dummyCard = 'tok_amex ' ;
235
+ Stripe::setApiKey ($ this ->secretKey );
236
+ $ sessionData = $ this ->retrieve ($ session );
237
+ $ r = \Stripe \SetupIntent::retrieve ($ sessionData ->setup_intent );
238
+ $ customer = Customer::create (['source ' =>$ dummyCard ]);
239
+ $ payment_method = \Stripe \PaymentMethod::retrieve ($ r ->payment_method );
240
+ $ payment_method ->attach (['customer ' => $ customer ->id ]);
241
+ return (object ) ['customer ' => $ payment_method ->customer ,'ref ' =>$ sessionData ->client_reference_id ];
242
+ } catch (\Exception $ e ) {
243
+ return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
244
+ }
151
245
152
246
}
153
247
}
0 commit comments