-
Notifications
You must be signed in to change notification settings - Fork 6
/
stripe_universal.php
466 lines (400 loc) · 14.5 KB
/
stripe_universal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/**
* Stripe Universal processing gateway. Redirect user to Stripe pre-build page
*
* The Stripe API can be found at: https://stripe.com/docs/api
*/
class StripeUniversal extends NonmerchantGateway
{
/**
* @var array An array of meta data for this gateway
*/
private $meta;
private $base_url = 'https://api.stripe.com/v1/';
/**
* Construct a new merchant gateway
*/
public function __construct()
{
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
// Load components required by this module
Loader::loadComponents($this, ['Input']);
// Load the language required by this module
Language::loadLang('stripe_universal', null, dirname(__FILE__) . DS . 'language' . DS);
// Load product configuration required by this module
Configure::load('stripe_universal', dirname(__FILE__) . DS . 'config' . DS);
}
/**
* {@inheritdoc}
*/
public function setCurrency($currency)
{
$this->currency = $currency;
}
/**
* {@inheritdoc}
*/
public function getSettings(array $meta = null)
{
// Load the view into this object, so helpers can be automatically added to the view
$this->view = new View('settings', 'default');
$this->view->setDefaultView('components' . DS . 'gateways' . DS . 'nonmerchant' . DS . 'stripe_universal' . DS);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
$this->view->set('meta', $meta);
return $this->view->fetch();
}
/**
* {@inheritdoc}
*/
public function editSettings(array $meta)
{
// Validate the given meta data to ensure it meets the requirements
$rules = [
'secret_key' => [
'empty' => [
'rule' => 'isEmpty',
'negate' => true,
'message' => Language::_('StripeUniversal.!error.secret_key.empty', true)
],
'valid' => [
'rule' => [[$this, 'validateConnection']],
'message' => Language::_('StripeUniversal.!error.secret_key.valid', true)
]
]
];
// @TODO enable a subset of available payment method
// https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-customer
$this->Input->setRules($rules);
return $meta;
}
/**
* {@inheritdoc}
*/
public function encryptableFields()
{
return ['secret_key'];
}
/**
* {@inheritdoc}
*/
public function setMeta(array $meta = null)
{
$this->meta = $meta;
}
/**
* {@inheritdoc}
*/
public function requiresCustomerPresent()
{
return true;
}
/**
* {@inheritdoc}
*/
public function buildProcess($contact_info, $amount, $invoice_amounts = null, $options = null)
{
$this->loadApi();
// Load the view into this object, so helpers can be automatically added to the view
$this->view = $this->makeView(
'payment_button',
'default',
str_replace(ROOTWEBDIR, '', dirname(__FILE__) . DS)
);
// Load the helpers required for this view
Loader::loadHelpers($this, ['Form', 'Html']);
// Find Client
Loader::loadModels($this, ['Contacts']);
$contact = $this->Contacts->get($contact_info['id']);
// Create Payment Session for user to jump to
// FIXME: Find a way to reuse existing session instead of creating one every time
try {
$sessionObj = [
'customer_email' => $contact->email,
'line_items' => $this->getLineItems($amount, $invoice_amounts),
'mode' => 'payment',
'success_url' => $options['return_url'] . "&session_id={CHECKOUT_SESSION_ID}",
'cancel_url' => $options['return_url'] . "&canceled=true",
'metadata' => array(
'client_id' => $contact_info['client_id'],
'invoices' => base64_encode(serialize($invoice_amounts)),
)
];
$session = \Stripe\Checkout\Session::create($sessionObj);
} catch (Exception $e) {
$this->Input->setErrors(['api' => ['internal' => $e->getMessage()]]);
return;
}
$this->view->set('goto_url', $session->url);
return $this->view->fetch();
}
/**
* {@inheritdoc}
*/
public function success(array $get, array $post)
{
$this->loadApi();
if (array_key_exists('session_id', $get)) {
$session_id = $get['session_id'];
$session = \Stripe\Checkout\Session::retrieve($session_id, [
'expand' => ['payment_intent']
]);
return $this->handleCheckoutSession($session);
}
if ($get['canceled'] === "true") {
$this->Input->setErrors([
"exceptions" => [
'message' => Language::_('StripeUniversal.!error.payment_canceled', true),
]
]);
} else {
$this->Input->setErrors([
"session_id" => [
'message' => Language::_('StripeUniversal.!error.session_id.missing', true),
]
]);
}
return [];
}
private function handleCheckoutSession(\Stripe\Checkout\Session $session) {
$status = "pending";
if ($session->payment_status === "paid") {
$status = "approved";
} else {
switch ($session->status) {
case "expired":
$this->Input->setErrors([
'payment_status' => [
'message' => Language::_('StripeUniversal.!error.payment_expired', true),
]
]);
$status = "void";
break;
case "complete":
$this->Input->setErrors([
'payment_status' => [
'message' => Language::_('StripeUniversal.!error.payment_in_progress', true),
]
]);
break;
case "open":
default:
$this->Input->setErrors([
'payment_status' => [
'message' => Language::_('StripeUniversal.!error.payment_not_received', true),
]
]);
}
}
$metadata = $this->extractMetadata($session->metadata);
if ($metadata === false) {
return [];
}
// if Stripe do the currency conversion
// we keep the source instead of using the real currency customer paid for
if (isset($session->currency_conversion)) {
$currency = strtoupper($session->currency_conversion->source_currency);
$amount = $this->formatAmount(
$session->currency_conversion->amount_total,
$currency,
'from'
);
} else {
$currency = strtoupper($session->currency);
$amount = $this->formatAmount(
$session->amount_total,
$currency,
'from'
);
}
return [
'client_id' => $metadata['client_id'],
'amount' => $amount,
'currency' => $currency,
'status' => $status,
'reference_id' => $session->id,
'transaction_id' => $session->payment_intent,
'invoices' => $metadata['invoices'],
];
}
private function extractMetadata(\Stripe\StripeObject $metadata)
{
if ($metadata === null) {
$this->Input->setErrors([
'metadata' => [
'message' => Language::_('StripeUniversal.!error.metadata.missing', true),
]
]);
return false;
}
$metadata = $metadata->toArray();
$client_id = $metadata['client_id'];
if (!$metadata['client_id']) {
$this->Input->setErrors([
'metadata' => [
'message' => Language::_('StripeUniversal.!error.metadata.missing_client_id', true),
]
]);
return false;
}
return [
'client_id' => $client_id,
'invoices' => unserialize(base64_decode($metadata['invoices'])),
];
}
/**
* Loads the API if not already loaded, can only be called after constructor has done its work
*/
private function loadApi()
{
if (!class_exists('\Stripe\Stripe', false)) {
Loader::load(dirname(__FILE__) . DS . 'vendor' . DS . 'stripe' . DS . 'stripe-php' . DS . 'init.php');
}
Stripe\Stripe::setApiKey((isset($this->meta['secret_key']) ? $this->meta['secret_key'] : null));
// Include identifying information about this being a gateway for Blesta
Stripe\Stripe::setAppInfo('Blesta ' . $this->getName(), $this->getVersion(), 'https://blesta.com');
}
/**
* Convert amount from decimal value to integer representation of cents
*
* @param float $amount
* @param string $currency
* @param string $direction
* @return int The amount in cents
*/
private function formatAmount($amount, $currency, $direction = 'to')
{
$non_decimal_currencies = ['BIF', 'CLP', 'DJF', 'GNF', 'JPY',
'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VUV', 'XAF', 'XOF', 'XPF'];
if (is_numeric($amount) && !in_array($currency, $non_decimal_currencies)) {
if ($direction === 'to') {
$amount *= 100;
} else {
$amount /= 100;
return round($amount, 2);
}
}
return (int)round($amount);
}
/**
* Checks whether a key can be used to connect to the Stripe API
*
* @param string $secret_key The API to connect with
* @return boolean True if a successful API call was made, false otherwise
*/
public function validateConnection($secret_key)
{
$this->loadApi();
$success = true;
// Skip test if test key is given
if (substr($secret_key, 0, 7) == 'sk_test') {
return $success;
}
try {
// Attempt to make an API request
Stripe\Stripe::setApiKey($secret_key);
Stripe\Balance::retrieve();
} catch (Exception $e) {
$success = false;
}
return $success;
}
/**
* Retrieves the description for CC charges
*
* @param float total amount
* @param array|null $invoice_amounts An array of invoice amounts (optional)
* @return array[] Line Items
*/
private function getLineItems(float $amount, array $invoice_amounts = null)
{
$defaultItem = [[
'price_data' => [
'currency' => $this->currency,
'product_data' => [
'name' => Language::_('StripeUniversal.charge_description_default', true),
],
'unit_amount_decimal' => $this->formatAmount($amount, $this->currency, 'to'),
],
'quantity' => 1,
]];
// No invoice amounts, set a default description
if (empty($invoice_amounts)) {
return $defaultItem;
}
Loader::loadModels($this, ['Invoices']);
// Create a list of invoices being paid
$id_codes = [];
foreach ($invoice_amounts as $invoice_amount) {
if (($invoice = $this->Invoices->get($invoice_amount['id']))) {
$id_codes[] = array(
"id" => $invoice->id_code,
"amount" => $invoice_amount['amount'],
);
}
}
// Use the default description if there are no valid invoices
if (empty($id_codes)) {
return $defaultItem;
}
$result = [];
foreach ($id_codes as $id_code) {
$result[] = [
'price_data' => [
'currency' => $this->currency,
'product_data' => [
'name' => Language::_('StripeUniversal.charge_description', true, $id_code['id']),
],
'unit_amount' => $this->formatAmount($id_code['amount'], $this->currency, 'to'),
],
'quantity' => 1,
];
}
return $result;
}
/**
* Validates the incoming POST/GET response from the gateway to ensure it is
* legitimate and can be trusted.
*
* @param array $get The GET data for this request
* @param array $post The POST data for this request
* @return array An array of transaction data, sets any errors using Input if the data fails to validate
*/
public function validate(array $get, array $post)
{
$this->loadApi();
// Get event payload
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? null;
try {
$webhook_secret = $this->meta['webhook_secret'] ?? null;
if ($webhook_secret) {
$event = Stripe\Webhook::constructEvent(
$payload,
$sig_header,
$webhook_secret
);
} else {
$event = \Stripe\Event::constructFrom($payload);
}
} catch (\Stripe\Exception\SignatureVerificationException $e) {
$this->log($this->base_url . 'Webhook - invalid_signature', $e->getMessage());
$this->Input->setErrors(['event' => ['internal' => "invalid_signature"]]);
return [];
} catch (\UnexpectedValueException $e) {
$this->log($this->base_url . 'Webhook - invalid_payload', $e->getMessage());
$this->Input->setErrors(['event' => ['internal' => "invalid_payload"]]);
return [];
} catch (Exception $e) {
$this->log($this->base_url . "Webhook - unknown", $e->getMessage());
$this->Input->setErrors(['event' => ['internal' => "unknown"]]);
return [];
}
switch ($event->type) {
case 'checkout.session.completed':
return $this->handleCheckoutSession($event->data->object);
break;
};
return [];
}
}