|
7 | 7 | * @copyright Kawsar Soft. (http://kawsarsoft.com)
|
8 | 8 | */
|
9 | 9 |
|
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; |
15 | 13 |
|
16 |
| -class StripeCheckout |
| 14 | +/** |
| 15 | + * Coupon class |
| 16 | + * @source https://stripe.com/docs/api/coupons |
| 17 | + */ |
| 18 | +class StripeCoupon |
17 | 19 | {
|
18 | 20 | /**
|
19 |
| - * Checkout Currency |
20 |
| - * @var string length 3 and lowercase |
| 21 | + * Secret key |
| 22 | + * @var string |
21 | 23 | */
|
22 |
| - private $currency = 'usd'; |
| 24 | + private $secretKey; |
23 | 25 |
|
24 | 26 | /**
|
25 |
| - * Checkout description |
| 27 | + * Currency when coupon amount is fixed |
26 | 28 | * @var string
|
27 | 29 | */
|
28 |
| - private $description = 'Stripe payment checkout by lara-stripe'; |
| 30 | + private $currency; |
29 | 31 |
|
30 | 32 | /**
|
31 |
| - * Checkout products data |
32 |
| - * @var array |
| 33 | + * Coupon amount |
| 34 | + * @var integer|float |
33 | 35 | */
|
34 |
| - private $products = []; |
| 36 | + private $amount; |
35 | 37 |
|
36 | 38 | /**
|
37 |
| - * Secret key |
38 |
| - * @var string |
| 39 | + * Coupon amount type |
| 40 | + * @var string per or fixed |
39 | 41 | */
|
40 |
| - private $secretKey; |
| 42 | + private $type; |
41 | 43 |
|
42 | 44 | /**
|
43 |
| - * Public key |
44 |
| - * @var string |
| 45 | + * Coupon duration |
| 46 | + * @var string once |
45 | 47 | */
|
46 |
| - private $publicKey; |
| 48 | + private $duration; |
47 | 49 |
|
48 | 50 | /**
|
49 |
| - * Checkout success url |
50 |
| - * @var string |
| 51 | + * Coupon name & coupon id |
| 52 | + * @var string must be camel_case |
51 | 53 | */
|
52 |
| - private $successURI; |
| 54 | + private $name; |
53 | 55 |
|
54 | 56 | /**
|
55 |
| - * Checkout cancel url |
56 |
| - * @var string |
| 57 | + * Coupon duration month |
| 58 | + * @var integer |
57 | 59 | */
|
58 |
| - private $cancelURI; |
| 60 | + private $durationMonth; |
59 | 61 |
|
60 | 62 | /**
|
61 |
| - * Checkout ref ex: product id , payment id, card id similar. |
62 |
| - * @var string |
| 63 | + * All coupon data for create |
| 64 | + * @var [type] |
63 | 65 | */
|
64 |
| - private $referenceKey; |
65 |
| - |
66 |
| - private $checkoutData = []; |
67 |
| - |
68 |
| - private $isFuture = false; |
| 66 | + private $couponData; |
69 | 67 |
|
70 | 68 | public function __construct()
|
71 | 69 | {
|
72 | 70 | if(config::get('lara-stripe.driver') === 'config') {
|
73 |
| - $this->currency = config::get('lara-stripe.currency'); |
74 | 71 | $this->secretKey = config::get('lara-stripe.secret_key');
|
75 |
| - $this->publicKey = config::get('lara-stripe.public_key'); |
76 | 72 | }
|
77 | 73 | }
|
78 | 74 | /**
|
79 |
| - * Set credentials, secret and public key |
80 |
| - * |
81 |
| - * Set stripe currency |
82 |
| - * @param array $data |
| 75 | + * Set secret key |
| 76 | + * @param string $data |
83 | 77 | * @return $this
|
84 | 78 | */
|
85 | 79 | public function setup($data)
|
86 | 80 | {
|
87 | 81 | if (isset($data['secret_key'])) {
|
88 | 82 | $this->secretKey = $data['secret_key'];
|
89 | 83 | }
|
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 |
| - } |
96 | 84 | return $this;
|
97 | 85 | }
|
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') |
105 | 88 | {
|
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'; |
111 | 96 | }
|
112 |
| - if (isset($data['ref_key'])) { |
113 |
| - $this->referenceKey = $data['ref_key']; |
114 |
| - } |
115 |
| - return $this; |
116 |
| - } |
117 | 97 |
|
118 |
| - /** |
119 |
| - * Retrieve public key |
120 |
| - * |
121 |
| - * @return $this |
122 |
| - */ |
123 |
| - public function publicKey() |
124 |
| - { |
125 |
| - return $this->publicKey; |
| 98 | + return $this; |
126 | 99 | }
|
127 | 100 |
|
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) |
135 | 102 | {
|
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; |
140 | 109 | }
|
141 | 110 |
|
142 |
| - public function future() |
| 111 | + public function name($name) |
143 | 112 | {
|
144 |
| - $this->isFuture = true; |
145 |
| - return $this; |
| 113 | + $this->name = $name; |
| 114 | + return $this; |
146 | 115 | }
|
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() |
153 | 118 | {
|
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; |
160 | 124 | }
|
161 | 125 |
|
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; |
191 | 129 | }
|
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; |
208 | 135 | }
|
| 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 | + } |
209 | 144 | }
|
210 | 145 |
|
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) |
218 | 147 | {
|
219 | 148 | 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; |
226 | 153 | } catch (\Exception $e) {
|
227 |
| - return (object)['isError' => 'true','message'=> $e->getMessage()]; |
| 154 | + return (object)['isError' => 'true','message'=> $e->getMessage()]; |
228 | 155 | }
|
229 | 156 | }
|
230 | 157 |
|
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 |
| - } |
245 | 158 |
|
246 |
| - } |
247 | 159 | }
|
0 commit comments