-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExtension.php
90 lines (73 loc) · 2.96 KB
/
Extension.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
<?php
namespace Thoughtco\StripeAuthorize;
use Admin\Facades\AdminAuth;
use Admin\Models\Orders_model;
use Admin\Models\Payments_model;
use Igniter\Flame\Exception\ApplicationException;
use Illuminate\Support\Facades\Event;
use System\Classes\BaseExtension;
use Thoughtco\OrderApprover\Events\OrderCreated;
/**
* StripeAuthorize Extension Information File
*/
class Extension extends BaseExtension
{
public function boot()
{
Event::listen('admin.controller.beforeResponse', function ($controller, $params) {
if (!AdminAuth::isLogged() OR !$controller->getLocationId()) return;
Payments_model::where([
'class_name' => 'Igniter\PayRegister\Payments\Stripe',
'status' => 1,
])
->each(function ($payment) {
if (!$payment->data)
return;
// dispatch any orders with default stripe status
Orders_model::where([
'status_id' => $payment->data['order_status'],
'payment' => $payment->code,
])
->each(function ($order) {
Event::dispatch(new OrderCreated($order));
});
});
});
// order accepted through orderApprover extension - accept payment
Event::listen('thoughtco.orderApprover.orderAccepted', function ($notifier, $order) {
$order = Orders_model::with(['payment_logs', 'payment_method'])->find($order->order_id);
if (!$this->isStripeOrder($order))
return;
$intentId = $this->getIntentFromOrder($order);
if (!$intentId) {
return;
}
$order->payment_method->capturePaymentIntent($intentId, $order);
});
// order rejected through orderApprover extension - cancel payment
Event::listen('thoughtco.orderApprover.orderRejected', function ($notifier, $order) {
$order = Orders_model::with(['payment_logs', 'payment_method'])->find($order->order_id);
if (!$this->isStripeOrder($order))
return;
$intentId = $this->getIntentFromOrder($order);
if (!$intentId) {
return;
}
$order->payment_method->cancelPaymentIntent($intentId, $order);
});
}
protected function isStripeOrder($order)
{
return isset($order->payment_method) && $order->payment_method->class_name == 'Igniter\PayRegister\Payments\Stripe';
}
protected function getIntentFromOrder($order)
{
foreach ($order->payment_logs as $paymentLog) {
if (array_get($paymentLog->response, 'status') === 'requires_capture') {
$intentId = array_get($paymentLog->response, 'id');
if ($intentId)
return $intentId;
}
}
}
}