|
7 | 7 | * @copyright Kawsar Soft. (http://kawsarsoft.com)
|
8 | 8 | */
|
9 | 9 |
|
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; |
13 | 15 |
|
14 |
| -/** |
15 |
| - * Coupon class |
16 |
| - * @source https://stripe.com/docs/api/coupons |
17 |
| - */ |
18 |
| -class StripeCoupon |
| 16 | +class StripeCheckout |
19 | 17 | {
|
20 |
| - /** |
21 |
| - * Secret key |
22 |
| - * @var string |
23 |
| - */ |
| 18 | + private $currency = 'usd'; |
| 19 | + private $description = 'Stripe payment checkout by lara-stripe'; |
| 20 | + private $products = []; |
24 | 21 | 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; |
67 | 26 |
|
68 | 27 | public function __construct()
|
69 | 28 | {
|
70 | 29 | if(config::get('lara-stripe.driver') === 'config') {
|
| 30 | + $this->currency = config::get('lara-stripe.currency'); |
71 | 31 | $this->secretKey = config::get('lara-stripe.secret_key');
|
| 32 | + $this->publicKey = config::get('lara-stripe.public_key'); |
72 | 33 | }
|
73 | 34 | }
|
74 | 35 | /**
|
75 |
| - * Set secret key |
76 |
| - * @param string $data |
| 36 | + * Set credentials, secret and public key |
| 37 | + * |
| 38 | + * Set stripe currency |
| 39 | + * @param array $data |
77 | 40 | * @return $this
|
78 | 41 | */
|
79 | 42 | public function setup($data)
|
80 | 43 | {
|
81 | 44 | if (isset($data['secret_key'])) {
|
82 | 45 | $this->secretKey = $data['secret_key'];
|
83 | 46 | }
|
| 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 | + } |
84 | 53 | return $this;
|
85 | 54 | }
|
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) |
88 | 62 | {
|
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']; |
96 | 65 | }
|
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; |
99 | 73 | }
|
100 | 74 |
|
101 |
| - public function duration($type,$month = 1) |
| 75 | + /** |
| 76 | + * Retrieve public key |
| 77 | + * |
| 78 | + * @return $this |
| 79 | + */ |
| 80 | + public function publicKey() |
102 | 81 | {
|
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; |
109 | 83 | }
|
110 | 84 |
|
111 |
| - public function name($name) |
| 85 | + /** |
| 86 | + * Set products |
| 87 | + * |
| 88 | + * @param array $data |
| 89 | + * @return $this |
| 90 | + */ |
| 91 | + public function products($data) |
112 | 92 | {
|
113 |
| - $this->name = $name; |
114 |
| - return $this; |
| 93 | + if (is_array($data) && sizeof($data) > 0) { |
| 94 | + $this->products = $data; |
| 95 | + } |
| 96 | + return $this; |
115 | 97 | }
|
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() |
118 | 104 | {
|
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 | + } |
124 | 111 | }
|
125 | 112 |
|
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()]; |
135 | 132 | }
|
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 |
| - } |
144 | 133 | }
|
145 | 134 |
|
146 |
| - public function delete($id) |
| 135 | + /** |
| 136 | + * Retrieve session. |
| 137 | + * |
| 138 | + * @param string $sessionToken |
| 139 | + * @return object $infos |
| 140 | + */ |
| 141 | + public function retrieve($sessionToken) |
147 | 142 | {
|
148 | 143 | 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; |
153 | 147 | } catch (\Exception $e) {
|
154 |
| - return (object)['isError' => 'true','message'=> $e->getMessage()]; |
| 148 | + return (object)['isError' => 'true','message'=> $e->getMessage()]; |
155 | 149 | }
|
156 |
| - } |
157 | 150 |
|
158 | 151 |
|
| 152 | + } |
159 | 153 | }
|
0 commit comments