Issues (37)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  Header Injection
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Traits/StripeWebhookHandlersTrait.php (5 issues)

Labels
Severity
1
<?php
2
3
namespace Phalcon\Cashier\Traits;
4
5
use Baka\Auth\Models\Users;
6
use Exception;
7
use Phalcon\Http\Response;
8
9
/**
10
 * Trait WebhookHandlers
11
 *
12
 * @package Phalcon\Cashier\Traits
13
 *
14
 * @property Users $user
15
 * @property Subscriptions $subscriptions
16
 *
17
 */
18
trait StripeWebhookHandlersTrait
19
{
20
    /**
21
     * Handle stripe webhoook calls
22
     *
23
     * @return Response
24
     */
25
    public function handleWebhook(): Response
26
    {
27
        //we cant processs if we dont find the stripe header
28
        if (!$this->request->hasHeader('Stripe-Signature')) {
29
            throw new Exception('Route not found for this call');
30
        }
31
32
        $request = $this->request->getPost();
33
34
        if (empty($request)) {
35
            $request = $this->request->getJsonRawBody(true);
36
        }
37
        $type = str_replace('.', '', ucwords(str_replace('_', '', $request['type']), '.'));
38
        $method = 'handle' . $type;
39
40
        $payloadContent = json_encode($request);
41
        $this->log->info("Webhook Handler Method: {$method} \n");
42
        $this->log->info("Payload: {$payloadContent} \n");
43
44
        if (method_exists($this, $method)) {
45
            return $this->{$method}($request, $method);
46
        } else {
47
            return $this->response(['Missing Method to Handled']);
48
        }
49
    }
50
51
    /**
52
     * Handle customer subscription updated.
53
     *
54
     * @param  array $payload
55
     * @return Response
56
     */
57
    protected function handleCustomerSubscriptionUpdated(array $payload, string $method): Response
58
    {
59
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
60
        if ($user) {
61
            //We need to send a mail to the user
62
            $this->sendWebhookResponseEmail($user, $payload);
0 ignored issues
show
The call to Phalcon\Cashier\Traits\S...dWebhookResponseEmail() has too few arguments starting with method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            $this->/** @scrutinizer ignore-call */ sendWebhookResponseEmail($user, $payload);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
        }
64
        return $this->response(['Webhook Handled']);
65
    }
66
67
    /**
68
     * Handle a cancelled customer from a Stripe subscription.
69
     *
70
     * @param  array  $payload
71
     * @return Response
72
     */
73
    protected function handleCustomerSubscriptionDeleted(array $payload, string $method): Response
74
    {
75
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
76
        if ($user) {
77
            $subscription = $user->getAllSubscriptions('stripe_id =' . $payload['data']['object']['id']);
78
79
            if (is_object($subscription)) {
80
                $subscription->markAsCancelled();
81
            }
82
        }
83
        return $this->response(['Webhook Handled']);
84
    }
85
86
    /**
87
     * Handle customer subscription free trial ending.
88
     *
89
     * @param  array $payload
90
     * @return Response
91
     */
92
    protected function handleCustomerSubscriptionTrialwillend(array $payload, string $method): Response
93
    {
94
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
95
        if ($user) {
96
            //We need to send a mail to the user
97
            $this->sendWebhookResponseEmail($user, $payload);
0 ignored issues
show
The call to Phalcon\Cashier\Traits\S...dWebhookResponseEmail() has too few arguments starting with method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            $this->/** @scrutinizer ignore-call */ sendWebhookResponseEmail($user, $payload);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
98
        }
99
        return $this->response(['Webhook Handled']);
100
    }
101
102
    /**
103
     * Handle customer updated.
104
     *
105
     * @param  array $payload
106
     * @return Response
107
     */
108
    protected function handleCustomerUpdated(array $payload, string $method): Response
109
    {
110
        if ($user = Users::findFirstByStripeId($payload['data']['object']['id'])) {
111
            $user->updateCardFromStripe();
112
        }
113
        return $this->response(['Webhook Handled']);
114
    }
115
116
    /**
117
     * Handle customer source deleted.
118
     *
119
     * @param  array $payload
120
     * @return Response
121
     */
122
    protected function handleCustomerSourceDeleted(array $payload, string $method): Response
123
    {
124
        if ($user = Users::findFirstByStripeId($payload['data']['object']['customer'])) {
125
            $user->updateCardFromStripe();
126
        }
127
        return $this->response(['Webhook Handled']);
128
    }
129
130
    /**
131
     * Handle deleted customer.
132
     *
133
     * @param  array $payload
134
     * @return Response
135
     */
136
    protected function handleCustomerDeleted(array $payload, string $method): Response
137
    {
138
        $user = Users::findFirstByStripeId($payload['data']['object']['id']);
139
        if ($user) {
140
            foreach ($user->subscriptions as $subscription) {
141
                $subscription->skipTrial()->markAsCancelled();
142
            }
143
144
            $user->stripe_id = null;
145
            $user->trial_ends_at = null;
146
            $user->card_brand = null;
147
            $user->card_last_four = null;
148
            $user->update();
149
        }
150
        return $this->response(['Webhook Handled']);
151
    }
152
153
    /**
154
     * Handle sucessfull payment
155
     *
156
     * @todo send email
157
     * @param array $payload
158
     * @return Response
159
     */
160
    protected function handleChargeSucceeded(array $payload, string $method): Response
161
    {
162
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
163
        if ($user) {
164
            //We need to send a mail to the user
165
            $this->sendWebhookResponseEmail($user, $payload);
0 ignored issues
show
The call to Phalcon\Cashier\Traits\S...dWebhookResponseEmail() has too few arguments starting with method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
            $this->/** @scrutinizer ignore-call */ sendWebhookResponseEmail($user, $payload);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
166
        }
167
        return $this->response(['Webhook Handled']);
168
    }
169
170
    /**
171
     * Handle bad payment
172
     *
173
     * @todo send email
174
     * @param array $payload
175
     * @return Response
176
     */
177
    protected function handleChargeFailed(array $payload, string $method): Response
178
    {
179
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
180
        if ($user) {
181
            //We need to send a mail to the user
182
            $this->sendWebhookResponseEmail($user, $payload);
0 ignored issues
show
The call to Phalcon\Cashier\Traits\S...dWebhookResponseEmail() has too few arguments starting with method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
            $this->/** @scrutinizer ignore-call */ sendWebhookResponseEmail($user, $payload);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
183
        }
184
        return $this->response(['Webhook Handled']);
185
    }
186
187
    /**
188
     * Handle looking for refund
189
     *
190
     * @todo send email
191
     * @param array $payload
192
     * @return Response
193
     */
194
    protected function handleChargeDisputeCreated(array $payload, string $method): Response
195
    {
196
        return $this->response(['Webhook Handled']);
197
    }
198
199
    /**
200
     * Handle pending payments
201
     *
202
     * @todo send email
203
     * @param array $payload
204
     * @return Response
205
     */
206
    protected function handleChargePending(array $payload, string $method): Response
207
    {
208
        $user = Users::findFirstByStripeId($payload['data']['object']['customer']);
209
        if ($user) {
210
            //We need to send a mail to the user
211
            $this->sendWebhookResponseEmail($user, $payload);
0 ignored issues
show
The call to Phalcon\Cashier\Traits\S...dWebhookResponseEmail() has too few arguments starting with method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
            $this->/** @scrutinizer ignore-call */ sendWebhookResponseEmail($user, $payload);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
212
        }
213
        return $this->response(['Webhook Handled']);
214
    }
215
216
    /**
217
     * Send webhook related emails to user
218
     * @param Users $user
219
     * @param array $payload
220
     * @return void
221
     */
222
    protected static function sendWebhookResponseEmail(Users $user, array $payload, string $method): void
223
    {
224
        // $subject = '';
225
        // $content = '';
226
        // Di::getDefault()->getMail()
227
        //     ->to($user->email)
228
        //     ->subject($subject)
229
        //     ->content($content)
230
        //     ->sendNow();
231
    }
232
}
233