Passed
Pull Request — master (#54)
by Rafael
08:05 queued 01:07
created

PaymentsController::handleChargeFailed()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Api\Controllers;
6
7
use Phalcon\Http\Response;
8
use Gewaer\Models\Users;
9
use Gewaer\Exception\NotFoundHttpException;
10
use Gewaer\Traits\EmailTrait;
11
12
/**
13
 * Class PaymentsController
14
 *
15
 * Class to handle payment webhook from our cashier library
16
 *
17
 * @package Gewaer\Api\Controllers
18
 * @property Log $log
19
 *
20
 */
21
class PaymentsController extends BaseController
22
{
23
    /**
24
     * Email Trait
25
     */
26
    use EmailTrait;
27
28
    /**
29
     * Handle stripe webhoook calls
30
     *
31
     * @return Response
32
     */
33 4
    public function handleWebhook(): Response
34
    {
35
        //we cant processs if we dont find the stripe header
36 4
        if (!defined('API_TESTS')) {
37
            if (!$this->request->hasHeader('Stripe-Signature')) {
38
                throw new NotFoundHttpException('Route not found for this call');
39
            }
40
        }
41
42 4
        $request = $this->request->getPost();
43
44 4
        if (empty($request)) {
45
            $request = $this->request->getJsonRawBody(true);
46
        }
47 4
        $type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.'));
48 4
        $method = 'handle' . $type;
49
50 4
        $payloadContent = json_encode($request);
51 4
        $this->log->info("Webhook Handler Method: {$method} \n");
52 4
        $this->log->info("Payload: {$payloadContent} \n");
53
54 4
        if (method_exists($this, $method)) {
55 4
            return $this->{$method}($request);
56
        } else {
57
            return $this->response(['Missing Method to Handled']);
58
        }
59
    }
60
61
    /**
62
     * Handle customer subscription updated.
63
     *
64
     * @param  array $payload
65
     * @return Response
66
     */
67
    protected function handleCustomerSubscriptionUpdated(array $payload): Response
68
    {
69
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
70
        if ($user) {
71
            //We need to send a mail to the user
72
            if (!defined('API_TESTS')) {
73
                $this->sendWebhookEmail($user, $payload);
74
            }
75
        }
76
        return $this->response(['Webhook Handled']);
77
    }
78
79
    /**
80
     * Handle a cancelled customer from a Stripe subscription.
81
     *
82
     * @param  array  $payload
83
     * @return Response
84
     */
85
    protected function handleCustomerSubscriptionDeleted(array $payload): Response
86
    {
87
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
88
        if ($user) {
89
            $subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']);
90
91
            if (is_object($subscription)) {
92
                $subscription->markAsCancelled();
93
            }
94
        }
95
        return $this->response(['Webhook Handled']);
96
    }
97
98
    /**
99
     * Handle customer subscription free trial ending.
100
     *
101
     * @param  array $payload
102
     * @return Response
103
     */
104 1
    protected function handleCustomerSubscriptionTrialwillend(array $payload): Response
105
    {
106 1
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
107 1
        if ($user) {
108
            //We need to send a mail to the user
109
            if (!defined('API_TESTS')) {
110
                $this->sendWebhookEmail($user, $payload);
111
            }
112
        }
113 1
        return $this->response(['Webhook Handled']);
114
    }
115
116
    /**
117
     * Handle customer updated.
118
     *
119
     * @param  array $payload
120
     * @return Response
121
     */
122
    protected function handleCustomerUpdated(array $payload): Response
123
    {
124
        if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) {
125
            $user->updateCardFromStripe();
126
        }
127
        return $this->response(['Webhook Handled']);
128
    }
129
130
    /**
131
     * Handle customer source deleted.
132
     *
133
     * @param  array $payload
134
     * @return Response
135
     */
136
    protected function handleCustomerSourceDeleted(array $payload) : Response
137
    {
138
        if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) {
139
            $user->updateCardFromStripe();
140
        }
141
        return $this->response(['Webhook Handled']);
142
    }
143
144
    /**
145
     * Handle deleted customer.
146
     *
147
     * @param  array $payload
148
     * @return Response
149
     */
150
    protected function handleCustomerDeleted(array $payload) : Response
151
    {
152
        $user = Users::findFirstByStripeId($payload['data']['object']['id']);
153
        if ($user) {
154
            foreach ($user->subscriptions as $subscription) {
155
                $subscription->skipTrial()->markAsCancelled();
156
            }
157
158
            $user->stripe_id = null;
159
            $user->trial_ends_at = null;
160
            $user->card_brand = null;
161
            $user->card_last_four = null;
162
            $user->update();
163
        }
164
        return $this->response(['Webhook Handled']);
165
    }
166
167
    /**
168
     * Handle sucessfull payment
169
     *
170
     * @todo send email
171
     * @param array $payload
172
     * @return Response
173
     */
174 1
    protected function handleChargeSucceeded(array $payload): Response
175
    {
176 1
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
177 1
        if ($user) {
178
            //We need to send a mail to the user
179
            if (!defined('API_TESTS')) {
180
                $this->sendWebhookEmail($user, $payload);
181
            }
182
        }
183 1
        return $this->response(['Webhook Handled']);
184
    }
185
186
    /**
187
     * Handle bad payment
188
     *
189
     * @todo send email
190
     * @param array $payload
191
     * @return Response
192
     */
193 1
    protected function handleChargeFailed(array $payload) : Response
194
    {
195 1
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
196 1
        if ($user) {
197
            //We need to send a mail to the user
198
            if (!defined('API_TESTS')) {
199
                $this->sendWebhookEmail($user, $payload);
200
            }
201
        }
202 1
        return $this->response(['Webhook Handled']);
203
    }
204
205
    /**
206
     * Handle looking for refund
207
     *
208
     * @todo send email
209
     * @param array $payload
210
     * @return Response
211
     */
212
    protected function handleChargeDisputeCreated(array $payload) : Response
213
    {
214
        return $this->response(['Webhook Handled']);
215
    }
216
217
    /**
218
     * Handle pending payments
219
     *
220
     * @todo send email
221
     * @param array $payload
222
     * @return Response
223
     */
224 1
    protected function handleChargePending(array $payload) : Response
225
    {
226 1
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
227 1
        if ($user) {
228
            //We need to send a mail to the user
229
            if (!defined('API_TESTS')) {
230
                $this->sendWebhookEmail($user, $payload);
231
            }
232
        }
233 1
        return $this->response(['Webhook Handled']);
234
    }
235
}
236