Skip to content

Commit efffbda

Browse files
author
Mostafa Kamal
committed
refactoring
1 parent cdaa392 commit efffbda

File tree

1 file changed

+82
-170
lines changed

1 file changed

+82
-170
lines changed

src/StripeCheckout.php

Lines changed: 82 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -7,241 +7,153 @@
77
* @copyright Kawsar Soft. (http://kawsarsoft.com)
88
*/
99

10-
use Stripe\Checkout\Session;
11-
use Stripe\Stripe;
12-
use Stripe\Refund;
13-
use Stripe\Customer;
14-
use Config;
10+
use Stripe\Stripe;
11+
use Stripe\Coupon;
12+
use Config;
1513

16-
class StripeCheckout
14+
/**
15+
* Coupon class
16+
* @source https://stripe.com/docs/api/coupons
17+
*/
18+
class StripeCoupon
1719
{
1820
/**
19-
* Checkout Currency
20-
* @var string length 3 and lowercase
21+
* Secret key
22+
* @var string
2123
*/
22-
private $currency = 'usd';
24+
private $secretKey;
2325

2426
/**
25-
* Checkout description
27+
* Currency when coupon amount is fixed
2628
* @var string
2729
*/
28-
private $description = 'Stripe payment checkout by lara-stripe';
30+
private $currency;
2931

3032
/**
31-
* Checkout products data
32-
* @var array
33+
* Coupon amount
34+
* @var integer|float
3335
*/
34-
private $products = [];
36+
private $amount;
3537

3638
/**
37-
* Secret key
38-
* @var string
39+
* Coupon amount type
40+
* @var string per or fixed
3941
*/
40-
private $secretKey;
42+
private $type;
4143

4244
/**
43-
* Public key
44-
* @var string
45+
* Coupon duration
46+
* @var string once
4547
*/
46-
private $publicKey;
48+
private $duration;
4749

4850
/**
49-
* Checkout success url
50-
* @var string
51+
* Coupon name & coupon id
52+
* @var string must be camel_case
5153
*/
52-
private $successURI;
54+
private $name;
5355

5456
/**
55-
* Checkout cancel url
56-
* @var string
57+
* Coupon duration month
58+
* @var integer
5759
*/
58-
private $cancelURI;
60+
private $durationMonth;
5961

6062
/**
61-
* Checkout ref ex: product id , payment id, card id similar.
62-
* @var string
63+
* All coupon data for create
64+
* @var [type]
6365
*/
64-
private $referenceKey;
65-
66-
private $checkoutData = [];
67-
68-
private $isFuture = false;
66+
private $couponData;
6967

7068
public function __construct()
7169
{
7270
if(config::get('lara-stripe.driver') === 'config') {
73-
$this->currency = config::get('lara-stripe.currency');
7471
$this->secretKey = config::get('lara-stripe.secret_key');
75-
$this->publicKey = config::get('lara-stripe.public_key');
7672
}
7773
}
7874
/**
79-
* Set credentials, secret and public key
80-
*
81-
* Set stripe currency
82-
* @param array $data
75+
* Set secret key
76+
* @param string $data
8377
* @return $this
8478
*/
8579
public function setup($data)
8680
{
8781
if (isset($data['secret_key'])) {
8882
$this->secretKey = $data['secret_key'];
8983
}
90-
if (isset($data['public_key'])) {
91-
$this->publicKey = $data['public_key'];
92-
}
93-
if (isset($data['currency'])) {
94-
$this->currency = strtolower($data['currency']);
95-
}
9684
return $this;
9785
}
98-
/**
99-
* Configure success url , cancel url & ref
100-
*
101-
* @param array $data
102-
* @return $this
103-
*/
104-
public function configure($data)
86+
87+
public function amount($amount,$type,$currency = 'usd')
10588
{
106-
if (isset($data['success_url'])) {
107-
$this->successURI = $data['success_url'];
108-
}
109-
if (isset($data['cancel_url'])) {
110-
$this->cancelURI = $data['cancel_url'];
89+
if ($type === 'fixed') {
90+
$this->amount = round($amount,2) * 100;
91+
$this->type = 'fixed';
92+
$this->currency = $currency;
93+
} else {
94+
$this->amount = $amount;
95+
$this->type = 'per';
11196
}
112-
if (isset($data['ref_key'])) {
113-
$this->referenceKey = $data['ref_key'];
114-
}
115-
return $this;
116-
}
11797

118-
/**
119-
* Retrieve public key
120-
*
121-
* @return $this
122-
*/
123-
public function publicKey()
124-
{
125-
return $this->publicKey;
98+
return $this;
12699
}
127100

128-
/**
129-
* Set products
130-
*
131-
* @param array $data
132-
* @return $this
133-
*/
134-
public function products($data)
101+
public function duration($type,$month = 1)
135102
{
136-
if (is_array($data) && sizeof($data) > 0) {
137-
$this->products = $data;
138-
}
139-
return $this;
103+
//forever, once, or repeating.
104+
if($type === 'repeating') {
105+
$this->durationMonth = $month;
106+
}
107+
$this->duration = $type;
108+
return $this;
140109
}
141110

142-
public function future()
111+
public function name($name)
143112
{
144-
$this->isFuture = true;
145-
return $this;
113+
$this->name = $name;
114+
return $this;
146115
}
147-
/**
148-
* Get session id and public key
149-
*
150-
* @return object sid and pkey
151-
*/
152-
public function getSession()
116+
117+
public function get()
153118
{
154-
for($i=0;$i<sizeof($this->products);$i++){
155-
$this->products[$i]['currency'] = $this->currency;
156-
$this->products[$i]['amount'] = round($this->products[$i]['amount'],2) * 100;
157-
if (!isset($this->products[$i]['quantity'])) {
158-
$this->products[$i]['quantity'] = 1;
159-
}
119+
if ($this->type === 'per') {
120+
$this->couponData['percent_off'] = $this->amount;
121+
} else {
122+
$this->couponData['amount_off'] = $this->amount;
123+
$this->couponData['currency'] = $this->currency;
160124
}
161125

162-
try {
163-
Stripe::setApiKey($this->secretKey);
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);
181-
$output = [
182-
'sid' => $session->id,
183-
'pkey' => $this->publicKey
184-
];
185-
return (object) $output;
186-
}
187-
188-
189-
} catch (\Exception $e) {
190-
return (object)['isError' => 'true','message'=> $e->getMessage()];
126+
if ($this->name) {
127+
$this->couponData['name'] = $this->name;
128+
$this->couponData['id'] = $this->name;
191129
}
192-
}
193-
194-
/**
195-
* Retrieve session (checkout).
196-
*
197-
* @param string $sessionToken
198-
* @return object $infos
199-
*/
200-
public function retrieve($sessionToken)
201-
{
202-
try {
203-
Stripe::setApiKey($this->secretKey);
204-
$infos = Session::retrieve($sessionToken);
205-
return $infos;
206-
} catch (\Exception $e) {
207-
return (object)['isError' => 'true','message'=> $e->getMessage()];
130+
if ($this->duration === 'forever' || $this->duration === 'once') {
131+
$this->couponData['duration'] = $this->duration;
132+
} else {
133+
$this->couponData['duration'] = $this->duration;
134+
$this->couponData['duration_in_months'] = $this->durationMonth;
208135
}
136+
137+
try {
138+
Stripe::setApiKey($this->secretKey);
139+
$coupon = Coupon::create($this->couponData);
140+
return $coupon;
141+
} catch (\Exception $e) {
142+
return (object)['isError' => 'true','message'=> $e->getMessage()];
143+
}
209144
}
210145

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)
146+
public function delete($id)
218147
{
219148
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;
149+
Stripe::setApiKey($this->secretKey);
150+
$coupon = Coupon::retrieve($id);
151+
$coupon->delete();
152+
return $coupon;
226153
} catch (\Exception $e) {
227-
return (object)['isError' => 'true','message'=> $e->getMessage()];
154+
return (object)['isError' => 'true','message'=> $e->getMessage()];
228155
}
229156
}
230157

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-
}
245158

246-
}
247159
}

0 commit comments

Comments
 (0)