Skip to content

Commit 8d0d75d

Browse files
author
Mostafa Kamal
committed
refactoring
1 parent 138888e commit 8d0d75d

File tree

2 files changed

+103
-109
lines changed

2 files changed

+103
-109
lines changed

src/StripeCheckout.php

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

10-
use Stripe\Stripe;
11-
use Stripe\Coupon;
12-
use Config;
10+
use Stripe\Stripe;
11+
use Stripe\Token;
12+
use Stripe\Charge;
13+
use Stripe\Checkout\Session;
14+
use Config;
1315

14-
/**
15-
* Coupon class
16-
* @source https://stripe.com/docs/api/coupons
17-
*/
18-
class StripeCoupon
16+
class StripeCheckout
1917
{
20-
/**
21-
* Secret key
22-
* @var string
23-
*/
18+
private $currency = 'usd';
19+
private $description = 'Stripe payment checkout by lara-stripe';
20+
private $products = [];
2421
private $secretKey;
25-
26-
/**
27-
* Currency when coupon amount is fixed
28-
* @var string
29-
*/
30-
private $currency;
31-
32-
/**
33-
* Coupon amount
34-
* @var integer|float
35-
*/
36-
private $amount;
37-
38-
/**
39-
* Coupon amount type
40-
* @var string per or fixed
41-
*/
42-
private $type;
43-
44-
/**
45-
* Coupon duration
46-
* @var string once
47-
*/
48-
private $duration;
49-
50-
/**
51-
* Coupon name & coupon id
52-
* @var string must be camel_case
53-
*/
54-
private $name;
55-
56-
/**
57-
* Coupon duration month
58-
* @var integer
59-
*/
60-
private $durationMonth;
61-
62-
/**
63-
* All coupon data for create
64-
* @var [type]
65-
*/
66-
private $couponData;
22+
private $publicKey;
23+
private $successURI;
24+
private $cancelURI;
25+
private $referenceKey;
6726

6827
public function __construct()
6928
{
7029
if(config::get('lara-stripe.driver') === 'config') {
30+
$this->currency = config::get('lara-stripe.currency');
7131
$this->secretKey = config::get('lara-stripe.secret_key');
32+
$this->publicKey = config::get('lara-stripe.public_key');
7233
}
7334
}
7435
/**
75-
* Set secret key
76-
* @param string $data
36+
* Set credentials, secret and public key
37+
*
38+
* Set stripe currency
39+
* @param array $data
7740
* @return $this
7841
*/
7942
public function setup($data)
8043
{
8144
if (isset($data['secret_key'])) {
8245
$this->secretKey = $data['secret_key'];
8346
}
47+
if (isset($data['public_key'])) {
48+
$this->publicKey = $data['public_key'];
49+
}
50+
if (isset($data['currency'])) {
51+
$this->currency = strtolower($data['currency']);
52+
}
8453
return $this;
8554
}
86-
87-
public function amount($amount,$type,$currency = 'usd')
55+
/**
56+
* Configure success url , cancel url & ref
57+
*
58+
* @param array $data
59+
* @return $this
60+
*/
61+
public function configure($data)
8862
{
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';
63+
if (isset($data['success_url'])) {
64+
$this->successURI = $data['success_url'];
9665
}
97-
98-
return $this;
66+
if (isset($data['cancel_url'])) {
67+
$this->cancelURI = $data['cancel_url'];
68+
}
69+
if (isset($data['ref_key'])) {
70+
$this->referenceKey = $data['ref_key'];
71+
}
72+
return $this;
9973
}
10074

101-
public function duration($type,$month = 1)
75+
/**
76+
* Retrieve public key
77+
*
78+
* @return $this
79+
*/
80+
public function publicKey()
10281
{
103-
//forever, once, or repeating.
104-
if($type === 'repeating') {
105-
$this->durationMonth = $month;
106-
}
107-
$this->duration = $type;
108-
return $this;
82+
return $this->publicKey;
10983
}
11084

111-
public function name($name)
85+
/**
86+
* Set products
87+
*
88+
* @param array $data
89+
* @return $this
90+
*/
91+
public function products($data)
11292
{
113-
$this->name = $name;
114-
return $this;
93+
if (is_array($data) && sizeof($data) > 0) {
94+
$this->products = $data;
95+
}
96+
return $this;
11597
}
116-
117-
public function get()
98+
/**
99+
* Get session id and public key
100+
*
101+
* @return object sid and pkey
102+
*/
103+
public function getSession()
118104
{
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;
105+
for($i=0;$i<sizeof($this->products);$i++){
106+
$this->products[$i]['currency'] = $this->currency;
107+
$this->products[$i]['amount'] = round($this->products[$i]['amount'],2) * 100;
108+
if (!isset($this->products[$i]['quantity'])) {
109+
$this->products[$i]['quantity'] = 1;
110+
}
124111
}
125112

126-
if ($this->name) {
127-
$this->couponData['name'] = $this->name;
128-
$this->couponData['id'] = $this->name;
129-
}
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;
113+
try {
114+
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+
]);
124+
$output = [
125+
'sid' => $session->id,
126+
'pkey' => $this->publicKey
127+
];
128+
return (object) $output;
129+
}
130+
} catch (\Exception $e) {
131+
return (object)['isError' => 'true','message'=> $e->getMessage()];
135132
}
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-
}
144133
}
145134

146-
public function delete($id)
135+
/**
136+
* Retrieve session.
137+
*
138+
* @param string $sessionToken
139+
* @return object $infos
140+
*/
141+
public function retrieve($sessionToken)
147142
{
148143
try {
149-
Stripe::setApiKey($this->secretKey);
150-
$coupon = Coupon::retrieve($id);
151-
$coupon->delete();
152-
return $coupon;
144+
Stripe::setApiKey($this->secretKey);
145+
$infos = Session::retrieve($sessionToken);
146+
return $infos;
153147
} catch (\Exception $e) {
154-
return (object)['isError' => 'true','message'=> $e->getMessage()];
148+
return (object)['isError' => 'true','message'=> $e->getMessage()];
155149
}
156-
}
157150

158151

152+
}
159153
}

src/StripeCoupon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @copyright Kawsar Soft. (http://kawsarsoft.com)
88
*/
99

10-
use Illuminate\Support\Str;
10+
use Illuminate\Support\Str;
1111
use Stripe\Stripe;
1212
use Stripe\Coupon;
1313
use Config;

0 commit comments

Comments
 (0)