1
1
<?php
2
2
namespace Code4mk \LaraStripe \Lib ;
3
3
4
- /**
5
- * @author @code4mk <[email protected] >
6
- * @author @kawsarsoft <[email protected] >
7
- * @copyright Kawsar Soft. (http://kawsarsoft.com)
8
- */
9
-
10
- use Stripe \Checkout \Session ;
11
- use Stripe \PaymentIntent ;
12
- use Stripe \Customer ;
13
4
use Stripe \Stripe ;
14
- use Stripe \Refund ;
15
- use Config ;
5
+ use Stripe \Customer ;
6
+ use Stripe \ StripeClient ;
16
7
17
8
class StripeCheckout
18
9
{
19
10
/**
20
- * Checkout Currency
11
+ * Checkout Currency.
12
+ *
21
13
* @var string length 3 and lowercase
22
14
*/
23
15
private $ currency = 'usd ' ;
24
16
25
17
/**
26
- * Checkout description
18
+ * Checkout description.
19
+ *
27
20
* @var string
28
21
*/
29
- private $ description = 'Stripe payment checkout by lara-stripe ' ;
22
+ private $ description = 'Stripe payment checkout ' ;
30
23
31
24
/**
32
- * Checkout products data
25
+ * Checkout products data.
26
+ *
33
27
* @var array
34
28
*/
35
29
private $ products = [];
36
30
37
31
/**
38
- * Secret key
32
+ * Secret key.
33
+ *
39
34
* @var string
40
35
*/
41
36
private $ secretKey ;
42
37
43
38
/**
44
- * Public key
39
+ * Public key.
40
+ *
45
41
* @var string
46
42
*/
47
43
private $ publicKey ;
48
44
49
45
/**
50
- * Checkout success url
46
+ * Checkout success url.
47
+ *
51
48
* @var string
52
49
*/
53
50
private $ successURI ;
54
51
55
52
/**
56
- * Checkout cancel url
53
+ * Checkout cancel url.
54
+ *
57
55
* @var string
58
56
*/
59
57
private $ cancelURI ;
60
58
61
59
/**
62
60
* Checkout ref ex: product id , payment id, card id similar.
61
+ *
63
62
* @var string
64
63
*/
65
- private $ referenceKey ;
64
+ private $ referenceKey = '' ;
66
65
67
66
private $ checkoutData = [];
68
67
69
- private $ isFuture = false ;
68
+ private $ amount = 0 ;
69
+
70
+ private $ stripe ;
71
+
72
+ private $ theTitle = 'Payment ' ;
73
+
74
+ private $ theAdditionalInfo = [];
70
75
71
76
public function __construct ()
72
77
{
73
- if (config::get ('lara-stripe.driver ' ) === 'config ' ) {
74
- $ this ->currency = config::get ('lara-stripe.currency ' );
75
- $ this ->secretKey = config::get ('lara-stripe.secret_key ' );
76
- $ this ->publicKey = config::get ('lara-stripe.public_key ' );
77
- }
78
+ $ this ->currency = config ('stripe.currency ' );
79
+ $ this ->secretKey = config ('stripe.secret_key ' );
80
+ $ this ->publicKey = config ('stripe.public_key ' );
81
+ $ this ->successURI = config ('stripe.success_url ' );
82
+ $ this ->cancelURI = config ('stripe.cancel_url ' );
83
+
84
+ $ this ->stripe = new StripeClient ($ this ->secretKey );
78
85
}
86
+
87
+
79
88
/**
80
- * Set credentials, secret and public key
81
- *
82
- * Set stripe currency
83
- * @param array $data
89
+ * Transaction id.
90
+ *
91
+ * @param $data.
84
92
* @return $this
85
93
*/
86
- public function setup ($ data )
87
- {
88
- if (isset ($ data ['secret_key ' ])) {
89
- $ this ->secretKey = $ data ['secret_key ' ];
90
- }
91
- if (isset ($ data ['public_key ' ])) {
92
- $ this ->publicKey = $ data ['public_key ' ];
93
- }
94
- if (isset ($ data ['currency ' ])) {
95
- $ this ->currency = strtolower ($ data ['currency ' ]);
96
- }
94
+ public function tnx ($ data ) {
95
+ $ this ->referenceKey = $ data ;
96
+ $ this ->theDescription = $ data ;
97
97
return $ this ;
98
98
}
99
+
99
100
/**
100
- * Configure success url , cancel url & ref
101
- *
102
- * @param array $data
101
+ * Payment title.
102
+ *
103
+ * @param $data.
103
104
* @return $this
104
105
*/
105
- public function configure ($ data )
106
- {
107
- if (isset ($ data ['success_url ' ])) {
108
- $ this ->successURI = $ data ['success_url ' ];
109
- }
110
- if (isset ($ data ['cancel_url ' ])) {
111
- $ this ->cancelURI = $ data ['cancel_url ' ];
112
- }
113
- if (isset ($ data ['ref_key ' ])) {
114
- $ this ->referenceKey = $ data ['ref_key ' ];
115
- }
106
+ public function title ($ data ) {
107
+ $ this ->theTitle = $ data ;
116
108
return $ this ;
117
109
}
118
110
111
+
119
112
/**
120
- * Retrieve public key
121
- *
113
+ * Additional data. associate array.
114
+ *
115
+ * @param array $data.
122
116
* @return $this
123
117
*/
124
- public function publicKey ()
125
- {
126
- return $ this -> publicKey ;
118
+ public function additionalData ( $ data ) {
119
+ $ this -> theAdditionalInfo = $ data ;
120
+ return $ this ;
127
121
}
128
122
129
123
/**
130
- * Set products
131
- *
132
- * @param array $data
124
+ * Payment amount.
125
+ *
126
+ * @param $data.
133
127
* @return $this
134
128
*/
135
- public function products ($ data )
136
- {
137
- if (is_array ($ data ) && sizeof ($ data ) > 0 ) {
138
- $ this ->products = $ data ;
139
- }
129
+ public function amount ($ data ) {
130
+ $ this ->amount = $ data ;
140
131
return $ this ;
141
132
}
142
133
143
- public function future ()
144
- {
145
- $ this ->isFuture = true ;
146
- return $ this ;
147
- }
134
+
148
135
/**
149
136
* Get session id and public key
150
137
*
151
138
* @return object sid and pkey
152
139
*/
153
- public function getSession ()
140
+ public function get ()
154
141
{
155
- for ($ i =0 ;$ i <sizeof ($ this ->products );$ i ++){
156
- $ this ->products [$ i ]['currency ' ] = $ this ->currency ;
157
- $ this ->products [$ i ]['amount ' ] = round ($ this ->products [$ i ]['amount ' ],2 ) * 100 ;
158
- if (!isset ($ this ->products [$ i ]['quantity ' ])) {
159
- $ this ->products [$ i ]['quantity ' ] = 1 ;
160
- }
161
- }
162
-
163
142
try {
164
- Stripe::setApiKey ($ this ->secretKey );
165
143
$ this ->checkoutData ['payment_method_types ' ] = ['card ' ];
166
- $ this ->checkoutData ['success_url ' ] = $ this ->successURI ;
167
- $ this ->checkoutData ['cancel_url ' ] = $ this ->cancelURI ;
144
+ $ this ->checkoutData ['success_url ' ] = $ this ->successURI . " ?session_id={CHECKOUT_SESSION_ID} " ;
145
+ $ this ->checkoutData ['cancel_url ' ] = $ this ->cancelURI . " ?session_id={CHECKOUT_SESSION_ID} " ;
168
146
$ this ->checkoutData ['client_reference_id ' ] = $ this ->referenceKey ;
169
- if (is_array ($ this ->products ) && (sizeof ($ this ->products ) > 0 ) && (!$ this ->isFuture )) {
170
- $ this ->checkoutData ['line_items ' ] = $ this ->products ;
171
- $ session = Session::create ($ this ->checkoutData );
172
- $ output = [
173
- 'sid ' => $ session ->id ,
174
- 'pkey ' => $ this ->publicKey
175
- ];
176
- return (object ) $ output ;
177
- }
178
- // https://stripe.com/docs/payments/checkout/collecting
179
- if ($ this ->isFuture ) {
180
- $ this ->checkoutData ['mode ' ] = 'setup ' ;
181
- $ session = Session::create ($ this ->checkoutData );
182
- $ output = [
183
- 'sid ' => $ session ->id ,
184
- 'pkey ' => $ this ->publicKey
185
- ];
186
- return (object ) $ output ;
187
- }
188
-
147
+ $ this ->checkoutData ['mode ' ] = 'payment ' ;
148
+
149
+ // Line items.
150
+ $ this ->checkoutData ['line_items ' ] = [[
151
+ 'price_data ' => [
152
+ 'currency ' => $ this ->currency ,
153
+ 'unit_amount ' => $ this ->amount * 100 , // Amount in cents (400 USD * 100)
154
+ 'product_data ' => [
155
+ 'name ' => $ this ->theTitle ],
156
+ ],
157
+ 'quantity ' => 1 ,
158
+ ]];
159
+
160
+ $ this ->checkoutData ['metadata ' ] = $ this ->theAdditionalInfo ;
161
+
162
+ // Create session.
163
+ $ session = $ this ->stripe ->checkout ->sessions ->create ($ this ->checkoutData );
164
+
165
+ $ output = [
166
+ 'session_id ' => $ session ->id ,
167
+ 'public_key ' => $ this ->publicKey ,
168
+ 'checkout_url ' => $ session ->url
169
+ ];
170
+
171
+ return (object ) $ output ;
189
172
190
173
} catch (\Exception $ e ) {
191
174
return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
@@ -201,67 +184,52 @@ public function getSession()
201
184
public function retrieve ($ sessionToken )
202
185
{
203
186
try {
204
- Stripe::setApiKey ($ this ->secretKey );
205
- $ infos = Session::retrieve ($ sessionToken );
206
- return $ infos ;
187
+ $ session = $ this ->stripe ->checkout ->sessions ->retrieve ($ sessionToken );
188
+ return $ session ;
207
189
} catch (\Exception $ e ) {
208
190
return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
209
191
}
210
192
}
211
193
212
194
/**
213
- * Checkout refund
195
+ * Checkout refund.
196
+ *
214
197
* Store payment_intent when checkout success in DB.
198
+ *
215
199
* @param string $payment_intent get from database
216
200
* @return object
217
201
*/
218
202
public function refund ($ payment_intent )
219
203
{
220
204
try {
221
- Stripe:: setApiKey ( $ this ->secretKey );
222
- $ intent = \ Stripe \PaymentIntent:: retrieve ( $ payment_intent );
223
- $ re = \ Stripe \Refund:: create ([
224
- 'charge ' => $ intent ->charges ->data [0 ]->id
205
+ $ paymentIntents = $ this ->stripe -> paymentIntents -> retrieve ( $ payment_intent );
206
+
207
+ $ refund = $ this -> stripe -> refunds -> create ([
208
+ 'charge ' => $ paymentIntents ->charges ->data [0 ]->id
225
209
]);
226
- return $ re ;
227
- } catch (\Exception $ e ) {
228
- return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
229
- }
230
- }
231
210
232
- public function storeFuture ($ session )
233
- {
234
- try {
235
- $ dummyCard = 'tok_amex ' ;
236
- Stripe::setApiKey ($ this ->secretKey );
237
- $ sessionData = $ this ->retrieve ($ session );
238
- $ r = \Stripe \SetupIntent::retrieve ($ sessionData ->setup_intent );
239
- $ customer = Customer::create (['source ' =>$ dummyCard ]);
240
- $ payment_method = \Stripe \PaymentMethod::retrieve ($ r ->payment_method );
241
- $ payment_method ->attach (['customer ' => $ customer ->id ]);
242
- return (object ) ['customer ' => $ payment_method ->customer ,'ref ' =>$ sessionData ->client_reference_id ];
211
+ return $ refund ;
243
212
} catch (\Exception $ e ) {
244
213
return (object )['isError ' => 'true ' ,'message ' => $ e ->getMessage ()];
245
214
}
246
-
247
215
}
248
216
217
+
218
+ /**
219
+ * Session status.
220
+ *
221
+ * @param $sessionToken - The session token.
222
+ */
249
223
public function status ($ sessionToken )
250
224
{
251
225
try {
252
- Stripe:: setApiKey ( $ this ->secretKey );
253
- $ session = Session:: retrieve ( $ sessionToken );
254
- $ pi = PaymentIntent:: retrieve (
226
+ $ session = $ this ->stripe -> checkout -> sessions -> retrieve ( $ sessionToken );
227
+
228
+ $ paymentIntents = $ this -> stripe -> paymentIntents -> retrieve (
255
229
$ session ->payment_intent
256
230
);
257
- // if ($pi->status === 'succeeded') {
258
- // return (object) [
259
- // 'status' => $pi->status,
260
- // 'sessions' => $session,
261
- // ];
262
- // }
263
231
return (object ) [
264
- 'status ' => $ pi ->status ,
232
+ 'status ' => $ paymentIntents ->status ,
265
233
'ref_id ' => $ session ->client_reference_id ,
266
234
'sessions ' => $ session ,
267
235
];
0 commit comments