Passed
Push — v3.0 ( 064208...41a1d8 )
by Raza
09:20
created

PayPalVerifyIPN::setWebHookID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Srmklive\PayPal\Traits;
4
5
trait PayPalVerifyIPN
6
{
7
    protected $webhook_id;
8
9
    public function setWebHookID(string $webhook_id): \Srmklive\PayPal\Services\PayPal
10
    {
11
        $this->webhook_id = $webhook_id;
12
13
        return $this;
14
    }
15
16
    /**
17
     * Verify incoming IPN through a web hook id.
18
     *
19
     * @throws \Throwable
20
     *
21
     * @return array|\Psr\Http\Message\StreamInterface|string
22
     */
23
    public function verifyIPN(Request $request)
0 ignored issues
show
Bug introduced by
The type Srmklive\PayPal\Traits\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
24
    {
25
        $headers = array_change_key_case($request->headers->all(), CASE_UPPER);
26
27
        if(!isset($headers['PAYPAL-AUTH-ALGO'][0]) ||
28
            !isset($headers['PAYPAL-TRANSMISSION-ID'][0]) ||
29
            !isset($headers['PAYPAL-CERT-URL'][0]) ||
30
            !isset($headers['PAYPAL-TRANSMISSION-SIG'][0]) ||
31
            !isset($headers['PAYPAL-TRANSMISSION-TIME'][0]) ||
32
            !isset($this->webhook_id)
33
        ){
34
            \Log::error('Invalid headers or webhook id supplied for paypal webhook');
35
            return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

35
            return /** @scrutinizer ignore-call */ response()->json([
Loading history...
36
                'status' => 'error',
37
                'message' => 'Invalid headers or web hook id provided',
38
            ]);
39
        }
40
41
        $params = $request->all();
42
43
        $payload = [
44
            'auth_algo'         => $headers['PAYPAL-AUTH-ALGO'][0],
45
            'cert_url'          => $headers['PAYPAL-CERT-URL'][0],
46
            'transmission_id'   => $headers['PAYPAL-TRANSMISSION-ID'][0],
47
            'transmission_sig'  => $headers['PAYPAL-TRANSMISSION-SIG'][0],
48
            'transmission_time' => $headers['PAYPAL-TRANSMISSION-TIME'][0],
49
            'webhook_id'        => $this->webhook_id,
50
            'webhook_event'     => $params,
51
        ];
52
53
        return $this->verifyWebHook($payload);
0 ignored issues
show
Bug introduced by
It seems like verifyWebHook() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
        return $this->/** @scrutinizer ignore-call */ verifyWebHook($payload);
Loading history...
54
    }
55
}
56