Skip to content

Commit d052cd8

Browse files
author
Mostafa Kamal
authored
refactoring
1 parent 8d0d75d commit d052cd8

File tree

1 file changed

+107
-13
lines changed

1 file changed

+107
-13
lines changed

src/StripeCheckout.php

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,66 @@
77
* @copyright Kawsar Soft. (http://kawsarsoft.com)
88
*/
99

10-
use Stripe\Stripe;
11-
use Stripe\Token;
12-
use Stripe\Charge;
1310
use Stripe\Checkout\Session;
11+
use Stripe\Stripe;
12+
use Stripe\Refund;
13+
use Stripe\Customer;
1414
use Config;
1515

1616
class StripeCheckout
1717
{
18+
/**
19+
* Checkout Currency
20+
* @var string length 3 and lowercase
21+
*/
1822
private $currency = 'usd';
23+
24+
/**
25+
* Checkout description
26+
* @var string
27+
*/
1928
private $description = 'Stripe payment checkout by lara-stripe';
29+
30+
/**
31+
* Checkout products data
32+
* @var array
33+
*/
2034
private $products = [];
35+
36+
/**
37+
* Secret key
38+
* @var string
39+
*/
2140
private $secretKey;
41+
42+
/**
43+
* Public key
44+
* @var string
45+
*/
2246
private $publicKey;
47+
48+
/**
49+
* Checkout success url
50+
* @var string
51+
*/
2352
private $successURI;
53+
54+
/**
55+
* Checkout cancel url
56+
* @var string
57+
*/
2458
private $cancelURI;
59+
60+
/**
61+
* Checkout ref ex: product id , payment id, card id similar.
62+
* @var string
63+
*/
2564
private $referenceKey;
2665

66+
private $checkoutData = [];
67+
68+
private $isFuture = false;
69+
2770
public function __construct()
2871
{
2972
if(config::get('lara-stripe.driver') === 'config') {
@@ -95,6 +138,12 @@ public function products($data)
95138
}
96139
return $this;
97140
}
141+
142+
public function future()
143+
{
144+
$this->isFuture = true;
145+
return $this;
146+
}
98147
/**
99148
* Get session id and public key
100149
*
@@ -112,28 +161,38 @@ public function getSession()
112161

113162
try {
114163
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);
124181
$output = [
125182
'sid' => $session->id,
126183
'pkey' => $this->publicKey
127184
];
128185
return (object) $output;
129186
}
187+
188+
130189
} catch (\Exception $e) {
131190
return (object)['isError' => 'true','message'=> $e->getMessage()];
132191
}
133192
}
134193

135194
/**
136-
* Retrieve session.
195+
* Retrieve session (checkout).
137196
*
138197
* @param string $sessionToken
139198
* @return object $infos
@@ -147,7 +206,42 @@ public function retrieve($sessionToken)
147206
} catch (\Exception $e) {
148207
return (object)['isError' => 'true','message'=> $e->getMessage()];
149208
}
209+
}
150210

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+
}
151245

152246
}
153247
}

0 commit comments

Comments
 (0)