Skip to content

Commit 0322c9c

Browse files
committed
checkout refactor
1 parent c3cefe4 commit 0322c9c

File tree

2 files changed

+117
-144
lines changed

2 files changed

+117
-144
lines changed

config/stripe.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
22

3+
/**
4+
* Stripe configuration.
5+
*/
36
return [
47
'driver' => 'config',
5-
'currency' => 'usd',
6-
'secret_key' => 'sk_test_mBGoFuccDy2KCD4pobbaixKK00qUW0ghu1',
7-
'public_key' => 'pk_test_VNi7F1zcwwffZIi1tAkX1dVs00JfKPsCGR'
8-
];
8+
'currency' => env('STRIPE_CURRENCY', 'usd'),
9+
'secret_key' => env('STRIPE_SECRET_KEY'),
10+
'public_key' => env('STRIPE_PUBLIC_KEY'),
11+
'success_url' => env('STRIPE_SUCCESS_URL'),
12+
'cancel_url' => env('STRIPE_CANCEL_URL')
13+
];

src/Lib/StripeCheckout.php

Lines changed: 108 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,191 +1,174 @@
11
<?php
22
namespace Code4mk\LaraStripe\Lib;
33

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;
134
use Stripe\Stripe;
14-
use Stripe\Refund;
15-
use Config;
5+
use Stripe\Customer;
6+
use Stripe\StripeClient;
167

178
class StripeCheckout
189
{
1910
/**
20-
* Checkout Currency
11+
* Checkout Currency.
12+
*
2113
* @var string length 3 and lowercase
2214
*/
2315
private $currency = 'usd';
2416

2517
/**
26-
* Checkout description
18+
* Checkout description.
19+
*
2720
* @var string
2821
*/
29-
private $description = 'Stripe payment checkout by lara-stripe';
22+
private $description = 'Stripe payment checkout';
3023

3124
/**
32-
* Checkout products data
25+
* Checkout products data.
26+
*
3327
* @var array
3428
*/
3529
private $products = [];
3630

3731
/**
38-
* Secret key
32+
* Secret key.
33+
*
3934
* @var string
4035
*/
4136
private $secretKey;
4237

4338
/**
44-
* Public key
39+
* Public key.
40+
*
4541
* @var string
4642
*/
4743
private $publicKey;
4844

4945
/**
50-
* Checkout success url
46+
* Checkout success url.
47+
*
5148
* @var string
5249
*/
5350
private $successURI;
5451

5552
/**
56-
* Checkout cancel url
53+
* Checkout cancel url.
54+
*
5755
* @var string
5856
*/
5957
private $cancelURI;
6058

6159
/**
6260
* Checkout ref ex: product id , payment id, card id similar.
61+
*
6362
* @var string
6463
*/
65-
private $referenceKey;
64+
private $referenceKey = '';
6665

6766
private $checkoutData = [];
6867

69-
private $isFuture = false;
68+
private $amount = 0;
69+
70+
private $stripe;
71+
72+
private $theTitle = 'Payment';
73+
74+
private $theAdditionalInfo = [];
7075

7176
public function __construct()
7277
{
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);
7885
}
86+
87+
7988
/**
80-
* Set credentials, secret and public key
81-
*
82-
* Set stripe currency
83-
* @param array $data
89+
* Transaction id.
90+
*
91+
* @param $data.
8492
* @return $this
8593
*/
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;
9797
return $this;
9898
}
99+
99100
/**
100-
* Configure success url , cancel url & ref
101-
*
102-
* @param array $data
101+
* Payment title.
102+
*
103+
* @param $data.
103104
* @return $this
104105
*/
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;
116108
return $this;
117109
}
118110

111+
119112
/**
120-
* Retrieve public key
121-
*
113+
* Additional data. associate array.
114+
*
115+
* @param array $data.
122116
* @return $this
123117
*/
124-
public function publicKey()
125-
{
126-
return $this->publicKey;
118+
public function additionalData($data) {
119+
$this->theAdditionalInfo = $data;
120+
return $this;
127121
}
128122

129123
/**
130-
* Set products
131-
*
132-
* @param array $data
124+
* Payment amount.
125+
*
126+
* @param $data.
133127
* @return $this
134128
*/
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;
140131
return $this;
141132
}
142133

143-
public function future()
144-
{
145-
$this->isFuture = true;
146-
return $this;
147-
}
134+
148135
/**
149136
* Get session id and public key
150137
*
151138
* @return object sid and pkey
152139
*/
153-
public function getSession()
140+
public function get()
154141
{
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-
163142
try {
164-
Stripe::setApiKey($this->secretKey);
165143
$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}";
168146
$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;
189172

190173
} catch (\Exception $e) {
191174
return (object)['isError' => 'true','message'=> $e->getMessage()];
@@ -201,67 +184,52 @@ public function getSession()
201184
public function retrieve($sessionToken)
202185
{
203186
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;
207189
} catch (\Exception $e) {
208190
return (object)['isError' => 'true','message'=> $e->getMessage()];
209191
}
210192
}
211193

212194
/**
213-
* Checkout refund
195+
* Checkout refund.
196+
*
214197
* Store payment_intent when checkout success in DB.
198+
*
215199
* @param string $payment_intent get from database
216200
* @return object
217201
*/
218202
public function refund($payment_intent)
219203
{
220204
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
225209
]);
226-
return $re;
227-
} catch (\Exception $e) {
228-
return (object)['isError' => 'true','message'=> $e->getMessage()];
229-
}
230-
}
231210

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;
243212
} catch (\Exception $e) {
244213
return (object)['isError' => 'true','message'=> $e->getMessage()];
245214
}
246-
247215
}
248216

217+
218+
/**
219+
* Session status.
220+
*
221+
* @param $sessionToken - The session token.
222+
*/
249223
public function status($sessionToken)
250224
{
251225
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(
255229
$session->payment_intent
256230
);
257-
// if ($pi->status === 'succeeded') {
258-
// return (object) [
259-
// 'status' => $pi->status,
260-
// 'sessions' => $session,
261-
// ];
262-
// }
263231
return (object) [
264-
'status' => $pi->status,
232+
'status' => $paymentIntents->status,
265233
'ref_id' => $session->client_reference_id,
266234
'sessions' => $session,
267235
];

0 commit comments

Comments
 (0)