|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Phalcon\Cashier\Traits\StripeWebhookHandlersTrait; |
|
8
|
|
|
use Phalcon\Http\Response; |
|
9
|
|
|
use Gewaer\Models\Users; |
|
10
|
|
|
use Gewaer\Models\EmailTemplates; |
|
11
|
|
|
use Phalcon\Di; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PaymentsController |
|
16
|
|
|
* |
|
17
|
|
|
* Class to handle payment webhook from our cashier library |
|
18
|
|
|
* |
|
19
|
|
|
* @package Gewaer\Api\Controllers |
|
20
|
|
|
* @property Log $log |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
class PaymentsController extends BaseController |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Stripe Webhook Handlers |
|
27
|
|
|
*/ |
|
28
|
|
|
use StripeWebhookHandlersTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Handle stripe webhoook calls |
|
32
|
|
|
* |
|
33
|
|
|
* @return Response |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function handleWebhook(): Response |
|
36
|
|
|
{ |
|
37
|
|
|
//we cant processs if we dont find the stripe header |
|
38
|
4 |
|
if (!$this->request->hasHeader('Stripe-Signature')) { |
|
39
|
|
|
throw new Exception('Route not found for this call'); |
|
40
|
|
|
} |
|
41
|
4 |
|
$request = $this->request->getPost(); |
|
42
|
4 |
|
if (empty($request)) { |
|
43
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
44
|
|
|
} |
|
45
|
4 |
|
$type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.')); |
|
46
|
4 |
|
$method = 'handle' . $type; |
|
47
|
4 |
|
$payloadContent = json_encode($request); |
|
48
|
4 |
|
$this->log->info("Webhook Handler Method: {$method} \n"); |
|
49
|
4 |
|
$this->log->info("Payload: {$payloadContent} \n"); |
|
50
|
4 |
|
if (method_exists($this, $method)) { |
|
51
|
4 |
|
return $this->{$method}($request, $method); |
|
52
|
|
|
} else { |
|
53
|
|
|
return $this->response(['Missing Method to Handled']); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Handle customer subscription updated. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $payload |
|
61
|
|
|
* @return Response |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function handleCustomerSubscriptionUpdated(array $payload, string $method): Response |
|
64
|
|
|
{ |
|
65
|
|
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
66
|
|
|
if ($user) { |
|
67
|
|
|
//We need to send a mail to the user |
|
68
|
|
|
$this->sendWebhookResponseEmail($user, $payload, $method); |
|
69
|
|
|
} |
|
70
|
|
|
return $this->response(['Webhook Handled']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Handle customer subscription free trial ending. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $payload |
|
77
|
|
|
* @return Response |
|
78
|
|
|
*/ |
|
79
|
1 |
|
protected function handleCustomerSubscriptionTrialwillend(array $payload, string $method): Response |
|
80
|
|
|
{ |
|
81
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
82
|
1 |
|
if ($user) { |
|
83
|
|
|
//We need to send a mail to the user |
|
84
|
|
|
$this->sendWebhookResponseEmail($user, $payload, $method); |
|
85
|
|
|
} |
|
86
|
1 |
|
return $this->response(['Webhook Handled']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Handle sucessfull payment |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $payload |
|
93
|
|
|
* @return Response |
|
94
|
|
|
*/ |
|
95
|
1 |
|
protected function handleChargeSucceeded(array $payload, string $method): Response |
|
96
|
|
|
{ |
|
97
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
98
|
1 |
|
if ($user) { |
|
99
|
|
|
//We need to send a mail to the user |
|
100
|
|
|
$this->sendWebhookResponseEmail($user, $payload, $method); |
|
101
|
|
|
} |
|
102
|
1 |
|
return $this->response(['Webhook Handled']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Handle bad payment |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $payload |
|
109
|
|
|
* @return Response |
|
110
|
|
|
*/ |
|
111
|
1 |
|
protected function handleChargeFailed(array $payload, string $method) : Response |
|
112
|
|
|
{ |
|
113
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
114
|
1 |
|
if ($user) { |
|
115
|
|
|
//We need to send a mail to the user |
|
116
|
|
|
$this->sendWebhookResponseEmail($user, $payload, $method); |
|
117
|
|
|
} |
|
118
|
1 |
|
return $this->response(['Webhook Handled']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Handle pending payments |
|
123
|
|
|
* |
|
124
|
|
|
* @param array $payload |
|
125
|
|
|
* @return Response |
|
126
|
|
|
*/ |
|
127
|
1 |
|
protected function handleChargePending(array $payload, string $method) : Response |
|
128
|
|
|
{ |
|
129
|
1 |
|
$user = Users::findFirstByStripeId($payload['data']['object']['customer']); |
|
130
|
1 |
|
if ($user) { |
|
131
|
|
|
//We need to send a mail to the user |
|
132
|
|
|
$this->sendWebhookResponseEmail($user, $payload, $method); |
|
133
|
|
|
} |
|
134
|
1 |
|
return $this->response(['Webhook Handled']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Send webhook related emails to user |
|
139
|
|
|
* @param Users $user |
|
140
|
|
|
* @param array $payload |
|
141
|
|
|
* @param string $method |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function sendWebhookResponseEmail(Users $user, array $payload, string $method): void |
|
145
|
|
|
{ |
|
146
|
|
|
switch ($method) { |
|
147
|
|
|
case 'handleCustomerSubscriptionTrialwillend': |
|
148
|
|
|
$templateName = 'users-trial-end'; |
|
149
|
|
|
break; |
|
150
|
|
|
case 'handleCustomerSubscriptionUpdated': |
|
151
|
|
|
$templateName = 'users-subscription-updated'; |
|
152
|
|
|
break; |
|
153
|
|
|
|
|
154
|
|
|
case 'handleChargeSucceeded': |
|
155
|
|
|
$templateName = 'users-charge-success'; |
|
156
|
|
|
break; |
|
157
|
|
|
|
|
158
|
|
|
case 'handleChargeFailed': |
|
159
|
|
|
$templateName = 'users-charge-failed'; |
|
160
|
|
|
break; |
|
161
|
|
|
|
|
162
|
|
|
case 'handleChargePending': |
|
163
|
|
|
$templateName = 'users-charge-pending'; |
|
164
|
|
|
break; |
|
165
|
|
|
|
|
166
|
|
|
default: |
|
167
|
|
|
break; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
//Search for actual template by templateName |
|
171
|
|
|
$emailTemplate = EmailTemplates::getByName($templateName); |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
Di::getDefault()->getMail() |
|
174
|
|
|
->to($user->email) |
|
175
|
|
|
->subject('Canvas Payments and Subscriptions') |
|
176
|
|
|
->content($emailTemplate->template) |
|
177
|
|
|
->sendNow(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|